UNPKG

1.44 kBJavaScriptView Raw
1import { DetailedError } from '@graphql-codegen/plugin-helpers';
2import { resolve } from 'path';
3export 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' && err.code !== 'ERR_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}