@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/load.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultDocumentsLoadOptions = exports.defaultSchemaLoadOptions = void 0;
|
|
4
|
+
exports.loadSchema = loadSchema;
|
|
5
|
+
exports.loadDocuments = loadDocuments;
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const apollo_engine_loader_1 = require("@graphql-tools/apollo-engine-loader");
|
|
8
|
+
const code_file_loader_1 = require("@graphql-tools/code-file-loader");
|
|
9
|
+
const git_loader_1 = require("@graphql-tools/git-loader");
|
|
10
|
+
const github_loader_1 = require("@graphql-tools/github-loader");
|
|
11
|
+
const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
|
|
12
|
+
const json_file_loader_1 = require("@graphql-tools/json-file-loader");
|
|
13
|
+
const load_1 = require("@graphql-tools/load");
|
|
14
|
+
const url_loader_1 = require("@graphql-tools/url-loader");
|
|
15
|
+
const graphql_1 = require("graphql");
|
|
16
|
+
exports.defaultSchemaLoadOptions = {
|
|
17
|
+
assumeValidSDL: true,
|
|
18
|
+
sort: true,
|
|
19
|
+
convertExtensions: true,
|
|
20
|
+
includeSources: true,
|
|
21
|
+
};
|
|
22
|
+
exports.defaultDocumentsLoadOptions = {
|
|
23
|
+
sort: true,
|
|
24
|
+
skipGraphQLImport: true,
|
|
25
|
+
};
|
|
26
|
+
async function loadSchema(schemaPointers, config) {
|
|
27
|
+
try {
|
|
28
|
+
const loaders = [
|
|
29
|
+
new code_file_loader_1.CodeFileLoader(),
|
|
30
|
+
new git_loader_1.GitLoader(),
|
|
31
|
+
new github_loader_1.GithubLoader(),
|
|
32
|
+
new graphql_file_loader_1.GraphQLFileLoader(),
|
|
33
|
+
new json_file_loader_1.JsonFileLoader(),
|
|
34
|
+
new url_loader_1.UrlLoader(),
|
|
35
|
+
new apollo_engine_loader_1.ApolloEngineLoader(),
|
|
36
|
+
];
|
|
37
|
+
const schema = await (0, load_1.loadSchema)(schemaPointers, {
|
|
38
|
+
...exports.defaultSchemaLoadOptions,
|
|
39
|
+
loaders,
|
|
40
|
+
...config,
|
|
41
|
+
...config.config,
|
|
42
|
+
});
|
|
43
|
+
return schema;
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
throw new Error([
|
|
47
|
+
`Failed to load schema from ${Object.keys(schemaPointers).join(',')}:`,
|
|
48
|
+
printError(e),
|
|
49
|
+
'\nGraphQL Code Generator supports:',
|
|
50
|
+
'\n- ES Modules and CommonJS exports (export as default or named export "schema")',
|
|
51
|
+
'- Introspection JSON File',
|
|
52
|
+
'- URL of GraphQL endpoint',
|
|
53
|
+
'- Multiple files with type definitions (glob expression)',
|
|
54
|
+
'- String in config file',
|
|
55
|
+
'\nTry to use one of above options and run codegen again.\n',
|
|
56
|
+
].join('\n'));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function loadDocuments(documentPointers, config) {
|
|
60
|
+
const loaders = [
|
|
61
|
+
new code_file_loader_1.CodeFileLoader({
|
|
62
|
+
pluckConfig: {
|
|
63
|
+
skipIndent: true,
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
new git_loader_1.GitLoader(),
|
|
67
|
+
new github_loader_1.GithubLoader(),
|
|
68
|
+
new graphql_file_loader_1.GraphQLFileLoader(),
|
|
69
|
+
];
|
|
70
|
+
const ignore = [];
|
|
71
|
+
for (const generatePath of Object.keys(config.generates)) {
|
|
72
|
+
if ((0, path_1.extname)(generatePath) === '') {
|
|
73
|
+
// we omit paths that don't resolve to a specific file
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
ignore.push((0, path_1.join)(process.cwd(), generatePath));
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const loadedFromToolkit = await (0, load_1.loadDocuments)(documentPointers, {
|
|
80
|
+
...exports.defaultDocumentsLoadOptions,
|
|
81
|
+
ignore,
|
|
82
|
+
loaders,
|
|
83
|
+
...config,
|
|
84
|
+
...config.config,
|
|
85
|
+
});
|
|
86
|
+
return loadedFromToolkit;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
// NoTypeDefinitionsFound from `@graphql-tools/load` already has a message with pointer, so we can just rethrow the error
|
|
90
|
+
if (error instanceof load_1.NoTypeDefinitionsFound) {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
// For other errors, we need to add an error message with documentPointers, so it's better for DevX
|
|
94
|
+
throw new Error([`Failed to load documents from ${Object.keys(documentPointers).join(',')}:`, printError(error)].join('\n'));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const printError = (error) => {
|
|
98
|
+
if (error instanceof graphql_1.GraphQLError) {
|
|
99
|
+
return String(error);
|
|
100
|
+
}
|
|
101
|
+
return [String(error.message || error), String(error.stack)].join('\n');
|
|
102
|
+
};
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/plugins.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPluginByName = getPluginByName;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
async function getPluginByName(name, pluginLoader) {
|
|
6
|
+
const possibleNames = [
|
|
7
|
+
`@graphql-codegen/${name}`,
|
|
8
|
+
`@graphql-codegen/${name}-template`,
|
|
9
|
+
`@graphql-codegen/${name}-plugin`,
|
|
10
|
+
`graphql-codegen-${name}`,
|
|
11
|
+
`graphql-codegen-${name}-template`,
|
|
12
|
+
`graphql-codegen-${name}-plugin`,
|
|
13
|
+
`codegen-${name}`,
|
|
14
|
+
`codegen-${name}-template`,
|
|
15
|
+
name,
|
|
16
|
+
];
|
|
17
|
+
const possibleModules = possibleNames.concat((0, path_1.resolve)(process.cwd(), name));
|
|
18
|
+
for (const moduleName of possibleModules) {
|
|
19
|
+
try {
|
|
20
|
+
return await pluginLoader(moduleName);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') {
|
|
24
|
+
throw new Error(`
|
|
25
|
+
Unable to load template plugin matching '${name}'.
|
|
26
|
+
Reason:
|
|
27
|
+
${err.message}
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const possibleNamesMsg = possibleNames
|
|
33
|
+
.map(name => `
|
|
34
|
+
- ${name}
|
|
35
|
+
`.trimEnd())
|
|
36
|
+
.join('');
|
|
37
|
+
throw new Error(`
|
|
38
|
+
Unable to find template plugin matching '${name}'
|
|
39
|
+
Install one of the following packages:
|
|
40
|
+
|
|
41
|
+
${possibleNamesMsg}
|
|
42
|
+
`);
|
|
43
|
+
}
|
package/cjs/presets.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPresetByName = getPresetByName;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
async function getPresetByName(name, loader) {
|
|
6
|
+
const possibleNames = [
|
|
7
|
+
`@graphql-codegen/${name}`,
|
|
8
|
+
`@graphql-codegen/${name}-preset`,
|
|
9
|
+
name,
|
|
10
|
+
(0, path_1.resolve)(process.cwd(), name),
|
|
11
|
+
];
|
|
12
|
+
for (const moduleName of possibleNames) {
|
|
13
|
+
try {
|
|
14
|
+
const loaded = await loader(moduleName);
|
|
15
|
+
if (loaded?.preset) {
|
|
16
|
+
return loaded.preset;
|
|
17
|
+
}
|
|
18
|
+
if (loaded?.default) {
|
|
19
|
+
return loaded.default;
|
|
20
|
+
}
|
|
21
|
+
return loaded;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
if (
|
|
25
|
+
/** CJS Error code */
|
|
26
|
+
err.code !== 'MODULE_NOT_FOUND' &&
|
|
27
|
+
/** ESM Error code */
|
|
28
|
+
err.code !== 'ERR_MODULE_NOT_FOUND') {
|
|
29
|
+
throw new Error(`Unable to load preset matching ${name}
|
|
30
|
+
|
|
31
|
+
Unable to load preset matching '${name}'.
|
|
32
|
+
Reason:
|
|
33
|
+
${err.message}
|
|
34
|
+
`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const possibleNamesMsg = possibleNames
|
|
39
|
+
.map(name => `
|
|
40
|
+
- ${name}
|
|
41
|
+
`.trimEnd())
|
|
42
|
+
.join('');
|
|
43
|
+
throw new Error(`Unable to find preset matching ${name}
|
|
44
|
+
|
|
45
|
+
Unable to find preset matching '${name}'
|
|
46
|
+
Install one of the following packages:
|
|
47
|
+
|
|
48
|
+
${possibleNamesMsg}
|
|
49
|
+
`);
|
|
50
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbortController = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
5
|
+
const debugging_js_1 = require("./debugging.js");
|
|
6
|
+
/**
|
|
7
|
+
* Node v14 does not have AbortSignal or AbortController, so to safely use it in
|
|
8
|
+
* another module, you can import it from here.
|
|
9
|
+
*
|
|
10
|
+
* Node v14.7+ does have it, but only with flag --experimental-abortcontroller
|
|
11
|
+
*
|
|
12
|
+
* We don't actually use AbortController anywhere except in tests, but it
|
|
13
|
+
* still gets called in watcher.ts, so by polyfilling it we can avoid breaking
|
|
14
|
+
* existing installations using Node v14 without flag --experimental-abortcontroller,
|
|
15
|
+
* and we also ensure that tests continue to pass under Node v14 without any new flags.
|
|
16
|
+
*
|
|
17
|
+
* This polyfill was adapted (TypeScript-ified) from here:
|
|
18
|
+
* https://github.com/southpolesteve/node-abort-controller/blob/master/index.js
|
|
19
|
+
*/
|
|
20
|
+
class AbortSignalPolyfill {
|
|
21
|
+
eventEmitter;
|
|
22
|
+
onabort;
|
|
23
|
+
aborted;
|
|
24
|
+
reason;
|
|
25
|
+
constructor() {
|
|
26
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
27
|
+
this.onabort = null;
|
|
28
|
+
this.aborted = false;
|
|
29
|
+
this.reason = undefined;
|
|
30
|
+
}
|
|
31
|
+
toString() {
|
|
32
|
+
return '[object AbortSignal]';
|
|
33
|
+
}
|
|
34
|
+
get [Symbol.toStringTag]() {
|
|
35
|
+
return 'AbortSignal';
|
|
36
|
+
}
|
|
37
|
+
removeEventListener(name, handler) {
|
|
38
|
+
this.eventEmitter.removeListener(name, handler);
|
|
39
|
+
}
|
|
40
|
+
addEventListener(name, handler) {
|
|
41
|
+
this.eventEmitter.on(name, handler);
|
|
42
|
+
}
|
|
43
|
+
// @ts-expect-error No Event type in Node 14
|
|
44
|
+
dispatchEvent(type) {
|
|
45
|
+
const event = { type, target: this };
|
|
46
|
+
const handlerName = `on${event.type}`;
|
|
47
|
+
if (typeof this[handlerName] === 'function')
|
|
48
|
+
this[handlerName](event);
|
|
49
|
+
return this.eventEmitter.emit(event.type, event);
|
|
50
|
+
}
|
|
51
|
+
throwIfAborted() {
|
|
52
|
+
if (this.aborted) {
|
|
53
|
+
throw this.reason;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static abort(reason) {
|
|
57
|
+
const controller = new AbortController();
|
|
58
|
+
controller.abort(reason);
|
|
59
|
+
return controller.signal;
|
|
60
|
+
}
|
|
61
|
+
static timeout(time) {
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
setTimeout(() => controller.abort(new Error('TimeoutError')), time);
|
|
64
|
+
return controller.signal;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const AbortSignal = global.AbortSignal ?? AbortSignalPolyfill;
|
|
68
|
+
class AbortControllerPolyfill {
|
|
69
|
+
signal;
|
|
70
|
+
constructor() {
|
|
71
|
+
(0, debugging_js_1.debugLog)('Using polyfilled AbortController');
|
|
72
|
+
// @ts-expect-error No Event type in Node 14
|
|
73
|
+
this.signal = new AbortSignal();
|
|
74
|
+
}
|
|
75
|
+
abort(reason) {
|
|
76
|
+
if (this.signal.aborted)
|
|
77
|
+
return;
|
|
78
|
+
// @ts-expect-error Not a read only property when polyfilling
|
|
79
|
+
this.signal.aborted = true;
|
|
80
|
+
if (reason) {
|
|
81
|
+
// @ts-expect-error Not a read only property when polyfilling
|
|
82
|
+
this.signal.reason = reason;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// @ts-expect-error Not a read only property when polyfilling
|
|
86
|
+
this.signal.reason = new Error('AbortError');
|
|
87
|
+
}
|
|
88
|
+
// @ts-expect-error No Event type in Node 14
|
|
89
|
+
this.signal.dispatchEvent('abort');
|
|
90
|
+
}
|
|
91
|
+
toString() {
|
|
92
|
+
return '[object AbortController]';
|
|
93
|
+
}
|
|
94
|
+
get [Symbol.toStringTag]() {
|
|
95
|
+
return 'AbortController';
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const AbortController = global.AbortController ?? AbortControllerPolyfill;
|
|
99
|
+
exports.AbortController = AbortController;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isListrError = isListrError;
|
|
4
|
+
exports.cliError = cliError;
|
|
5
|
+
function isListrError(err) {
|
|
6
|
+
return err.name === 'ListrError' && Array.isArray(err.errors) && err.errors.length > 0;
|
|
7
|
+
}
|
|
8
|
+
function cliError(err, exitOnError = true) {
|
|
9
|
+
let msg;
|
|
10
|
+
if (err instanceof Error) {
|
|
11
|
+
msg = err;
|
|
12
|
+
}
|
|
13
|
+
else if (typeof err === 'string') {
|
|
14
|
+
msg = err;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
msg = JSON.stringify(err);
|
|
18
|
+
}
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
console.error(msg);
|
|
21
|
+
if (exitOnError) {
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.debugLog = debugLog;
|
|
4
|
+
exports.printLogs = printLogs;
|
|
5
|
+
exports.resetLogs = resetLogs;
|
|
6
|
+
const logger_js_1 = require("./logger.js");
|
|
7
|
+
let queue = [];
|
|
8
|
+
function debugLog(message, ...meta) {
|
|
9
|
+
queue.push({
|
|
10
|
+
message,
|
|
11
|
+
meta,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function printLogs() {
|
|
15
|
+
for (const log of queue) {
|
|
16
|
+
(0, logger_js_1.getLogger)().info(log.message, ...log.meta);
|
|
17
|
+
}
|
|
18
|
+
resetLogs();
|
|
19
|
+
}
|
|
20
|
+
function resetLogs() {
|
|
21
|
+
queue = [];
|
|
22
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.access = access;
|
|
4
|
+
exports.writeFile = writeFile;
|
|
5
|
+
exports.readFile = readFile;
|
|
6
|
+
exports.unlinkFile = unlinkFile;
|
|
7
|
+
exports.mkdirp = mkdirp;
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const { access: fsAccess, writeFile: fsWriteFile, readFile: fsReadFile, mkdir } = fs_1.promises;
|
|
10
|
+
function access(...args) {
|
|
11
|
+
return fsAccess(...args);
|
|
12
|
+
}
|
|
13
|
+
function writeFile(filepath, content) {
|
|
14
|
+
return fsWriteFile(filepath, content);
|
|
15
|
+
}
|
|
16
|
+
function readFile(filepath) {
|
|
17
|
+
return fsReadFile(filepath, 'utf-8');
|
|
18
|
+
}
|
|
19
|
+
function unlinkFile(filePath, cb) {
|
|
20
|
+
(0, fs_1.unlink)(filePath, cb);
|
|
21
|
+
}
|
|
22
|
+
function mkdirp(filePath) {
|
|
23
|
+
return mkdir(filePath, { recursive: true });
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLatestVersion = getLatestVersion;
|
|
4
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
5
|
+
/**
|
|
6
|
+
* Fetches the version directly from the registry instead of depending on
|
|
7
|
+
* an ESM only module as latest-version does.
|
|
8
|
+
* @param packageName
|
|
9
|
+
*/
|
|
10
|
+
async function getLatestVersion(packageName) {
|
|
11
|
+
return (0, fetch_1.fetch)(`https://unpkg.com/${packageName}/package.json`)
|
|
12
|
+
.then(res => res.json())
|
|
13
|
+
.then(pkg => pkg.version);
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogger = getLogger;
|
|
4
|
+
exports.setLogger = setLogger;
|
|
5
|
+
exports.setSilentLogger = setSilentLogger;
|
|
6
|
+
exports.useWinstonLogger = useWinstonLogger;
|
|
7
|
+
const ts_log_1 = require("ts-log");
|
|
8
|
+
let logger;
|
|
9
|
+
function getLogger() {
|
|
10
|
+
return logger || ts_log_1.dummyLogger;
|
|
11
|
+
}
|
|
12
|
+
useWinstonLogger();
|
|
13
|
+
function setLogger(newLogger) {
|
|
14
|
+
logger = newLogger;
|
|
15
|
+
}
|
|
16
|
+
function setSilentLogger() {
|
|
17
|
+
logger = ts_log_1.dummyLogger;
|
|
18
|
+
}
|
|
19
|
+
function useWinstonLogger() {
|
|
20
|
+
if (logger?.levels) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
logger = console;
|
|
24
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sortPatterns = exports.makeLocalPatternSet = exports.makeGlobalPatternSet = exports.makeShouldRebuild = exports.allAffirmativePatternsFromPatternSets = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
7
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
8
|
+
const is_glob_1 = tslib_1.__importDefault(require("is-glob"));
|
|
9
|
+
const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
|
|
10
|
+
const helpers_js_1 = require("./helpers.js");
|
|
11
|
+
/**
|
|
12
|
+
* Flatten a list of pattern sets to be a list of only the affirmative patterns
|
|
13
|
+
* are contained in all of them.
|
|
14
|
+
*
|
|
15
|
+
* This can be used, for example, to find the "longest common prefix directory"
|
|
16
|
+
* by examining `mm.scan(pattern).base` for each `pattern`.
|
|
17
|
+
*/
|
|
18
|
+
const allAffirmativePatternsFromPatternSets = (patternSets) => {
|
|
19
|
+
return patternSets.flatMap(patternSet => [
|
|
20
|
+
...patternSet.watch.affirmative,
|
|
21
|
+
...patternSet.documents.affirmative,
|
|
22
|
+
...patternSet.schemas.affirmative,
|
|
23
|
+
]);
|
|
24
|
+
};
|
|
25
|
+
exports.allAffirmativePatternsFromPatternSets = allAffirmativePatternsFromPatternSets;
|
|
26
|
+
/**
|
|
27
|
+
* Create a rebuild trigger that follows the algorithm described here:
|
|
28
|
+
* https://github.com/dotansimha/graphql-code-generator/issues/9270#issuecomment-1496765045
|
|
29
|
+
*
|
|
30
|
+
* There is a flow chart diagram in that comment.
|
|
31
|
+
*
|
|
32
|
+
* Basically:
|
|
33
|
+
*
|
|
34
|
+
* * "Global" patterns are defined at top level of config file, and "local"
|
|
35
|
+
* patterns are defined for each output target
|
|
36
|
+
* * Each pattern can have "watch", "documents", and "schemas"
|
|
37
|
+
* * Watch patterns (global and local) always take precedence over documents and
|
|
38
|
+
* schemas patterns, i.e. a watch negation always negates, and a watch match is
|
|
39
|
+
* a match even if it would be negated by some pattern in documents or schemas
|
|
40
|
+
* * The trigger returns true if any output target's local patterns result in
|
|
41
|
+
* a match, after considering the precedence of any global and local negations
|
|
42
|
+
*/
|
|
43
|
+
const makeShouldRebuild = ({ globalPatternSet, localPatternSets, }) => {
|
|
44
|
+
const localMatchers = localPatternSets.map(localPatternSet => {
|
|
45
|
+
return (path) => {
|
|
46
|
+
// Is path negated by any negating watch pattern?
|
|
47
|
+
if (matchesAnyNegatedPattern(path, [...globalPatternSet.watch.negated, ...localPatternSet.watch.negated])) {
|
|
48
|
+
// Short circut: negations in watch patterns take priority
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
// Does path match any affirmative watch pattern?
|
|
52
|
+
if (matchesAnyAffirmativePattern(path, [
|
|
53
|
+
...globalPatternSet.watch.affirmative,
|
|
54
|
+
...localPatternSet.watch.affirmative,
|
|
55
|
+
])) {
|
|
56
|
+
// Immediately return true: Watch pattern takes priority, even if documents or schema would negate it
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
// Does path match documents patterns (without being negated)?
|
|
60
|
+
if (matchesAnyAffirmativePattern(path, [
|
|
61
|
+
...globalPatternSet.documents.affirmative,
|
|
62
|
+
...localPatternSet.documents.affirmative,
|
|
63
|
+
]) &&
|
|
64
|
+
!matchesAnyNegatedPattern(path, [...globalPatternSet.documents.negated, ...localPatternSet.documents.negated])) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
// Does path match schemas patterns (without being negated)?
|
|
68
|
+
if (matchesAnyAffirmativePattern(path, [
|
|
69
|
+
...globalPatternSet.schemas.affirmative,
|
|
70
|
+
...localPatternSet.schemas.affirmative,
|
|
71
|
+
]) &&
|
|
72
|
+
!matchesAnyNegatedPattern(path, [...globalPatternSet.schemas.negated, ...localPatternSet.schemas.negated])) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
// Otherwise, there is no match
|
|
76
|
+
return false;
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* Return `true` if `path` should trigger a rebuild
|
|
81
|
+
*/
|
|
82
|
+
return ({ path: absolutePath }) => {
|
|
83
|
+
if (!(0, path_1.isAbsolute)(absolutePath)) {
|
|
84
|
+
throw new Error('shouldRebuild trigger should be called with absolute path');
|
|
85
|
+
}
|
|
86
|
+
const path = (0, path_1.relative)(process.cwd(), absolutePath);
|
|
87
|
+
const shouldRebuild = localMatchers.some(matcher => matcher(path));
|
|
88
|
+
return shouldRebuild;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
exports.makeShouldRebuild = makeShouldRebuild;
|
|
92
|
+
/**
|
|
93
|
+
* Create the pattern set for the "global" (top level) config.
|
|
94
|
+
*
|
|
95
|
+
* In the `shouldRebuild` algorithm, any of these watch patterns will take
|
|
96
|
+
* precedence over local configs, and any schemas and documents patterns will be
|
|
97
|
+
* mixed into the pattern set of each local config.
|
|
98
|
+
*/
|
|
99
|
+
const makeGlobalPatternSet = (initialContext) => {
|
|
100
|
+
const config = initialContext.getConfig();
|
|
101
|
+
return {
|
|
102
|
+
watch: (0, exports.sortPatterns)([
|
|
103
|
+
...(typeof config.watch === 'boolean' ? [] : (0, plugin_helpers_1.normalizeInstanceOrArray)(config.watch ?? [])),
|
|
104
|
+
(0, path_1.relative)(process.cwd(), initialContext.filepath),
|
|
105
|
+
]),
|
|
106
|
+
schemas: (0, exports.sortPatterns)(makePatternsFromSchemas((0, plugin_helpers_1.normalizeInstanceOrArray)(config.schema))),
|
|
107
|
+
documents: (0, exports.sortPatterns)(makePatternsFromDocuments((0, plugin_helpers_1.normalizeInstanceOrArray)(config.documents))),
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
exports.makeGlobalPatternSet = makeGlobalPatternSet;
|
|
111
|
+
/**
|
|
112
|
+
* Create the pattern set for a "local" (output target) config
|
|
113
|
+
*
|
|
114
|
+
* In the `shouldRebuild` algorithm, any of these watch patterns will take
|
|
115
|
+
* precedence over documents or schemas patterns, and the documents and schemas
|
|
116
|
+
* patterns will be mixed into the pattern set of their respective gobal pattern
|
|
117
|
+
* set equivalents.
|
|
118
|
+
*/
|
|
119
|
+
const makeLocalPatternSet = (conf) => {
|
|
120
|
+
return {
|
|
121
|
+
watch: (0, exports.sortPatterns)((0, plugin_helpers_1.normalizeInstanceOrArray)(conf.watchPattern)),
|
|
122
|
+
documents: (0, exports.sortPatterns)(makePatternsFromDocuments((0, plugin_helpers_1.normalizeInstanceOrArray)(conf.documents))),
|
|
123
|
+
schemas: (0, exports.sortPatterns)(makePatternsFromSchemas((0, plugin_helpers_1.normalizeInstanceOrArray)(conf.schema))),
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
exports.makeLocalPatternSet = makeLocalPatternSet;
|
|
127
|
+
/**
|
|
128
|
+
* Parse a list of micromatch patterns from a list of documents, which should
|
|
129
|
+
* already have been normalized from their raw config values.
|
|
130
|
+
*/
|
|
131
|
+
const makePatternsFromDocuments = (documents) => {
|
|
132
|
+
const patterns = [];
|
|
133
|
+
if (documents) {
|
|
134
|
+
for (const doc of documents) {
|
|
135
|
+
if (typeof doc === 'string') {
|
|
136
|
+
patterns.push(doc);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
patterns.push(...Object.keys(doc));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return patterns;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Parse a list of micromatch patterns from a list of schemas, which should
|
|
147
|
+
* already have been normalized from their raw config values.
|
|
148
|
+
*/
|
|
149
|
+
const makePatternsFromSchemas = (schemas) => {
|
|
150
|
+
const patterns = [];
|
|
151
|
+
for (const s of schemas) {
|
|
152
|
+
const schema = s;
|
|
153
|
+
if (!(0, helpers_js_1.isURL)(schema) && ((0, is_glob_1.default)(schema) || (0, utils_1.isValidPath)(schema))) {
|
|
154
|
+
patterns.push(schema);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return patterns;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Given a list of micromatch patterns, sort them into `patterns` (all of them),
|
|
161
|
+
* `affirmative` (only the affirmative patterns), and `negated` (only the negated patterns)
|
|
162
|
+
*
|
|
163
|
+
* @param patterns List of micromatch patterns
|
|
164
|
+
*/
|
|
165
|
+
const sortPatterns = (patterns) => ({
|
|
166
|
+
patterns,
|
|
167
|
+
affirmative: onlyAffirmativePatterns(patterns),
|
|
168
|
+
negated: onlyNegatedPatterns(patterns),
|
|
169
|
+
});
|
|
170
|
+
exports.sortPatterns = sortPatterns;
|
|
171
|
+
/**
|
|
172
|
+
* Filter the provided list of patterns to include only "affirmative" (non-negated) patterns.
|
|
173
|
+
*
|
|
174
|
+
* @param patterns List of micromatch patterns (or paths) to filter
|
|
175
|
+
*/
|
|
176
|
+
const onlyAffirmativePatterns = (patterns) => {
|
|
177
|
+
return patterns.filter(pattern => !micromatch_1.default.scan(pattern).negated);
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Filter the provided list of patterns to include only negated patterns.
|
|
181
|
+
*
|
|
182
|
+
* @param patterns List of micromatch patterns (or paths) to filter
|
|
183
|
+
*/
|
|
184
|
+
const onlyNegatedPatterns = (patterns) => {
|
|
185
|
+
return patterns.filter(pattern => micromatch_1.default.scan(pattern).negated);
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Given a list of negated patterns, invert them by removing their negation prefix
|
|
189
|
+
*
|
|
190
|
+
* If there is a non-negated pattern in the list, throw an error, because this
|
|
191
|
+
* function should only be called after filtering the list to be only negated patterns
|
|
192
|
+
*
|
|
193
|
+
* @param patterns List of negated micromatch patterns
|
|
194
|
+
*/
|
|
195
|
+
const invertNegatedPatterns = (patterns) => {
|
|
196
|
+
return patterns.map(pattern => {
|
|
197
|
+
const scanned = micromatch_1.default.scan(pattern);
|
|
198
|
+
if (!scanned.negated) {
|
|
199
|
+
throw new Error(`onlyNegatedPatterns got a non-negated pattern: ${pattern}`);
|
|
200
|
+
}
|
|
201
|
+
// Remove the leading prefix (NOTE: this is not always "!")
|
|
202
|
+
// e.g. mm.scan("!./foo/bar/never-watch.graphql").prefix === '!./'
|
|
203
|
+
return pattern.slice(scanned.prefix.length);
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Return true if relativeCandidatePath matches any of the affirmativePatterns
|
|
208
|
+
*
|
|
209
|
+
* @param relativeCandidatePath A relative path to evaluate against the supplied affirmativePatterns
|
|
210
|
+
* @param affirmativePatterns A list of patterns, containing no negated patterns, to evaluate
|
|
211
|
+
*/
|
|
212
|
+
const matchesAnyAffirmativePattern = (relativeCandidatePath, affirmativePatterns) => {
|
|
213
|
+
if ((0, path_1.isAbsolute)(relativeCandidatePath)) {
|
|
214
|
+
throw new Error('matchesAny should only be called with relative candidate path');
|
|
215
|
+
}
|
|
216
|
+
// Developer error: This function is not intended to work with pattern sets including negations
|
|
217
|
+
if (affirmativePatterns.some(pattern => micromatch_1.default.scan(pattern).negated)) {
|
|
218
|
+
throw new Error('matchesAnyAffirmativePattern should only include affirmative patterns');
|
|
219
|
+
}
|
|
220
|
+
// micromatch.isMatch does not omit matches that are negated by negation patterns,
|
|
221
|
+
// which is why we require this function only examine affirmative patterns
|
|
222
|
+
return micromatch_1.default.isMatch(relativeCandidatePath, affirmativePatterns);
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Return true if relativeCandidatePath matches any of the negatedPatterns
|
|
226
|
+
*
|
|
227
|
+
* This function will invert the negated patterns and then call matchesAnyAffirmativePattern
|
|
228
|
+
*
|
|
229
|
+
* @param relativeCandidatePath A relative path to evaluate against the suppliednegatedPatterns
|
|
230
|
+
* @param negatedPatterns A list of patterns, containing no negated patterns, to evaluate
|
|
231
|
+
*/
|
|
232
|
+
const matchesAnyNegatedPattern = (relativeCandidatePath, negatedPatterns) => {
|
|
233
|
+
// NOTE: No safety check that negatedPatterns contains only negated, because that will happen in invertedNegatedPatterns
|
|
234
|
+
return matchesAnyAffirmativePattern(relativeCandidatePath, invertNegatedPatterns(negatedPatterns));
|
|
235
|
+
};
|