Skip to content

Commit

Permalink
[infra] Use ESM (DefinitelyTyped#59750)
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko authored Apr 26, 2022
1 parent 6f77b38 commit 2b60fd6
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ghostbuster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions scripts/close-old-issues.cjs → scripts/close-old-issues.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down
15 changes: 7 additions & 8 deletions scripts/fix-tslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

/// <reference types="node" />

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);
Expand Down
11 changes: 5 additions & 6 deletions scripts/generate-tsconfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
/// <reference types="node" />

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');
Expand Down
21 changes: 10 additions & 11 deletions scripts/ghostbuster.cjs → scripts/ghostbuster.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -62,8 +61,8 @@ function getAllHeaders() {
/** @type {Record<string, hp.Header & { raw: string }>} */
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;
Expand Down
5 changes: 2 additions & 3 deletions scripts/not-needed.cjs → scripts/not-needed.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/// <reference lib="esnext"/>
// 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];
Expand Down
4 changes: 2 additions & 2 deletions scripts/remove-empty.cjs → scripts/remove-empty.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
12 changes: 5 additions & 7 deletions scripts/update-codeowners.cjs → scripts/update-codeowners.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/// <reference lib="esnext.asynciterable" />
// 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 };
Expand Down
6 changes: 3 additions & 3 deletions scripts/update-config/LintPackage.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down
4 changes: 2 additions & 2 deletions scripts/update-config/dependencies.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 2 additions & 2 deletions scripts/update-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 3 additions & 3 deletions scripts/update-config/updatePackage.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 2b60fd6

Please sign in to comment.