Skip to content

Commit 1a3ac0c

Browse files
committed
Switch internal scripts to ts
Also adds a formatting script and runs it against all TS files in the samples
1 parent 6097cc7 commit 1a3ac0c

File tree

39 files changed

+3044
-780
lines changed

39 files changed

+3044
-780
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-node@v4
14-
- run: node ./.scripts/validate.js
14+
- run: npm run validate

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.DS_Store
22
npm-debug.log
33
Thumbs.db
4-
*/node_modules/
4+
**/node_modules/
55
*/out/
66
*/.vs/
77
tsconfig.lsif.json

.scripts/copy-into.js

-22
This file was deleted.

.scripts/copy-into.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copies a file into every sample, overwriting the existing file if it exists.
3+
*/
4+
import * as fs from 'fs';
5+
import * as path from 'path';
6+
import { lspSamples, samples } from './samples';
7+
8+
if (require.main === module) {
9+
const filePath = process.argv[2];
10+
if (!filePath) {
11+
console.error('Please provide a file path as the argument.');
12+
process.exit(1);
13+
}
14+
15+
const fileName = path.basename(filePath);
16+
const fileContent = fs.readFileSync(filePath);
17+
18+
for (const sample of [...samples, ...lspSamples]) {
19+
const destinationPath = path.join(sample.path, fileName);
20+
fs.writeFileSync(destinationPath, fileContent);
21+
console.log(`Copied ${fileName} to ${sample.path}`);
22+
}
23+
}

.scripts/format.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as fs from 'fs';
2+
import * as glob from 'glob';
3+
import * as ts from 'typescript';
4+
5+
class LanguageServiceHost implements ts.LanguageServiceHost {
6+
files: ts.MapLike<ts.IScriptSnapshot> = {};
7+
addFile(fileName: string, text: string) {
8+
this.files[fileName] = ts.ScriptSnapshot.fromString(text);
9+
}
10+
11+
fileExists(path: string): boolean {
12+
return !!this.files[path];
13+
}
14+
15+
readFile(path: string): string | undefined {
16+
return this.files[path]?.getText(0, this.files[path]!.getLength());
17+
}
18+
19+
// for ts.LanguageServiceHost
20+
21+
getCompilationSettings = () => ts.getDefaultCompilerOptions();
22+
getScriptFileNames = () => Object.keys(this.files);
23+
getScriptVersion = (_fileName: string) => '0';
24+
getScriptSnapshot = (fileName: string) => this.files[fileName];
25+
getCurrentDirectory = () => process.cwd();
26+
getDefaultLibFileName = (options: ts.CompilerOptions) => ts.getDefaultLibFilePath(options);
27+
}
28+
29+
const defaults: ts.FormatCodeSettings = {
30+
baseIndentSize: 0,
31+
indentSize: 4,
32+
tabSize: 4,
33+
indentStyle: ts.IndentStyle.Smart,
34+
newLineCharacter: '\r\n',
35+
convertTabsToSpaces: false,
36+
insertSpaceAfterCommaDelimiter: true,
37+
insertSpaceAfterSemicolonInForStatements: true,
38+
insertSpaceBeforeAndAfterBinaryOperators: true,
39+
insertSpaceAfterConstructor: false,
40+
insertSpaceAfterKeywordsInControlFlowStatements: true,
41+
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
42+
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
43+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
44+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
45+
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
46+
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
47+
insertSpaceAfterTypeAssertion: false,
48+
insertSpaceBeforeFunctionParenthesis: false,
49+
placeOpenBraceOnNewLineForFunctions: false,
50+
placeOpenBraceOnNewLineForControlBlocks: false,
51+
insertSpaceBeforeTypeAnnotation: false,
52+
};
53+
54+
function format(fileName: string, text: string) {
55+
56+
const host = new LanguageServiceHost();
57+
host.addFile(fileName, text);
58+
59+
const languageService = ts.createLanguageService(host);
60+
const edits = languageService.getFormattingEditsForDocument(fileName, { ...defaults });
61+
edits
62+
.sort((a, b) => a.span.start - b.span.start)
63+
.reverse()
64+
.forEach(edit => {
65+
const head = text.slice(0, edit.span.start);
66+
const tail = text.slice(edit.span.start + edit.span.length);
67+
text = `${head}${edit.newText}${tail}`;
68+
});
69+
70+
return text;
71+
}
72+
73+
if (require.main === module) {
74+
glob.sync(`${__dirname}/../**/*.ts`).forEach((file) => {
75+
if (file.endsWith('.d.ts') || file.includes('node_modules')) {
76+
return;
77+
}
78+
79+
const out = format(file, fs.readFileSync(file, 'utf8'));
80+
fs.writeFileSync(file, out);
81+
});
82+
}

.scripts/run-command.js .scripts/run-command.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
/**
33
* Try running install for all the samples
44
*/
5-
const fs = require('fs');
6-
const path = require('path');
7-
const child_process = require('child_process');
8-
const { samples, lspSamples } = require('./samples');
5+
import * as child_process from 'child_process';
6+
import * as fs from 'fs';
7+
import * as path from 'path';
8+
import { lspSamples, Sample, samples } from './samples';
99

10-
async function tryRunCommand(
11-
/** @type {string} */ command,
12-
/** @type {import('./samples').Sample} */ sample,
13-
) {
10+
async function tryRunCommand(command: string, sample: Sample) {
1411
const packageJsonPath = path.join(sample.path, 'package.json');
1512
if (fs.existsSync(packageJsonPath)) {
1613
try {
@@ -28,7 +25,9 @@ async function tryRunCommand(
2825
}
2926
}
3027

31-
const command = process.argv.slice(2).join(' ');
32-
for (const sample of [...samples, ...lspSamples]) {
33-
tryRunCommand(command, sample);
28+
if (require.main === module) {
29+
const command = process.argv.slice(2).join(' ');
30+
for (const sample of [...samples, ...lspSamples]) {
31+
tryRunCommand(command, sample);
32+
}
3433
}

.scripts/run-script.js

-28
This file was deleted.

.scripts/run-script.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Try running an npm script for each of the samples.
3+
*/
4+
import * as child_process from 'child_process';
5+
import * as fs from 'fs';
6+
import * as path from 'path';
7+
import { lspSamples, Sample, samples } from './samples';
8+
9+
function tryRun(scriptName: string, sample: Sample) {
10+
const packageJsonPath = path.join(sample.path, 'package.json');
11+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
12+
if (Object.keys(packageJson['scripts'] || {}).includes(scriptName)) {
13+
console.log(`=== Running ${scriptName} on ${path.basename(sample.path)} ===`)
14+
child_process.execSync(`npm run ${scriptName}`, {
15+
cwd: sample.path,
16+
stdio: 'inherit'
17+
});
18+
}
19+
}
20+
21+
if (require.main === module) {
22+
const scriptName = process.argv[2];
23+
for (const sample of [...samples, ...lspSamples]) {
24+
tryRun(scriptName, sample);
25+
}
26+
}

0 commit comments

Comments
 (0)