1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var old_cli_config_1 = require("./old-cli-config");
|
4 | var fs_1 = require("fs");
|
5 | var path_1 = require("path");
|
6 | var errors_1 = require("./errors");
|
7 | var yml_1 = require("./yml");
|
8 | function getCustomConfig() {
|
9 | var _a = process.argv, flag = _a[2], filepath = _a[3];
|
10 | if (!flag) {
|
11 | return null;
|
12 | }
|
13 | var flagIsConfig = ['--config', '-c'].includes(flag.toLocaleLowerCase());
|
14 | var hasFilepath = typeof filepath === 'string' && filepath.length > 0;
|
15 | var noOtherFlags = process.argv.length === 4;
|
16 | if (flagIsConfig && hasFilepath && noOtherFlags) {
|
17 | var configPath = path_1.resolve(process.cwd(), filepath);
|
18 | if (!fs_1.existsSync(configPath)) {
|
19 | throw new errors_1.DetailedError("Config " + configPath + " does not exist", "\n Config " + configPath + " does not exist.\n\n $ gql-gen --config " + configPath + "\n\n Please make sure the --config points to a correct file.\n ");
|
20 | }
|
21 | return configPath;
|
22 | }
|
23 | return null;
|
24 | }
|
25 | function loadAndParseConfig(filepath) {
|
26 | var ext = filepath.substr(filepath.lastIndexOf('.') + 1);
|
27 | switch (ext) {
|
28 | case 'yml':
|
29 | return yml_1.parseConfigFile(fs_1.readFileSync(filepath, 'utf-8'));
|
30 | case 'json':
|
31 | return JSON.parse(fs_1.readFileSync(filepath, 'utf-8'));
|
32 | default:
|
33 | throw new errors_1.DetailedError("Extension '" + ext + "' is not supported", "\n Config " + filepath + " couldn't be parsed. Extension '" + ext + "' is not supported.\n ");
|
34 | }
|
35 | }
|
36 | function createConfig() {
|
37 | var customConfigPath = getCustomConfig();
|
38 | var locations = [path_1.join(process.cwd(), './codegen.yml'), path_1.join(process.cwd(), './codegen.json')];
|
39 | if (customConfigPath) {
|
40 | locations.unshift(customConfigPath);
|
41 | }
|
42 | var filepath = locations.find(fs_1.existsSync);
|
43 | if (!filepath) {
|
44 | return old_cli_config_1.createConfigFromOldCli(old_cli_config_1.initCLI(process.argv));
|
45 | }
|
46 | return loadAndParseConfig(filepath);
|
47 | }
|
48 | exports.createConfig = createConfig;
|
49 |
|
\ | No newline at end of file |