-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
591 lines (519 loc) · 17.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import ts = require('typescript');
import path = require('path');
import core = require('@tsslint/core');
import cache = require('./lib/cache.js');
import worker = require('./lib/worker.js');
import glob = require('glob');
import fs = require('fs');
import os = require('os');
import languagePlugins = require('./lib/languagePlugins.js');
import { setTimeout } from 'node:timers/promises';
import { getVSCodeFormattingSettings } from './lib/formatting.js';
const _reset = '\x1b[0m';
const purple = (s: string) => '\x1b[35m' + s + _reset;
const cyan = (s: string) => '\x1b[36m' + s + _reset;
const darkGray = (s: string) => '\x1b[90m' + s + _reset;
const lightRed = (s: string) => '\x1b[91m' + s + _reset;
const lightGreen = (s: string) => '\x1b[92m' + s + _reset;
const lightYellow = (s: string) => '\x1b[93m' + s + _reset;
// https://talyian.github.io/ansicolors/
const tsColor = (s: string) => '\x1b[34m' + s + _reset;
const tsMacroColor = (s: string) => '\x1b[38;5;135m' + s + _reset;
const vueColor = (s: string) => '\x1b[32m' + s + _reset;
const vueVineColor = (s: string) => '\x1b[38;5;48m' + s + _reset;
const mdxColor = (s: string) => '\x1b[33m' + s + _reset;
const astroColor = (s: string) => '\x1b[38;5;209m' + s + _reset;
let threads = 1;
if (process.argv.includes('--threads')) {
const threadsIndex = process.argv.indexOf('--threads');
const threadsArg = process.argv[threadsIndex + 1];
if (!threadsArg || threadsArg.startsWith('-')) {
console.error(lightRed(`Missing argument for --threads.`));
process.exit(1);
}
threads = Math.min(os.availableParallelism(), Number(threadsArg));
}
class Project {
workers: ReturnType<typeof worker.create>[] = [];
fileNames: string[] = [];
options: ts.CompilerOptions = {};
configFile: string | undefined;
currentFileIndex = 0;
builtConfig: string | undefined;
cache: cache.CacheData = {};
constructor(
public tsconfig: string,
public languages: string[]
) { }
async init(
// @ts-expect-error
clack: typeof import('@clack/prompts')
) {
this.configFile = ts.findConfigFile(path.dirname(this.tsconfig), ts.sys.fileExists, 'tsslint.config.ts');
const labels: string[] = [];
if (this.languages.length === 0) {
labels.push(tsColor('TS'));
} else {
if (this.languages.includes('ts-macro')) {
labels.push(tsMacroColor('TS Macro'));
}
if (this.languages.includes('vue')) {
labels.push(vueColor('Vue'));
}
if (this.languages.includes('vue-vine')) {
labels.push(vueVineColor('Vue Vine'));
}
if (this.languages.includes('mdx')) {
labels.push(mdxColor('MDX'));
}
if (this.languages.includes('astro')) {
labels.push(astroColor('Astro'));
}
}
const label = labels.join(darkGray(' | '));
if (!this.configFile) {
clack.log.error(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${darkGray('(No tsslint.config.ts found)')}`);
return this;
}
const commonLine = await parseCommonLine(this.tsconfig, this.languages);
this.fileNames = commonLine.fileNames;
this.options = commonLine.options;
if (!this.fileNames.length) {
clack.log.warn(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${darkGray('(No included files)')}`);
return this;
}
clack.log.info(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${darkGray(`(${this.fileNames.length})`)}`);
if (!process.argv.includes('--force')) {
this.cache = cache.loadCache(this.tsconfig, this.configFile, ts.sys.createHash);
}
return this;
}
}
(async () => {
const builtConfigs = new Map<string, Promise<string | undefined>>();
const clack = await import('@clack/prompts');
const processFiles = new Set<string>();
const tsconfigAndLanguages = new Map<string, string[]>();
const formattingSettings = getFormattingSettings();
let projects: Project[] = [];
let spinner = clack.spinner();
let lastSpinnerUpdate = Date.now();
let hasFix = false;
let allFilesNum = 0;
let processed = 0;
let excluded = 0;
let passed = 0;
let errors = 0;
let warnings = 0;
let cached = 0;
if (
![
'--project',
'--projects',
'--vue-project',
'--vue-projects',
'--vue-vine-project',
'--vue-vine-projects',
'--mdx-project',
'--mdx-projects',
'--astro-project',
'--astro-projects',
'--ts-macro-project',
'--ts-macro-projects',
].some(flag => process.argv.includes(flag))
) {
const language = await clack.select({
message: 'Select framework',
initialValue: undefined,
options: [{
label: 'Vanilla JS/TS',
value: undefined,
}, {
label: 'TS Macro',
value: 'ts-macro',
}, {
label: 'Vue',
value: 'vue',
}, {
label: 'Vue Vine',
value: 'vue-vine',
}, {
label: 'MDX',
value: 'mdx',
}, {
label: 'Astro',
value: 'astro',
}],
});
if (clack.isCancel(language)) {
process.exit(1);
}
const tsconfigOptions = glob.sync('**/{tsconfig.json,tsconfig.*.json,jsconfig.json}');
let options = await Promise.all(
tsconfigOptions.map(async tsconfigOption => {
const tsconfig = require.resolve(
tsconfigOption.startsWith('.') ? tsconfigOption : `./${tsconfigOption}`,
{ paths: [process.cwd()] }
);
try {
const commonLine = await parseCommonLine(tsconfig, language ? [language] : []);
return {
label: path.relative(process.cwd(), tsconfig) + ` (${commonLine.fileNames.length})`,
value: tsconfigOption,
};
} catch {
return undefined;
}
})
);
options = options.filter(option => !!option);
if (options.some(option => !option!.label.endsWith('(0)'))) {
options = options.filter(option => !option!.label.endsWith('(0)'));
}
if (!options.length) {
clack.log.error(lightRed('No projects found.'));
process.exit(1);
}
const selectedTsconfigs = await clack.multiselect({
message: 'Select one or multiple projects',
initialValues: [options[0]!.value],
// @ts-expect-error
options,
});
if (clack.isCancel(selectedTsconfigs)) {
process.exit(1);
}
let command = 'tsslint';
if (!language) {
command += ' --project ' + selectedTsconfigs.join(' ');
} else {
command += ` --${language}-project ` + selectedTsconfigs.join(' ');
}
clack.log.info(`${darkGray('Command:')} ${purple(command)}`);
for (let tsconfig of selectedTsconfigs) {
tsconfig = resolvePath(tsconfig);
tsconfigAndLanguages.set(tsconfig, language ? [language] : []);
}
} else {
const options = [
{
projectFlags: ['--project', '--projects'],
language: undefined,
},
{
projectFlags: ['--vue-project', '--vue-projects'],
language: 'vue',
},
{
projectFlags: ['--vue-vine-project', '--vue-vine-projects'],
language: 'vue-vine',
},
{
projectFlags: ['--mdx-project', '--mdx-projects'],
projectsFlag: '--mdx-projects',
language: 'mdx',
},
{
projectFlags: ['--astro-project', '--astro-projects'],
language: 'astro',
},
{
projectFlags: ['--ts-macro-project', '--ts-macro-projects'],
language: 'ts-macro',
},
];
for (const { projectFlags, language } of options) {
const projectFlag = projectFlags.find(flag => process.argv.includes(flag));
if (!projectFlag) {
continue;
}
let foundArg = false;
const projectsIndex = process.argv.indexOf(projectFlag);
for (let i = projectsIndex + 1; i < process.argv.length; i++) {
if (process.argv[i].startsWith('-')) {
break;
}
foundArg = true;
const searchGlob = process.argv[i];
const tsconfigs = glob.sync(searchGlob);
if (!tsconfigs.length) {
clack.log.error(lightRed(`No projects found for ${projectFlag} ${searchGlob}.`));
process.exit(1);
}
for (let tsconfig of tsconfigs) {
tsconfig = resolvePath(tsconfig);
if (!tsconfigAndLanguages.has(tsconfig)) {
tsconfigAndLanguages.set(tsconfig, []);
}
if (language) {
tsconfigAndLanguages.get(tsconfig)!.push(language);
}
}
}
if (!foundArg) {
clack.log.error(lightRed(`Missing argument for ${projectFlag}.`));
process.exit(1);
}
}
}
for (const [tsconfig, languages] of tsconfigAndLanguages) {
projects.push(await new Project(tsconfig, languages).init(clack));
}
spinner.start();
projects = projects.filter(project => !!project.configFile);
projects = projects.filter(project => !!project.fileNames.length);
for (const project of projects) {
project.builtConfig = await getBuiltConfig(project.configFile!);
}
projects = projects.filter(project => !!project.builtConfig);
for (const project of projects) {
allFilesNum += project.fileNames.length;
}
if (allFilesNum === 0) {
spinner.stop(lightYellow('No input files.'));
process.exit(1);
}
if (threads === 1) {
await startWorker(worker.createLocal() as any);
} else {
await Promise.all(new Array(threads).fill(0).map(() => {
return startWorker(worker.create());
}));
}
spinner.stop(
cached
? darkGray(`Processed ${processed} files with cache. (Use `) + cyan(`--force`) + darkGray(` to ignore cache.)`)
: darkGray(`Processed ${processed} files.`)
);
if (process.argv.includes('--fix') && !formattingSettings) {
const vscodeSettings = ts.findConfigFile(process.cwd(), ts.sys.fileExists, '.vscode/settings.json');
if (vscodeSettings) {
clack.log.message(darkGray(`Found available editor settings, you can use `) + cyan(`--vscode-settings ${path.relative(process.cwd(), vscodeSettings)}`) + darkGray(` to enable code format.`));
}
}
const projectsFlag = process.argv.find(arg => arg.endsWith('-projects'));
if (projectsFlag) {
clack.log.warn(
darkGray(`Please use `)
+ cyan(`${projectsFlag.slice(0, -1)}`)
+ darkGray(` instead of `)
+ cyan(`${projectsFlag}`)
+ darkGray(` starting from version 1.5.0.`)
);
}
const data = [
[passed, 'passed', lightGreen] as const,
[errors, 'errors', lightRed] as const,
[warnings, 'warnings', lightYellow] as const,
[excluded, 'excluded', darkGray] as const,
];
let summary = data
.filter(([count]) => count)
.map(([count, label, color]) => color(`${count} ${label}`))
.join(darkGray(' | '));
if (hasFix) {
summary += darkGray(` (Use `) + cyan(`--fix`) + darkGray(` to apply automatic fixes.)`);
} else if (errors || warnings) {
summary += darkGray(` (No fixes available.)`);
}
clack.outro(summary);
process.exit(errors ? 1 : 0);
async function startWorker(linterWorker: ReturnType<typeof worker.create>) {
const unfinishedProjects = projects.filter(project => project.currentFileIndex < project.fileNames.length);
if (!unfinishedProjects.length) {
return;
}
// Select a project that has not has a worker yet
let project = unfinishedProjects.find(project => !project.workers.length);
if (!project) {
// Choose a project with the most files left per worker
project = unfinishedProjects.sort((a, b) => {
const aFilesPerWorker = (a.fileNames.length - a.currentFileIndex) / a.workers.length;
const bFilesPerWorker = (b.fileNames.length - b.currentFileIndex) / b.workers.length;
return bFilesPerWorker - aFilesPerWorker;
})[0];
}
project.workers.push(linterWorker);
const setupSuccess = await linterWorker.setup(
project.tsconfig,
project.languages,
project.configFile!,
project.builtConfig!,
project.fileNames,
project.options,
formattingSettings
);
if (!setupSuccess) {
projects = projects.filter(p => p !== project);
startWorker(linterWorker);
return;
}
while (project.currentFileIndex < project.fileNames.length) {
const i = project.currentFileIndex++;
const fileName = project.fileNames[i];
const fileStat = fs.statSync(fileName, { throwIfNoEntry: false });
if (!fileStat) {
continue;
}
addProcessFile(fileName);
if (Date.now() - lastSpinnerUpdate > 100) {
lastSpinnerUpdate = Date.now();
await setTimeout(0);
}
let fileCache = project.cache[fileName];
if (fileCache) {
if (fileCache[0] !== fileStat.mtimeMs) {
fileCache[0] = fileStat.mtimeMs;
fileCache[1] = {};
fileCache[2] = {};
}
else {
cached++;
}
}
else {
project.cache[fileName] = fileCache = [fileStat.mtimeMs, {}, {}, false];
}
let diagnostics = await linterWorker.lint(
fileName,
process.argv.includes('--fix'),
fileCache
);
diagnostics = diagnostics.filter(diagnostic => diagnostic.category !== ts.DiagnosticCategory.Suggestion);
if (diagnostics.length) {
hasFix ||= await linterWorker.hasCodeFixes(fileName);
for (const diagnostic of diagnostics) {
hasFix ||= !!fileCache[1][diagnostic.code]?.[0];
let output = ts.formatDiagnosticsWithColorAndContext([diagnostic], {
getCurrentDirectory: ts.sys.getCurrentDirectory,
getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? x => x : x => x.toLowerCase(),
getNewLine: () => ts.sys.newLine,
});
output = output.trimEnd();
output = output.replace(`TS${diagnostic.code}`, String(diagnostic.code));
if (diagnostic.category === ts.DiagnosticCategory.Error) {
errors++;
log(output, 1);
}
else if (diagnostic.category === ts.DiagnosticCategory.Warning) {
warnings++;
log(output, 2);
}
else {
log(output);
}
}
} else if (!(await linterWorker.hasRules(fileName, fileCache[2]))) {
excluded++;
} else {
passed++;
}
processed++;
removeProcessFile(fileName);
}
cache.saveCache(project.tsconfig, project.configFile!, project.cache, ts.sys.createHash);
await startWorker(linterWorker);
}
function getFormattingSettings() {
let formattingSettings: ReturnType<typeof getVSCodeFormattingSettings> | undefined;
if (process.argv.includes('--vscode-settings')) {
formattingSettings = {
typescript: {},
javascript: {},
vue: {},
};
for (const section of ['typescript', 'javascript'] as const) {
formattingSettings[section] = {
...ts.getDefaultFormatCodeSettings('\n'),
indentStyle: ts.IndentStyle.Smart,
newLineCharacter: '\n',
insertSpaceAfterCommaDelimiter: true,
insertSpaceAfterConstructor: false,
insertSpaceAfterSemicolonInForStatements: true,
insertSpaceBeforeAndAfterBinaryOperators: true,
insertSpaceAfterKeywordsInControlFlowStatements: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: true,
insertSpaceBeforeFunctionParenthesis: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: true,
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
insertSpaceAfterTypeAssertion: false,
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
semicolons: ts.SemicolonPreference.Ignore,
};
}
let vscodeSettingsConfig = process.argv[process.argv.indexOf('--vscode-settings') + 1];
if (!vscodeSettingsConfig || vscodeSettingsConfig.startsWith('-')) {
clack.log.error(lightRed(`Missing argument for --vscode-settings.`));
process.exit(1);
}
const vscodeSettingsFile = resolvePath(vscodeSettingsConfig);
const vscodeSettings = getVSCodeFormattingSettings(vscodeSettingsFile);
formattingSettings.typescript = {
...formattingSettings.typescript,
...vscodeSettings.typescript,
};
formattingSettings.javascript = {
...formattingSettings.javascript,
...vscodeSettings.javascript,
};
formattingSettings.vue = {
...formattingSettings.vue,
...vscodeSettings.vue,
};
}
return formattingSettings;
}
async function getBuiltConfig(configFile: string) {
if (!builtConfigs.has(configFile)) {
builtConfigs.set(configFile, core.buildConfig(configFile, ts.sys.createHash, spinner, (s, code) => log(darkGray(s), code)));
}
return await builtConfigs.get(configFile);
}
function addProcessFile(fileName: string) {
processFiles.add(fileName);
updateSpinner();
}
function removeProcessFile(fileName: string) {
processFiles.delete(fileName);
updateSpinner();
}
function updateSpinner() {
if (processFiles.size === 1) {
const fileName = processFiles.values().next().value!;
spinner.message(darkGray(`[${processed + processFiles.size}/${allFilesNum}] ${path.relative(process.cwd(), fileName)}`));
} else {
spinner.message(darkGray(`[${processed + processFiles.size}/${allFilesNum}] Processing ${processFiles.size} files`));
}
}
function log(msg: string, code?: number) {
spinner.stop(msg, code);
spinner = clack.spinner();
spinner.start();
}
function resolvePath(p: string) {
if (
!path.isAbsolute(p)
&& !p.startsWith('./')
&& !p.startsWith('../')
) {
p = `./${p}`;
}
try {
return require.resolve(p, { paths: [process.cwd()] });
} catch {
clack.log.error(lightRed(`No such file: ${p}`));
process.exit(1);
}
}
})();
async function parseCommonLine(tsconfig: string, languages: string[]) {
const jsonConfigFile = ts.readJsonConfigFile(tsconfig, ts.sys.readFile);
const plugins = await languagePlugins.load(tsconfig, languages);
const extraFileExtensions = plugins.flatMap(plugin => plugin.typescript?.extraFileExtensions ?? []).flat();
return ts.parseJsonSourceFileConfigFileContent(jsonConfigFile, ts.sys, path.dirname(tsconfig), {}, tsconfig, undefined, extraFileExtensions);
}