-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.ts
330 lines (291 loc) · 12.9 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import Generator from 'yeoman-generator';
import type { CopyOptions } from 'mem-fs-editor';
import _ from 'lodash';
import chalk from 'chalk';
import * as path from 'node:path';
import which from 'which';
import { EOL } from 'node:os';
import * as url from 'node:url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const TEMPLATE_CORE_DIR = '../templates/core';
const TEMPLATE_VSCODE_DIR = '../templates/vscode';
const TEMPLATE_CLI_DIR = '../templates/cli';
const TEMPLATE_WEB_DIR = '../templates/web';
const TEMPLATE_TEST_DIR = '../templates/test';
const USER_DIR = '.';
const EXTENSION_NAME = /<%= extension-name %>/g;
const RAW_LANGUAGE_NAME = /<%= RawLanguageName %>/g;
const FILE_EXTENSION = /"?<%= file-extension %>"?/g;
const FILE_EXTENSION_GLOB = /<%= file-glob-extension %>/g;
const TSCONFIG_BASE_NAME = /<%= tsconfig %>/g;
const LANGUAGE_NAME = /<%= LanguageName %>/g;
const LANGUAGE_ID = /<%= language-id %>/g;
const LANGUAGE_PATH_ID = /language-id/g;
const NEWLINES = /\r?\n/g;
export interface Answers {
extensionName: string;
rawLanguageName: string;
fileExtensions: string;
includeVSCode: boolean;
includeCLI: boolean;
includeWeb: boolean;
includeTest: boolean;
}
export interface PostAnwers {
openWith: 'code' | false
}
function printLogo(log: (message: string) => void): void {
log('\u001b[36m┌─────┐ ─┐');
log('\u001b[36;1m┌───┐ │ ╶─╮ ┌─╮ ╭─╮ \u001b[36m╷ ╷ ╷ ┌─┬─╮');
log('\u001b[36;1m│ ,´ │ ╭─┤ │ │ │ │ \u001b[36m│ │ │ │ │ │');
log('\u001b[36;1m│╱ ╰─ ╰─┘ ╵ ╵ ╰─┤ \u001b[36m╵ ╰─╯ ╵ ╵ ╵');
log('\u001b[36;1m` ╶─╯');
log('');
}
function description(...d: string[]): string {
return chalk.reset(chalk.dim(d.join(' ') + '\n')) + chalk.green('?');
}
export class LangiumGenerator extends Generator {
private answers: Answers;
constructor(args: string | string[], options: Record<string, unknown>) {
super(args, options);
}
async prompting(): Promise<void> {
printLogo(this.log);
this.answers = await this.prompt<Answers>([
{
type: 'input',
name: 'extensionName',
prefix: description(
'Welcome to Langium!',
'This tool generates a VS Code extension with a "Hello World" language to get started quickly.',
'The extension name is an identifier used in the extension marketplace or package registry.'
),
message: 'Your extension name:',
default: 'hello-world',
},
{
type: 'input',
name: 'rawLanguageName',
prefix: description(
'The language name is used to identify your language in VS Code.',
'Please provide a name to be shown in the UI.',
'CamelCase and kebab-case variants will be created and used in different parts of the extension and language server.'
),
message: 'Your language name:',
default: 'Hello World',
validate: (input: string): boolean | string =>
/^[a-zA-Z].*$/.test(input)
? true
: 'The language name must start with a letter.',
},
{
type: 'input',
name: 'fileExtensions',
prefix: description(
'Source files of your language are identified by their file name extension.',
'You can specify multiple file extensions separated by commas.'
),
message: 'File extensions:',
default: '.hello',
validate: (input: string): boolean | string =>
/^\.?\w+(\s*,\s*\.?\w+)*$/.test(input)
? true
: 'A file extension can start with . and must contain only letters and digits. Extensions must be separated by commas.',
},
{
type: 'confirm',
name: 'includeVSCode',
prefix: description(
'Your language can be run inside of a VSCode extension.'
),
message: 'Include VSCode extension?',
default: 'yes'
},
{
type: 'confirm',
name: 'includeCLI',
prefix: description(
'You can add CLI to your language.'
),
message: 'Include CLI?',
default: 'yes'
},
{
type: 'confirm',
name: 'includeWeb',
prefix: description(
'You can run the language server in your web browser.'
),
message: 'Include Web worker?',
default: 'yes'
},
{
type: 'confirm',
name: 'includeTest',
prefix: description(
'You can add the setup for language tests using Vitest.'
),
message: 'Include language tests?',
default: 'yes'
}
]);
}
writing(): void {
const fileExtensions = Array.from(
new Set(
this.answers.fileExtensions
.split(',')
.map(ext => ext.replace(/\./g, '').trim()),
)
);
this.answers.fileExtensions = `[${fileExtensions.map(ext => `".${ext}"`).join(', ')}]`;
const fileExtensionGlob = fileExtensions.length > 1 ? `{${fileExtensions.join(',')}}` : fileExtensions[0];
this.answers.rawLanguageName = this.answers.rawLanguageName.replace(
/(?![\w| |\-|_])./g,
''
);
const languageName = _.upperFirst(
_.camelCase(this.answers.rawLanguageName)
);
const languageId = _.kebabCase(this.answers.rawLanguageName);
const referencedTsconfigBaseName = this.answers.includeTest ? 'tsconfig.src.json' : 'tsconfig.json';
const templateCopyOptions: CopyOptions = {
process: content => this._replaceTemplateWords(fileExtensionGlob, languageName, languageId, referencedTsconfigBaseName, content),
processDestinationPath: path => this._replaceTemplateNames(languageId, path)
};
this.sourceRoot(path.join(__dirname, TEMPLATE_CORE_DIR));
const pkgJson = this.fs.readJSON(path.join(this.sourceRoot(), '.package.json'));
this.fs.extendJSON(this._extensionPath('package-template.json'), pkgJson, undefined, 4);
for (const path of ['.', '.vscode', '.eslintrc.json']) {
this.fs.copy(
this.templatePath(path),
this._extensionPath(path),
templateCopyOptions
);
}
// .gitignore files don't get published to npm, so we need to copy it under a different name
this.fs.copy(this.templatePath('../gitignore.txt'), this._extensionPath('.gitignore'));
if (this.answers.includeVSCode) {
this.sourceRoot(path.join(__dirname, TEMPLATE_VSCODE_DIR));
const pkgJson = this.fs.readJSON(path.join(this.sourceRoot(), '.package.json'));
this.fs.extendJSON(this._extensionPath('package-template.json'), pkgJson, undefined, 4);
this.sourceRoot(path.join(__dirname, TEMPLATE_VSCODE_DIR));
for (const path of ['.', '.vscode', '.vscodeignore']) {
this.fs.copy(
this.templatePath(path),
this._extensionPath(path),
templateCopyOptions
);
}
}
if (this.answers.includeCLI) {
this.sourceRoot(path.join(__dirname, TEMPLATE_CLI_DIR));
const pkgJson = this.fs.readJSON(path.join(this.sourceRoot(), '.package.json'));
this.fs.extendJSON(this._extensionPath('package-template.json'),pkgJson, undefined, 4);
for (const path of ['.']) {
this.fs.copy(
this.templatePath(path),
this._extensionPath(path),
templateCopyOptions
);
}
}
if (this.answers.includeWeb) {
this.sourceRoot(path.join(__dirname, TEMPLATE_WEB_DIR));
const pkgJson = this.fs.readJSON(path.join(this.sourceRoot(), '.package.json'));
this.fs.extendJSON(this._extensionPath('package-template.json'), pkgJson, undefined, 4);
this.sourceRoot(path.join(__dirname, TEMPLATE_WEB_DIR));
for (const path of ['.']) {
this.fs.copy(
this.templatePath(path),
this._extensionPath(path),
templateCopyOptions
);
}
}
if (this.answers.includeTest) {
this.sourceRoot(path.join(__dirname, TEMPLATE_TEST_DIR));
this.fs.copy(
this.templatePath('.'),
this._extensionPath(),
templateCopyOptions
);
// update the scripts section in the package.json to use 'tsconfig.src.json' for building
const pkgJson = this.fs.readJSON(this.templatePath('.package.json'));
this.fs.extendJSON(this._extensionPath('package-template.json'), pkgJson, undefined, 4);
// update the 'includes' property in the existing 'tsconfig.json' and adds '"noEmit": true'
const tsconfigJson = this.fs.readJSON(this.templatePath('.tsconfig.json'));
this.fs.extendJSON(this._extensionPath('tsconfig.json'), tsconfigJson, undefined, 4);
// the initial '.vscode/extensions.json' can't be extended as above, as it contains comments, which is tolerated by vscode,
// but not by `this.fs.extendJSON(...)`, so
this.fs.copy(this.templatePath('.vscode-extensions.json'), this._extensionPath('.vscode/extensions.json'), templateCopyOptions);
}
this.fs.copy(
this._extensionPath('package-template.json'),
this._extensionPath('package.json'),
templateCopyOptions
);
this.fs.delete(this._extensionPath('package-template.json'));
}
async install(): Promise<void> {
const extensionPath = this._extensionPath();
const opts = { cwd: extensionPath };
if(!this.args.includes('skip-install')) {
this.spawnSync('npm', ['install'], opts);
}
this.spawnSync('npm', ['run', 'langium:generate'], opts);
if (this.answers.includeVSCode || this.answers.includeCLI) {
this.spawnSync('npm', ['run', 'build'], opts);
}
if (this.answers.includeWeb) {
this.spawnSync('npm', ['run', 'build:web'], opts);
}
}
async end(): Promise<void> {
const code = await which('code').catch(() => undefined);
if (code) {
const answer = await this.prompt<PostAnwers>({
type: 'list',
name: 'openWith',
message: 'Do you want to open the new folder with Visual Studio Code?',
choices: [
{
name: 'Open with `code`',
value: code
},
{
name: 'Skip',
value: false
}
]
});
if (answer?.openWith) {
this.spawn(answer.openWith, [this._extensionPath()]);
}
}
}
_extensionPath(...path: string[]): string {
return this.destinationPath(USER_DIR, this.answers.extensionName, ...path);
}
_replaceTemplateWords(fileExtensionGlob: string, languageName: string, languageId: string, tsconfigBaseName: string, content: string | Buffer): string {
return content.toString()
.replace(EXTENSION_NAME, this.answers.extensionName)
.replace(RAW_LANGUAGE_NAME, this.answers.rawLanguageName)
.replace(FILE_EXTENSION, this.answers.fileExtensions)
.replace(FILE_EXTENSION_GLOB, fileExtensionGlob)
.replace(LANGUAGE_NAME, languageName)
.replace(LANGUAGE_ID, languageId)
.replace(TSCONFIG_BASE_NAME, tsconfigBaseName)
.replace(NEWLINES, EOL);
}
_replaceTemplateNames(languageId: string, path: string): string {
return path.replace(LANGUAGE_PATH_ID, languageId);
}
}
export default LangiumGenerator;