|
1 | 1 | const AmplifyCodeGenConfig = require('./AmplifyCodeGenConfig');
|
| 2 | +const fs = require('fs-extra'); |
| 3 | +const path = require('path'); |
2 | 4 |
|
3 | 5 | let config = null;
|
4 | 6 |
|
5 | 7 | function loadConfig(context, withoutInit = false) {
|
6 | 8 | if (!config) {
|
7 |
| - config = new AmplifyCodeGenConfig(context, withoutInit); |
| 9 | + const projectPath = context.amplify.getEnvInfo().projectPath; |
| 10 | + config = new AmplifyCodeGenConfig(projectPath, withoutInit); |
8 | 11 | }
|
9 | 12 | return config;
|
10 | 13 | }
|
11 | 14 |
|
12 |
| -module.exports = loadConfig; |
| 15 | +function getCodegenConfig(projectPath) { |
| 16 | + if (!projectPath) { |
| 17 | + throw new Error('Invalid projectPath provided to get codegen configuration'); |
| 18 | + } |
| 19 | + |
| 20 | + if (!fs.existsSync(projectPath)) { |
| 21 | + throw new Error(`Provided projectPath for getting the codegen configuration does not exist: ${projectPath}`); |
| 22 | + } |
| 23 | + |
| 24 | + if (!fs.existsSync(path.join(projectPath, AmplifyCodeGenConfig.configFileName))) { |
| 25 | + throw new Error(`Cannot find the codegen configuration file at provided projectPath: ${projectPath}`); |
| 26 | + } |
| 27 | + |
| 28 | + const codegenConfig = new AmplifyCodeGenConfig(projectPath); |
| 29 | + if (!codegenConfig) { |
| 30 | + throw new Error(`Fetched codegen configuration is empty: ${codegenConfig}`); |
| 31 | + } |
| 32 | + |
| 33 | + const projects = codegenConfig.getProjects(); |
| 34 | + if (!projects.length > 0) { |
| 35 | + throw new Error(`No projects were found in fetched codegen configuration: ${codegenConfig}`); |
| 36 | + } |
| 37 | + |
| 38 | + const cfg = projects[0]; |
| 39 | + const includeFiles = cfg.includes[0]; |
| 40 | + |
| 41 | + const opsGenDirectory = cfg.amplifyExtension.docsFilePath |
| 42 | + ? cfg.amplifyExtension.docsFilePath |
| 43 | + : path.dirname(path.dirname(includeFiles)); |
| 44 | + |
| 45 | + const { generatedFileName } = cfg.amplifyExtension || {}; |
| 46 | + |
| 47 | + return { |
| 48 | + getGeneratedQueriesPath: function() { |
| 49 | + return path.join(opsGenDirectory, 'queries'); |
| 50 | + }, |
| 51 | + getGeneratedMutationsPath: function() { |
| 52 | + return path.join(opsGenDirectory, 'mutations'); |
| 53 | + }, |
| 54 | + getGeneratedSubscriptionsPath: function() { |
| 55 | + return path.join(opsGenDirectory, 'subscriptions'); |
| 56 | + }, |
| 57 | + getGeneratedFragmentsPath: function() { |
| 58 | + return path.join(opsGenDirectory, 'fragments'); |
| 59 | + }, |
| 60 | + getGeneratedTypesPath: function() { |
| 61 | + if (!generatedFileName || generatedFileName === '') { |
| 62 | + return; |
| 63 | + } |
| 64 | + return path.normalize(generatedFileName); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +module.exports = { loadConfig, getCodegenConfig }; |
0 commit comments