UNPKG

3.23 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 case 'js':
28 return require(path_1.resolve(process.cwd(), filepath));
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(argv) {
34 if (argv === void 0) { argv = process.argv; }
35 var cliFlags = new commander_1.Command()
36 .usage('gql-gen [options]')
37 .allowUnknownOption(true)
38 .option('-c, --config <path>', 'Path to GraphQL codegen YAML config file, defaults to "codegen.yml" on the current directory')
39 .option('-w, --watch', 'Watch for changes and execute generation automatically')
40 .option('-o, --overwrite', 'Overwrites existing files')
41 .parse(argv);
42 var isUsingOldApi = cliFlags.rawArgs.includes('--template') ||
43 cliFlags.rawArgs.includes('-t') ||
44 cliFlags.rawArgs.includes('--schema') ||
45 cliFlags.rawArgs.includes('--s');
46 if (isUsingOldApi) {
47 return old_cli_config_1.createConfigFromOldCli(old_cli_config_1.initCLI(argv));
48 }
49 else {
50 var customConfigPath = getCustomConfigPath(cliFlags);
51 var locations = [path_1.join(process.cwd(), './codegen.yml'), path_1.join(process.cwd(), './codegen.json')];
52 if (customConfigPath) {
53 locations.unshift(customConfigPath);
54 }
55 var filepath = locations.find(fs_1.existsSync);
56 if (!filepath) {
57 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 ");
58 }
59 var parsedConfigFile = loadAndParseConfig(filepath);
60 if (cliFlags.watch === true) {
61 parsedConfigFile.watch = cliFlags.watch;
62 }
63 if (cliFlags.overwrite === true) {
64 parsedConfigFile.overwrite = cliFlags.overwrite;
65 }
66 return parsedConfigFile;
67 }
68}
69exports.createConfig = createConfig;
70//# sourceMappingURL=config.js.map
\No newline at end of file