-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-tables.js
executable file
·65 lines (52 loc) · 1.33 KB
/
sql-tables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
const { strictify, writeTsFromSchema } = require('./dist');
const { readFileSync } = require('fs');
const ts = require('typescript');
function usage() {
console.log('');
console.log(`${process.argv[1]} path-to-input-schema.ts path-to-output-file.ts [schema-prop]`);
console.log('');
}
if (!process.argv[2]) {
usage();
process.exit(-1);
}
if (!process.argv[3]) {
usage();
process.exit(-2);
}
if (process.argv.length > 5) {
usage();
process.exit(-3);
}
if (process.argv.length < 4) {
usage();
process.exit(-4);
}
const schemaPath = process.argv[2];
const tsPath = process.argv[3];
const schemaProp = process.argv[4] || 'schema';
const tsOpts = {
lib: [ 'dom', 'es6' ],
module: 'commonjs',
moduleResolution: 'node',
target: 'es5',
};
writeSchema();
function writeSchema() {
const fullPath = schemaPath;
const input = readFileSync(fullPath, 'utf8');
const jsSchema = ts.transpileModule(input, tsOpts);
const schema = require('require-from-string')(
jsSchema.outputText,
)[schemaProp];
console.log('Generating TypeScript from the schema', schemaPath);
writeTsFromSchema(
tsPath,
strictify(schema),
).then(() => {
console.log('Wrote TypeScript to', tsPath);
}).catch((err) => {
console.log('Fatal Error Writing File', err.message);
});
}