@depup/graphql-codegen__cli 6.2.1-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/changes.json +90 -0
- package/cjs/bin.js +13 -0
- package/cjs/cli.js +64 -0
- package/cjs/codegen.js +388 -0
- package/cjs/config.js +370 -0
- package/cjs/documentTransforms.js +63 -0
- package/cjs/generate-and-save.js +153 -0
- package/cjs/graphql-config.js +67 -0
- package/cjs/hooks.js +93 -0
- package/cjs/index.js +13 -0
- package/cjs/init/helpers.js +90 -0
- package/cjs/init/index.js +62 -0
- package/cjs/init/plugins.js +136 -0
- package/cjs/init/questions.js +177 -0
- package/cjs/init/targets.js +45 -0
- package/cjs/init/types.js +15 -0
- package/cjs/load.js +102 -0
- package/cjs/package.json +1 -0
- package/cjs/plugins.js +43 -0
- package/cjs/presets.js +50 -0
- package/cjs/utils/abort-controller-polyfill.js +99 -0
- package/cjs/utils/cli-error.js +24 -0
- package/cjs/utils/debugging.js +22 -0
- package/cjs/utils/file-system.js +24 -0
- package/cjs/utils/get-latest-version.js +14 -0
- package/cjs/utils/helpers.js +12 -0
- package/cjs/utils/logger.js +24 -0
- package/cjs/utils/patterns.js +235 -0
- package/cjs/utils/watcher.js +244 -0
- package/esm/bin.js +11 -0
- package/esm/cli.js +37 -0
- package/esm/codegen.js +384 -0
- package/esm/config.js +357 -0
- package/esm/documentTransforms.js +59 -0
- package/esm/generate-and-save.js +149 -0
- package/esm/graphql-config.js +62 -0
- package/esm/hooks.js +89 -0
- package/esm/index.js +7 -0
- package/esm/init/helpers.js +82 -0
- package/esm/init/index.js +59 -0
- package/esm/init/plugins.js +133 -0
- package/esm/init/questions.js +172 -0
- package/esm/init/targets.js +42 -0
- package/esm/init/types.js +12 -0
- package/esm/load.js +97 -0
- package/esm/plugins.js +40 -0
- package/esm/presets.js +47 -0
- package/esm/utils/abort-controller-polyfill.js +96 -0
- package/esm/utils/cli-error.js +20 -0
- package/esm/utils/debugging.js +17 -0
- package/esm/utils/file-system.js +17 -0
- package/esm/utils/get-latest-version.js +11 -0
- package/esm/utils/helpers.js +9 -0
- package/esm/utils/logger.js +18 -0
- package/esm/utils/patterns.js +226 -0
- package/esm/utils/watcher.js +239 -0
- package/package.json +205 -0
- package/typings/bin.d.cts +2 -0
- package/typings/bin.d.ts +2 -0
- package/typings/cli.d.cts +2 -0
- package/typings/cli.d.ts +2 -0
- package/typings/codegen.d.cts +6 -0
- package/typings/codegen.d.ts +6 -0
- package/typings/config.d.cts +152 -0
- package/typings/config.d.ts +152 -0
- package/typings/documentTransforms.d.cts +3 -0
- package/typings/documentTransforms.d.ts +3 -0
- package/typings/generate-and-save.d.cts +10 -0
- package/typings/generate-and-save.d.ts +10 -0
- package/typings/graphql-config.d.cts +3 -0
- package/typings/graphql-config.d.ts +3 -0
- package/typings/hooks.d.cts +11 -0
- package/typings/hooks.d.ts +11 -0
- package/typings/index.d.cts +8 -0
- package/typings/index.d.ts +8 -0
- package/typings/init/helpers.d.cts +10 -0
- package/typings/init/helpers.d.ts +10 -0
- package/typings/init/index.d.cts +1 -0
- package/typings/init/index.d.ts +1 -0
- package/typings/init/plugins.d.cts +2 -0
- package/typings/init/plugins.d.ts +2 -0
- package/typings/init/questions.d.cts +13 -0
- package/typings/init/questions.d.ts +13 -0
- package/typings/init/targets.d.cts +2 -0
- package/typings/init/targets.d.ts +2 -0
- package/typings/init/types.d.cts +30 -0
- package/typings/init/types.d.ts +30 -0
- package/typings/load.d.cts +15 -0
- package/typings/load.d.ts +15 -0
- package/typings/plugins.d.cts +2 -0
- package/typings/plugins.d.ts +2 -0
- package/typings/presets.d.cts +5 -0
- package/typings/presets.d.ts +5 -0
- package/typings/utils/abort-controller-polyfill.d.cts +5 -0
- package/typings/utils/abort-controller-polyfill.d.ts +5 -0
- package/typings/utils/cli-error.d.cts +10 -0
- package/typings/utils/cli-error.d.ts +10 -0
- package/typings/utils/debugging.d.cts +3 -0
- package/typings/utils/debugging.d.ts +3 -0
- package/typings/utils/file-system.d.cts +8 -0
- package/typings/utils/file-system.d.ts +8 -0
- package/typings/utils/get-latest-version.d.cts +6 -0
- package/typings/utils/get-latest-version.d.ts +6 -0
- package/typings/utils/helpers.d.cts +1 -0
- package/typings/utils/helpers.d.ts +1 -0
- package/typings/utils/logger.d.cts +5 -0
- package/typings/utils/logger.d.ts +5 -0
- package/typings/utils/patterns.d.cts +91 -0
- package/typings/utils/patterns.d.ts +91 -0
- package/typings/utils/watcher.d.cts +16 -0
- package/typings/utils/watcher.d.ts +16 -0
package/cjs/hooks.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lifecycleHooks = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const shell_quote_1 = require("shell-quote");
|
|
7
|
+
const debugging_js_1 = require("./utils/debugging.js");
|
|
8
|
+
const DEFAULT_HOOKS = {
|
|
9
|
+
afterStart: [],
|
|
10
|
+
beforeDone: [],
|
|
11
|
+
onWatchTriggered: [],
|
|
12
|
+
onError: [],
|
|
13
|
+
afterOneFileWrite: [],
|
|
14
|
+
afterAllFileWrite: [],
|
|
15
|
+
beforeOneFileWrite: [],
|
|
16
|
+
beforeAllFileWrite: [],
|
|
17
|
+
};
|
|
18
|
+
function execShellCommand(cmd) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
(0, child_process_1.exec)(cmd, {
|
|
21
|
+
env: {
|
|
22
|
+
...process.env,
|
|
23
|
+
PATH: `${process.env.PATH}${path_1.delimiter}${process.cwd()}${path_1.sep}node_modules${path_1.sep}.bin`,
|
|
24
|
+
},
|
|
25
|
+
}, (error, stdout, stderr) => {
|
|
26
|
+
if (error) {
|
|
27
|
+
reject(error);
|
|
28
|
+
// eslint-disable-next-line no-console
|
|
29
|
+
console.error(error);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
(0, debugging_js_1.debugLog)(stdout || stderr);
|
|
33
|
+
resolve(stdout || stderr);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function executeHooks(hookName, _scripts = [], args = [], initialState) {
|
|
39
|
+
(0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" scripts...`);
|
|
40
|
+
let state = initialState;
|
|
41
|
+
const scripts = Array.isArray(_scripts) ? _scripts : [_scripts];
|
|
42
|
+
const quotedArgs = (0, shell_quote_1.quote)(args);
|
|
43
|
+
for (const script of scripts) {
|
|
44
|
+
if (typeof script === 'string') {
|
|
45
|
+
(0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script} with args: ${quotedArgs}...`);
|
|
46
|
+
await execShellCommand(`${script} ${quotedArgs}`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
(0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script.name} with args: ${args.join(' ')}...`);
|
|
50
|
+
const hookArgs = state === undefined ? args : [...args, state];
|
|
51
|
+
const hookResult = await script(...hookArgs);
|
|
52
|
+
if (typeof hookResult === 'string' && typeof state === 'string') {
|
|
53
|
+
(0, debugging_js_1.debugLog)(`Received new content from lifecycle hook "${hookName}" script: ${script.name}`);
|
|
54
|
+
state = hookResult;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return state;
|
|
59
|
+
}
|
|
60
|
+
const lifecycleHooks = (_hooks = {}) => {
|
|
61
|
+
const hooks = {
|
|
62
|
+
...DEFAULT_HOOKS,
|
|
63
|
+
..._hooks,
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
afterStart: async () => {
|
|
67
|
+
await executeHooks('afterStart', hooks.afterStart);
|
|
68
|
+
},
|
|
69
|
+
onWatchTriggered: async (event, path) => {
|
|
70
|
+
await executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]);
|
|
71
|
+
},
|
|
72
|
+
onError: async (error) => {
|
|
73
|
+
await executeHooks('onError', hooks.onError, [error]);
|
|
74
|
+
},
|
|
75
|
+
afterOneFileWrite: async (path) => {
|
|
76
|
+
await executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]);
|
|
77
|
+
},
|
|
78
|
+
afterAllFileWrite: async (paths) => {
|
|
79
|
+
await executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths);
|
|
80
|
+
},
|
|
81
|
+
beforeOneFileWrite: async (path, content) => {
|
|
82
|
+
const result = await executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path], content);
|
|
83
|
+
return typeof result === 'string' ? result : content;
|
|
84
|
+
},
|
|
85
|
+
beforeAllFileWrite: async (paths) => {
|
|
86
|
+
await executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths);
|
|
87
|
+
},
|
|
88
|
+
beforeDone: async () => {
|
|
89
|
+
await executeHooks('beforeDone', hooks.beforeDone);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
exports.lifecycleHooks = lifecycleHooks;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generate = exports.executeCodegen = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./cli.js"), exports);
|
|
6
|
+
var codegen_js_1 = require("./codegen.js");
|
|
7
|
+
Object.defineProperty(exports, "executeCodegen", { enumerable: true, get: function () { return codegen_js_1.executeCodegen; } });
|
|
8
|
+
tslib_1.__exportStar(require("./config.js"), exports);
|
|
9
|
+
var generate_and_save_js_1 = require("./generate-and-save.js");
|
|
10
|
+
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_and_save_js_1.generate; } });
|
|
11
|
+
tslib_1.__exportStar(require("./graphql-config.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./init/index.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./utils/cli-error.js"), exports);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeConfig = writeConfig;
|
|
4
|
+
exports.writePackage = writePackage;
|
|
5
|
+
exports.bold = bold;
|
|
6
|
+
exports.grey = grey;
|
|
7
|
+
exports.italic = italic;
|
|
8
|
+
const tslib_1 = require("tslib");
|
|
9
|
+
const fs_1 = require("fs");
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const generator_1 = tslib_1.__importDefault(require("@babel/generator"));
|
|
12
|
+
const template_1 = tslib_1.__importDefault(require("@babel/template"));
|
|
13
|
+
const t = tslib_1.__importStar(require("@babel/types"));
|
|
14
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
15
|
+
const detect_indent_1 = tslib_1.__importDefault(require("detect-indent"));
|
|
16
|
+
const get_latest_version_js_1 = require("../utils/get-latest-version.js");
|
|
17
|
+
const types_js_1 = require("./types.js");
|
|
18
|
+
function jsObjectToBabelObjectExpression(obj) {
|
|
19
|
+
const objExp = t.objectExpression([]);
|
|
20
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
21
|
+
if (Array.isArray(val)) {
|
|
22
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), t.arrayExpression(val.map(v => (typeof v === 'object' ? jsObjectToBabelObjectExpression(v) : t.valueToNode(v))))));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), typeof val === 'object' ? jsObjectToBabelObjectExpression(val) : t.valueToNode(val)));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return objExp;
|
|
29
|
+
}
|
|
30
|
+
// Parses config and writes it to a file
|
|
31
|
+
async function writeConfig(answers, config) {
|
|
32
|
+
const YAML = await Promise.resolve().then(() => tslib_1.__importStar(require('json-to-pretty-yaml'))).then(m => ('default' in m ? m.default : m));
|
|
33
|
+
const ext = answers.config.toLocaleLowerCase().split('.')[1];
|
|
34
|
+
const fullPath = (0, path_1.resolve)(process.cwd(), answers.config);
|
|
35
|
+
const relativePath = (0, path_1.relative)(process.cwd(), answers.config);
|
|
36
|
+
let content;
|
|
37
|
+
if (ext === 'ts') {
|
|
38
|
+
const buildRequire = template_1.default.statement(`%%config%%`);
|
|
39
|
+
const ast = buildRequire({
|
|
40
|
+
config: jsObjectToBabelObjectExpression(config),
|
|
41
|
+
});
|
|
42
|
+
content = `
|
|
43
|
+
import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
44
|
+
|
|
45
|
+
const config: CodegenConfig = ${(0, generator_1.default)(ast).code.replace(/\(|\)/g, '')}
|
|
46
|
+
|
|
47
|
+
export default config;
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
|
|
52
|
+
}
|
|
53
|
+
(0, fs_1.writeFileSync)(fullPath, content, 'utf8');
|
|
54
|
+
return {
|
|
55
|
+
relativePath,
|
|
56
|
+
fullPath,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// Updates package.json (script and plugins as dependencies)
|
|
60
|
+
async function writePackage(answers, configLocation) {
|
|
61
|
+
// script
|
|
62
|
+
const pkgPath = (0, path_1.resolve)(process.cwd(), 'package.json');
|
|
63
|
+
const pkgContent = (0, fs_1.readFileSync)(pkgPath, 'utf8');
|
|
64
|
+
const pkg = JSON.parse(pkgContent);
|
|
65
|
+
const { indent } = (0, detect_indent_1.default)(pkgContent);
|
|
66
|
+
pkg.scripts ||= {};
|
|
67
|
+
pkg.scripts[answers.script] = `graphql-codegen --config ${configLocation}`;
|
|
68
|
+
// plugin
|
|
69
|
+
pkg.devDependencies ||= {};
|
|
70
|
+
await Promise.all((answers.plugins || []).map(async (plugin) => {
|
|
71
|
+
pkg.devDependencies[plugin.package] = await (0, get_latest_version_js_1.getLatestVersion)(plugin.package);
|
|
72
|
+
}));
|
|
73
|
+
if (answers.introspection) {
|
|
74
|
+
pkg.devDependencies['@graphql-codegen/introspection'] = await (0, get_latest_version_js_1.getLatestVersion)('@graphql-codegen/introspection');
|
|
75
|
+
}
|
|
76
|
+
pkg.devDependencies['@graphql-codegen/cli'] = await (0, get_latest_version_js_1.getLatestVersion)('@graphql-codegen/cli');
|
|
77
|
+
if (answers.targets.includes(types_js_1.Tags.client)) {
|
|
78
|
+
pkg.devDependencies['@graphql-codegen/client-preset'] = await (0, get_latest_version_js_1.getLatestVersion)('@graphql-codegen/client-preset');
|
|
79
|
+
}
|
|
80
|
+
(0, fs_1.writeFileSync)(pkgPath, JSON.stringify(pkg, null, indent));
|
|
81
|
+
}
|
|
82
|
+
function bold(str) {
|
|
83
|
+
return chalk_1.default.bold(str);
|
|
84
|
+
}
|
|
85
|
+
function grey(str) {
|
|
86
|
+
return chalk_1.default.grey(str);
|
|
87
|
+
}
|
|
88
|
+
function italic(str) {
|
|
89
|
+
return chalk_1.default.italic(str);
|
|
90
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.init = init;
|
|
4
|
+
const helpers_js_1 = require("./helpers.js");
|
|
5
|
+
const questions_js_1 = require("./questions.js");
|
|
6
|
+
const targets_js_1 = require("./targets.js");
|
|
7
|
+
const types_js_1 = require("./types.js");
|
|
8
|
+
function log(...msgs) {
|
|
9
|
+
// eslint-disable-next-line no-console
|
|
10
|
+
console.log(...msgs);
|
|
11
|
+
}
|
|
12
|
+
async function init() {
|
|
13
|
+
log(`
|
|
14
|
+
Welcome to ${(0, helpers_js_1.bold)('GraphQL Code Generator')}!
|
|
15
|
+
Answer few questions and we will setup everything for you.
|
|
16
|
+
`);
|
|
17
|
+
const possibleTargets = await (0, targets_js_1.guessTargets)();
|
|
18
|
+
const answers = await (0, questions_js_1.getAnswers)(possibleTargets);
|
|
19
|
+
// define config
|
|
20
|
+
const config = {
|
|
21
|
+
overwrite: true,
|
|
22
|
+
schema: answers.schema,
|
|
23
|
+
...(answers.targets.includes(types_js_1.Tags.client) ||
|
|
24
|
+
answers.targets.includes(types_js_1.Tags.angular) ||
|
|
25
|
+
answers.targets.includes(types_js_1.Tags.stencil)
|
|
26
|
+
? { documents: answers.documents }
|
|
27
|
+
: {}),
|
|
28
|
+
generates: {
|
|
29
|
+
[answers.output]: {
|
|
30
|
+
...(answers.targets.includes(types_js_1.Tags.client) ? { preset: 'client' } : {}),
|
|
31
|
+
plugins: answers.plugins ? answers.plugins.map(p => p.value) : [],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
// introspection
|
|
36
|
+
if (answers.introspection) {
|
|
37
|
+
addIntrospection(config);
|
|
38
|
+
}
|
|
39
|
+
// config file
|
|
40
|
+
const { relativePath } = await (0, helpers_js_1.writeConfig)(answers, config);
|
|
41
|
+
log(`Fetching latest versions of selected plugins...`);
|
|
42
|
+
// write package.json
|
|
43
|
+
await (0, helpers_js_1.writePackage)(answers, relativePath);
|
|
44
|
+
// Emit status to the terminal
|
|
45
|
+
log(`
|
|
46
|
+
Config file generated at ${(0, helpers_js_1.bold)(relativePath)}
|
|
47
|
+
|
|
48
|
+
${(0, helpers_js_1.bold)('$ npm install')}
|
|
49
|
+
|
|
50
|
+
To install the plugins.
|
|
51
|
+
|
|
52
|
+
${(0, helpers_js_1.bold)(`$ npm run ${answers.script}`)}
|
|
53
|
+
|
|
54
|
+
To run GraphQL Code Generator.
|
|
55
|
+
`);
|
|
56
|
+
}
|
|
57
|
+
// adds an introspection to `generates`
|
|
58
|
+
function addIntrospection(config) {
|
|
59
|
+
config.generates['./graphql.schema.json'] = {
|
|
60
|
+
plugins: ['introspection'],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.plugins = void 0;
|
|
4
|
+
const helpers_js_1 = require("./helpers.js");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
6
|
+
exports.plugins = [
|
|
7
|
+
{
|
|
8
|
+
name: `TypeScript ${(0, helpers_js_1.italic)('(required by other typescript plugins)')}`,
|
|
9
|
+
package: '@graphql-codegen/typescript',
|
|
10
|
+
value: 'typescript',
|
|
11
|
+
pathInRepo: 'typescript/typescript',
|
|
12
|
+
available: hasTag(types_js_1.Tags.typescript),
|
|
13
|
+
shouldBeSelected: tags => oneOf(tags, types_js_1.Tags.angular, types_js_1.Tags.stencil) || allOf(tags, types_js_1.Tags.typescript, types_js_1.Tags.react) || noneOf(tags, types_js_1.Tags.flow),
|
|
14
|
+
defaultExtension: '.ts',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: `TypeScript Operations ${(0, helpers_js_1.italic)('(operations and fragments)')}`,
|
|
18
|
+
package: '@graphql-codegen/typescript-operations',
|
|
19
|
+
value: 'typescript-operations',
|
|
20
|
+
pathInRepo: 'typescript/operations',
|
|
21
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
22
|
+
shouldBeSelected: tags => oneOf(tags, types_js_1.Tags.angular, types_js_1.Tags.stencil) || allOf(tags, types_js_1.Tags.typescript, types_js_1.Tags.react),
|
|
23
|
+
defaultExtension: '.ts',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: `TypeScript Resolvers ${(0, helpers_js_1.italic)('(strongly typed resolve functions)')}`,
|
|
27
|
+
package: '@graphql-codegen/typescript-resolvers',
|
|
28
|
+
value: 'typescript-resolvers',
|
|
29
|
+
pathInRepo: 'typescript/resolvers',
|
|
30
|
+
available: tags => allOf(tags, types_js_1.Tags.node, types_js_1.Tags.typescript),
|
|
31
|
+
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.flow),
|
|
32
|
+
defaultExtension: '.ts',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: `Flow ${(0, helpers_js_1.italic)('(required by other flow plugins)')}`,
|
|
36
|
+
package: '@graphql-codegen/flow',
|
|
37
|
+
value: 'flow',
|
|
38
|
+
pathInRepo: 'flow/flow',
|
|
39
|
+
available: hasTag(types_js_1.Tags.flow),
|
|
40
|
+
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.typescript),
|
|
41
|
+
defaultExtension: '.js',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: `Flow Operations ${(0, helpers_js_1.italic)('(operations and fragments)')}`,
|
|
45
|
+
package: '@graphql-codegen/flow-operations',
|
|
46
|
+
value: 'flow-operations',
|
|
47
|
+
pathInRepo: 'flow/operations',
|
|
48
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.flow),
|
|
49
|
+
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.typescript),
|
|
50
|
+
defaultExtension: '.js',
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: `Flow Resolvers ${(0, helpers_js_1.italic)('(strongly typed resolve functions)')}`,
|
|
54
|
+
package: '@graphql-codegen/flow-resolvers',
|
|
55
|
+
value: 'flow-resolvers',
|
|
56
|
+
pathInRepo: 'flow/resolvers',
|
|
57
|
+
available: tags => allOf(tags, types_js_1.Tags.node, types_js_1.Tags.flow),
|
|
58
|
+
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.typescript),
|
|
59
|
+
defaultExtension: '.js',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: `TypeScript Stencil Apollo ${(0, helpers_js_1.italic)('(typed components)')}`,
|
|
63
|
+
package: '@graphql-codegen/typescript-stencil-apollo',
|
|
64
|
+
value: 'typescript-stencil-apollo',
|
|
65
|
+
pathInRepo: 'typescript/stencil-apollo',
|
|
66
|
+
available: hasTag(types_js_1.Tags.stencil),
|
|
67
|
+
shouldBeSelected: () => true,
|
|
68
|
+
defaultExtension: '.tsx',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: `TypeScript MongoDB ${(0, helpers_js_1.italic)('(typed MongoDB objects)')}`,
|
|
72
|
+
package: '@graphql-codegen/typescript-mongodb',
|
|
73
|
+
value: 'typescript-mongodb',
|
|
74
|
+
pathInRepo: 'typescript/mongodb',
|
|
75
|
+
available: tags => allOf(tags, types_js_1.Tags.node, types_js_1.Tags.typescript),
|
|
76
|
+
shouldBeSelected: () => false,
|
|
77
|
+
defaultExtension: '.ts',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: `TypeScript GraphQL files modules ${(0, helpers_js_1.italic)('(declarations for .graphql files)')}`,
|
|
81
|
+
package: '@graphql-codegen/typescript-graphql-files-modules',
|
|
82
|
+
value: 'typescript-graphql-files-modules',
|
|
83
|
+
pathInRepo: 'typescript/graphql-files-modules',
|
|
84
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
85
|
+
shouldBeSelected: () => false,
|
|
86
|
+
defaultExtension: '.ts',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: `TypeScript GraphQL document nodes ${(0, helpers_js_1.italic)('(embedded GraphQL document)')}`,
|
|
90
|
+
package: '@graphql-codegen/typescript-document-nodes',
|
|
91
|
+
value: 'typescript-document-nodes',
|
|
92
|
+
pathInRepo: 'typescript/document-nodes',
|
|
93
|
+
available: tags => allOf(tags, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
94
|
+
shouldBeSelected: () => false,
|
|
95
|
+
defaultExtension: '.ts',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: `Introspection Fragment Matcher ${(0, helpers_js_1.italic)('(for Apollo Client)')}`,
|
|
99
|
+
package: '@graphql-codegen/fragment-matcher',
|
|
100
|
+
value: 'fragment-matcher',
|
|
101
|
+
pathInRepo: 'other/fragment-matcher',
|
|
102
|
+
available: tags => hasTag(types_js_1.Tags.client)(tags) || hasTag(types_js_1.Tags.angular)(tags) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
103
|
+
shouldBeSelected: () => false,
|
|
104
|
+
defaultExtension: '.ts',
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: `Urql Introspection ${(0, helpers_js_1.italic)('(for Urql Client)')}`,
|
|
108
|
+
package: '@graphql-codegen/urql-introspection',
|
|
109
|
+
value: 'urql-introspection',
|
|
110
|
+
pathInRepo: 'other/urql-introspection',
|
|
111
|
+
available: tags => hasTag(types_js_1.Tags.client)(tags) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
112
|
+
shouldBeSelected: () => false,
|
|
113
|
+
defaultExtension: '.ts',
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: `TypeScript Apollo Angular ${(0, helpers_js_1.italic)('(typed GQL services)')}`,
|
|
117
|
+
package: '@graphql-codegen/typescript-apollo-angular',
|
|
118
|
+
value: 'typescript-apollo-angular',
|
|
119
|
+
pathInRepo: 'typescript/apollo-angular',
|
|
120
|
+
available: hasTag(types_js_1.Tags.angular),
|
|
121
|
+
shouldBeSelected: () => true,
|
|
122
|
+
defaultExtension: '.ts',
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
function hasTag(tag) {
|
|
126
|
+
return (tags) => tags.includes(tag);
|
|
127
|
+
}
|
|
128
|
+
function oneOf(list, ...items) {
|
|
129
|
+
return list.some(i => items.includes(i));
|
|
130
|
+
}
|
|
131
|
+
function noneOf(list, ...items) {
|
|
132
|
+
return !list.some(i => items.includes(i));
|
|
133
|
+
}
|
|
134
|
+
function allOf(list, ...items) {
|
|
135
|
+
return items.every(i => list.includes(i));
|
|
136
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAnswers = getAnswers;
|
|
4
|
+
exports.getApplicationTypeChoices = getApplicationTypeChoices;
|
|
5
|
+
exports.getPluginChoices = getPluginChoices;
|
|
6
|
+
const prompts_1 = require("@inquirer/prompts");
|
|
7
|
+
const helpers_js_1 = require("./helpers.js");
|
|
8
|
+
const plugins_js_1 = require("./plugins.js");
|
|
9
|
+
const types_js_1 = require("./types.js");
|
|
10
|
+
async function getAnswers(possibleTargets) {
|
|
11
|
+
try {
|
|
12
|
+
const targetChoices = getApplicationTypeChoices(possibleTargets);
|
|
13
|
+
const targets = await (0, prompts_1.select)({
|
|
14
|
+
message: `What type of application are you building?`,
|
|
15
|
+
choices: targetChoices,
|
|
16
|
+
default: targetChoices.find(c => c.checked)?.value,
|
|
17
|
+
});
|
|
18
|
+
const schema = await (0, prompts_1.input)({
|
|
19
|
+
message: `Where is your schema?: ${(0, helpers_js_1.grey)('(path or url)')}`,
|
|
20
|
+
default: 'http://localhost:4000', // matches Apollo Server's default
|
|
21
|
+
validate: str => str.length > 0,
|
|
22
|
+
});
|
|
23
|
+
let documents;
|
|
24
|
+
if (targets.includes(types_js_1.Tags.client) || targets.includes(types_js_1.Tags.angular) || targets.includes(types_js_1.Tags.stencil))
|
|
25
|
+
documents = await (0, prompts_1.input)({
|
|
26
|
+
message: 'Where are your operations and fragments?:',
|
|
27
|
+
default: getDocumentsDefaultValue(targets),
|
|
28
|
+
validate: str => str.length > 0,
|
|
29
|
+
});
|
|
30
|
+
let plugins;
|
|
31
|
+
if (!targets.includes(types_js_1.Tags.client)) {
|
|
32
|
+
plugins = await (0, prompts_1.checkbox)({
|
|
33
|
+
message: 'Pick plugins:',
|
|
34
|
+
choices: getPluginChoices(targets),
|
|
35
|
+
validate: plugins => plugins.length > 0,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const output = await (0, prompts_1.input)({
|
|
39
|
+
message: 'Where to write the output:',
|
|
40
|
+
default: getOutputDefaultValue({ targets, plugins }),
|
|
41
|
+
validate: str => str.length > 0,
|
|
42
|
+
});
|
|
43
|
+
const introspection = await (0, prompts_1.confirm)({
|
|
44
|
+
message: 'Do you want to generate an introspection file?',
|
|
45
|
+
default: false,
|
|
46
|
+
});
|
|
47
|
+
const config = await (0, prompts_1.input)({
|
|
48
|
+
message: 'How to name the config file?',
|
|
49
|
+
default: (() => targets.includes(types_js_1.Tags.client) || targets.includes(types_js_1.Tags.typescript) || targets.includes(types_js_1.Tags.angular)
|
|
50
|
+
? 'codegen.ts'
|
|
51
|
+
: 'codegen.yml')(),
|
|
52
|
+
validate: str => {
|
|
53
|
+
const isNotEmpty = str.length > 0;
|
|
54
|
+
const hasCorrectExtension = ['json', 'yml', 'yaml', 'js', 'ts'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
|
|
55
|
+
return isNotEmpty && hasCorrectExtension;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const script = await (0, prompts_1.input)({
|
|
59
|
+
default: 'codegen',
|
|
60
|
+
message: 'What script in package.json should run the codegen?',
|
|
61
|
+
validate: (str) => str.length > 0,
|
|
62
|
+
});
|
|
63
|
+
return {
|
|
64
|
+
targets,
|
|
65
|
+
schema,
|
|
66
|
+
documents,
|
|
67
|
+
plugins,
|
|
68
|
+
output,
|
|
69
|
+
introspection,
|
|
70
|
+
config,
|
|
71
|
+
script,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
76
|
+
// This error because user exited using CMD+C, just exit gracefully or else user would see an ugly error message
|
|
77
|
+
// https://github.com/SBoudrias/Inquirer.js/blob/ee16061a1e3f99a6cc714a3d473f7cd12b06a3f1/packages/prompts/README.md#handling-ctrlc-gracefully
|
|
78
|
+
process.exit();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function getApplicationTypeChoices(possibleTargets) {
|
|
86
|
+
function withFlowOrTypescript(tags) {
|
|
87
|
+
if (possibleTargets.TypeScript) {
|
|
88
|
+
tags.push(types_js_1.Tags.typescript);
|
|
89
|
+
}
|
|
90
|
+
else if (possibleTargets.Flow) {
|
|
91
|
+
tags.push(types_js_1.Tags.flow);
|
|
92
|
+
}
|
|
93
|
+
else if (possibleTargets.Node) {
|
|
94
|
+
tags.push(types_js_1.Tags.typescript, types_js_1.Tags.flow);
|
|
95
|
+
}
|
|
96
|
+
return tags;
|
|
97
|
+
}
|
|
98
|
+
return [
|
|
99
|
+
{
|
|
100
|
+
name: 'Backend - API or server',
|
|
101
|
+
key: 'backend',
|
|
102
|
+
value: withFlowOrTypescript([types_js_1.Tags.node]),
|
|
103
|
+
checked: possibleTargets.Node,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: 'Application built with Angular',
|
|
107
|
+
key: 'angular',
|
|
108
|
+
value: [types_js_1.Tags.angular],
|
|
109
|
+
checked: possibleTargets.Angular,
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'Application built with React',
|
|
113
|
+
key: 'react',
|
|
114
|
+
value: withFlowOrTypescript([types_js_1.Tags.react, types_js_1.Tags.client]),
|
|
115
|
+
checked: possibleTargets.React,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: 'Application built with Stencil',
|
|
119
|
+
key: 'stencil',
|
|
120
|
+
value: [types_js_1.Tags.stencil, types_js_1.Tags.typescript],
|
|
121
|
+
checked: possibleTargets.Stencil,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'Application built with Vue',
|
|
125
|
+
key: 'vue',
|
|
126
|
+
value: [types_js_1.Tags.vue, types_js_1.Tags.client],
|
|
127
|
+
checked: possibleTargets.Vue,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: 'Application using graphql-request',
|
|
131
|
+
key: 'graphqlRequest',
|
|
132
|
+
value: [types_js_1.Tags.graphqlRequest, types_js_1.Tags.client],
|
|
133
|
+
checked: possibleTargets.graphqlRequest,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'Application built with other framework or vanilla JS',
|
|
137
|
+
key: 'client',
|
|
138
|
+
value: [types_js_1.Tags.typescript, types_js_1.Tags.flow],
|
|
139
|
+
checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil,
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
function getPluginChoices(targets) {
|
|
144
|
+
return plugins_js_1.plugins
|
|
145
|
+
.filter(p => p.available(targets))
|
|
146
|
+
.map(p => {
|
|
147
|
+
return {
|
|
148
|
+
name: p.name,
|
|
149
|
+
value: p,
|
|
150
|
+
checked: p.shouldBeSelected(targets),
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function getOutputDefaultValue({ targets, plugins }) {
|
|
155
|
+
if (targets.includes(types_js_1.Tags.client)) {
|
|
156
|
+
return 'src/gql/';
|
|
157
|
+
}
|
|
158
|
+
if (plugins.some(plugin => plugin.defaultExtension === '.tsx')) {
|
|
159
|
+
return 'src/generated/graphql.tsx';
|
|
160
|
+
}
|
|
161
|
+
if (plugins.some(plugin => plugin.defaultExtension === '.ts')) {
|
|
162
|
+
return 'src/generated/graphql.ts';
|
|
163
|
+
}
|
|
164
|
+
return 'src/generated/graphql.js';
|
|
165
|
+
}
|
|
166
|
+
function getDocumentsDefaultValue(targets) {
|
|
167
|
+
if (targets.includes(types_js_1.Tags.vue)) {
|
|
168
|
+
return 'src/**/*.vue';
|
|
169
|
+
}
|
|
170
|
+
if (targets.includes(types_js_1.Tags.angular)) {
|
|
171
|
+
return 'src/**/*.ts';
|
|
172
|
+
}
|
|
173
|
+
if (targets.includes(types_js_1.Tags.client)) {
|
|
174
|
+
return 'src/**/*.tsx';
|
|
175
|
+
}
|
|
176
|
+
return 'src/**/*.graphql';
|
|
177
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.guessTargets = guessTargets;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const types_js_1 = require("./types.js");
|
|
7
|
+
async function guessTargets() {
|
|
8
|
+
const pkg = JSON.parse((0, fs_1.readFileSync)((0, path_1.resolve)(process.cwd(), 'package.json'), 'utf8'));
|
|
9
|
+
const dependencies = Object.keys({
|
|
10
|
+
...pkg.dependencies,
|
|
11
|
+
...pkg.devDependencies,
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
[types_js_1.Tags.angular]: isAngular(dependencies),
|
|
15
|
+
[types_js_1.Tags.react]: isReact(dependencies),
|
|
16
|
+
[types_js_1.Tags.stencil]: isStencil(dependencies),
|
|
17
|
+
[types_js_1.Tags.vue]: isVue(dependencies),
|
|
18
|
+
[types_js_1.Tags.client]: false,
|
|
19
|
+
[types_js_1.Tags.node]: false,
|
|
20
|
+
[types_js_1.Tags.typescript]: isTypescript(dependencies),
|
|
21
|
+
[types_js_1.Tags.flow]: isFlow(dependencies),
|
|
22
|
+
[types_js_1.Tags.graphqlRequest]: isGraphqlRequest(dependencies),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function isAngular(dependencies) {
|
|
26
|
+
return dependencies.includes('@angular/core');
|
|
27
|
+
}
|
|
28
|
+
function isReact(dependencies) {
|
|
29
|
+
return dependencies.includes('react');
|
|
30
|
+
}
|
|
31
|
+
function isStencil(dependencies) {
|
|
32
|
+
return dependencies.includes('@stencil/core');
|
|
33
|
+
}
|
|
34
|
+
function isVue(dependencies) {
|
|
35
|
+
return dependencies.includes('vue') || dependencies.includes('nuxt');
|
|
36
|
+
}
|
|
37
|
+
function isTypescript(dependencies) {
|
|
38
|
+
return dependencies.includes('typescript');
|
|
39
|
+
}
|
|
40
|
+
function isFlow(dependencies) {
|
|
41
|
+
return dependencies.includes('flow');
|
|
42
|
+
}
|
|
43
|
+
function isGraphqlRequest(dependencies) {
|
|
44
|
+
return dependencies.includes('graphql-request');
|
|
45
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tags = void 0;
|
|
4
|
+
var Tags;
|
|
5
|
+
(function (Tags) {
|
|
6
|
+
Tags["client"] = "Browser";
|
|
7
|
+
Tags["node"] = "Node";
|
|
8
|
+
Tags["typescript"] = "TypeScript";
|
|
9
|
+
Tags["flow"] = "Flow";
|
|
10
|
+
Tags["angular"] = "Angular";
|
|
11
|
+
Tags["stencil"] = "Stencil";
|
|
12
|
+
Tags["react"] = "React";
|
|
13
|
+
Tags["vue"] = "Vue";
|
|
14
|
+
Tags["graphqlRequest"] = "graphqlRequest";
|
|
15
|
+
})(Tags || (exports.Tags = Tags = {}));
|