-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.ts
115 lines (99 loc) · 3.11 KB
/
create.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { ux } from '@oclif/core';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import BaseCommand from '../../base';
import { fetchRemoteTypes } from '../../utils/extension-utils';
import * as questions from '../../utils/questions';
import templates from '../../utils/templates';
export default class Create extends BaseCommand {
static description = 'Create an example extension';
static flags = {
...BaseCommand.flags,
};
async run() {
const extensionAnswers = await inquirer.prompt(
questions.extensionQuestions
);
const directoryName = extensionAnswers.identifier.match(
/^[^.]+\.([^.]+)$/
)[1];
// Check if the extension already exists.
if (fs.existsSync(directoryName)) {
throw new Error(`A directory named '${directoryName}' already exists.`);
}
const ahaExtensionSchema: { [k: string]: any } = {};
ahaExtensionSchema.contributes = {};
const createContributions = (
await inquirer.prompt(questions.addContributionsQuestion)
).createContributions;
if (createContributions) {
do {
process.stdout.write('\n');
const contribution = await questions.getContributionFromQuestions();
ahaExtensionSchema.contributes[contribution.type] =
ahaExtensionSchema.contributes[contribution.type] || {};
ahaExtensionSchema.contributes[contribution.type][contribution.name] =
contribution.contribution;
} while (
(await inquirer.prompt(questions.addAnotherContributionQuestion))
.add === 'yes'
);
} else {
ahaExtensionSchema.contributes = {
views: {
samplePage: {
title: 'Sample Page',
entryPoint: 'src/views/samplePage.js',
host: 'page',
location: {
menu: 'Work',
},
},
},
commands: {
sampleCommand: {
title: 'Sample Command',
entryPoint: 'src/commands/sampleCommand.js',
},
},
};
}
// Create the extension and template files.
ux.action.start('Creating extension');
fs.mkdirSync(directoryName);
fs.writeFileSync(
`${directoryName}/package.json`,
templates.packageTemplate(
extensionAnswers.identifier,
extensionAnswers.name,
extensionAnswers.author,
ahaExtensionSchema
)
);
fs.writeFileSync(
`${directoryName}/README.md`,
templates.readmeTemplate(extensionAnswers.name)
);
fs.writeFileSync(
`${directoryName}/tsconfig.json`,
templates.tsconfigTemplate()
);
fs.mkdirSync(`${directoryName}/.vscode`);
fs.writeFileSync(
`${directoryName}/.vscode/settings.json`,
templates.vscodeTemplate()
);
fs.writeFileSync(
`${directoryName}/.gitignore`,
templates.gitignoreTemplate()
);
templates.writeContributionTemplates(
fs,
directoryName,
extensionAnswers.identifier,
ahaExtensionSchema.contributes
);
await fetchRemoteTypes(directoryName);
ux.action.stop(`Extension created in directory '${directoryName}'`);
}
}