Friday 2 June 2017

Relative paths in NodeJS

I'm working on a simple folder watching utility, and all it's supposed to just copy new files with a specified file extension to another folder.

Here's the code.

var ck = require('chokidar');
var mv = require('mv');
var pth = require('path');
var toWatch = process.argv[2];
var toMove = process.argv[3];
var log = console.log.bind(console);
if(toWatch == ('help' || '')){
  console.log(`
  The standard syntax of this command is "node wotcha folder1 folder2"
    `);
}else{
    var watcher = ck.watch(pth.join(toWatch, '*.exp'), {
      ignored: /[\/\\]\./, persistent: true
    });

    watcher.on('add', function(path){
      log(`File ${path} has been added, and will now be moved to ${toMove}`);
      var file = path.replace(toWatch, "");
      log(file);
      mv(path, pth.join(toMove, file), function(err){log(`Sorry there was an 
      error moving the file ${path}, ${err}`);});
    });
}

The issue is that when the error throws, it has the full path, and I'd like to work with relative, and if needed, then use the full path.



via Chris Rutherford

No comments:

Post a Comment