@graphql-codegen/cli 2.7.0 → 2.7.1-alpha-9d615db71.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.
Files changed (78) hide show
  1. package/cjs/bin.js +13 -0
  2. package/cjs/cli.js +60 -0
  3. package/cjs/codegen.js +319 -0
  4. package/cjs/config.js +303 -0
  5. package/cjs/generate-and-save.js +110 -0
  6. package/cjs/graphql-config.js +69 -0
  7. package/cjs/hooks.js +87 -0
  8. package/cjs/index.js +13 -0
  9. package/cjs/init/helpers.js +64 -0
  10. package/cjs/init/index.js +60 -0
  11. package/cjs/init/plugins.js +163 -0
  12. package/cjs/init/questions.js +147 -0
  13. package/cjs/init/targets.js +44 -0
  14. package/cjs/init/types.js +14 -0
  15. package/cjs/load.js +93 -0
  16. package/cjs/package.json +1 -0
  17. package/cjs/plugins.js +45 -0
  18. package/cjs/presets.js +50 -0
  19. package/cjs/utils/cli-error.js +25 -0
  20. package/cjs/utils/debugging.js +27 -0
  21. package/cjs/utils/file-system.js +26 -0
  22. package/cjs/utils/get-latest-version.js +15 -0
  23. package/cjs/utils/logger.js +25 -0
  24. package/cjs/utils/watcher.js +140 -0
  25. package/esm/bin.js +11 -0
  26. package/esm/cli.js +32 -0
  27. package/esm/codegen.js +314 -0
  28. package/esm/config.js +290 -0
  29. package/esm/generate-and-save.js +105 -0
  30. package/esm/graphql-config.js +64 -0
  31. package/esm/hooks.js +83 -0
  32. package/esm/index.js +7 -0
  33. package/esm/init/helpers.js +55 -0
  34. package/esm/init/index.js +55 -0
  35. package/esm/init/plugins.js +160 -0
  36. package/esm/init/questions.js +140 -0
  37. package/esm/init/targets.js +40 -0
  38. package/esm/init/types.js +11 -0
  39. package/esm/load.js +88 -0
  40. package/esm/plugins.js +41 -0
  41. package/esm/presets.js +46 -0
  42. package/esm/utils/cli-error.js +20 -0
  43. package/esm/utils/debugging.js +21 -0
  44. package/esm/utils/file-system.js +19 -0
  45. package/esm/utils/get-latest-version.js +11 -0
  46. package/esm/utils/logger.js +18 -0
  47. package/esm/utils/watcher.js +135 -0
  48. package/package.json +34 -30
  49. package/typings/bin.d.ts +2 -0
  50. package/{cli.d.ts → typings/cli.d.ts} +0 -0
  51. package/{codegen.d.ts → typings/codegen.d.ts} +1 -1
  52. package/{config.d.ts → typings/config.d.ts} +0 -0
  53. package/{generate-and-save.d.ts → typings/generate-and-save.d.ts} +1 -1
  54. package/{graphql-config.d.ts → typings/graphql-config.d.ts} +0 -0
  55. package/{hooks.d.ts → typings/hooks.d.ts} +0 -0
  56. package/typings/index.d.ts +7 -0
  57. package/{init → typings/init}/helpers.d.ts +1 -1
  58. package/{init → typings/init}/index.d.ts +0 -0
  59. package/{init → typings/init}/plugins.d.ts +1 -1
  60. package/{init → typings/init}/questions.d.ts +1 -1
  61. package/{init → typings/init}/targets.d.ts +1 -1
  62. package/{init → typings/init}/types.d.ts +0 -0
  63. package/{load.d.ts → typings/load.d.ts} +0 -0
  64. package/{plugins.d.ts → typings/plugins.d.ts} +0 -0
  65. package/{presets.d.ts → typings/presets.d.ts} +0 -0
  66. package/{utils → typings/utils}/cli-error.d.ts +0 -0
  67. package/{utils → typings/utils}/debugging.d.ts +0 -0
  68. package/{utils → typings/utils}/file-system.d.ts +0 -0
  69. package/typings/utils/get-latest-version.d.ts +6 -0
  70. package/{utils → typings/utils}/logger.d.ts +0 -0
  71. package/{utils → typings/utils}/watcher.d.ts +1 -1
  72. package/bin.d.ts +0 -1
  73. package/bin.js +0 -1891
  74. package/index.d.ts +0 -7
  75. package/index.js +0 -1902
  76. package/index.mjs +0 -1858
  77. package/utils/indentString.d.ts +0 -26
  78. package/utils/listr-renderer.d.ts +0 -18
@@ -0,0 +1,160 @@
1
+ import { italic } from './helpers.js';
2
+ import { Tags } from './types.js';
3
+ export const plugins = [
4
+ {
5
+ name: `TypeScript ${italic('(required by other typescript plugins)')}`,
6
+ package: '@graphql-codegen/typescript',
7
+ value: 'typescript',
8
+ pathInRepo: 'typescript/typescript',
9
+ available: hasTag(Tags.typescript),
10
+ shouldBeSelected: tags => oneOf(tags, Tags.angular, Tags.stencil) || allOf(tags, Tags.typescript, Tags.react) || noneOf(tags, Tags.flow),
11
+ defaultExtension: '.ts',
12
+ },
13
+ {
14
+ name: `TypeScript Operations ${italic('(operations and fragments)')}`,
15
+ package: '@graphql-codegen/typescript-operations',
16
+ value: 'typescript-operations',
17
+ pathInRepo: 'typescript/operations',
18
+ available: tags => allOf(tags, Tags.browser, Tags.typescript),
19
+ shouldBeSelected: tags => oneOf(tags, Tags.angular, Tags.stencil) || allOf(tags, Tags.typescript, Tags.react),
20
+ defaultExtension: '.ts',
21
+ },
22
+ {
23
+ name: `TypeScript Resolvers ${italic('(strongly typed resolve functions)')}`,
24
+ package: '@graphql-codegen/typescript-resolvers',
25
+ value: 'typescript-resolvers',
26
+ pathInRepo: 'typescript/resolvers',
27
+ available: tags => allOf(tags, Tags.node, Tags.typescript),
28
+ shouldBeSelected: tags => noneOf(tags, Tags.flow),
29
+ defaultExtension: '.ts',
30
+ },
31
+ {
32
+ name: `Flow ${italic('(required by other flow plugins)')}`,
33
+ package: '@graphql-codegen/flow',
34
+ value: 'flow',
35
+ pathInRepo: 'flow/flow',
36
+ available: hasTag(Tags.flow),
37
+ shouldBeSelected: tags => noneOf(tags, Tags.typescript),
38
+ defaultExtension: '.js',
39
+ },
40
+ {
41
+ name: `Flow Operations ${italic('(operations and fragments)')}`,
42
+ package: '@graphql-codegen/flow-operations',
43
+ value: 'flow-operations',
44
+ pathInRepo: 'flow/operations',
45
+ available: tags => allOf(tags, Tags.browser, Tags.flow),
46
+ shouldBeSelected: tags => noneOf(tags, Tags.typescript),
47
+ defaultExtension: '.js',
48
+ },
49
+ {
50
+ name: `Flow Resolvers ${italic('(strongly typed resolve functions)')}`,
51
+ package: '@graphql-codegen/flow-resolvers',
52
+ value: 'flow-resolvers',
53
+ pathInRepo: 'flow/resolvers',
54
+ available: tags => allOf(tags, Tags.node, Tags.flow),
55
+ shouldBeSelected: tags => noneOf(tags, Tags.typescript),
56
+ defaultExtension: '.js',
57
+ },
58
+ {
59
+ name: `TypeScript Apollo Angular ${italic('(typed GQL services)')}`,
60
+ package: '@graphql-codegen/typescript-apollo-angular',
61
+ value: 'typescript-apollo-angular',
62
+ pathInRepo: 'typescript/apollo-angular',
63
+ available: hasTag(Tags.angular),
64
+ shouldBeSelected: () => true,
65
+ defaultExtension: '.js',
66
+ },
67
+ {
68
+ name: `TypeScript Vue Apollo Composition API ${italic('(typed functions)')}`,
69
+ package: '@graphql-codegen/typescript-vue-apollo',
70
+ value: 'typescript-vue-apollo',
71
+ pathInRepo: 'typescript/vue-apollo',
72
+ available: tags => allOf(tags, Tags.vue, Tags.typescript),
73
+ shouldBeSelected: () => true,
74
+ defaultExtension: '.ts',
75
+ },
76
+ {
77
+ name: `TypeScript Vue Apollo Smart Operations ${italic('(typed functions)')}`,
78
+ package: '@graphql-codegen/typescript-vue-apollo-smart-ops',
79
+ value: 'typescript-vue-apollo-smart-ops',
80
+ pathInRepo: 'typescript/vue-apollo-smart-ops',
81
+ available: tags => allOf(tags, Tags.vue, Tags.typescript),
82
+ shouldBeSelected: () => true,
83
+ defaultExtension: '.ts',
84
+ },
85
+ {
86
+ name: `TypeScript React Apollo ${italic('(typed components and HOCs)')}`,
87
+ package: '@graphql-codegen/typescript-react-apollo',
88
+ value: 'typescript-react-apollo',
89
+ pathInRepo: 'typescript/react-apollo',
90
+ available: tags => allOf(tags, Tags.react, Tags.typescript),
91
+ shouldBeSelected: () => true,
92
+ defaultExtension: '.tsx',
93
+ },
94
+ {
95
+ name: `TypeScript Stencil Apollo ${italic('(typed components)')}`,
96
+ package: '@graphql-codegen/typescript-stencil-apollo',
97
+ value: 'typescript-stencil-apollo',
98
+ pathInRepo: 'typescript/stencil-apollo',
99
+ available: hasTag(Tags.stencil),
100
+ shouldBeSelected: () => true,
101
+ defaultExtension: '.tsx',
102
+ },
103
+ {
104
+ name: `TypeScript MongoDB ${italic('(typed MongoDB objects)')}`,
105
+ package: '@graphql-codegen/typescript-mongodb',
106
+ value: 'typescript-mongodb',
107
+ pathInRepo: 'typescript/mongodb',
108
+ available: tags => allOf(tags, Tags.node, Tags.typescript),
109
+ shouldBeSelected: () => false,
110
+ defaultExtension: '.ts',
111
+ },
112
+ {
113
+ name: `TypeScript GraphQL files modules ${italic('(declarations for .graphql files)')}`,
114
+ package: '@graphql-codegen/typescript-graphql-files-modules',
115
+ value: 'typescript-graphql-files-modules',
116
+ pathInRepo: 'typescript/graphql-files-modules',
117
+ available: tags => allOf(tags, Tags.browser, Tags.typescript),
118
+ shouldBeSelected: () => false,
119
+ defaultExtension: '.ts',
120
+ },
121
+ {
122
+ name: `TypeScript GraphQL document nodes ${italic('(embedded GraphQL document)')}`,
123
+ package: '@graphql-codegen/typescript-document-nodes',
124
+ value: 'typescript-document-nodes',
125
+ pathInRepo: 'typescript/document-nodes',
126
+ available: tags => allOf(tags, Tags.typescript),
127
+ shouldBeSelected: () => false,
128
+ defaultExtension: '.ts',
129
+ },
130
+ {
131
+ name: `Introspection Fragment Matcher ${italic('(for Apollo Client)')}`,
132
+ package: '@graphql-codegen/fragment-matcher',
133
+ value: 'fragment-matcher',
134
+ pathInRepo: 'other/fragment-matcher',
135
+ available: hasTag(Tags.browser),
136
+ shouldBeSelected: () => false,
137
+ defaultExtension: '.ts',
138
+ },
139
+ {
140
+ name: `Urql Introspection ${italic('(for Urql Client)')}`,
141
+ package: '@graphql-codegen/urql-introspection',
142
+ value: 'urql-introspection',
143
+ pathInRepo: 'other/urql-introspection',
144
+ available: hasTag(Tags.browser),
145
+ shouldBeSelected: () => false,
146
+ defaultExtension: '.ts',
147
+ },
148
+ ];
149
+ function hasTag(tag) {
150
+ return (tags) => tags.includes(tag);
151
+ }
152
+ function oneOf(list, ...items) {
153
+ return list.some(i => items.includes(i));
154
+ }
155
+ function noneOf(list, ...items) {
156
+ return !list.some(i => items.includes(i));
157
+ }
158
+ function allOf(list, ...items) {
159
+ return items.every(i => list.includes(i));
160
+ }
@@ -0,0 +1,140 @@
1
+ import { grey } from './helpers.js';
2
+ import { Tags } from './types.js';
3
+ import { plugins } from './plugins.js';
4
+ export function getQuestions(possibleTargets) {
5
+ return [
6
+ {
7
+ type: 'checkbox',
8
+ name: 'targets',
9
+ message: `What type of application are you building?`,
10
+ choices: getApplicationTypeChoices(possibleTargets),
11
+ validate: ((targets) => targets.length > 0),
12
+ },
13
+ {
14
+ type: 'input',
15
+ name: 'schema',
16
+ message: 'Where is your schema?:',
17
+ suffix: grey(' (path or url)'),
18
+ default: 'http://localhost:4000',
19
+ validate: (str) => str.length > 0,
20
+ },
21
+ {
22
+ type: 'input',
23
+ name: 'documents',
24
+ message: 'Where are your operations and fragments?:',
25
+ when: answers => {
26
+ // flatten targets
27
+ // I can't find an API in Inquirer that would do that
28
+ answers.targets = normalizeTargets(answers.targets);
29
+ return answers.targets.includes(Tags.browser);
30
+ },
31
+ default: 'src/**/*.graphql',
32
+ validate: (str) => str.length > 0,
33
+ },
34
+ {
35
+ type: 'checkbox',
36
+ name: 'plugins',
37
+ message: 'Pick plugins:',
38
+ choices: getPluginChoices,
39
+ validate: ((plugins) => plugins.length > 0),
40
+ },
41
+ {
42
+ type: 'input',
43
+ name: 'output',
44
+ message: 'Where to write the output:',
45
+ default: getOutputDefaultValue,
46
+ validate: (str) => str.length > 0,
47
+ },
48
+ {
49
+ type: 'confirm',
50
+ name: 'introspection',
51
+ message: 'Do you want to generate an introspection file?',
52
+ },
53
+ {
54
+ type: 'input',
55
+ name: 'config',
56
+ message: 'How to name the config file?',
57
+ default: 'codegen.yml',
58
+ validate: (str) => {
59
+ const isNotEmpty = str.length > 0;
60
+ const hasCorrectExtension = ['json', 'yml', 'yaml'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
61
+ return isNotEmpty && hasCorrectExtension;
62
+ },
63
+ },
64
+ {
65
+ type: 'input',
66
+ name: 'script',
67
+ message: 'What script in package.json should run the codegen?',
68
+ validate: (str) => str.length > 0,
69
+ },
70
+ ];
71
+ }
72
+ export function getApplicationTypeChoices(possibleTargets) {
73
+ function withFlowOrTypescript(tags) {
74
+ if (possibleTargets.TypeScript) {
75
+ tags.push(Tags.typescript);
76
+ }
77
+ else if (possibleTargets.Flow) {
78
+ tags.push(Tags.flow);
79
+ }
80
+ else {
81
+ tags.push(Tags.flow, Tags.typescript);
82
+ }
83
+ return tags;
84
+ }
85
+ return [
86
+ {
87
+ name: 'Backend - API or server',
88
+ key: 'backend',
89
+ value: withFlowOrTypescript([Tags.node]),
90
+ checked: possibleTargets.Node,
91
+ },
92
+ {
93
+ name: 'Application built with Angular',
94
+ key: 'angular',
95
+ value: [Tags.angular, Tags.browser, Tags.typescript],
96
+ checked: possibleTargets.Angular,
97
+ },
98
+ {
99
+ name: 'Application built with React',
100
+ key: 'react',
101
+ value: withFlowOrTypescript([Tags.react, Tags.browser]),
102
+ checked: possibleTargets.React,
103
+ },
104
+ {
105
+ name: 'Application built with Stencil',
106
+ key: 'stencil',
107
+ value: [Tags.stencil, Tags.browser, Tags.typescript],
108
+ checked: possibleTargets.Stencil,
109
+ },
110
+ {
111
+ name: 'Application built with other framework or vanilla JS',
112
+ key: 'client',
113
+ value: [Tags.browser, Tags.typescript, Tags.flow],
114
+ checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil,
115
+ },
116
+ ];
117
+ }
118
+ export function getPluginChoices(answers) {
119
+ return plugins
120
+ .filter(p => p.available(answers.targets))
121
+ .map(p => {
122
+ return {
123
+ name: p.name,
124
+ value: p,
125
+ checked: p.shouldBeSelected(answers.targets),
126
+ };
127
+ });
128
+ }
129
+ function normalizeTargets(targets) {
130
+ return [].concat(...targets);
131
+ }
132
+ export function getOutputDefaultValue(answers) {
133
+ if (answers.plugins.some(plugin => plugin.defaultExtension === '.tsx')) {
134
+ return 'src/generated/graphql.tsx';
135
+ }
136
+ if (answers.plugins.some(plugin => plugin.defaultExtension === '.ts')) {
137
+ return 'src/generated/graphql.ts';
138
+ }
139
+ return 'src/generated/graphql.js';
140
+ }
@@ -0,0 +1,40 @@
1
+ import { resolve } from 'path';
2
+ import { readFileSync } from 'fs';
3
+ import { Tags } from './types.js';
4
+ export async function guessTargets() {
5
+ const pkg = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json'), {
6
+ encoding: 'utf-8',
7
+ }));
8
+ const dependencies = Object.keys({
9
+ ...pkg.dependencies,
10
+ ...pkg.devDependencies,
11
+ });
12
+ return {
13
+ [Tags.angular]: isAngular(dependencies),
14
+ [Tags.react]: isReact(dependencies),
15
+ [Tags.stencil]: isStencil(dependencies),
16
+ [Tags.vue]: isVue(dependencies),
17
+ [Tags.browser]: false,
18
+ [Tags.node]: false,
19
+ [Tags.typescript]: isTypescript(dependencies),
20
+ [Tags.flow]: isFlow(dependencies),
21
+ };
22
+ }
23
+ function isAngular(dependencies) {
24
+ return dependencies.includes('@angular/core');
25
+ }
26
+ function isReact(dependencies) {
27
+ return dependencies.includes('react');
28
+ }
29
+ function isStencil(dependencies) {
30
+ return dependencies.includes('@stencil/core');
31
+ }
32
+ function isVue(dependencies) {
33
+ return dependencies.includes('vue') || dependencies.includes('nuxt');
34
+ }
35
+ function isTypescript(dependencies) {
36
+ return dependencies.includes('typescript');
37
+ }
38
+ function isFlow(dependencies) {
39
+ return dependencies.includes('flow');
40
+ }
@@ -0,0 +1,11 @@
1
+ export var Tags;
2
+ (function (Tags) {
3
+ Tags["browser"] = "Browser";
4
+ Tags["node"] = "Node";
5
+ Tags["typescript"] = "TypeScript";
6
+ Tags["flow"] = "Flow";
7
+ Tags["angular"] = "Angular";
8
+ Tags["stencil"] = "Stencil";
9
+ Tags["react"] = "React";
10
+ Tags["vue"] = "Vue";
11
+ })(Tags || (Tags = {}));
package/esm/load.js ADDED
@@ -0,0 +1,88 @@
1
+ import { loadSchema as loadSchemaToolkit, loadDocuments as loadDocumentsToolkit, } from '@graphql-tools/load';
2
+ import { DetailedError } from '@graphql-codegen/plugin-helpers';
3
+ import { CodeFileLoader } from '@graphql-tools/code-file-loader';
4
+ import { GitLoader } from '@graphql-tools/git-loader';
5
+ import { GithubLoader } from '@graphql-tools/github-loader';
6
+ import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
7
+ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
8
+ import { UrlLoader } from '@graphql-tools/url-loader';
9
+ import { ApolloEngineLoader } from '@graphql-tools/apollo-engine-loader';
10
+ import { PrismaLoader } from '@graphql-tools/prisma-loader';
11
+ import { join, extname } from 'path';
12
+ export const defaultSchemaLoadOptions = {
13
+ assumeValidSDL: true,
14
+ sort: true,
15
+ convertExtensions: true,
16
+ includeSources: true,
17
+ };
18
+ export const defaultDocumentsLoadOptions = {
19
+ sort: true,
20
+ skipGraphQLImport: true,
21
+ };
22
+ export async function loadSchema(schemaPointers, config) {
23
+ try {
24
+ const loaders = [
25
+ new CodeFileLoader(),
26
+ new GitLoader(),
27
+ new GithubLoader(),
28
+ new GraphQLFileLoader(),
29
+ new JsonFileLoader(),
30
+ new UrlLoader(),
31
+ new ApolloEngineLoader(),
32
+ new PrismaLoader(),
33
+ ];
34
+ const schema = await loadSchemaToolkit(schemaPointers, {
35
+ ...defaultSchemaLoadOptions,
36
+ loaders,
37
+ ...config,
38
+ ...config.config,
39
+ });
40
+ return schema;
41
+ }
42
+ catch (e) {
43
+ throw new DetailedError('Failed to load schema', `
44
+ Failed to load schema from ${Object.keys(schemaPointers).join(',')}:
45
+
46
+ ${e.message || e}
47
+ ${e.stack || ''}
48
+
49
+ GraphQL Code Generator supports:
50
+ - 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
+
56
+ Try to use one of above options and run codegen again.
57
+
58
+ `);
59
+ }
60
+ }
61
+ export async function loadDocuments(documentPointers, config) {
62
+ const loaders = [
63
+ new CodeFileLoader({
64
+ pluckConfig: {
65
+ skipIndent: true,
66
+ },
67
+ }),
68
+ new GitLoader(),
69
+ new GithubLoader(),
70
+ new GraphQLFileLoader(),
71
+ ];
72
+ const ignore = [];
73
+ for (const generatePath of Object.keys(config.generates)) {
74
+ if (extname(generatePath) === '') {
75
+ // we omit paths that don't resolve to a specific file
76
+ continue;
77
+ }
78
+ ignore.push(join(process.cwd(), generatePath));
79
+ }
80
+ const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, {
81
+ ...defaultDocumentsLoadOptions,
82
+ ignore,
83
+ loaders,
84
+ ...config,
85
+ ...config.config,
86
+ });
87
+ return loadedFromToolkit;
88
+ }
package/esm/plugins.js ADDED
@@ -0,0 +1,41 @@
1
+ import { DetailedError } from '@graphql-codegen/plugin-helpers';
2
+ import { resolve } from 'path';
3
+ export async function getPluginByName(name, pluginLoader) {
4
+ const possibleNames = [
5
+ `@graphql-codegen/${name}`,
6
+ `@graphql-codegen/${name}-template`,
7
+ `@graphql-codegen/${name}-plugin`,
8
+ `graphql-codegen-${name}`,
9
+ `graphql-codegen-${name}-template`,
10
+ `graphql-codegen-${name}-plugin`,
11
+ `codegen-${name}`,
12
+ `codegen-${name}-template`,
13
+ name,
14
+ ];
15
+ const possibleModules = possibleNames.concat(resolve(process.cwd(), name));
16
+ for (const moduleName of possibleModules) {
17
+ try {
18
+ return await pluginLoader(moduleName);
19
+ }
20
+ catch (err) {
21
+ if (err.code !== 'MODULE_NOT_FOUND') {
22
+ throw new DetailedError(`Unable to load template plugin matching ${name}`, `
23
+ Unable to load template plugin matching '${name}'.
24
+ Reason:
25
+ ${err.message}
26
+ `);
27
+ }
28
+ }
29
+ }
30
+ const possibleNamesMsg = possibleNames
31
+ .map(name => `
32
+ - ${name}
33
+ `.trimRight())
34
+ .join('');
35
+ throw new DetailedError(`Unable to find template plugin matching ${name}`, `
36
+ Unable to find template plugin matching '${name}'
37
+ Install one of the following packages:
38
+
39
+ ${possibleNamesMsg}
40
+ `);
41
+ }
package/esm/presets.js ADDED
@@ -0,0 +1,46 @@
1
+ import { DetailedError } from '@graphql-codegen/plugin-helpers';
2
+ import { resolve } from 'path';
3
+ export async function getPresetByName(name, loader) {
4
+ const possibleNames = [
5
+ `@graphql-codegen/${name}`,
6
+ `@graphql-codegen/${name}-preset`,
7
+ name,
8
+ resolve(process.cwd(), name),
9
+ ];
10
+ for (const moduleName of possibleNames) {
11
+ try {
12
+ const loaded = await loader(moduleName);
13
+ if (loaded && loaded.preset) {
14
+ return loaded.preset;
15
+ }
16
+ if (loaded && loaded.default) {
17
+ return loaded.default;
18
+ }
19
+ return loaded;
20
+ }
21
+ catch (err) {
22
+ if (
23
+ /** CJS Error code */
24
+ err.code !== 'MODULE_NOT_FOUND' &&
25
+ /** ESM Error code */
26
+ err.code !== 'ERR_MODULE_NOT_FOUND') {
27
+ throw new DetailedError(`Unable to load preset matching ${name}`, `
28
+ Unable to load preset matching '${name}'.
29
+ Reason:
30
+ ${err.message}
31
+ `);
32
+ }
33
+ }
34
+ }
35
+ const possibleNamesMsg = possibleNames
36
+ .map(name => `
37
+ - ${name}
38
+ `.trimRight())
39
+ .join('');
40
+ throw new DetailedError(`Unable to find preset matching ${name}`, `
41
+ Unable to find preset matching '${name}'
42
+ Install one of the following packages:
43
+
44
+ ${possibleNamesMsg}
45
+ `);
46
+ }
@@ -0,0 +1,20 @@
1
+ export function isListrError(err) {
2
+ return err.name === 'ListrError' && Array.isArray(err.errors) && err.errors.length > 0;
3
+ }
4
+ export function cliError(err, exitOnError = true) {
5
+ let msg;
6
+ if (err instanceof Error) {
7
+ msg = err.message || err.toString();
8
+ }
9
+ else if (typeof err === 'string') {
10
+ msg = err;
11
+ }
12
+ else {
13
+ msg = JSON.stringify(err);
14
+ }
15
+ // eslint-disable-next-line no-console
16
+ console.error(msg);
17
+ if (exitOnError) {
18
+ process.exit(1);
19
+ }
20
+ }
@@ -0,0 +1,21 @@
1
+ import { getLogger } from './logger.js';
2
+ let queue = [];
3
+ export function debugLog(message, ...meta) {
4
+ if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
5
+ queue.push({
6
+ message,
7
+ meta,
8
+ });
9
+ }
10
+ }
11
+ export function printLogs() {
12
+ if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
13
+ queue.forEach(log => {
14
+ getLogger().info(log.message, ...log.meta);
15
+ });
16
+ resetLogs();
17
+ }
18
+ }
19
+ export function resetLogs() {
20
+ queue = [];
21
+ }
@@ -0,0 +1,19 @@
1
+ import { unlink as fsUnlink, promises } from 'fs';
2
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = promises;
3
+ export function writeFile(filepath, content) {
4
+ return fsWriteFile(filepath, content);
5
+ }
6
+ export function readFile(filepath) {
7
+ return fsReadFile(filepath, 'utf-8');
8
+ }
9
+ export async function fileExists(filePath) {
10
+ try {
11
+ return (await fsStat(filePath)).isFile();
12
+ }
13
+ catch (err) {
14
+ return false;
15
+ }
16
+ }
17
+ export function unlinkFile(filePath, cb) {
18
+ fsUnlink(filePath, cb);
19
+ }
@@ -0,0 +1,11 @@
1
+ import { fetch } from 'cross-undici-fetch';
2
+ /**
3
+ * Fetches the version directly from the registry instead of depending on
4
+ * an ESM only module as latest-version does.
5
+ * @param packageName
6
+ */
7
+ export async function getLatestVersion(packageName) {
8
+ return fetch(`https://unpkg.com/${packageName}/package.json`)
9
+ .then(res => res.json())
10
+ .then(pkg => pkg.version);
11
+ }
@@ -0,0 +1,18 @@
1
+ import { dummyLogger } from 'ts-log';
2
+ let logger;
3
+ export function getLogger() {
4
+ return logger || dummyLogger;
5
+ }
6
+ useWinstonLogger();
7
+ export function setLogger(newLogger) {
8
+ logger = newLogger;
9
+ }
10
+ export function setSilentLogger() {
11
+ logger = dummyLogger;
12
+ }
13
+ export function useWinstonLogger() {
14
+ if (logger && logger.levels) {
15
+ return;
16
+ }
17
+ logger = console;
18
+ }