Skip to content

Commit b4e56da

Browse files
committedSep 10, 2019
separate cli and bin to allow programatic usage of the cli
1 parent 0f98e84 commit b4e56da

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed
 

‎dev-test/generate-all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#/bin/sh
22

3-
node packages/graphql-codegen-cli/dist/commonjs/cli.js --config ./dev-test/codegen.yml
3+
node packages/graphql-codegen-cli/dist/commonjs/bin.js --config ./dev-test/codegen.yml

‎dev-test/generate-watch.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#/bin/sh
22

3-
node packages/graphql-codegen-cli/dist/commonjs/cli.js --config ./dev-test/codegen.yml -w
3+
node packages/graphql-codegen-cli/dist/commonjs/bin.js --config ./dev-test/codegen.yml -w

‎dev-test/init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#/bin/sh
22

3-
node packages/graphql-codegen-cli/dist/commonjs/cli.js init
3+
node packages/graphql-codegen-cli/dist/commonjs/bin.js init

‎packages/graphql-codegen-cli/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "1.7.0",
44
"license": "MIT",
55
"bin": {
6-
"gql-gen": "dist/commonjs/cli.js",
7-
"graphql-codegen": "dist/commonjs/cli.js",
8-
"graphql-code-generator": "dist/commonjs/cli.js"
6+
"gql-gen": "dist/commonjs/bin.js",
7+
"graphql-codegen": "dist/commonjs/bin.js",
8+
"graphql-code-generator": "dist/commonjs/bin.js"
99
},
1010
"repository": {
1111
"type": "git",
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
3+
import { runCli } from './cli';
4+
import { cliError } from './utils/cli-error';
5+
6+
const [, , cmd] = process.argv;
7+
8+
runCli(cmd)
9+
.then(() => {
10+
process.exit(0);
11+
})
12+
.catch(error => {
13+
cliError(error);
14+
});
+12-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
#!/usr/bin/env node
2-
31
import { generate } from './generate-and-save';
42
import { init } from './init';
5-
import { cliError } from './utils/cli-error';
63
import { createConfig } from './config';
74
import { lifecycleHooks } from './hooks';
85

9-
const [, , cmd] = process.argv;
10-
11-
switch (cmd) {
12-
case 'init':
13-
init()
14-
.then(() => {
15-
process.exit(0);
16-
})
17-
.catch(cliError);
18-
break;
6+
export function runCli(cmd: string): Promise<any> {
7+
switch (cmd) {
8+
case 'init':
9+
return init();
10+
default: {
11+
const config = createConfig();
1912

20-
default: {
21-
const config = createConfig();
22-
23-
config.then(config => {
24-
return generate(config)
25-
.then(() => {
26-
process.exit(0);
27-
})
28-
.catch(async error => {
13+
return config.then(config => {
14+
return generate(config).catch(async error => {
2915
await lifecycleHooks(config.hooks).onError(error.toString());
30-
cliError(error);
16+
17+
throw error;
3118
});
32-
});
19+
});
20+
}
3321
}
3422
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
export { executeCodegen } from './codegen';
22
export { generate } from './generate-and-save';
3+
export * from './config';
4+
export * from './init';
5+
export * from './utils/cli-error';
6+
export * from './cli';

‎packages/graphql-codegen-cli/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"noUnusedLocals": true,
2020
"noUnusedParameters": false
2121
},
22-
"files": ["src/index.ts", "src/cli.ts", "src/declarations.d.ts"],
22+
"files": ["src/index.ts", "src/bin.ts", "src/declarations.d.ts"],
2323
"exclude": ["node_modules"]
2424
}

0 commit comments

Comments
 (0)
Please sign in to comment.