forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
68 lines (63 loc) · 1.61 KB
/
command.ts
File metadata and controls
68 lines (63 loc) · 1.61 KB
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
66
67
68
import 'regenerator-runtime/runtime';
const fs = require('fs');
const { sdl, actionsCodegen } = require('./services');
const { getFlagValue, OUTPUT_FILE_FLAG } = require('./utils/commandUtils');
const commandArgs = process.argv;
const outputFilePath = getFlagValue(commandArgs, OUTPUT_FILE_FLAG);
const logOutput = (log: any) => {
try {
fs.writeFile(outputFilePath, log, 'utf8', () => {
console.log(
JSON.stringify({
success: true,
output_file_path: outputFilePath,
}),
);
});
} catch (e) {
console.error(`could not write output to "${outputFilePath}"`);
process.exit(1);
}
};
const handleArgs = () => {
const rootArg = commandArgs[2];
switch (rootArg) {
case 'sdl':
const sdlSubCommands = commandArgs.slice(3);
return sdl(sdlSubCommands);
case 'actions-codegen':
const actionCodegenSubCommands = commandArgs.slice(3);
return actionsCodegen(actionCodegenSubCommands);
default:
}
};
try {
const cliResponse = handleArgs();
if (cliResponse.error) {
throw Error(cliResponse.error);
}
if (cliResponse.constructor.name === 'Promise') {
cliResponse
.then((r: { error?: string }) => {
if (r.error) {
throw Error(r.error);
}
logOutput(JSON.stringify(r));
})
.catch((e: Error) => {
console.error(e);
process.exit(1);
});
} else {
logOutput(JSON.stringify(cliResponse));
}
} catch (e) {
if (e) {
if (e.error) {
console.error(e.error);
} else {
console.error(e.message ? e.message : e);
}
}
process.exit(1);
}