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

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"));
}