Skip to content

Commit 83a2e84

Browse files
resources: switch internal scripts to TS (#3562)
1 parent 2d1ec99 commit 83a2e84

14 files changed

+269
-170
lines changed

.eslintrc.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,17 @@ overrides:
702702
node: true
703703
rules:
704704
internal-rules/only-ascii: [error, { allowEmoji: true }]
705-
node/no-unpublished-require: off
705+
node/no-unpublished-import: off
706706
node/no-sync: off
707+
import/no-namespace: off
707708
import/no-extraneous-dependencies: [error, { devDependencies: true }]
708709
import/no-nodejs-modules: off
709-
import/no-commonjs: off
710710
no-console: off
711+
- files: 'resources/eslint-internal-rules/**'
712+
env:
713+
node: true
714+
rules:
715+
import/no-commonjs: off
711716
- files: '**/*.jsx'
712717
parserOptions:
713718
sourceType: module

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: npm ci --ignore-scripts
2929

3030
- name: Generate report
31-
run: 'node resources/diff-npm-package.js BASE HEAD'
31+
run: 'npm run diff:npm BASE HEAD'
3232

3333
- name: Upload generated report
3434
uses: actions/upload-artifact@v2

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
},
3030
"scripts": {
3131
"preversion": ". ./resources/checkgit.sh && npm ci --ignore-scripts",
32-
"version": "node resources/gen-version.js && npm test && git add src/version.ts",
32+
"version": "ts-node resources/gen-version.ts && npm test && git add src/version.ts",
3333
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.ts",
34-
"changelog": "node resources/gen-changelog.js",
34+
"changelog": "ts-node resources/gen-changelog.ts",
3535
"benchmark": "ts-node benchmark/benchmark.ts",
3636
"test": "npm run lint && npm run check && npm run testonly:cover && npm run prettier:check && npm run check:spelling && npm run check:integrations",
3737
"lint": "eslint --cache --max-warnings 0 .",
@@ -45,8 +45,9 @@
4545
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.ts",
4646
"start": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus start ./website",
4747
"build:website": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus build --out-dir=\"$(pwd)/websiteDist\" ./website",
48-
"build:npm": "node resources/build-npm.js",
49-
"build:deno": "node resources/build-deno.js",
48+
"build:npm": "ts-node resources/build-npm.ts",
49+
"build:deno": "ts-node resources/build-deno.ts",
50+
"diff:npm": "ts-node resources/diff-npm-package.ts",
5051
"gitpublish:npm": "bash ./resources/gitpublish.sh npm npmDist",
5152
"gitpublish:deno": "bash ./resources/gitpublish.sh deno denoDist"
5253
},
@@ -58,6 +59,7 @@
5859
"@types/chai": "4.3.1",
5960
"@types/mocha": "9.1.1",
6061
"@types/node": "17.0.31",
62+
"@types/prettier": "2.6.0",
6163
"@typescript-eslint/eslint-plugin": "5.21.0",
6264
"@typescript-eslint/parser": "5.21.0",
6365
"c8": "7.11.2",

resources/add-extension-to-import-paths.js

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as assert from 'node:assert';
2+
import * as util from 'node:util';
3+
4+
import * as ts from 'typescript';
5+
6+
/**
7+
* Adds extension to all paths imported inside MJS files
8+
*
9+
* Transforms:
10+
*
11+
* ```
12+
* import { foo } from './bar';
13+
* export { foo } from './bar';
14+
* ```
15+
*
16+
* to:
17+
*
18+
* ```
19+
* import { foo } from './bar.mjs';
20+
* export { foo } from './bar.mjs';
21+
* ```
22+
*
23+
*/
24+
export function addExtensionToImportPaths(config: { extension: string }) {
25+
const { extension } = config;
26+
return (context: ts.TransformationContext) => {
27+
const { factory } = context;
28+
29+
return visitSourceFile;
30+
31+
function visitSourceFile(sourceFile: ts.SourceFile) {
32+
return ts.visitNode(sourceFile, visitNode);
33+
}
34+
35+
function visitNode(node: ts.Node): ts.Node {
36+
const source: string | undefined = (node as any).moduleSpecifier?.text;
37+
if (source?.startsWith('./') || source?.startsWith('../')) {
38+
if (ts.isImportDeclaration(node)) {
39+
return factory.updateImportDeclaration(
40+
node,
41+
node.decorators,
42+
node.modifiers,
43+
node.importClause,
44+
ts.createStringLiteral(source + extension),
45+
node.assertClause,
46+
);
47+
}
48+
if (ts.isExportDeclaration(node)) {
49+
return factory.updateExportDeclaration(
50+
node,
51+
node.decorators,
52+
node.modifiers,
53+
node.isTypeOnly,
54+
node.exportClause,
55+
ts.createStringLiteral(source + extension),
56+
node.assertClause,
57+
);
58+
}
59+
60+
assert(
61+
false,
62+
'Unexpected node with moduleSpecifier: ' + util.inspect(node),
63+
);
64+
}
65+
return ts.visitEachChild(node, visitNode, context);
66+
}
67+
};
68+
}

resources/build-deno.js renamed to resources/build-deno.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
'use strict';
1+
import * as fs from 'node:fs';
2+
import * as path from 'node:path';
23

3-
const fs = require('fs');
4-
const path = require('path');
4+
import * as ts from 'typescript';
55

6-
const ts = require('typescript');
7-
8-
const inlineInvariant = require('./inline-invariant.js');
9-
const addExtensionToImportPaths = require('./add-extension-to-import-paths.js');
10-
const {
11-
writeGeneratedFile,
12-
readdirRecursive,
13-
showDirStats,
14-
} = require('./utils.js');
6+
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
7+
import { inlineInvariant } from './inline-invariant';
8+
import { readdirRecursive, showDirStats, writeGeneratedFile } from './utils';
159

1610
if (require.main === module) {
1711
fs.rmSync('./denoDist', { recursive: true, force: true });

resources/build-npm.js renamed to resources/build-npm.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict';
1+
import * as assert from 'node:assert';
2+
import * as fs from 'node:fs';
3+
import * as path from 'node:path';
24

3-
const fs = require('fs');
4-
const path = require('path');
5-
const assert = require('assert');
5+
import * as ts from 'typescript';
66

7-
const ts = require('typescript');
8-
9-
const inlineInvariant = require('./inline-invariant.js');
10-
const addExtensionToImportPaths = require('./add-extension-to-import-paths.js');
11-
const { writeGeneratedFile, showDirStats } = require('./utils.js');
7+
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
8+
import { inlineInvariant } from './inline-invariant';
9+
import { readPackageJSON, showDirStats, writeGeneratedFile } from './utils';
1210

1311
if (require.main === module) {
1412
fs.rmSync('./npmDist', { recursive: true, force: true });
@@ -92,9 +90,7 @@ if (require.main === module) {
9290
}
9391

9492
function buildPackageJSON() {
95-
const packageJSON = JSON.parse(
96-
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
97-
);
93+
const packageJSON = readPackageJSON();
9894

9995
delete packageJSON.private;
10096
delete packageJSON.scripts;
@@ -113,7 +109,7 @@ function buildPackageJSON() {
113109

114110
const { version } = packageJSON;
115111
const versionMatch = /^\d+\.\d+\.\d+-?(?<preReleaseTag>.*)?$/.exec(version);
116-
if (!versionMatch) {
112+
if (versionMatch?.groups == null) {
117113
throw new Error('Version does not match semver spec: ' + version);
118114
}
119115

resources/diff-npm-package.js renamed to resources/diff-npm-package.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict';
1+
import * as assert from 'node:assert';
2+
import * as fs from 'node:fs';
3+
import * as os from 'node:os';
4+
import * as path from 'node:path';
25

3-
const os = require('os');
4-
const fs = require('fs');
5-
const path = require('path');
6-
7-
const { exec } = require('./utils.js');
6+
import { exec, execOutput } from './utils';
87

98
const LOCAL = 'local';
109
const localRepoDir = path.join(__dirname, '..');
@@ -29,7 +28,7 @@ console.log(`📦 Building NPM package for ${toRevision}...`);
2928
const toPackage = prepareNPMPackage(toRevision);
3029

3130
console.log('➖➕ Generating diff...');
32-
const diff = exec(`npm diff --diff=${fromPackage} --diff=${toPackage}`);
31+
const diff = execOutput(`npm diff --diff=${fromPackage} --diff=${toPackage}`);
3332

3433
if (diff === '') {
3534
console.log('No changes found!');
@@ -39,7 +38,7 @@ if (diff === '') {
3938
console.log('Report saved to: ', reportPath);
4039
}
4140

42-
function generateReport(diffString) {
41+
function generateReport(diffString: string): string {
4342
return `
4443
<!DOCTYPE html>
4544
<html lang="en-us">
@@ -77,14 +76,15 @@ function generateReport(diffString) {
7776
</html>
7877
`;
7978
}
80-
function prepareNPMPackage(revision) {
79+
function prepareNPMPackage(revision: string): string {
8180
if (revision === LOCAL) {
8281
exec('npm --quiet run build:npm', { cwd: localRepoDir });
8382
return path.join(localRepoDir, 'npmDist');
8483
}
8584

8685
// Returns the complete git hash for a given git revision reference.
87-
const hash = exec(`git rev-parse "${revision}"`);
86+
const hash = execOutput(`git rev-parse "${revision}"`);
87+
assert(hash != null);
8888

8989
const repoDir = path.join(tmpDir, hash);
9090
fs.rmSync(repoDir, { recursive: true, force: true });

0 commit comments

Comments
 (0)