nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1#!/usr/bin/env node
2const fs = require("fs");
3const path = require("path");
4
5// When installing files rewritten to the Nix store with npm
6// npm writes the symlinks relative to the build directory.
7//
8// This makes relocating node_modules tricky when refering to the store.
9// This script walks node_modules and canonicalizes symlinks.
10
11async function canonicalize(storePrefix, root) {
12 console.log(storePrefix, root)
13 const entries = await fs.promises.readdir(root);
14 const paths = entries.map((entry) => path.join(root, entry));
15
16 const stats = await Promise.all(
17 paths.map(async (path) => {
18 return {
19 path: path,
20 stat: await fs.promises.lstat(path),
21 };
22 })
23 );
24
25 const symlinks = stats.filter((stat) => stat.stat.isSymbolicLink());
26 const dirs = stats.filter((stat) => stat.stat.isDirectory());
27
28 // Canonicalize symlinks to their real path
29 await Promise.all(
30 symlinks.map(async (stat) => {
31 const target = await fs.promises.realpath(stat.path);
32 if (target.startsWith(storePrefix)) {
33 await fs.promises.unlink(stat.path);
34 await fs.promises.symlink(target, stat.path);
35 }
36 })
37 );
38
39 // Recurse into directories
40 await Promise.all(dirs.map((dir) => canonicalize(storePrefix, dir.path)));
41}
42
43async function main() {
44 const args = process.argv.slice(2);
45 const storePrefix = args[0];
46
47 if (fs.existsSync("node_modules")) {
48 await canonicalize(storePrefix, "node_modules");
49 }
50}
51
52main();