-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-contribution.ts
76 lines (62 loc) · 2.13 KB
/
add-contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import BaseCommand from '../../base';
import ux from 'cli-ux';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import templates from '../../utils/templates';
import * as questions from '../../utils/questions';
export default class AddContribution extends BaseCommand {
static description = 'Add a contribution to an extension';
static flags = {
...BaseCommand.flags,
};
async run() {
if (!fs.existsSync('./package.json')) {
throw new Error('Unable to find package.json in the current directory.');
}
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
if (!packageJson.ahaExtension) {
throw new Error('Unable to find the ahaExtension entry in package.json.');
}
packageJson.ahaExtension.contributes =
packageJson.ahaExtension.contributes || {};
const newContributions: { [k: string]: any } = {};
do {
process.stdout.write('\n');
const contribution = await questions.getContributionFromQuestions();
newContributions[contribution.type] =
newContributions[contribution.type] || {};
newContributions[contribution.type][contribution.name] =
contribution.contribution;
packageJson.ahaExtension.contributes[contribution.type] =
packageJson.ahaExtension.contributes[contribution.type] || {};
packageJson.ahaExtension.contributes[contribution.type][
contribution.name
] = contribution.contribution;
} while (
(await inquirer.prompt(questions.addAnotherContributionQuestion)).add ===
'yes'
);
// Update the package.json file
ux.action.start('Writing package.json file');
fs.writeFileSync(
`./package.json`,
JSON.stringify(
packageJson,
(key, value) => {
if (value !== null) return value;
},
2
)
);
ux.action.stop('Finished writing package.json file');
// Create the contributions
ux.action.start('Creating contributions');
templates.writeContributionTemplates(
fs,
'.',
packageJson.name,
newContributions
);
ux.action.stop('Contributions created');
}
}