Skip to content

Commit

Permalink
resources: switch internal scripts to TS (#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored May 8, 2022
1 parent 2d1ec99 commit 83a2e84
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 170 deletions.
9 changes: 7 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,17 @@ overrides:
node: true
rules:
internal-rules/only-ascii: [error, { allowEmoji: true }]
node/no-unpublished-require: off
node/no-unpublished-import: off
node/no-sync: off
import/no-namespace: off
import/no-extraneous-dependencies: [error, { devDependencies: true }]
import/no-nodejs-modules: off
import/no-commonjs: off
no-console: off
- files: 'resources/eslint-internal-rules/**'
env:
node: true
rules:
import/no-commonjs: off
- files: '**/*.jsx'
parserOptions:
sourceType: module
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: npm ci --ignore-scripts

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

- name: Upload generated report
uses: actions/upload-artifact@v2
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
},
"scripts": {
"preversion": ". ./resources/checkgit.sh && npm ci --ignore-scripts",
"version": "node resources/gen-version.js && npm test && git add src/version.ts",
"version": "ts-node resources/gen-version.ts && npm test && git add src/version.ts",
"fuzzonly": "mocha --full-trace src/**/__tests__/**/*-fuzz.ts",
"changelog": "node resources/gen-changelog.js",
"changelog": "ts-node resources/gen-changelog.ts",
"benchmark": "ts-node benchmark/benchmark.ts",
"test": "npm run lint && npm run check && npm run testonly:cover && npm run prettier:check && npm run check:spelling && npm run check:integrations",
"lint": "eslint --cache --max-warnings 0 .",
Expand All @@ -45,8 +45,9 @@
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.ts",
"start": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus start ./website",
"build:website": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus build --out-dir=\"$(pwd)/websiteDist\" ./website",
"build:npm": "node resources/build-npm.js",
"build:deno": "node resources/build-deno.js",
"build:npm": "ts-node resources/build-npm.ts",
"build:deno": "ts-node resources/build-deno.ts",
"diff:npm": "ts-node resources/diff-npm-package.ts",
"gitpublish:npm": "bash ./resources/gitpublish.sh npm npmDist",
"gitpublish:deno": "bash ./resources/gitpublish.sh deno denoDist"
},
Expand All @@ -58,6 +59,7 @@
"@types/chai": "4.3.1",
"@types/mocha": "9.1.1",
"@types/node": "17.0.31",
"@types/prettier": "2.6.0",
"@typescript-eslint/eslint-plugin": "5.21.0",
"@typescript-eslint/parser": "5.21.0",
"c8": "7.11.2",
Expand Down
54 changes: 0 additions & 54 deletions resources/add-extension-to-import-paths.js

This file was deleted.

68 changes: 68 additions & 0 deletions resources/add-extension-to-import-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as assert from 'node:assert';
import * as util from 'node:util';

import * as ts from 'typescript';

/**
* Adds extension to all paths imported inside MJS files
*
* Transforms:
*
* ```
* import { foo } from './bar';
* export { foo } from './bar';
* ```
*
* to:
*
* ```
* import { foo } from './bar.mjs';
* export { foo } from './bar.mjs';
* ```
*
*/
export function addExtensionToImportPaths(config: { extension: string }) {
const { extension } = config;
return (context: ts.TransformationContext) => {
const { factory } = context;

return visitSourceFile;

function visitSourceFile(sourceFile: ts.SourceFile) {
return ts.visitNode(sourceFile, visitNode);
}

function visitNode(node: ts.Node): ts.Node {
const source: string | undefined = (node as any).moduleSpecifier?.text;
if (source?.startsWith('./') || source?.startsWith('../')) {
if (ts.isImportDeclaration(node)) {
return factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
ts.createStringLiteral(source + extension),
node.assertClause,
);
}
if (ts.isExportDeclaration(node)) {
return factory.updateExportDeclaration(
node,
node.decorators,
node.modifiers,
node.isTypeOnly,
node.exportClause,
ts.createStringLiteral(source + extension),
node.assertClause,
);
}

assert(
false,
'Unexpected node with moduleSpecifier: ' + util.inspect(node),
);
}
return ts.visitEachChild(node, visitNode, context);
}
};
}
18 changes: 6 additions & 12 deletions resources/build-deno.js → resources/build-deno.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';
import * as fs from 'node:fs';
import * as path from 'node:path';

const fs = require('fs');
const path = require('path');
import * as ts from 'typescript';

const ts = require('typescript');

const inlineInvariant = require('./inline-invariant.js');
const addExtensionToImportPaths = require('./add-extension-to-import-paths.js');
const {
writeGeneratedFile,
readdirRecursive,
showDirStats,
} = require('./utils.js');
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
import { inlineInvariant } from './inline-invariant';
import { readdirRecursive, showDirStats, writeGeneratedFile } from './utils';

if (require.main === module) {
fs.rmSync('./denoDist', { recursive: true, force: true });
Expand Down
22 changes: 9 additions & 13 deletions resources/build-npm.js → resources/build-npm.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';
import * as assert from 'node:assert';
import * as fs from 'node:fs';
import * as path from 'node:path';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
import * as ts from 'typescript';

const ts = require('typescript');

const inlineInvariant = require('./inline-invariant.js');
const addExtensionToImportPaths = require('./add-extension-to-import-paths.js');
const { writeGeneratedFile, showDirStats } = require('./utils.js');
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
import { inlineInvariant } from './inline-invariant';
import { readPackageJSON, showDirStats, writeGeneratedFile } from './utils';

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

function buildPackageJSON() {
const packageJSON = JSON.parse(
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
);
const packageJSON = readPackageJSON();

delete packageJSON.private;
delete packageJSON.scripts;
Expand All @@ -113,7 +109,7 @@ function buildPackageJSON() {

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

Expand Down
20 changes: 10 additions & 10 deletions resources/diff-npm-package.js → resources/diff-npm-package.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
import * as assert from 'node:assert';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

const os = require('os');
const fs = require('fs');
const path = require('path');

const { exec } = require('./utils.js');
import { exec, execOutput } from './utils';

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

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

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

function generateReport(diffString) {
function generateReport(diffString: string): string {
return `
<!DOCTYPE html>
<html lang="en-us">
Expand Down Expand Up @@ -77,14 +76,15 @@ function generateReport(diffString) {
</html>
`;
}
function prepareNPMPackage(revision) {
function prepareNPMPackage(revision: string): string {
if (revision === LOCAL) {
exec('npm --quiet run build:npm', { cwd: localRepoDir });
return path.join(localRepoDir, 'npmDist');
}

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

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

0 comments on commit 83a2e84

Please sign in to comment.