Skip to content

Commit c824b99

Browse files
updates
1 parent 7f2c9c8 commit c824b99

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/cli.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
#!/usr/bin/env node
22

3-
import { whiteBright } from 'cli-color'
3+
import {whiteBright} from 'cli-color'
44
import minimist = require('minimist')
5-
import { readFile, writeFile, existsSync } from 'mz/fs'
5+
import {readFile, writeFile, existsSync} from 'mz/fs'
66
import * as _mkdirp from 'mkdirp'
77
import * as _glob from 'glob'
88
import isGlob = require('is-glob')
9-
import { promisify } from 'util'
10-
import { join, resolve, basename } from 'path'
9+
import {promisify} from 'util'
10+
import {join, resolve, basename} from 'path'
1111
import stdin = require('stdin')
1212
import {compile, Options} from './index'
1313

1414
// Promisify mkdirp
15-
const mkdirp = (path: string) => new Promise((res, rej) => {
16-
_mkdirp(path, (err, made) => {
17-
if (err) rej(err)
18-
else res(made === null ? undefined : made)
15+
const mkdirp = (path: string) =>
16+
new Promise((res, rej) => {
17+
_mkdirp(path, (err, made) => {
18+
if (err) rej(err)
19+
else res(made === null ? undefined : made)
20+
})
1921
})
20-
})
2122

2223
const glob = promisify(_glob)
2324

24-
main(minimist(process.argv.slice(2), {
25-
alias: {
26-
help: ['h'],
27-
input: ['i'],
28-
output: ['o'],
29-
recursive: ['r']
30-
}
31-
}))
25+
main(
26+
minimist(process.argv.slice(2), {
27+
alias: {
28+
help: ['h'],
29+
input: ['i'],
30+
output: ['o'],
31+
recursive: ['r']
32+
}
33+
})
34+
)
3235

3336
async function main(argv: minimist.ParsedArgs) {
3437
if (argv.help) {
@@ -40,7 +43,7 @@ async function main(argv: minimist.ParsedArgs) {
4043
const argOut: string = argv._[1] || argv.output
4144

4245
try {
43-
let files = await getFilesToProcess(argIn, argOut, argv as Partial<Options>)
46+
const files = await getFilesToProcess(argIn, argOut, argv as Partial<Options>)
4447
await Promise.all(files)
4548
} catch (e) {
4649
console.error(whiteBright.bgRedBright('error'), e)
@@ -52,20 +55,20 @@ function getFilesToProcess(argIn: string, argOut: string, argv: Partial<Options>
5255
return new Promise(async (res, rej) => {
5356
try {
5457
if (isGlob(argIn)) {
55-
let files = await glob(join(process.cwd(), argIn))
56-
58+
const files = await glob(join(process.cwd(), argIn))
59+
5760
if (files.length === 0) {
5861
rej('No files match glob pattern')
5962
}
60-
63+
6164
if (argOut && !existsSync(argOut)) {
6265
await mkdirp(argOut)
6366
}
64-
65-
res(files.map(file => processFile(file, { dir: argOut }, argv)))
67+
68+
res(files.map(file => processFile(file, {dir: argOut}, argv)))
6669
return
6770
} else {
68-
res([processFile(argIn, { file: argOut }, argv)])
71+
res([processFile(argIn, {file: argOut}, argv)])
6972
}
7073
} catch (e) {
7174
console.error(whiteBright.bgRedBright('error'), e)
@@ -74,15 +77,12 @@ function getFilesToProcess(argIn: string, argOut: string, argv: Partial<Options>
7477
})
7578
}
7679

77-
function processFile(file: string, out: {dir?: string, file?: string}, argv: Partial<Options>): Promise<void> {
80+
function processFile(file: string, out: {dir?: string; file?: string}, argv: Partial<Options>): Promise<void> {
7881
return new Promise(async (res, rej) => {
7982
try {
8083
const schema = JSON.parse(await readInput(file))
8184
const ts = await compile(schema, file, argv)
82-
await writeOutput(
83-
ts,
84-
out.dir ? join(process.cwd(), out.dir, `${basename(file, '.json')}.d.ts`) : out.file || ''
85-
)
85+
await writeOutput(ts, out.dir ? join(process.cwd(), out.dir, `${basename(file, '.json')}.d.ts`) : out.file || '')
8686
res()
8787
} catch (err) {
8888
rej(err)

test/__snapshots__/test/test.ts.snap

148 Bytes
Binary file not shown.

test/testCLI.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
2-
import { execSync } from 'child_process'
3-
import { readFileSync, unlinkSync, readdirSync, rmdirSync } from 'fs'
2+
import {execSync} from 'child_process'
3+
import {readFileSync, unlinkSync, readdirSync, rmdirSync} from 'fs'
44

55
export function run() {
66
test('pipe in, pipe out', t => {
@@ -80,8 +80,10 @@ export function run() {
8080
})
8181

8282
test('files in (-i), files out (-o)', t => {
83-
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/out').toString()
84-
83+
execSync(
84+
'node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/out'
85+
).toString()
86+
8587
readdirSync('./test/resources/MultiSchema/out').forEach(f => {
8688
const path = `./test/resources/MultiSchema/out/${f}`
8789
t.snapshot(readFileSync(path, 'utf-8'))
@@ -91,14 +93,14 @@ export function run() {
9193
})
9294

9395
test('files in (-i), pipe out', t => {
94-
t.snapshot(
95-
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json').toString()
96-
)
96+
t.snapshot(execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json').toString())
9797
})
9898

9999
test('files in (-i), files out (-o) nested dir does not exist', t => {
100-
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/foo/bar/out').toString()
101-
100+
execSync(
101+
'node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/foo/bar/out'
102+
).toString()
103+
102104
readdirSync('./test/resources/MultiSchema/foo/bar/out').forEach(f => {
103105
const path = `./test/resources/MultiSchema/foo/bar/out/${f}`
104106
t.snapshot(readFileSync(path, 'utf-8'))

0 commit comments

Comments
 (0)