Unverified Commit decc06ee authored by Doron Behar's avatar Doron Behar Committed by GitHub
Browse files

linkNodeModulesHook: respect NODE_PATH (#493427)

parents 077e3d4d 345c0e28
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@
const fs = require("fs");
const path = require("path");

const node_modules = process.env.NODE_PATH || "node_modules"

async function asyncFilter(arr, pred) {
  const filtered = [];
  for (const elem of arr) {
@@ -16,7 +18,7 @@ async function asyncFilter(arr, pred) {
// This means every file in node_modules that is _not_ a symlink to the Nix store.
async function getUnmanagedFiles(storePrefix, files) {
  return await asyncFilter(files, async (file) => {
    const filePath = path.join("node_modules", file);
    const filePath = path.join(node_modules, file);

    // Is file a symlink
    const stat = await fs.promises.lstat(filePath);
@@ -37,14 +39,14 @@ async function main() {

  // Ensure node_modules exists
  try {
    await fs.promises.mkdir("node_modules");
    await fs.promises.mkdir(node_modules);
  } catch (err) {
    if (err.code !== "EEXIST") {
      throw err;
    }
  }

  const files = await fs.promises.readdir("node_modules");
  const files = await fs.promises.readdir(node_modules);

  // Get deny list of files that we don't manage.
  // We do manage nix store symlinks, but not other files.
@@ -58,7 +60,7 @@ async function main() {
  await Promise.all(
    sourceFiles.map(async (file) => {
      const sourcePath = path.join(sourceModules, file);
      const targetPath = path.join("node_modules", file);
      const targetPath = path.join(node_modules, file);

      // Skip file if it's not a symlink to a store path
      if (unmanaged.includes(file)) {
@@ -86,7 +88,7 @@ async function main() {
  // Clean up store symlinks not included in this generation of node_modules
  await Promise.all(
    Array.from(managed).map((file) =>
      fs.promises.unlink(path.join("node_modules", file)),
      fs.promises.unlink(path.join(node_modules, file)),
    )
  );
}