Skip to content

Commit 556098e

Browse files
committed
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.
1 parent 7c3f607 commit 556098e

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

scripts/build/findUpDir.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { join, resolve, dirname } = require("path");
2+
const { existsSync } = require("fs");
3+
4+
// search directories upward to avoid hard-wired paths based on the
5+
// build tree (same as src/harness/findUpDir.ts)
6+
7+
function findUpFile(name) {
8+
let dir = __dirname;
9+
while (true) {
10+
const fullPath = join(dir, name);
11+
if (existsSync(fullPath)) return fullPath;
12+
const up = resolve(dir, "..");
13+
if (up === dir) return name; // it'll fail anyway
14+
dir = up;
15+
}
16+
}
17+
exports.findUpFile = findUpFile;
18+
19+
const findUpRoot = () =>
20+
findUpRoot.cached || (findUpRoot.cached = dirname(findUpFile("Gulpfile.js")));
21+
exports.findUpRoot = findUpRoot;

scripts/build/projects.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @ts-check
22
const { exec, Debouncer } = require("./utils");
3+
const { resolve } = require("path");
4+
const { findUpRoot } = require("./findUpDir");
35

46
class ProjectQueue {
57
/**
@@ -33,7 +35,13 @@ class ProjectQueue {
3335
}
3436
}
3537

36-
const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", ...(force ? ["--force"] : []), ...projects], { hidePrompt: true }));
38+
const execTsc = (lkg, ...args) =>
39+
exec(process.execPath,
40+
[resolve(findUpRoot(), lkg ? "./lib/tsc" : "./built/local/tsc"),
41+
"-b", ...args],
42+
{ hidePrompt: true })
43+
44+
const projectBuilder = new ProjectQueue((projects, lkg, force) => execTsc(lkg, ...(force ? ["--force"] : []), ...projects));
3745

3846
/**
3947
* @param {string} project
@@ -43,14 +51,14 @@ const projectBuilder = new ProjectQueue((projects, lkg, force) => exec(process.e
4351
*/
4452
exports.buildProject = (project, { lkg, force } = {}) => projectBuilder.enqueue(project, { lkg, force });
4553

46-
const projectCleaner = new ProjectQueue((projects, lkg) => exec(process.execPath, [lkg ? "./lib/tsc" : "./built/local/tsc", "-b", "--clean", ...projects], { hidePrompt: true }));
54+
const projectCleaner = new ProjectQueue((projects, lkg) => execTsc(lkg, "--clean", ...projects));
4755

4856
/**
4957
* @param {string} project
5058
*/
5159
exports.cleanProject = (project) => projectCleaner.enqueue(project);
5260

53-
const projectWatcher = new ProjectQueue((projects) => exec(process.execPath, ["./lib/tsc", "-b", "--watch", ...projects], { hidePrompt: true }));
61+
const projectWatcher = new ProjectQueue((projects) => execTsc(true, "--watch", ...projects));
5462

5563
/**
5664
* @param {string} project

scripts/build/tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const log = require("fancy-log");
99
const cmdLineOptions = require("./options");
1010
const { CancellationToken } = require("prex");
1111
const { exec } = require("./utils");
12+
const { findUpFile } = require("./findUpDir");
1213

1314
const mochaJs = require.resolve("mocha/bin/_mocha");
1415
exports.localBaseline = "tests/baselines/local/";
@@ -73,11 +74,11 @@ async function runConsoleTests(runJs, defaultReporter, runInParallel, watchMode,
7374
/** @type {string[]} */
7475
let args = [];
7576

76-
// timeout normally isn"t necessary but Travis-CI has been timing out on compiler baselines occasionally
77+
// timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
7778
// default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
7879
if (!runInParallel) {
7980
args.push(mochaJs);
80-
args.push("-R", "scripts/failed-tests");
81+
args.push("-R", findUpFile("scripts/failed-tests.js"));
8182
args.push("-O", '"reporter=' + reporter + (keepFailed ? ",keepFailed=true" : "") + '"');
8283
if (tests) {
8384
args.push("-g", `"${tests}"`);

src/harness/findUpDir.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace findUpDir {
2+
import { join, resolve, dirname } from "path";
3+
import { existsSync } from "fs";
4+
5+
// search directories upward to avoid hard-wired paths based on the
6+
// build tree (same as scripts/build/findUpDir.js)
7+
8+
export function findUpFile(name: string) {
9+
let dir = __dirname;
10+
while (true) {
11+
const fullPath = join(dir, name);
12+
if (existsSync(fullPath)) return fullPath;
13+
const up = resolve(dir, "..");
14+
if (up === dir) return name; // it'll fail anyway
15+
dir = up;
16+
}
17+
}
18+
19+
export const findUpRoot: { (): string; cached?: string; } = () =>
20+
findUpRoot.cached ||= dirname(findUpFile("Gulpfile.js"));
21+
}

0 commit comments

Comments
 (0)