UNPKG

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