Skip to content

Commit 7f2c9c8

Browse files
Add directory support
1 parent 0082d4f commit 7f2c9c8

File tree

7 files changed

+233
-17
lines changed

7 files changed

+233
-17
lines changed

.vscode/settings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
// Place your settings in this file to overwrite default and user settings.
22
{
33
"typescript.tsdk": "node_modules/typescript/lib",
4+
<<<<<<< HEAD
5+
"editor.tabSize": 2,
6+
"workbench.colorCustomizations": {
7+
"titleBar.activeBackground": "#b483d5",
8+
"titleBar.inactiveBackground": "#b483d599",
9+
"titleBar.activeForeground": "#15202b",
10+
"titleBar.inactiveForeground": "#15202b99",
11+
"statusBar.background": "#b483d5",
12+
"statusBarItem.hoverBackground": "#9d5dc8",
13+
"statusBar.foreground": "#15202b"
14+
},
15+
"peacock.color": "#b483d5"
16+
=======
417
"editor.tabSize": 2
18+
>>>>>>> f506531... fix revisions
519
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,26 @@
4545
},
4646
"homepage": "https://github.com/bcherny/json-schema-to-typescript#readme",
4747
"dependencies": {
48+
"@types/is-glob": "^4.0.1",
4849
"@types/json-schema": "^7.0.3",
50+
"@types/mkdirp": "^0.5.2",
4951
"@types/node": ">=4.5.0",
5052
"@types/prettier": "^1.16.1",
5153
"cli-color": "^1.4.0",
54+
"glob": "^7.1.4",
55+
"is-glob": "^4.0.1",
5256
"json-schema-ref-parser": "^6.1.0",
5357
"json-stringify-safe": "^5.0.1",
5458
"lodash": "^4.17.11",
5559
"minimist": "^1.2.0",
60+
"mkdirp": "^0.5.1",
5661
"mz": "^2.7.0",
5762
"prettier": "^1.19.1",
5863
"stdin": "0.0.1"
5964
},
6065
"devDependencies": {
6166
"@types/cli-color": "^0.3.29",
67+
"@types/glob": "^7.1.1",
6268
"@types/lodash": "^4.14.121",
6369
"@types/minimist": "^1.2.0",
6470
"@types/mz": "0.0.32",

src/cli.ts

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

3-
import {whiteBright} from 'cli-color'
4-
import {JSONSchema4} from 'json-schema'
3+
import { whiteBright } from 'cli-color'
54
import minimist = require('minimist')
6-
import {readFile, writeFile} from 'mz/fs'
7-
import {resolve} from 'path'
5+
import { readFile, writeFile, existsSync } from 'mz/fs'
6+
import * as _mkdirp from 'mkdirp'
7+
import * as _glob from 'glob'
8+
import isGlob = require('is-glob')
9+
import { promisify } from 'util'
10+
import { join, resolve, basename } from 'path'
811
import stdin = require('stdin')
912
import {compile, Options} from './index'
1013

11-
main(
12-
minimist(process.argv.slice(2), {
13-
alias: {
14-
help: ['h'],
15-
input: ['i'],
16-
output: ['o']
17-
}
14+
// 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)
1819
})
19-
)
20+
})
21+
22+
const glob = promisify(_glob)
23+
24+
main(minimist(process.argv.slice(2), {
25+
alias: {
26+
help: ['h'],
27+
input: ['i'],
28+
output: ['o'],
29+
recursive: ['r']
30+
}
31+
}))
2032

2133
async function main(argv: minimist.ParsedArgs) {
2234
if (argv.help) {
@@ -28,15 +40,56 @@ async function main(argv: minimist.ParsedArgs) {
2840
const argOut: string = argv._[1] || argv.output
2941

3042
try {
31-
const schema: JSONSchema4 = JSON.parse(await readInput(argIn))
32-
const ts = await compile(schema, argIn, argv as Partial<Options>)
33-
await writeOutput(ts, argOut)
43+
let files = await getFilesToProcess(argIn, argOut, argv as Partial<Options>)
44+
await Promise.all(files)
3445
} catch (e) {
3546
console.error(whiteBright.bgRedBright('error'), e)
3647
process.exit(1)
3748
}
3849
}
3950

51+
function getFilesToProcess(argIn: string, argOut: string, argv: Partial<Options>): Promise<Promise<void>[]> {
52+
return new Promise(async (res, rej) => {
53+
try {
54+
if (isGlob(argIn)) {
55+
let files = await glob(join(process.cwd(), argIn))
56+
57+
if (files.length === 0) {
58+
rej('No files match glob pattern')
59+
}
60+
61+
if (argOut && !existsSync(argOut)) {
62+
await mkdirp(argOut)
63+
}
64+
65+
res(files.map(file => processFile(file, { dir: argOut }, argv)))
66+
return
67+
} else {
68+
res([processFile(argIn, { file: argOut }, argv)])
69+
}
70+
} catch (e) {
71+
console.error(whiteBright.bgRedBright('error'), e)
72+
process.exit(1)
73+
}
74+
})
75+
}
76+
77+
function processFile(file: string, out: {dir?: string, file?: string}, argv: Partial<Options>): Promise<void> {
78+
return new Promise(async (res, rej) => {
79+
try {
80+
const schema = JSON.parse(await readInput(file))
81+
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+
)
86+
res()
87+
} catch (err) {
88+
rej(err)
89+
}
90+
})
91+
}
92+
4093
function readInput(argIn?: string) {
4194
if (!argIn) {
4295
return new Promise(stdin)

test/__snapshots__/test/test.ts.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,101 @@ Generated by [AVA](https://ava.li).
15871587
}␊
15881588
`
15891589

1590+
## files in (-i), files out (-o)
1591+
1592+
> Snapshot 1
1593+
1594+
`/* tslint:disable */␊
1595+
/**␊
1596+
* This file was automatically generated by json-schema-to-typescript.␊
1597+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1598+
* and run json-schema-to-typescript to regenerate this file.␊
1599+
*/␊
1600+
1601+
export interface ASchema {␊
1602+
f: string;␊
1603+
g?: number;␊
1604+
}␊
1605+
`
1606+
1607+
> Snapshot 2
1608+
1609+
`/* tslint:disable */␊
1610+
/**␊
1611+
* This file was automatically generated by json-schema-to-typescript.␊
1612+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1613+
* and run json-schema-to-typescript to regenerate this file.␊
1614+
*/␊
1615+
1616+
export interface BSchema {␊
1617+
x?: string;␊
1618+
y: number;␊
1619+
[k: string]: any;␊
1620+
}␊
1621+
`
1622+
1623+
## files in (-i), files out (-o) nested dir does not exist
1624+
1625+
> Snapshot 1
1626+
1627+
`/* tslint:disable */␊
1628+
/**␊
1629+
* This file was automatically generated by json-schema-to-typescript.␊
1630+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1631+
* and run json-schema-to-typescript to regenerate this file.␊
1632+
*/␊
1633+
1634+
export interface ASchema {␊
1635+
f: string;␊
1636+
g?: number;␊
1637+
}␊
1638+
`
1639+
1640+
> Snapshot 2
1641+
1642+
`/* tslint:disable */␊
1643+
/**␊
1644+
* This file was automatically generated by json-schema-to-typescript.␊
1645+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1646+
* and run json-schema-to-typescript to regenerate this file.␊
1647+
*/␊
1648+
1649+
export interface BSchema {␊
1650+
x?: string;␊
1651+
y: number;␊
1652+
[k: string]: any;␊
1653+
}␊
1654+
`
1655+
1656+
## files in (-i), pipe out
1657+
1658+
> Snapshot 1
1659+
1660+
`/* tslint:disable */␊
1661+
/**␊
1662+
* This file was automatically generated by json-schema-to-typescript.␊
1663+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1664+
* and run json-schema-to-typescript to regenerate this file.␊
1665+
*/␊
1666+
1667+
export interface ASchema {␊
1668+
f: string;␊
1669+
g?: number;␊
1670+
}␊
1671+
/* tslint:disable */␊
1672+
/**␊
1673+
* This file was automatically generated by json-schema-to-typescript.␊
1674+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1675+
* and run json-schema-to-typescript to regenerate this file.␊
1676+
*/␊
1677+
1678+
export interface BSchema {␊
1679+
x?: string;␊
1680+
y: number;␊
1681+
[k: string]: any;␊
1682+
}␊
1683+
`
1684+
15901685
## formatterOptions.js
15911686

15921687
> Snapshot 1

test/resources/MultiSchema/a.json

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/resources/MultiSchema/b.json

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+
}

test/testCLI.ts

Lines changed: 30 additions & 2 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} 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 => {
@@ -78,4 +78,32 @@ export function run() {
7878
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
7979
unlinkSync('./ReferencedType.d.ts')
8080
})
81+
82+
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+
85+
readdirSync('./test/resources/MultiSchema/out').forEach(f => {
86+
const path = `./test/resources/MultiSchema/out/${f}`
87+
t.snapshot(readFileSync(path, 'utf-8'))
88+
unlinkSync(path)
89+
})
90+
rmdirSync('./test/resources/MultiSchema/out')
91+
})
92+
93+
test('files in (-i), pipe out', t => {
94+
t.snapshot(
95+
execSync('node dist/src/cli.js -i ./test/resources/MultiSchema/**/*.json').toString()
96+
)
97+
})
98+
99+
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+
102+
readdirSync('./test/resources/MultiSchema/foo/bar/out').forEach(f => {
103+
const path = `./test/resources/MultiSchema/foo/bar/out/${f}`
104+
t.snapshot(readFileSync(path, 'utf-8'))
105+
unlinkSync(path)
106+
})
107+
rmdirSync('./test/resources/MultiSchema/foo/bar/out')
108+
})
81109
}

0 commit comments

Comments
 (0)