Skip to content

Commit 8b42de2

Browse files
fixes on fixes
1 parent 5f3ab6d commit 8b42de2

File tree

5 files changed

+58
-58
lines changed

5 files changed

+58
-58
lines changed

src/cli.ts

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

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

@@ -40,54 +40,40 @@ async function main(argv: minimist.ParsedArgs) {
4040
}
4141

4242
const argIn: string = argv._[0] || argv.input
43-
const argOut: string = argv._[1] || argv.output
43+
const argOut: string | undefined = argv._[1] || argv.output
4444

4545
try {
46-
const files = await getFilesToProcess(argIn, argOut, argv as Partial<Options>)
47-
await Promise.all(files)
46+
await processFiles(argIn, argOut, argv as Partial<Options>)
4847
} catch (e) {
4948
console.error(whiteBright.bgRedBright('error'), e)
5049
process.exit(1)
5150
}
5251
}
5352

54-
function getFilesToProcess(argIn: string, argOut: string, argv: Partial<Options>): Promise<Promise<void>[]> {
55-
return new Promise(async (res, rej) => {
56-
try {
57-
if (isGlob(argIn)) {
58-
const files = await glob(join(process.cwd(), argIn))
59-
60-
if (files.length === 0) {
61-
rej('No files match glob pattern')
62-
}
63-
64-
if (argOut && !existsSync(argOut)) {
65-
await mkdirp(argOut)
66-
}
67-
68-
res(files.map(file => processFile(file, {dir: argOut}, argv)))
69-
return
70-
} else {
71-
res([processFile(argIn, {file: argOut}, argv)])
72-
}
73-
} catch (e) {
74-
console.error(whiteBright.bgRedBright('error'), e)
75-
process.exit(1)
76-
}
77-
})
53+
async function processFiles(argIn: string, argOut: string | undefined, argv: Partial<Options>): Promise<void[]> {
54+
const files = isGlob(argIn) ? await glob(join(process.cwd(), argIn)) : [argIn]
55+
56+
if (files.length === 0) {
57+
throw ReferenceError(
58+
`You passed a glob pattern "${argIn}", but there are no files that match that pattern in ${process.cwd()}`
59+
)
60+
}
61+
62+
if (argOut && extname(argOut) === '' && !existsSync(argOut)) {
63+
await mkdirp(argOut)
64+
}
65+
66+
return Promise.all(files.map(file => processFile(file, argOut, argv)))
7867
}
7968

80-
function processFile(file: string, out: {dir?: string; file?: string}, argv: Partial<Options>): Promise<void> {
81-
return new Promise(async (res, rej) => {
82-
try {
83-
const schema = JSON.parse(await readInput(file))
84-
const ts = await compile(schema, file, argv)
85-
await writeOutput(ts, out.dir ? join(process.cwd(), out.dir, `${basename(file, '.json')}.d.ts`) : out.file || '')
86-
res()
87-
} catch (err) {
88-
rej(err)
89-
}
90-
})
69+
async function processFile(file: string, out: string | undefined, argv: Partial<Options>): Promise<void> {
70+
out = out ? join(process.cwd(), out) : ''
71+
const schema = JSON.parse(await readInput(file))
72+
const ts = await compile(schema, file, argv)
73+
return await writeOutput(
74+
ts,
75+
existsSync(out) && lstatSync(out).isDirectory() ? join(out, `${basename(file, '.json')}.d.ts`) : out
76+
)
9177
}
9278

9379
function readInput(argIn?: string) {
@@ -97,7 +83,7 @@ function readInput(argIn?: string) {
9783
return readFile(resolve(process.cwd(), argIn), 'utf-8')
9884
}
9985

100-
function writeOutput(ts: string, argOut: string): Promise<void> {
86+
function writeOutput(ts: string, argOut: string | undefined): Promise<void> {
10187
if (!argOut) {
10288
try {
10389
process.stdout.write(ts)

test/__snapshots__/test/test.ts.snap

0 Bytes
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"title": "B schema",
3+
"type": "object",
4+
"properties": {
5+
"x": {"type": "string"},
6+
"y": {"type": "integer"}
7+
},
8+
"additionalProperties": true,
9+
"required": ["y"]
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"title": "A schema",
3+
"type": "object",
4+
"properties": {
5+
"f": {"type": "string"},
6+
"g": {"type": "integer"}
7+
},
8+
"additionalProperties": false,
9+
"required": ["f"]
10+
}

test/testCLI.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import test from 'ava'
1+
import {serial as test} from 'ava'
22
import {execSync} from 'child_process'
33
import {readFileSync, unlinkSync, readdirSync, rmdirSync} from 'fs'
44

@@ -46,43 +46,37 @@ export function run() {
4646
})
4747

4848
test('pipe in, file out (--output)', t => {
49-
execSync(
50-
'shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js --output ./ReferencedType.d.ts'
51-
).toString()
49+
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js --output ./ReferencedType.d.ts')
5250
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
5351
unlinkSync('./ReferencedType.d.ts')
5452
})
5553

5654
test('pipe in, file out (-o)', t => {
57-
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js -o ./ReferencedType.d.ts').toString()
55+
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js -o ./ReferencedType.d.ts')
5856
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
5957
unlinkSync('./ReferencedType.d.ts')
6058
})
6159

6260
test('file in (no flags), file out (no flags)', t => {
63-
execSync('node dist/src/cli.js ./test/resources/ReferencedType.json ./ReferencedType.d.ts').toString()
61+
execSync('node dist/src/cli.js ./test/resources/ReferencedType.json ./ReferencedType.d.ts')
6462
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
6563
unlinkSync('./ReferencedType.d.ts')
6664
})
6765

6866
test('file in (-i), file out (-o)', t => {
69-
execSync('node dist/src/cli.js -i ./test/resources/ReferencedType.json -o ./ReferencedType.d.ts').toString()
67+
execSync('node dist/src/cli.js -i ./test/resources/ReferencedType.json -o ./ReferencedType.d.ts')
7068
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
7169
unlinkSync('./ReferencedType.d.ts')
7270
})
7371

7472
test('file in (--input), file out (--output)', t => {
75-
execSync(
76-
'node dist/src/cli.js --input ./test/resources/ReferencedType.json --output ./ReferencedType.d.ts'
77-
).toString()
73+
execSync('node dist/src/cli.js --input ./test/resources/ReferencedType.json --output ./ReferencedType.d.ts')
7874
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
7975
unlinkSync('./ReferencedType.d.ts')
8076
})
8177

8278
test('files in (-i), files out (-o)', t => {
83-
execSync(
84-
'node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/out'
85-
).toString()
79+
execSync("node dist/src/cli.js -i './test/resources/MultiSchema/**/*.json' -o ./test/resources/MultiSchema/out")
8680

8781
readdirSync('./test/resources/MultiSchema/out').forEach(f => {
8882
const path = `./test/resources/MultiSchema/out/${f}`
@@ -93,19 +87,19 @@ export function run() {
9387
})
9488

9589
test('files in (-i), pipe out', t => {
96-
t.snapshot(execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json').toString())
90+
t.snapshot(execSync("node dist/src/cli.js -i './test/resources/MultiSchema/**/*.json'").toString())
9791
})
9892

9993
test('files in (-i), files out (-o) nested dir does not exist', t => {
10094
execSync(
101-
'node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/foo/bar/out'
102-
).toString()
95+
"node dist/src/cli.js -i './test/resources/MultiSchema/**/*.json' -o ./test/resources/MultiSchema/foo/bar/out"
96+
)
10397

10498
readdirSync('./test/resources/MultiSchema/foo/bar/out').forEach(f => {
10599
const path = `./test/resources/MultiSchema/foo/bar/out/${f}`
106100
t.snapshot(readFileSync(path, 'utf-8'))
107101
unlinkSync(path)
108102
})
109-
rmdirSync('./test/resources/MultiSchema/foo/bar/out')
103+
rmdirSync('./test/resources/MultiSchema/foo', {recursive: true})
110104
})
111105
}

0 commit comments

Comments
 (0)