NullVoxPopuli / pnpm-sync-dependencies-meta-injected

Temp work-around while https://github.com/pnpm/pnpm/pull/6135 is iterated on
3 stars 3 forks source link

Folder removing is detected by watchers #5

Open Kolobok12309 opened 1 year ago

Kolobok12309 commented 1 year ago

We have library for nuxt application and pnpm install or your script trigger full project rebuild, even if none of the files have been modified. Simple hard-links worked perfect, but their can't help with creation or deleting files...

Can you give some advice for that case?

Currently i see only one way, watcher for create/delete files and synchronously create hard-link or delete file

NullVoxPopuli commented 1 year ago

for no-op updates, it's totally reasonable that this tool should maybe do a content-check on all the files and only create links for what's changed, rather than everything. I like this idea a lot, actually. thank you <3

This is the underlying tool used tho:

So it's likely that the behavior to fix is there, especially as you describe the problem you're experiencing to be in both pnpm install and the script from this tool.

Kolobok12309 commented 1 year ago

Code like this, fix my problem for the first purpose.

let watcher = new Watcher(fromPaths, {
  ignoreInitial: true,
  recursive: true,
});
// Mb useless var
let actionPromise = Promise.resolve();

const getTargetPath = (sourcePath) => {
  let path = fromPaths.find((p) => sourcePath.startsWith(p));

  if (path === undefined) {
    debug(`path not under watched root ${dirtyPath}`);
    return null;
  } else {
    return sourcePath.replace(path, paths[path]);
  }
};

watcher.on('add', async (sourcePath) => {
  const targetPath = getTargetPath(sourcePath);
  await actionPromise;
  actionPromise = new Promise(async (res) => {
    try {
      debug(`link or copy file "${targetPath}"`);
      await linkOrCopyFile(sourcePath, targetPath);
    } finally {
      res();
    }
  });
  await actionPromise;
});
watcher.on('unlink', async (sourcePath) => {
  const targetPath = getTargetPath(sourcePath);
  await actionPromise;
  actionPromise = new Promise(async (res) => {
    try {
      debug(`unlink file "${targetPath}"`);
      await remove(targetPath);
    } finally {
      res();
    }
  });
  await actionPromise;
});
... same for folders

File changes handled by hard-links(or mb if it not link we can update it), add/unlink handled by this watchers

It only for watch mode

P.S. How i can use fork of your repo in our projects without customize file structure? Monorepo not so good for this

Kolobok12309 commented 1 year ago

Or you can simply add recursive: true, to watcher and make it optional, it's although worked, but not delete existed files/folders)

https://github.com/NullVoxPopuli/pnpm-sync-dependencies-meta-injected/blob/70a56ae7f8b03497e223fb64ddbca94762e9f333/cli/src/index.js#L269

NullVoxPopuli commented 1 year ago

How i can use fork of your repo in our projects without customize file structure?

How do you mean? You can use pnpm link to test local changes to this code.

Monorepo not so good for this

Sure it is!