2020-09-17 03:39:25 -04:00
|
|
|
import fs from "fs";
|
|
|
|
import { dirname, resolve } from "path";
|
|
|
|
|
2020-09-21 01:14:06 -04:00
|
|
|
import { getDependencies } from "../local";
|
2020-09-22 22:54:48 -04:00
|
|
|
import { colorify } from "../log";
|
|
|
|
import { dirs, getDirname, getPackagePath, packages } from "../path";
|
2020-09-23 00:23:02 -04:00
|
|
|
import { mkdir } from "../utils";
|
2020-09-17 03:39:25 -04:00
|
|
|
|
|
|
|
function link(existing: string, path: string): void {
|
|
|
|
try {
|
|
|
|
const current = fs.readlinkSync(path);
|
|
|
|
|
|
|
|
// Alerady linked
|
|
|
|
if (current === existing) { return; }
|
|
|
|
|
|
|
|
fs.unlinkSync(path);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== "ENOENT") { throw error; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link
|
|
|
|
const dir = dirname(path);
|
2020-09-23 00:23:02 -04:00
|
|
|
mkdir(dir);
|
2021-03-30 19:59:28 -04:00
|
|
|
fs.symlinkSync(existing, path, "junction");
|
2020-09-17 03:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
(async function() {
|
2020-09-22 22:54:48 -04:00
|
|
|
console.log(colorify.bold(`Linking ${ packages.length } package node_modules rat nests...`));
|
|
|
|
|
2020-09-17 03:39:25 -04:00
|
|
|
const nodeModulesBase = resolve(dirs.root, ".package_node_modules");
|
|
|
|
|
2022-05-14 01:21:44 -04:00
|
|
|
// Make a symlink in the ROOT/node_modules to each package in this repo
|
2020-09-17 03:39:25 -04:00
|
|
|
packages.forEach((name) => {
|
|
|
|
|
|
|
|
// e.g. /node_modules/@ethersproject/abi => /packages/abi
|
|
|
|
link(getPackagePath(name), resolve(dirs.root, "node_modules", name));
|
|
|
|
|
|
|
|
// e.g. /packages/abi/node_modules => /.package_node_modules/abi/
|
|
|
|
const nodeModules = resolve(nodeModulesBase, getDirname(name));
|
2020-09-23 00:23:02 -04:00
|
|
|
mkdir(nodeModules);
|
2020-09-17 03:39:25 -04:00
|
|
|
link(nodeModules, resolve(getPackagePath(name), "node_modules"));
|
|
|
|
});
|
|
|
|
|
|
|
|
packages.forEach((name) => {
|
|
|
|
const nodeModules = resolve(nodeModulesBase, getDirname(name));
|
|
|
|
|
|
|
|
const deps = getDependencies(name);
|
|
|
|
Object.keys(deps).forEach((name) => {
|
|
|
|
link(resolve(dirs.root, "node_modules", name), resolve(nodeModules, name));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-21 01:14:06 -04:00
|
|
|
})().catch((error) => {
|
|
|
|
console.log(`Error running ${ process.argv[0] }: ${ error.message }`);
|
|
|
|
process.exit(1);
|
|
|
|
});;
|