Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert scripts/Gulpfile to checked mjs/cjs so they can run without compilation #50988

Merged
merged 13 commits into from
Oct 7, 2022
Merged
Prev Previous commit
Next Next commit
Get stuff working
  • Loading branch information
jakebailey committed Sep 29, 2022
commit 2d0e8d58fbdeade53a687cab5fbfca226ab87ee6
16 changes: 2 additions & 14 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,8 @@ build.json
*.config
scripts/debug.bat
scripts/run.bat
scripts/word2md.js
scripts/buildProtocol.js
scripts/ior.js
scripts/configurePrerelease.js
scripts/open-user-pr.js
scripts/open-cherry-pick-pr.js
scripts/processDiagnosticMessages.d.ts
scripts/processDiagnosticMessages.js
scripts/produceLKG.js
scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.js
scripts/generateLocalizedDiagnosticMessages.js
scripts/configureLanguageServiceBuild.js
scripts/*.js.map
scripts/typings/
scripts/**/*.js
scripts/**/*.js.map
coverage/
internal/
**/.DS_Store
Expand Down
39 changes: 27 additions & 12 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
"plugins": [
"@typescript-eslint", "jsdoc", "no-null", "import", "eslint-plugin-local"
],
"overrides": [
// By default, the ESLint CLI only looks at .js files. But, it will also look at
// any files which are referenced in an override config. Most users of typescript-eslint
// get this behavior by default by extending a recommended typescript-eslint config, which
// just so happens to override some core ESLint rules. We don't extend from any config, so
// explicitly reference TS files here so the CLI picks them up.
//
// ESLint in VS Code will lint any opened file (so long as it's not eslintignore'd), so
// that will work regardless of the below.
{ "files": ["*.ts", "*.mts", "*.cts", "*.mjs", "*.cjs"] }
],
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
Expand Down Expand Up @@ -151,5 +140,31 @@
"no-prototype-builtins": "error",
"no-self-assign": "error",
"no-dupe-else-if": "error"
}
},
"overrides": [
// By default, the ESLint CLI only looks at .js files. But, it will also look at
// any files which are referenced in an override config. Most users of typescript-eslint
// get this behavior by default by extending a recommended typescript-eslint config, which
// just so happens to override some core ESLint rules. We don't extend from any config, so
// explicitly reference TS files here so the CLI picks them up.
//
// ESLint in VS Code will lint any opened file (so long as it's not eslintignore'd), so
// that will work regardless of the below.
//
// The same applies to mjs files; ESLint appears to not scan those either.
{ "files": ["*.ts", "*.mts", "*.cts", "*.mjs", "*.cjs"] },
{
"files": ["*.mjs", "*.mts"],
"rules": {
// These globals don't exist outside of CJS files.
"no-restricted-globals": ["error",
{ "name": "__filename" },
{ "name": "__dirname" },
{ "name": "require" },
{ "name": "module" },
{ "name": "exports" }
]
}
}
]
}
34 changes: 18 additions & 16 deletions Gulpfile.js → Gulpfile.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// @ts-check
const path = require("path");
const fs = require("fs");
const log = require("fancy-log");
const newer = require("gulp-newer");
const sourcemaps = require("gulp-sourcemaps");
const del = require("del");
const rename = require("gulp-rename");
const concat = require("gulp-concat");
const merge2 = require("merge2");
const { src, dest, task, parallel, series, watch } = require("gulp");
const { append, transform } = require("gulp-insert");
const { prependFile } = require("./scripts/build/prepend");
const { exec, readJson, needsUpdate, getDiffTool, getDirSize, rm } = require("./scripts/build/utils");
const { runConsoleTests, refBaseline, localBaseline, refRwcBaseline, localRwcBaseline } = require("./scripts/build/tests");
const { buildProject, cleanProject, watchProject } = require("./scripts/build/projects");
const cmdLineOptions = require("./scripts/build/options");
import path from "path";
import fs from "fs";
import log from "fancy-log";
import newer from "gulp-newer";
import sourcemaps from "gulp-sourcemaps";
import del from "del";
import rename from "gulp-rename";
import concat from "gulp-concat";
import merge2 from "merge2";
import gulp from "gulp";
import { append, transform } from "gulp-insert";
import { prependFile } from "./scripts/build/prepend.mjs";
import { exec, readJson, needsUpdate, getDiffTool, getDirSize, rm } from "./scripts/build/utils.mjs";
import { runConsoleTests, refBaseline, localBaseline, refRwcBaseline, localRwcBaseline } from "./scripts/build/tests.mjs";
import { buildProject, cleanProject, watchProject } from "./scripts/build/projects.mjs";
import cmdLineOptions from "./scripts/build/options.mjs";

const { src, dest, task, parallel, series, watch } = gulp;

const copyright = "CopyrightNotice.txt";
const cleanTasks = [];
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"@octokit/rest": "latest",
"@types/chai": "latest",
"@types/fancy-log": "^2.0.0",
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
"@types/fs-extra": "^9.0.13",
"@types/glob": "latest",
"@types/gulp": "^4.0.9",
Expand All @@ -56,6 +57,7 @@
"@types/ms": "latest",
"@types/node": "latest",
"@types/source-map-support": "latest",
"@types/which": "^2.0.1",
"@types/xml2js": "^0.4.11",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
Expand Down
21 changes: 0 additions & 21 deletions scripts/build/findUpDir.js

This file was deleted.

29 changes: 29 additions & 0 deletions scripts/build/findUpDir.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join, resolve, dirname } from "path";
import { existsSync } from "fs";
import url from "url";

const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = dirname(__filename);

// search directories upward to avoid hard-wired paths based on the
// build tree (same as src/harness/findUpDir.ts)

/**
* @param {string} name
* @returns {string}
*/
export function findUpFile(name) {
sandersn marked this conversation as resolved.
Show resolved Hide resolved
let dir = __dirname;
while (true) {
const fullPath = join(dir, name);
if (existsSync(fullPath)) return fullPath;
const up = resolve(dir, "..");
if (up === dir) return name; // it'll fail anyway
dir = up;
}
}

/** @type {string | undefined} */
let findUpRootCache;

export const findUpRoot = () => findUpRootCache || (findUpRootCache = dirname(findUpFile("Gulpfile.mjs")));
28 changes: 17 additions & 11 deletions scripts/build/options.js → scripts/build/options.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// @ts-check
const minimist = require("minimist");
const os = require("os");
import minimist from "minimist";
import os from "os";

const ci = ["1", "true"].includes(process.env.CI);
const ci = ["1", "true"].includes(process.env.CI ?? "");

/** @type {CommandLineOptions} */
module.exports = minimist(process.argv.slice(2), {
const parsed = minimist(process.argv.slice(2), {
boolean: ["dirty", "light", "colors", "lkg", "soft", "fix", "failed", "keepFailed", "force", "built", "ci"],
string: ["browser", "tests", "break", "host", "reporter", "stackTraceLimit", "timeout", "shards", "shardId"],
alias: {
Expand Down Expand Up @@ -44,12 +42,19 @@ module.exports = minimist(process.argv.slice(2), {
}
});

if (module.exports.built) {
module.exports.lkg = false;
/** @type {CommandLineOptions} */
const options = /** @type {any} */ (parsed);

if (options.built) {
options.lkg = false;
}

export default options;



/**
* @typedef TypedOptions
* @typedef CommandLineOptions
* @property {boolean} dirty
* @property {boolean} light
* @property {boolean} colors
Expand All @@ -59,6 +64,7 @@ if (module.exports.built) {
* @property {boolean} fix
* @property {string} browser
* @property {string} tests
* @property {string | boolean} break
* @property {string | boolean} inspect
* @property {string} runners
* @property {string|number} workers
Expand All @@ -69,7 +75,7 @@ if (module.exports.built) {
* @property {boolean} failed
* @property {boolean} keepFailed
* @property {boolean} ci
*
* @typedef {import("minimist").ParsedArgs & TypedOptions} CommandLineOptions
* @property {string} shards
* @property {string} shardId
*/
void 0;
23 changes: 10 additions & 13 deletions scripts/build/prepend.js → scripts/build/prepend.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// @ts-check
const stream = require("stream");
const ts = require("../../lib/typescript");
const fs = require("fs");
const { base64VLQFormatEncode } = require("./sourcemaps");
import stream from "stream";
import ts from "../../lib/typescript.js";
import fs from "fs";
import { base64VLQFormatEncode } from "./sourcemaps.mjs";

/**
* @param {string | ((file: import("vinyl")) => string)} data
*/
function prepend(data) {
export function prepend(data) {
return new stream.Transform({
objectMode: true,
/**
* @param {string | Buffer | import("vinyl")} input
* @param {(error: Error, data?: any) => void} cb
* @param {(error: Error | null, data?: any) => void} cb
*/
transform(input, _, cb) {
if (typeof input === "string" || Buffer.isBuffer(input)) return cb(new Error("Only Vinyl files are supported."));
Expand All @@ -22,7 +21,7 @@ function prepend(data) {
const prependContent = typeof data === "function" ? data(input) : data;
output.contents = Buffer.concat([Buffer.from(prependContent, "utf8"), input.contents]);
if (input.sourceMap) {
if (typeof input.sourceMap === "string") input.sourceMap = /**@type {import("./sourcemaps").RawSourceMap}*/(JSON.parse(input.sourceMap));
if (typeof input.sourceMap === "string") input.sourceMap = /**@type {import("./sourcemaps.mjs").RawSourceMap}*/(JSON.parse(input.sourceMap));
const lineStarts = /**@type {*}*/(ts).computeLineStarts(prependContent);
let prependMappings = "";
for (let i = 1; i < lineStarts.length; i++) {
Expand All @@ -46,19 +45,17 @@ function prepend(data) {
return cb(null, output);
}
catch (e) {
return cb(e);
return cb(/** @type {Error} */(e));
}
}
});
}
exports.prepend = prepend;

/**
* @param {string | ((file: import("vinyl")) => string)} file
*/
function prependFile(file) {
export function prependFile(file) {
const data = typeof file === "string" ? fs.readFileSync(file, "utf8") :
vinyl => fs.readFileSync(file(vinyl), "utf8");
(/** @type {import("vinyl")} */ vinyl) => fs.readFileSync(file(vinyl), "utf8");
return prepend(data);
}
exports.prependFile = prependFile;
Loading