zont / copy-and-watch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

--watch not working on file rename?

Rechdan opened this issue · comments

I tried to rename a file and got these errors:

(node:20668) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
    at validateString (internal/validators.js:120:11)
    at Object.relative (path.js:437:5)
    at findTarget (...\node_modules\copy-and-watch\index.js:40:33)
    at FSWatcher.copy (...\node_modules\copy-and-watch\index.js:60:14)
    at FSWatcher.emit (events.js:315:20)
    at FSWatcher.emitWithAll (...\node_modules\chokidar\index.js:524:8)
    at FSWatcher._emit (...\node_modules\chokidar\index.js:616:8)
    at NodeFsHandler._handleFile (...\node_modules\chokidar\lib\nodefs-handler.js:400:14)
    at NodeFsHandler._addToNodeFs (...\node_modules\chokidar\lib\nodefs-handler.js:628:21)
(node:20668) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20668) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
internal/validators.js:120
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
    at validateString (internal/validators.js:120:11)
    at Object.relative (path.js:437:5)
    at findTarget (...\node_modules\copy-and-watch\index.js:40:33)
    at FSWatcher.remove (...\node_modules\copy-and-watch\index.js:70:14)
    at FSWatcher.emit (events.js:315:20)
    at ...\node_modules\chokidar\index.js:565:16
    at Map.forEach (<anonymous>)
    at Timeout._onTimeout (...\node_modules\chokidar\index.js:564:30)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) {
  code: 'ERR_INVALID_ARG_TYPE'
}
commented

Cannot reproduce.

Maybe you try to rename file in destination folder?

Maybe thats a problem with my node version?

node -v
v12.18.4

And here is the command I used:

copy-and-watch --clean ./src/public/**/*.* ./dist/public

This is not restricted for file rename, simple content updates doesn't work either on Windows. I have encountered the same error, this is most likely due to different separator between MacOS and Windows. After adding a console.log to the findTarget function where the error is thrown the following can be seen:

[build:copy] {
[build:copy]   target: 'build',
[build:copy]   parents: [ 'src/assets' ],
[build:copy]   parent: 'src/assets',     
[build:copy]   from: 'src/assets/styles'
[build:copy] }
[build:copy] {
[build:copy]   target: 'build',
[build:copy]   parents: [ 'src/assets' ],
[build:copy]   parent: 'src/assets',
[build:copy]   from: 'src/assets/styles/main.css'
[build:copy] }
[build:copy] [COPY] src/assets/styles/main.css to build\styles\main.css
[build:copy] [WATCH] src\assets\**\*
[build:copy] {
[build:copy]   target: 'build',
[build:copy]   parents: [ 'src/assets' ],
[build:copy]   parent: undefined,
[build:copy]   from: 'src\\assets\\styles\\main.css'
[build:copy] }
[build:copy] (node:8544) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
[build:copy]     at validateString (internal/validators.js:121:11)
[build:copy]     at Object.relative (path.js:437:5)
[build:copy]     at findTarget (C:\Users\attil\code\spreadmonitor\alteo\website\node_modules\copy-and-watch\index.js:41:33)
[build:copy]     at FSWatcher.copy (C:\Users\attil\code\spreadmonitor\alteo\website\node_modules\copy-and-watch\index.js:61:14)
[build:copy]     at FSWatcher.emit (events.js:314:20)
[build:copy]     at FSWatcher.emitWithAll (C:\Users\attil\code\spreadmonitor\alteo\website\node_modules\chokidar\index.js:524:8)
[build:copy]     at FSWatcher._emit (C:\Users\attil\code\spreadmonitor\alteo\website\node_modules\chokidar\index.js:616:8)
[build:copy]     at listener (C:\Users\attil\code\spreadmonitor\alteo\website\node_modules\chokidar\lib\nodefs-handler.js:370:20)
[build:copy] (Use `node --trace-warnings ...` to show where the warning was created)
[build:copy] (node:8544) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not 
handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)[build:copy] (node:8544) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.    

The first two print is from the startup, where the from parameter is not escaped, but then on update I assume the from parameter comes from the watcher library used, what escapes it Windows style so consequent checks will always fail.

A super dirty quick fix to showcase the problem:

const findTarget = from => {
  const parent = parents
    .filter(p => {
      console.log({ p, fromreplaced: from.replace(/\\/g, '/'), from })
      return from.replace(/\\/g, '/').indexOf(p) >= 0;
    })
    .sort()
    .reverse()[0];
  console.log({ target, parents, parent, from });
  return path.join(target, path.relative(parent, from));
};

Replace can be used to fix this, but proably would be better to use path.seprator from Node in path handling to made it platform agnostic.

any news about this problem? i'm working in windows and the initial copy works fine but the watch shows the error and end the execution
For the workaround if I understand I have to replace the internal code of findTarget

For the workaround if I understand I have to replace the internal code of findTarget

Yes, you can use https://github.com/ds300/patch-package to apply the fix automatically while the fix doesn't land in this repo.

same issue on linux

Currently using this with other package run-when-changed

npx run-when-changed --watch "FolderToWatch/**/*" --exec 'npx copy-and-watch ../FolderToWatch/**/* outputFolder/'