-
Notifications
You must be signed in to change notification settings - Fork 23
/
cli-launcher.ts
executable file
·98 lines (94 loc) · 3.56 KB
/
cli-launcher.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
/*
This is the CLI tool to validate scripts against a schema. It uses the API found
in the src/ directory to validate all the files in the file glob CLI parameter.
*/
import * as program from 'commander';
import * as fs from 'fs';
import * as path from 'path';
import * as cli from './cli';
import { consoleLogger } from './logger';
import { printSchemaDefault, printSchemaWithDirectives } from './printers';
// @ts-ignore
const packageJson = require('../package.json');
program
.version(packageJson.version)
.usage(`[options] (<glob.graphql>)`)
.option('-o, --output [pattern]',
'The file path where the merged schema will be outputted to.')
.option('-s, --schema [pattern]',
'Use a glob path that would define all of your schema files to merge them into a valid schema.', '')
.option('-r, --rules [pattern]',
'The file path for your custom rules to validate your operations, and your merged schema.', '')
.option('-p, --operations [pattern]',
'Use a glob that that contains your graphql operation files to test against the merged schema file.', '')
.option('-d, --includeDirectives',
'By default will NOT merge the directives, unless you added this flag.', false)
.parse(process.argv);
if (!program.schema) {
program.outputHelp();
} else {
cli.mergeGQLSchemas(program.schema)
.then((schema) => {
let data = '';
if (program.includeDirectives) {
data = printSchemaWithDirectives(schema);
} else {
data = printSchemaDefault(schema);
}
if (schema.getQueryType().toString() === 'Query' && (!schema.getMutationType()
|| schema.getMutationType().toString() === 'Mutation')
&& (!schema.getSubscriptionType()
|| schema.getSubscriptionType().toString() === 'Subscription')) {
const typeDefs =
`schema { \n query: Query ${schema.getMutationType()
? '\n mutation: Mutation' : ''} ${schema.getSubscriptionType()
? '\n subscription: Subscription' : ''} \n}\n\n`;
data = typeDefs + data;
}
process.stdout.write(data);
if (program.output) {
ensureDirectoryExistence(program.output);
fs.writeFile(program.output, data, (err) => {
if (err) {
consoleLogger.error('Error while copying the merged schema into the file. ', program.output, err);
process.exit(1);
} else {
consoleLogger.log('Successfully written merged schema into a file.');
}
});
}
if (program.operations) {
if (!program.rules) {
cli.validateOperations(program.operations, schema).catch((error) => {
consoleLogger.error('Operation files are not valid!');
process.exit(1);
});
} else {
try {
const setOfRules = require(`${program.rules}`);
cli.validateOperations(program.operations, schema, setOfRules.specifiedRules).catch((error) => {
consoleLogger.error('Operation files are not valid!\n');
process.exit(1);
});
} catch (err) {
consoleLogger.error(`Could not read ${(program.rules)} for the custom validation rules. Exiting...`, err);
process.exit(1);
}
}
}
},
)
.catch((err) => {
consoleLogger.error('Could not merge Schema files!\n');
process.exit(1);
});
}
function ensureDirectoryExistence(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}