UNPKG

1.26 kBJavaScriptView Raw
1import { generate } from './generate-and-save.js';
2import { init } from './init/index.js';
3import { createContext } from './config.js';
4import { lifecycleHooks } from './hooks.js';
5import { DetailedError } from '@graphql-codegen/plugin-helpers';
6export 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 // eslint-disable-next-line no-console
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}
27export 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}