1 | import chalk from 'chalk';
|
2 | import { resolve, relative } from 'path';
|
3 | import { writeFileSync, readFileSync } from 'fs';
|
4 | import * as YAML from 'json-to-pretty-yaml';
|
5 | import * as detectIndent from 'detect-indent';
|
6 |
|
7 | export 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 |
|
21 | export function writePackage(answers, configLocation) {
|
22 |
|
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 |
|
34 | if (!pkg.devDependencies) {
|
35 | pkg.devDependencies = {};
|
36 | }
|
37 |
|
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 | }
|
46 | export function bold(str) {
|
47 | return chalk.bold(str);
|
48 | }
|
49 | export function grey(str) {
|
50 | return chalk.grey(str);
|
51 | }
|
52 | export function italic(str) {
|
53 | return chalk.italic(str);
|
54 | }
|
55 |
|
\ | No newline at end of file |