Avoid hard-wired build-tree paths

Instead, search for stuff up the directory tree, with the main
functionality being to look for `Gulpfile.js` and assume the resulting
directory is the root.

(Unfortunatley, this is implemented twice, one in `scripts` and another
in `src`.  It's not possible to use a single implementation for both
since that would require assuming a directory structure which this is
intended to avoid.)

Also, in `scripts/build/projects.js`, abstracdt common exec
functionality into a local helper, and use full paths based on the above
search instead of assuming relative paths assuming CWD being in the
project root.
This commit is contained in:
Eli Barzilay 2021-10-18 13:13:57 -04:00
parent 7c3f607032
commit 556098ed50
4 changed files with 56 additions and 5 deletions

View file

@ -0,0 +1,21 @@
const { join, resolve, dirname } = require("path");
const { existsSync } = require("fs");
// search directories upward to avoid hard-wired paths based on the
// build tree (same as src/harness/findUpDir.ts)
function findUpFile(name) {
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;
}
}
exports.findUpFile = findUpFile;
const findUpRoot = () =>
findUpRoot.cached || (findUpRoot.cached = dirname(findUpFile("Gulpfile.js")));
exports.findUpRoot = findUpRoot;

View file

@ -1,5 +1,7 @@
// @ts-check
const { exec, Debouncer } = require("./utils");
const { resolve } = require("path");
const { findUpRoot } = require("./findUpDir");
class ProjectQueue {
/**
@ -33,7 +35,13 @@ class ProjectQueue {
}
}
const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", ...(force ? ["--force"] : []), ...projects], { hidePrompt: true }));
const execTsc = (lkg, ...args) =>
exec(process.execPath,
[resolve(findUpRoot(), lkg ? "./lib/tsc" : "./built/local/tsc"),
"-b", ...args],
{ hidePrompt: true })
const projectBuilder = new ProjectQueue((projects, lkg, force) => execTsc(lkg, ...(force ? ["--force"] : []), ...projects));
/**
* @param {string} project
@ -43,14 +51,14 @@ const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.e
*/
exports.buildProject = (project, { lkg, force } = {}) => projectBuilder.enqueue(project, { lkg, force });
const projectCleaner = new ProjectQueue((projects, lkg) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", "--clean", ...projects], { hidePrompt: true }));
const projectCleaner = new ProjectQueue((projects, lkg) => execTsc(lkg, "--clean", ...projects));
/**
* @param {string} project
*/
exports.cleanProject = (project) => projectCleaner.enqueue(project);
const projectWatcher = new ProjectQueue((projects) => exec(process.execPath, ["./lib/tsc", "-b", "--watch", ...projects], { hidePrompt: true }));
const projectWatcher = new ProjectQueue((projects) => execTsc(true, "--watch", ...projects));
/**
* @param {string} project

View file

@ -9,6 +9,7 @@ const log = require("fancy-log");
const cmdLineOptions = require("./options");
const { CancellationToken } = require("prex");
const { exec } = require("./utils");
const { findUpFile } = require("./findUpDir");
const mochaJs = require.resolve("mocha/bin/_mocha");
exports.localBaseline = "tests/baselines/local/";
@ -73,11 +74,11 @@ async function runConsoleTests(runJs, defaultReporter, runInParallel, watchMode,
/** @type {string[]} */
let args = [];
// timeout normally isn"t necessary but Travis-CI has been timing out on compiler baselines occasionally
// timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
// default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
if (!runInParallel) {
args.push(mochaJs);
args.push("-R", "scripts/failed-tests");
args.push("-R", findUpFile("scripts/failed-tests.js"));
args.push("-O", '"reporter=' + reporter + (keepFailed ? ",keepFailed=true" : "") + '"');
if (tests) {
args.push("-g", `"${tests}"`);

21
src/harness/findUpDir.ts Normal file
View file

@ -0,0 +1,21 @@
namespace findUpDir {
import { join, resolve, dirname } from "path";
import { existsSync } from "fs";
// search directories upward to avoid hard-wired paths based on the
// build tree (same as scripts/build/findUpDir.js)
export function findUpFile(name: string) {
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;
}
}
export const findUpRoot: { (): string; cached?: string; } = () =>
findUpRoot.cached ||= dirname(findUpFile("Gulpfile.js"));
}