-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (45 loc) · 1.51 KB
/
index.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
const fs = require('fs')
const { loadBinding } = require("@node-rs/helper");
const { createReducer } = require("./dist");
/**
* __dirname means load native addon from current dir
* 'core' is the name of native addon
* the second arguments was decided by `napi.name` field in `package.json`
* the third arguments was decided by `name` field in `package.json`
* `loadBinding` helper will load `core.[PLATFORM].node` from `__dirname` first
* If failed to load addon, it will fallback to load from `@modfy/tsgql-[PLATFORM]`
*/
const native = loadBinding(__dirname, "core", "@modfy/tsgql");
const defaultArgs = {
tsconfigPath: "./tsconfig.json",
schema: "./src/schema.ts",
out: "./schema.graphql",
};
const readConfig = (path) => {
const conf = require(path)
if (!conf.tsconfigPath) conf.tsconfigPath = defaultArgs.tsconfigPath
if (!conf.schema) conf.tsconfigPath = defaultArgs.schema
if (!conf.out) conf.tsconfigPath = defaultArgs.out
return conf
}
const run = () => {
let path = "./tsgql.config.js";
if (process.argv.length > 2) {
path = process.argv[2];
}
const { tsconfigPath, schema, out } = readConfig(path)
const reducer = createReducer({tsconfigPath, path: schema})
const [reduced, manifest] = reducer.generate();
const contents = native.generateSchema(
reduced,
JSON.stringify(manifest),
`{
"syntax": "typescript",
"tsx": true,
"decorators": false,
"dynamicImport": false
}`,
);
fs.writeFileSync(out, contents);
};
run();