UNPKG

2.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const chalk_1 = require("chalk");
4const path_1 = require("path");
5const fs_1 = require("fs");
6const YAML = require("json-to-pretty-yaml");
7const detectIndent = require("detect-indent");
8// Parses config and writes it to a file
9function writeConfig(answers, config) {
10 const ext = answers.config.toLocaleLowerCase().endsWith('.json') ? 'json' : 'yml';
11 const content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
12 const fullPath = path_1.resolve(process.cwd(), answers.config);
13 const relativePath = path_1.relative(process.cwd(), answers.config);
14 fs_1.writeFileSync(fullPath, content, {
15 encoding: 'utf-8'
16 });
17 return {
18 relativePath,
19 fullPath
20 };
21}
22exports.writeConfig = writeConfig;
23// Updates package.json (script and plugins as dependencies)
24function writePackage(answers, configLocation) {
25 // script
26 const pkgPath = path_1.resolve(process.cwd(), 'package.json');
27 const pkgContent = fs_1.readFileSync(pkgPath, {
28 encoding: 'utf-8'
29 });
30 const pkg = JSON.parse(pkgContent);
31 const { indent } = detectIndent(pkgContent);
32 if (!pkg.scripts) {
33 pkg.scripts = {};
34 }
35 pkg.scripts[answers.script] = `gql-gen --config ${configLocation}`;
36 // plugin
37 if (!pkg.devDependencies) {
38 pkg.devDependencies = {};
39 }
40 // read codegen's version
41 const { version } = JSON.parse(fs_1.readFileSync(path_1.resolve(__dirname, '../../package.json'), {
42 encoding: 'utf-8'
43 }));
44 answers.plugins.forEach(plugin => {
45 pkg.devDependencies[plugin.package] = version;
46 });
47 fs_1.writeFileSync(pkgPath, JSON.stringify(pkg, null, indent));
48}
49exports.writePackage = writePackage;
50function bold(str) {
51 return chalk_1.default.bold(str);
52}
53exports.bold = bold;
54function grey(str) {
55 return chalk_1.default.grey(str);
56}
57exports.grey = grey;
58function italic(str) {
59 return chalk_1.default.italic(str);
60}
61exports.italic = italic;
62//# sourceMappingURL=helpers.js.map
\No newline at end of file