Skip to content

Commit c66722b

Browse files
committed
code cleanup
Signed-off-by: shmck <[email protected]>
1 parent 14afae3 commit c66722b

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

src/build.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as yamlParser from 'js-yaml'
2-
import * as path from 'path'
3-
import * as fs from 'fs'
4-
import * as util from 'util'
1+
import { load } from 'js-yaml'
2+
import { join } from 'path'
3+
import { writeFile, readFile } from 'fs'
4+
import { promisify } from 'util'
55
import { parse } from './utils/parse'
66
import { getArg } from './utils/args'
77
import { getCommits, CommitLogObject } from './utils/commits'
@@ -11,8 +11,8 @@ import { validateSchema } from './utils/validateSchema'
1111
import { validateMarkdown } from './utils/validateMarkdown'
1212
import * as T from '../typings/tutorial'
1313

14-
const write = util.promisify(fs.writeFile)
15-
const read = util.promisify(fs.readFile)
14+
const write = promisify(writeFile)
15+
const read = promisify(readFile)
1616

1717
export type BuildConfigOptions = {
1818
text: string // text document from markdown
@@ -60,15 +60,15 @@ async function build (args: string[]) {
6060
}
6161

6262
// path to run build from
63-
const localPath = path.join(process.cwd(), options.dir)
63+
const localPath = join(process.cwd(), options.dir)
6464

6565
// load markdown and files
6666
let _markdown: string
6767
let _yaml: string
6868
try {
6969
;[_markdown, _yaml] = await Promise.all([
70-
read(path.join(localPath, options.markdown), 'utf8'),
71-
read(path.join(localPath, options.yaml), 'utf8')
70+
read(join(localPath, options.markdown), 'utf8'),
71+
read(join(localPath, options.yaml), 'utf8')
7272
])
7373
} catch (e) {
7474
console.error('Error reading file:')
@@ -91,7 +91,7 @@ async function build (args: string[]) {
9191
// parse yaml skeleton config
9292
let skeleton
9393
try {
94-
skeleton = yamlParser.load(_yaml) as T.TutorialSkeleton
94+
skeleton = load(_yaml) as T.TutorialSkeleton
9595
if (!skeleton || !Object.keys(skeleton).length) {
9696
throw new Error(`Skeleton at "${options.yaml}" is invalid`)
9797
}

src/utils/commits.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as fs from 'fs'
1+
import { mkdir, exists, rmdir } from 'fs'
22
import util from 'util'
33
import * as path from 'path'
4-
import { ListLogSummary } from 'simple-git/typings/response'
4+
import { LogResult } from 'simple-git/typings/response'
55
import gitP, { SimpleGit } from 'simple-git/promise'
66
import { validateCommitOrder } from './validateCommits'
77

8-
const mkdir = util.promisify(fs.mkdir)
9-
const exists = util.promisify(fs.exists)
10-
const rmdir = util.promisify(fs.rmdir)
8+
const mkdirPromise = util.promisify(mkdir)
9+
const existsPromise = util.promisify(exists)
10+
const rmdirPromise = util.promisify(rmdir)
1111

1212
type GetCommitOptions = {
1313
localDir: string
@@ -17,7 +17,7 @@ type GetCommitOptions = {
1717
export type CommitLogObject = { [position: string]: string[] }
1818

1919
export function parseCommits (
20-
logs: ListLogSummary<any>
20+
logs: LogResult<any>
2121
): { [hash: string]: string[] } {
2222
// Filter relevant logs
2323
const commits: CommitLogObject = {}
@@ -78,11 +78,11 @@ export async function getCommits ({
7878

7979
// setup .tmp directory
8080
const tmpDir = path.join(localDir, '.tmp')
81-
const tmpDirExists = await exists(tmpDir)
81+
const tmpDirExists = await existsPromise(tmpDir)
8282
if (tmpDirExists) {
83-
await rmdir(tmpDir, { recursive: true })
83+
await rmdirPromise(tmpDir, { recursive: true })
8484
}
85-
await mkdir(tmpDir)
85+
await mkdirPromise(tmpDir)
8686

8787
const tempGit = gitP(tmpDir)
8888
await tempGit.clone(localDir, tmpDir)
@@ -115,6 +115,6 @@ export async function getCommits ({
115115
// revert back to the original branch on failure
116116
await git.checkout(originalBranch)
117117
// cleanup the tmp directory
118-
await rmdir(tmpDir, { recursive: true })
118+
await rmdirPromise(tmpDir, { recursive: true })
119119
}
120120
}

src/utils/exec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as T from '../../typings/tutorial'
22
import { exec as cpExec } from 'child_process'
3-
import * as path from 'path'
3+
import { join } from 'path'
44
import { promisify } from 'util'
55

66
const asyncExec = promisify(cpExec)
@@ -50,7 +50,7 @@ export function createCommandRunner (cwd: string) {
5050
console.log(`--> ${command}`)
5151
let cwdDir = cwd
5252
if (dir) {
53-
cwdDir = path.join(cwd, dir)
53+
cwdDir = join(cwd, dir)
5454
}
5555
const { stdout, stderr } = await createExec(cwdDir)(command)
5656

@@ -67,7 +67,7 @@ export function createCommandRunner (cwd: string) {
6767
}
6868

6969
// function isAbsolute(p: string) {
70-
// return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
70+
// return normalize(p + "/") === normalize(resolve(p) + "/");
7171
// }
7272

7373
export function createTestRunner (cwd: string, config: T.TestRunnerConfig) {
@@ -77,7 +77,7 @@ export function createTestRunner (cwd: string, config: T.TestRunnerConfig) {
7777

7878
let wd = cwd
7979
if (directory) {
80-
wd = path.join(cwd, directory)
80+
wd = join(cwd, directory)
8181
}
8282

8383
const commandWithArgs = `${command} ${args.tap}`

src/validate.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as path from 'path'
2-
import * as fs from 'fs-extra'
3-
import * as yamlParser from 'js-yaml'
1+
import { join } from 'path'
2+
import { readFile, pathExists, emptyDir } from 'fs-extra'
3+
import { load } from 'js-yaml'
44
import { getArg } from './utils/args'
55
import gitP, { SimpleGit } from 'simple-git/promise'
66
import {
@@ -19,23 +19,20 @@ interface Options {
1919
async function validate (args: string[]) {
2020
// dir - default .
2121
const dir = !args.length || args[0].match(/^-/) ? '.' : args[0]
22-
const localDir = path.join(process.cwd(), dir)
22+
const localDir = join(process.cwd(), dir)
2323

2424
// -y --yaml - default coderoad-config.yml
2525
const options: Options = {
2626
yaml: getArg(args, { name: 'yaml', alias: 'y' }) || 'coderoad.yaml',
2727
clean: getArg(args, { name: 'clean', alias: 'c' }) !== 'false'
2828
}
2929

30-
const _yaml: string = await fs.readFile(
31-
path.join(localDir, options.yaml),
32-
'utf8'
33-
)
30+
const _yaml: string = await readFile(join(localDir, options.yaml), 'utf8')
3431

3532
// parse yaml config
3633
let skeleton
3734
try {
38-
skeleton = yamlParser.load(_yaml) as TutorialSkeleton
35+
skeleton = load(_yaml) as TutorialSkeleton
3936

4037
if (!skeleton) {
4138
throw new Error('Invalid yaml file contents')
@@ -52,11 +49,11 @@ async function validate (args: string[]) {
5249
const commits: CommitLogObject = await getCommits({ localDir, codeBranch })
5350

5451
// setup tmp dir
55-
const tmpDir = path.join(localDir, '.tmp')
52+
const tmpDir = join(localDir, '.tmp')
5653

5754
try {
58-
if (!(await fs.pathExists(tmpDir))) {
59-
await fs.emptyDir(tmpDir)
55+
if (!(await pathExists(tmpDir))) {
56+
await emptyDir(tmpDir)
6057
}
6158
const tempGit: SimpleGit = gitP(tmpDir)
6259

0 commit comments

Comments
 (0)