UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs_1 = require("fs");
4var path_1 = require("path");
5var errors_1 = require("./errors");
6var yml_1 = require("./yml");
7var commander_1 = require("commander");
8function getCustomConfigPath(cliFlags) {
9 var configFile = cliFlags.config;
10 if (configFile) {
11 var configPath = path_1.resolve(process.cwd(), configFile);
12 if (!fs_1.existsSync(configPath)) {
13 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 ");
14 }
15 return configPath;
16 }
17 return null;
18}
19function loadAndParseConfig(filepath) {
20 var ext = filepath.substr(filepath.lastIndexOf('.') + 1);
21 switch (ext) {
22 case 'yml':
23 return yml_1.parseConfigFile(fs_1.readFileSync(filepath, 'utf-8'));
24 case 'json':
25 return JSON.parse(fs_1.readFileSync(filepath, 'utf-8'));
26 case 'js':
27 return require(path_1.resolve(process.cwd(), filepath));
28 default:
29 throw new errors_1.DetailedError("Extension '" + ext + "' is not supported", "\n Config " + filepath + " couldn't be parsed. Extension '" + ext + "' is not supported.\n ");
30 }
31}
32function createConfig(argv) {
33 if (argv === void 0) { argv = process.argv; }
34 var cliFlags = new commander_1.Command()
35 .usage('gql-gen [options]')
36 .allowUnknownOption(true)
37 .option('-c, --config <path>', 'Path to GraphQL codegen YAML config file, defaults to "codegen.yml" on the current directory')
38 .option('-w, --watch', 'Watch for changes and execute generation automatically')
39 .option('-o, --overwrite', 'Overwrites existing files')
40 .parse(argv);
41 var customConfigPath = getCustomConfigPath(cliFlags);
42 var locations = [path_1.join(process.cwd(), './codegen.yml'), path_1.join(process.cwd(), './codegen.json')];
43 if (customConfigPath) {
44 locations.unshift(customConfigPath);
45 }
46 var filepath = locations.find(fs_1.existsSync);
47 if (!filepath) {
48 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 ");
49 }
50 var parsedConfigFile = loadAndParseConfig(filepath);
51 if (cliFlags.watch === true) {
52 parsedConfigFile.watch = cliFlags.watch;
53 }
54 if (cliFlags.overwrite === true) {
55 parsedConfigFile.overwrite = cliFlags.overwrite;
56 }
57 return parsedConfigFile;
58}
59exports.createConfig = createConfig;
60//# sourceMappingURL=config.js.map
\No newline at end of file