1 | import { resolve } from 'path';
|
2 | import { DetailedError } from './errors';
|
3 | export async function getPluginByName(name, pluginLoader) {
|
4 | const possibleNames = [
|
5 | `graphql-codegen-${name}`,
|
6 | `graphql-codegen-${name}-template`,
|
7 | `codegen-${name}`,
|
8 | `codegen-${name}-template`,
|
9 | name
|
10 | ];
|
11 | const possibleModules = possibleNames.concat(resolve(process.cwd(), name));
|
12 | for (const moduleName of possibleModules) {
|
13 | try {
|
14 | return (await pluginLoader(moduleName));
|
15 | }
|
16 | catch (err) {
|
17 | if (err.message.indexOf(`Cannot find module '${moduleName}'`) === -1) {
|
18 | throw new DetailedError(`Unable to load template plugin matching ${name}`, `
|
19 | Unable to load template plugin matching '${name}'.
|
20 | Reason:
|
21 | ${err.message}
|
22 | `);
|
23 | }
|
24 | }
|
25 | }
|
26 | const possibleNamesMsg = possibleNames
|
27 | .map(name => `
|
28 | - ${name}
|
29 | `.trimRight())
|
30 | .join('');
|
31 | throw new DetailedError(`Unable to find template plugin matching ${name}`, `
|
32 | Unable to find template plugin matching '${name}'
|
33 | Install one of the following packages:
|
34 |
|
35 | ${possibleNamesMsg}
|
36 | `);
|
37 | }
|
38 |
|
\ | No newline at end of file |