2020-02-14 00:52:16 +01:00
|
|
|
/*
|
2021-06-18 18:38:22 +02:00
|
|
|
Copyright 2020-2021 The Matrix.org Foundation C.I.C.
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
import path from "path";
|
|
|
|
import findNpmPrefix from "find-npm-prefix";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
import HakEnv from "./hakEnv";
|
|
|
|
import { TargetId } from "./target";
|
|
|
|
import { DependencyInfo } from "./dep";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const GENERALCOMMANDS = ["target"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
// These can only be run on specific modules
|
2023-04-24 14:19:10 +02:00
|
|
|
const MODULECOMMANDS = ["check", "fetch", "link", "build", "copy", "clean"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2021-07-07 00:52:19 +02:00
|
|
|
// Shortcuts for multiple commands at once (useful for building universal binaries
|
|
|
|
// because you can run the fetch/fetchDeps/build for each arch and then copy/link once)
|
2022-12-05 12:50:49 +01:00
|
|
|
const METACOMMANDS: Record<string, string[]> = {
|
2023-04-24 14:19:10 +02:00
|
|
|
fetchandbuild: ["check", "fetch", "build"],
|
2022-12-15 12:00:58 +01:00
|
|
|
copyandlink: ["copy", "link"],
|
2021-07-07 00:52:19 +02:00
|
|
|
};
|
|
|
|
|
2020-02-17 16:44:06 +01:00
|
|
|
// Scripts valid in a hak.json 'scripts' section
|
2023-04-24 14:19:10 +02:00
|
|
|
const HAKSCRIPTS = ["check", "fetch", "build"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-01 08:20:55 +01:00
|
|
|
async function main(): Promise<void> {
|
2020-02-14 00:52:16 +01:00
|
|
|
const prefix = await findNpmPrefix(process.cwd());
|
|
|
|
let packageJson;
|
|
|
|
try {
|
|
|
|
packageJson = require(path.join(prefix, "package.json"));
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Can't find a package.json!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
const targetIds: TargetId[] = [];
|
2021-06-18 18:38:22 +02:00
|
|
|
// Apply `--target <target>` option if specified
|
2021-07-07 00:52:19 +02:00
|
|
|
// Can be specified multiple times for the copy command to bundle
|
|
|
|
// multiple archs into a single universal output module)
|
2022-12-15 12:00:58 +01:00
|
|
|
for (;;) {
|
|
|
|
// eslint-disable-line no-constant-condition
|
|
|
|
const targetIndex = process.argv.indexOf("--target");
|
2021-07-07 00:52:19 +02:00
|
|
|
if (targetIndex === -1) break;
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
if (targetIndex + 1 >= process.argv.length) {
|
2021-06-18 18:38:22 +02:00
|
|
|
console.error("--target option specified without a target");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
// Extract target ID and remove from args
|
2021-12-14 15:32:27 +01:00
|
|
|
targetIds.push(process.argv.splice(targetIndex, 2)[1] as TargetId);
|
2021-06-18 18:38:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const hakEnvs = targetIds.map((tid) => new HakEnv(prefix, tid));
|
2021-12-13 22:15:17 +01:00
|
|
|
if (hakEnvs.length == 0) hakEnvs.push(new HakEnv(prefix, null));
|
|
|
|
for (const h of hakEnvs) {
|
|
|
|
await h.init();
|
|
|
|
}
|
2021-07-07 00:52:19 +02:00
|
|
|
const hakEnv = hakEnvs[0];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
const deps: Record<string, DependencyInfo> = {};
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
const hakDepsCfg = packageJson.hakDependencies || {};
|
|
|
|
|
|
|
|
for (const dep of Object.keys(hakDepsCfg)) {
|
2022-12-15 12:00:58 +01:00
|
|
|
const hakJsonPath = path.join(prefix, "hak", dep, "hak.json");
|
2022-05-27 00:05:59 +02:00
|
|
|
let hakJson: Record<string, any>;
|
2020-02-14 00:52:16 +01:00
|
|
|
try {
|
|
|
|
hakJson = await require(hakJsonPath);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("No hak.json found for " + dep + ".");
|
|
|
|
console.log("Expecting " + hakJsonPath);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
deps[dep] = {
|
|
|
|
name: dep,
|
|
|
|
version: hakDepsCfg[dep],
|
|
|
|
cfg: hakJson,
|
2022-12-15 12:00:58 +01:00
|
|
|
moduleHakDir: path.join(prefix, "hak", dep),
|
2020-02-15 17:52:41 +01:00
|
|
|
moduleDotHakDir: path.join(hakEnv.dotHakDir, dep),
|
2021-07-07 00:52:19 +02:00
|
|
|
moduleTargetDotHakDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId()),
|
2022-12-15 12:00:58 +01:00
|
|
|
moduleBuildDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "build"),
|
|
|
|
moduleBuildDirs: hakEnvs.map((h) => path.join(h.dotHakDir, dep, h.getTargetId(), "build")),
|
|
|
|
moduleOutDir: path.join(hakEnv.dotHakDir, "hakModules", dep),
|
|
|
|
nodeModuleBinDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "build", "node_modules", ".bin"),
|
|
|
|
depPrefix: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "opt"),
|
2020-02-14 00:52:16 +01:00
|
|
|
scripts: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const s of HAKSCRIPTS) {
|
|
|
|
if (hakJson.scripts && hakJson.scripts[s]) {
|
2022-12-15 12:00:58 +01:00
|
|
|
const scriptModule = await import(path.join(prefix, "hak", dep, hakJson.scripts[s]));
|
2021-12-14 15:32:27 +01:00
|
|
|
if (scriptModule.__esModule) {
|
|
|
|
deps[dep].scripts[s] = scriptModule.default;
|
|
|
|
} else {
|
|
|
|
deps[dep].scripts[s] = scriptModule;
|
|
|
|
}
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
let cmds: string[];
|
2020-02-14 00:52:16 +01:00
|
|
|
if (process.argv.length < 3) {
|
2023-04-24 14:19:10 +02:00
|
|
|
cmds = ["check", "fetch", "build", "copy", "link"];
|
2021-07-07 00:52:19 +02:00
|
|
|
} else if (METACOMMANDS[process.argv[2]]) {
|
|
|
|
cmds = METACOMMANDS[process.argv[2]];
|
2020-02-17 21:10:58 +01:00
|
|
|
} else {
|
|
|
|
cmds = [process.argv[2]];
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
if (hakEnvs.length > 1 && cmds.some((c) => !["copy", "link"].includes(c))) {
|
2021-07-07 00:52:19 +02:00
|
|
|
// We allow link here too for convenience because it's completely arch independent
|
|
|
|
console.error("Multiple targets only supported with the copy command");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
let modules = process.argv.slice(3);
|
|
|
|
if (modules.length === 0) modules = Object.keys(deps);
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
for (const cmd of cmds) {
|
|
|
|
if (GENERALCOMMANDS.includes(cmd)) {
|
2022-12-15 12:00:58 +01:00
|
|
|
if (cmd === "target") {
|
2020-02-17 21:10:58 +01:00
|
|
|
console.log(hakEnv.getNodeTriple());
|
|
|
|
}
|
|
|
|
return;
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
if (!MODULECOMMANDS.includes(cmd)) {
|
|
|
|
console.error("Unknown command: " + cmd);
|
|
|
|
console.log("Commands I know about:");
|
|
|
|
for (const cmd of MODULECOMMANDS) {
|
|
|
|
console.log("\t" + cmd);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const cmdFunc = (await import("./" + cmd)).default;
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
for (const mod of modules) {
|
|
|
|
const depInfo = deps[mod];
|
|
|
|
if (depInfo === undefined) {
|
2022-12-15 12:00:58 +01:00
|
|
|
console.log("Module " + mod + " not found - is it in hakDependencies " + "in your package.json?");
|
2020-02-17 21:10:58 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
console.log("hak " + cmd + ": " + mod);
|
|
|
|
await cmdFunc(hakEnv, depInfo);
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
main().catch((err) => {
|
2021-06-22 14:35:39 +02:00
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|