UNPKG

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