UNPKG

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