UNPKG

3.14 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");
8var commander_1 = require("commander");
9function getCustomConfigPath(cliFlags) {
10 var configFile = cliFlags.config;
11 if (configFile) {
12 var configPath = path_1.resolve(process.cwd(), configFile);
13 if (!fs_1.existsSync(configPath)) {
14 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 ");
15 }
16 return configPath;
17 }
18 return null;
19}
20function loadAndParseConfig(filepath) {
21 var ext = filepath.substr(filepath.lastIndexOf('.') + 1);
22 switch (ext) {
23 case 'yml':
24 return yml_1.parseConfigFile(fs_1.readFileSync(filepath, 'utf-8'));
25 case 'json':
26 return JSON.parse(fs_1.readFileSync(filepath, 'utf-8'));
27 default:
28 throw new errors_1.DetailedError("Extension '" + ext + "' is not supported", "\n Config " + filepath + " couldn't be parsed. Extension '" + ext + "' is not supported.\n ");
29 }
30}
31function createConfig(argv) {
32 if (argv === void 0) { argv = process.argv; }
33 var cliFlags = new commander_1.Command()
34 .usage('gql-gen [options]')
35 .allowUnknownOption(true)
36 .option('-c, --config <path>', 'Path to GraphQL codegen YAML config file, defaults to "codegen.yml" on the current directory')
37 .option('-w, --watch', 'Watch for changes and execute generation automatically')
38 .option('-o, --overwrite', 'Overwrites existing files')
39 .parse(argv);
40 var isUsingOldApi = cliFlags.rawArgs.includes('--template') ||
41 cliFlags.rawArgs.includes('-t') ||
42 cliFlags.rawArgs.includes('--schema') ||
43 cliFlags.rawArgs.includes('--s');
44 if (isUsingOldApi) {
45 return old_cli_config_1.createConfigFromOldCli(old_cli_config_1.initCLI(argv));
46 }
47 else {
48 var customConfigPath = getCustomConfigPath(cliFlags);
49 var locations = [path_1.join(process.cwd(), './codegen.yml'), path_1.join(process.cwd(), './codegen.json')];
50 if (customConfigPath) {
51 locations.unshift(customConfigPath);
52 }
53 var filepath = locations.find(fs_1.existsSync);
54 if (!filepath) {
55 throw new errors_1.DetailedError("Unable to find Codegen config file!", "\n Please make sure that you have a configuration file under the current directory! \n ");
56 }
57 var parsedConfigFile = loadAndParseConfig(filepath);
58 if (cliFlags.watch === true) {
59 parsedConfigFile.watch = cliFlags.watch;
60 }
61 if (cliFlags.overwrite === true) {
62 parsedConfigFile.overwrite = cliFlags.overwrite;
63 }
64 return parsedConfigFile;
65 }
66}
67exports.createConfig = createConfig;
68//# sourceMappingURL=config.js.map
\No newline at end of file