1 | import { generate } from './generate-and-save.js';
|
2 | import { init } from './init/index.js';
|
3 | import { createContext } from './config.js';
|
4 | import { lifecycleHooks } from './hooks.js';
|
5 | import { DetailedError } from '@graphql-codegen/plugin-helpers';
|
6 | export async function runCli(cmd) {
|
7 | await ensureGraphQlPackage();
|
8 | if (cmd === 'init') {
|
9 | await init();
|
10 | return 0;
|
11 | }
|
12 | const context = await createContext();
|
13 | try {
|
14 | await generate(context);
|
15 | if (context.checkMode && context.checkModeStaleFiles.length > 0) {
|
16 |
|
17 | console.log(`The following stale files were detected:\n${context.checkModeStaleFiles.map(file => ` - ${file}\n`)}`);
|
18 | return 1;
|
19 | }
|
20 | return 0;
|
21 | }
|
22 | catch (error) {
|
23 | await lifecycleHooks(context.getConfig().hooks).onError(error.toString());
|
24 | return 1;
|
25 | }
|
26 | }
|
27 | export async function ensureGraphQlPackage() {
|
28 | try {
|
29 | await import('graphql');
|
30 | }
|
31 | catch (e) {
|
32 | throw new DetailedError(`Unable to load "graphql" package. Please make sure to install "graphql" as a dependency!`, `
|
33 | To install "graphql", run:
|
34 | yarn add graphql
|
35 | Or, with NPM:
|
36 | npm install --save graphql
|
37 | `);
|
38 | }
|
39 | }
|