-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cli.ts
46 lines (42 loc) · 1.21 KB
/
cli.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
import { generate } from './generate-and-save.js';
import { init } from './init/index.js';
import { createContext } from './config.js';
import { lifecycleHooks } from './hooks.js';
import { DetailedError } from '@graphql-codegen/plugin-helpers';
export async function runCli(cmd: string): Promise<number> {
await ensureGraphQlPackage();
if (cmd === 'init') {
await init();
return 0;
}
const context = await createContext();
try {
await generate(context);
if (context.checkMode && context.checkModeStaleFiles.length > 0) {
// eslint-disable-next-line no-console
console.log(
`The following stale files were detected:\n${context.checkModeStaleFiles.map(file => ` - ${file}\n`)}`
);
return 1;
}
return 0;
} catch (error) {
await lifecycleHooks(context.getConfig().hooks).onError(error.toString());
return 1;
}
}
export async function ensureGraphQlPackage() {
try {
await import('graphql');
} catch (e) {
throw new DetailedError(
`Unable to load "graphql" package. Please make sure to install "graphql" as a dependency!`,
`
To install "graphql", run:
yarn add graphql
Or, with NPM:
npm install --save graphql
`
);
}
}