Skip to content

Commit f506531

Browse files
fix revisions
1 parent 56d0459 commit f506531

File tree

6 files changed

+90
-28
lines changed

6 files changed

+90
-28
lines changed

.vscode/settings.json

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
// Place your settings in this file to overwrite default and user settings.
22
{
33
"typescript.tsdk": "node_modules/typescript/lib",
4-
"editor.tabSize": 2,
5-
"workbench.colorCustomizations": {
6-
"activityBar.background": "#cba9e2",
7-
"activityBar.foreground": "#15202b",
8-
"activityBar.inactiveForeground": "#15202b99",
9-
"activityBarBadge.background": "#8f6930",
10-
"activityBarBadge.foreground": "#e7e7e7",
11-
"titleBar.activeBackground": "#b483d5",
12-
"titleBar.inactiveBackground": "#b483d599",
13-
"titleBar.activeForeground": "#15202b",
14-
"titleBar.inactiveForeground": "#15202b99",
15-
"statusBar.background": "#b483d5",
16-
"statusBarItem.hoverBackground": "#9d5dc8",
17-
"statusBar.foreground": "#15202b"
18-
}
4+
"editor.tabSize": 2
195
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dependencies": {
4848
"@types/is-glob": "^4.0.1",
4949
"@types/json-schema": "^7.0.3",
50+
"@types/mkdirp": "^0.5.2",
5051
"@types/node": ">=4.5.0",
5152
"@types/prettier": "^1.16.1",
5253
"cli-color": "^1.4.0",
@@ -56,6 +57,7 @@
5657
"json-stringify-safe": "^5.0.1",
5758
"lodash": "^4.17.11",
5859
"minimist": "^1.2.0",
60+
"mkdirp": "^0.5.1",
5961
"mz": "^2.7.0",
6062
"prettier": "^1.18.2",
6163
"stdin": "0.0.1"

src/cli.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
import { whiteBright } from 'cli-color'
44
import { JSONSchema4 } from 'json-schema'
55
import minimist = require('minimist')
6-
import { readFile, writeFile, mkdir, existsSync } from 'mz/fs'
6+
import { readFile, writeFile, existsSync } from 'mz/fs'
7+
import * as _mkdirp from 'mkdirp'
78
import * as _glob from 'glob'
89
import isGlob = require('is-glob')
910
import { promisify } from 'util'
1011
import { join, resolve, basename } from 'path'
1112
import stdin = require('stdin')
1213
import { compile, Options } from './index'
1314

15+
// Promisify mkdirp
16+
const mkdirp = (path: string) => new Promise((res, rej) => {
17+
_mkdirp(path, (err, made) => {
18+
if (err) rej(err)
19+
else res(made)
20+
})
21+
})
22+
1423
const glob = promisify(_glob)
1524

1625
main(minimist(process.argv.slice(2), {
@@ -33,24 +42,34 @@ async function main(argv: minimist.ParsedArgs) {
3342
const argOut: string = argv._[1] || argv.output
3443

3544
try {
36-
if (isGlob(argIn)) {
37-
if (!existsSync(argOut)) {
38-
await mkdir(argOut)
39-
}
40-
41-
const files = await glob(join(process.cwd(), argIn))
42-
const processFiles = files.map(file => processFile(file, { dir: argOut }, argv as Partial<Options>))
43-
await Promise.all(processFiles)
44-
} else {
45-
await processFile(argIn, { file: argOut }, argv as Partial<Options>)
46-
}
45+
let files = await getFilesToProcess(argIn, argOut, argv as Partial<Options>)
46+
await Promise.all(files)
4747
} catch (e) {
4848
console.error(whiteBright.bgRedBright('error'), e)
4949
process.exit(1)
5050
}
5151
}
5252

53-
function processFile(file: string, out: {dir?: string, file?: string}, argv: Partial<Options>) {
53+
function getFilesToProcess(argIn: string, argOut: string, argv: Partial<Options>): Promise<Promise<void>[]> {
54+
return new Promise(async (res, rej) => {
55+
if (isGlob(argIn)){
56+
let files = await glob(join(process.cwd(), argIn))
57+
58+
if (files.length === 0) {
59+
rej('No files match glob pattern')
60+
}
61+
62+
if (!existsSync(argOut)) {
63+
await mkdirp(argOut)
64+
}
65+
66+
res(files.map(file => processFile(file, { dir: argOut }, argv)))
67+
}
68+
res([processFile(argIn, { file: argOut }, argv)])
69+
})
70+
}
71+
72+
function processFile(file: string, out: {dir?: string, file?: string}, argv: Partial<Options>): Promise<void> {
5473
return new Promise(async (res, rej) => {
5574
try {
5675
const schema = JSON.parse(await readInput(file))

test/__snapshots__/test/test.ts.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9460,3 +9460,42 @@ Generated by [AVA](https://ava.li).
94609460
[k: string]: any;␊
94619461
}␊
94629462
`
9463+
9464+
## files in (-i), files out (-o) nested dir does not exist
9465+
9466+
> Snapshot 1
9467+
9468+
`/* tslint:disable */␊
9469+
/**␊
9470+
* This file was automatically generated by json-schema-to-typescript.␊
9471+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
9472+
* and run json-schema-to-typescript to regenerate this file.␊
9473+
*/␊
9474+
9475+
export interface ASchema {␊
9476+
f: string;␊
9477+
g?: number;␊
9478+
}␊
9479+
`
9480+
9481+
> Snapshot 2
9482+
9483+
`/* tslint:disable */␊
9484+
/**␊
9485+
* This file was automatically generated by json-schema-to-typescript.␊
9486+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
9487+
* and run json-schema-to-typescript to regenerate this file.␊
9488+
*/␊
9489+
9490+
export interface BSchema {␊
9491+
x?: string;␊
9492+
y: number;␊
9493+
[k: string]: any;␊
9494+
}␊
9495+
`
9496+
9497+
## files in (-i), pipe out
9498+
9499+
> Snapshot 1
9500+
9501+
''

test/__snapshots__/test/test.ts.snap

38.3 KB
Binary file not shown.

test/testCLI.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,20 @@ export function run() {
9393
rmdirSync('./test/resources/MultiSchema/out')
9494
})
9595

96+
test('files in (-i), pipe out', t => {
97+
t.snapshot(
98+
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json').toString()
99+
)
100+
})
101+
102+
test('files in (-i), files out (-o) nested dir does not exist', t => {
103+
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json -o ./test/resources/MultiSchema/foo/bar/out').toString()
104+
105+
readdirSync('./test/resources/MultiSchema/foo/bar/out').forEach(f => {
106+
const path = `./test/resources/MultiSchema/foo/bar/out/${f}`
107+
t.snapshot(readFileSync(path, 'utf-8'))
108+
unlinkSync(path)
109+
})
110+
rmdirSync('./test/resources/MultiSchema/foo/bar/out')
111+
})
96112
}

0 commit comments

Comments
 (0)