From 2b60fd68ab1ad545290dbb85d77f7d91ab347454 Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Tue, 26 Apr 2022 12:29:04 -0700 Subject: [PATCH] [infra] Use ESM (#59750) --- .github/workflows/ghostbuster.yml | 2 +- .gitignore | 5 ----- package.json | 6 +++--- ...ose-old-issues.cjs => close-old-issues.js} | 4 ++-- scripts/fix-tslint.ts | 15 +++++++------ scripts/generate-tsconfigs.ts | 11 +++++----- scripts/{ghostbuster.cjs => ghostbuster.js} | 21 +++++++++---------- scripts/{not-needed.cjs => not-needed.js} | 5 ++--- scripts/{remove-empty.cjs => remove-empty.js} | 4 ++-- ...te-codeowners.cjs => update-codeowners.js} | 12 +++++------ scripts/update-config/LintPackage.ts | 6 +++--- scripts/update-config/dependencies.ts | 4 ++-- scripts/update-config/index.ts | 4 ++-- scripts/update-config/updatePackage.ts | 6 +++--- 14 files changed, 47 insertions(+), 58 deletions(-) rename scripts/{close-old-issues.cjs => close-old-issues.js} (95%) rename scripts/{ghostbuster.cjs => ghostbuster.js} (90%) rename scripts/{not-needed.cjs => not-needed.js} (92%) rename scripts/{remove-empty.cjs => remove-empty.js} (81%) rename scripts/{update-codeowners.cjs => update-codeowners.js} (86%) diff --git a/.github/workflows/ghostbuster.yml b/.github/workflows/ghostbuster.yml index 314371b47e7713..29cac312968f45 100644 --- a/.github/workflows/ghostbuster.yml +++ b/.github/workflows/ghostbuster.yml @@ -24,7 +24,7 @@ jobs: - uses: actions/setup-node@v2 - run: npm install --no-package-lock - - run: node ./scripts/ghostbuster.cjs + - run: node ./scripts/ghostbuster.js env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN_API_READ }} diff --git a/.gitignore b/.gitignore index 70366f18b4746a..64e29c0efef40f 100644 --- a/.gitignore +++ b/.gitignore @@ -33,11 +33,6 @@ _infrastructure/tests/build *.js.map !*.js/ -!scripts/not-needed.cjs -!scripts/close-old-issues.cjs -!scripts/ghostbuster.cjs -!scripts/remove-empty.cjs -!scripts/update-codeowners.cjs # npm node_modules diff --git a/package.json b/package.json index 580d04540f189e..8e7361aecadb48 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,10 @@ }, "scripts": { "compile-scripts": "tsc -p scripts", - "not-needed": "node scripts/not-needed.cjs", - "update-codeowners": "node scripts/update-codeowners.cjs", + "not-needed": "node scripts/not-needed.js", + "update-codeowners": "node scripts/update-codeowners.js", "test-all": "node node_modules/@definitelytyped/dtslint-runner/dist/index.js --path .", - "clean": "node scripts/remove-empty.cjs", + "clean": "node scripts/remove-empty.js", "test": "dtslint types", "lint": "dtslint types", "prettier": "prettier" diff --git a/scripts/close-old-issues.cjs b/scripts/close-old-issues.js similarity index 95% rename from scripts/close-old-issues.cjs rename to scripts/close-old-issues.js index 8e1959c6fdab5a..7f7c1357f04088 100644 --- a/scripts/close-old-issues.cjs +++ b/scripts/close-old-issues.js @@ -1,5 +1,5 @@ -const { Octokit } = require('@octokit/rest'); -const { paginateRest } = require('@octokit/plugin-paginate-rest'); +import { Octokit } from '@octokit/rest'; +import { paginateRest } from '@octokit/plugin-paginate-rest'; const CustomOctokit = Octokit.plugin(paginateRest); const octokit = new CustomOctokit({ auth: process.env.GITHUB_API_TOKEN }); diff --git a/scripts/fix-tslint.ts b/scripts/fix-tslint.ts index 7f64d9e17e5f26..0272a747c3f485 100644 --- a/scripts/fix-tslint.ts +++ b/scripts/fix-tslint.ts @@ -2,32 +2,31 @@ /// -import * as fs from "fs"; -import * as path from "path"; -import JSON = require("comment-json"); +import * as fs from 'node:fs'; +import JSON from 'comment-json'; -const home = path.join(__dirname, "..", "types"); +const home = new URL('../types/', import.meta.url); for (const dirName of fs.readdirSync(home)) { if (dirName.startsWith(".") || dirName === "node_modules" || dirName === "scripts") { continue; } - const dir = path.join(home, dirName); + const dir = new URL(`${dirName}/`, home); const stats = fs.lstatSync(dir); if (stats.isDirectory()) { fixTslint(dir); // Also do it for old versions for (const subdir of fs.readdirSync(dir)) { if (/^v\d+$/.test(subdir)) { - fixTslint(path.join(dir, subdir)); + fixTslint(new URL(`${subdir}/`, dir)); } } } } -function fixTslint(dir: string): void { - const target = path.join(dir, 'tslint.json'); +function fixTslint(dir: URL): void { + const target = new URL('tslint.json', dir); if (!fs.existsSync(target)) return; let json = JSON.parse(fs.readFileSync(target, 'utf-8')); json = fix(json); diff --git a/scripts/generate-tsconfigs.ts b/scripts/generate-tsconfigs.ts index ddbf2e3d88a715..2e229deb2b9c99 100644 --- a/scripts/generate-tsconfigs.ts +++ b/scripts/generate-tsconfigs.ts @@ -3,30 +3,29 @@ /// import * as fs from 'node:fs'; -import * as path from 'node:path'; -const home = path.join('.', 'types'); +const home = new URL('../types/', import.meta.url); for (const dirName of fs.readdirSync(home)) { if (dirName.startsWith('.') || dirName === 'node_modules' || dirName === 'scripts') { continue; } - const dir = path.join(home, dirName); + const dir = new URL(`${dirName}/`, home); const stats = fs.lstatSync(dir); if (stats.isDirectory()) { fixTsconfig(dir); // Also do it for old versions for (const subdir of fs.readdirSync(dir)) { if (/^v\d+$/.test(subdir)) { - fixTsconfig(path.join(dir, subdir)); + fixTsconfig(new URL(`${subdir}/`, dir)); } } } } -function fixTsconfig(dir: string): void { - const target = path.join(dir, 'tsconfig.json'); +function fixTsconfig(dir: URL): void { + const target = new URL('tsconfig.json', dir); let json = JSON.parse(fs.readFileSync(target, 'utf-8')); json = fix(json); fs.writeFileSync(target, JSON.stringify(json, undefined, 4), 'utf-8'); diff --git a/scripts/ghostbuster.cjs b/scripts/ghostbuster.js similarity index 90% rename from scripts/ghostbuster.cjs rename to scripts/ghostbuster.js index c6b5fa36be4759..2fbccc42a42755 100644 --- a/scripts/ghostbuster.cjs +++ b/scripts/ghostbuster.js @@ -1,10 +1,9 @@ // @ts-check -const { flatMap, mapDefined } = require('@definitelytyped/utils'); -const os = require('node:os'); -const path = require("path"); -const { writeFileSync, readFileSync, readdirSync, existsSync } = require('fs-extra'); -const hp = require("@definitelytyped/header-parser"); -const { Octokit } = require('@octokit/core'); +import { flatMap, mapDefined } from "@definitelytyped/utils"; +import * as os from "node:os"; +import { writeFileSync, readFileSync, readdirSync, existsSync } from "fs-extra"; +import hp from "@definitelytyped/header-parser"; +import { Octokit } from "@octokit/core"; /** * @param {string} indexPath @@ -44,14 +43,14 @@ function bust(indexPath, header, ghosts) { } /** - * @param {string} dir - * @param {(subpath: string) => void} fn + * @param {URL} dir + * @param {(subpath: URL) => void} fn */ function recurse(dir, fn) { const entryPoints = readdirSync(dir, { withFileTypes: true }) for (const subdir of entryPoints) { if (subdir.isDirectory() && subdir.name !== "node_modules") { - const subpath = path.join(dir, subdir.name); + const subpath = new URL(`${subdir.name}/`, dir); fn(subpath); recurse(subpath, fn); } @@ -62,8 +61,8 @@ function getAllHeaders() { /** @type {Record} */ const headers = {}; console.log("Reading headers..."); - recurse(path.join(__dirname, "../types"), subpath => { - const index = path.join(subpath, "index.d.ts"); + recurse(new URL("../types/", import.meta.url), subpath => { + const index = new URL("index.d.ts", subpath); if (existsSync(index)) { const indexContent = readFileSync(index, "utf-8"); let parsed; diff --git a/scripts/not-needed.cjs b/scripts/not-needed.js similarity index 92% rename from scripts/not-needed.cjs rename to scripts/not-needed.js index a533e5e9940924..1fff52f839ff95 100644 --- a/scripts/not-needed.cjs +++ b/scripts/not-needed.js @@ -1,8 +1,7 @@ -/// // Script to remove a package from DefinitelyTyped and add it to notNeededPackages.json -const fs = require('node:fs'); -const path = require('node:path'); +import * as fs from 'node:fs'; +import * as path from 'node:path'; const typingsPackageName = process.argv[2]; const asOfVersion = process.argv[3]; diff --git a/scripts/remove-empty.cjs b/scripts/remove-empty.js similarity index 81% rename from scripts/remove-empty.cjs rename to scripts/remove-empty.js index 7eecfb15f97cec..f5d09467de9c2f 100644 --- a/scripts/remove-empty.cjs +++ b/scripts/remove-empty.js @@ -1,5 +1,5 @@ -const fs = require('node:fs') -const path = require('node:path'); +import * as fs from 'node:fs'; +import * as path from 'node:path'; for (const d of fs.readdirSync('./types')) { const dir = path.join('./types', d); const files = fs.readdirSync(dir); diff --git a/scripts/update-codeowners.cjs b/scripts/update-codeowners.js similarity index 86% rename from scripts/update-codeowners.cjs rename to scripts/update-codeowners.js index 51d128c6efd8d8..c5a4fd39cd99e8 100644 --- a/scripts/update-codeowners.cjs +++ b/scripts/update-codeowners.js @@ -1,10 +1,8 @@ -/// -// Must reference esnext.asynciterable lib, since octokit uses AsyncIterable internally -const cp = require('node:child_process'); -const os = require('node:os'); -const { AllPackages, getDefinitelyTyped, parseDefinitions, clean } = require('@definitelytyped/definitions-parser'); -const { loggerWithErrors } = require('@definitelytyped/utils'); -const { writeFile } = require('fs-extra'); +import * as cp from 'node:child_process'; +import * as os from 'node:os'; +import { AllPackages, getDefinitelyTyped, parseDefinitions, clean } from '@definitelytyped/definitions-parser'; +import { loggerWithErrors } from '@definitelytyped/utils'; +import { writeFile } from 'fs-extra'; async function main() { const options = { definitelyTypedPath: '.', progress: false, parseInParallel: true }; diff --git a/scripts/update-config/LintPackage.ts b/scripts/update-config/LintPackage.ts index d3e6d967c09228..96b7e5204c75a7 100644 --- a/scripts/update-config/LintPackage.ts +++ b/scripts/update-config/LintPackage.ts @@ -1,8 +1,8 @@ -import * as fs from "fs"; +import * as fs from "node:fs"; import * as stringify from "json-stable-stringify"; -import * as path from "path"; +import * as path from "node:path"; import { Configuration as Config, ILinterOptions, Linter, LintResult } from "tslint"; -import * as ts from "typescript"; +import ts from "typescript"; import { isExternalDependency } from "./dependencies"; /** diff --git a/scripts/update-config/dependencies.ts b/scripts/update-config/dependencies.ts index 8a6925e3d66eb7..b1172e1e8bc718 100644 --- a/scripts/update-config/dependencies.ts +++ b/scripts/update-config/dependencies.ts @@ -1,5 +1,5 @@ -import * as path from "path"; -import * as ts from "typescript"; +import * as path from "node:path"; +import ts from "typescript"; export function isExternalDependency(file: ts.SourceFile, dirPath: string, program: ts.Program): boolean { return !startsWithDirectory(file.fileName, dirPath) || program.isSourceFileFromExternalLibrary(file); diff --git a/scripts/update-config/index.ts b/scripts/update-config/index.ts index 6f96cf5a7ef9dc..34af98b1134ab0 100644 --- a/scripts/update-config/index.ts +++ b/scripts/update-config/index.ts @@ -7,8 +7,8 @@ // configuration with your rule, then register a disabler function for your rule // (check `disableRules` function below), then run this script with your rule as argument. -import * as fs from "fs"; -import * as path from "path"; +import * as fs from "node:fs"; +import * as path from "node:path"; import { Configuration as Config } from "tslint"; import * as yargs from "yargs"; import { normalizePath } from "./dependencies"; diff --git a/scripts/update-config/updatePackage.ts b/scripts/update-config/updatePackage.ts index 01a9a97f46dead..e900a3a7fc01e4 100644 --- a/scripts/update-config/updatePackage.ts +++ b/scripts/update-config/updatePackage.ts @@ -1,6 +1,6 @@ -import * as cp from "child_process"; -import * as fs from "fs"; -import * as path from "path"; +import * as cp from "node:child_process"; +import * as fs from "node:fs"; +import * as path from "node:path"; import { Configuration as Config, ILinterOptions, IRuleFailureJson, RuleFailure } from "tslint"; import { ignoredRules } from "./ignoredRules";