|
| 1 | +import { resolve } from 'path'; |
| 2 | +import { Types } from '@graphql-codegen/plugin-helpers'; |
| 3 | + |
| 4 | +export async function getDocumentTransform( |
| 5 | + documentTransform: Types.OutputDocumentTransform, |
| 6 | + loader: Types.PackageLoaderFn<Types.DocumentTransformObject>, |
| 7 | + defaultName: string |
| 8 | +): Promise<Types.ConfiguredDocumentTransform> { |
| 9 | + if (typeof documentTransform === 'string') { |
| 10 | + const transformObject = await getDocumentTransformByName(documentTransform, loader); |
| 11 | + return { name: documentTransform, transformObject }; |
| 12 | + } |
| 13 | + if (isTransformObject(documentTransform)) { |
| 14 | + return { name: defaultName, transformObject: documentTransform }; |
| 15 | + } |
| 16 | + if (isTransformFileConfig(documentTransform)) { |
| 17 | + const name = Object.keys(documentTransform)[0]; |
| 18 | + const transformObject = await getDocumentTransformByName(name, loader); |
| 19 | + return { name, transformObject, config: Object.values(documentTransform)[0] }; |
| 20 | + } |
| 21 | + throw new Error( |
| 22 | + ` |
| 23 | + An unknown format document transform: '${defaultName}'. |
| 24 | + ` |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function isTransformObject(config: Types.OutputDocumentTransform): config is Types.DocumentTransformObject { |
| 29 | + return typeof config === 'object' && config.transform && typeof config.transform === 'function'; |
| 30 | +} |
| 31 | + |
| 32 | +function isTransformFileConfig(config: Types.OutputDocumentTransform): config is Types.DocumentTransformFileConfig { |
| 33 | + const keys = Object.keys(config); |
| 34 | + return keys.length === 1 && typeof keys[0] === 'string'; |
| 35 | +} |
| 36 | + |
| 37 | +export async function getDocumentTransformByName( |
| 38 | + name: string, |
| 39 | + loader: Types.PackageLoaderFn<Types.DocumentTransformObject> |
| 40 | +): Promise<Types.DocumentTransformObject> { |
| 41 | + const possibleNames = [ |
| 42 | + `@graphql-codegen/${name}`, |
| 43 | + `@graphql-codegen/${name}-document-transform`, |
| 44 | + name, |
| 45 | + resolve(process.cwd(), name), |
| 46 | + ]; |
| 47 | + |
| 48 | + const possibleModules = possibleNames.concat(resolve(process.cwd(), name)); |
| 49 | + |
| 50 | + for (const moduleName of possibleModules) { |
| 51 | + try { |
| 52 | + return await loader(moduleName); |
| 53 | + } catch (err) { |
| 54 | + if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') { |
| 55 | + throw new Error( |
| 56 | + ` |
| 57 | + Unable to load document transform matching '${name}'. |
| 58 | + Reason: |
| 59 | + ${err.message} |
| 60 | + ` |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const possibleNamesMsg = possibleNames |
| 67 | + .map(name => |
| 68 | + ` |
| 69 | + - ${name} |
| 70 | + `.trimEnd() |
| 71 | + ) |
| 72 | + .join(''); |
| 73 | + |
| 74 | + throw new Error( |
| 75 | + ` |
| 76 | + Unable to find document transform matching '${name}' |
| 77 | + Install one of the following packages: |
| 78 | +
|
| 79 | + ${possibleNamesMsg} |
| 80 | + ` |
| 81 | + ); |
| 82 | +} |
0 commit comments