Comparing version 1.0.11 to 1.0.12
42
index.js
@@ -18,14 +18,17 @@ const fs = require('fs'); | ||
const opts = isObj(_opts) ? _opts : { overwrite: true, copy: false }; | ||
let msg; | ||
// are src and dest arguments valid? | ||
if (!src || !dest) { | ||
log('Invalid argument(s).'); | ||
return; | ||
msg = 'Invalid argument(s).'; | ||
log(msg); | ||
return new CustomError(1, msg); | ||
} | ||
// does src exist? | ||
msg = ['No such file or directory: ', src]; | ||
await access(src).catch(err => { | ||
log('No such file or directory: ', src); | ||
log(...msg); | ||
src = false; | ||
}); | ||
if (!src) return; | ||
if (!src) return new CustomError(2, ...msg); | ||
@@ -49,7 +52,8 @@ // src exists. | ||
}); | ||
if (done) return true; | ||
if (done) return; | ||
// dest exists. | ||
if (!opts.overwrite) { | ||
log('Destination already exists: ', dest); | ||
return; | ||
msg = ['Destination already exists: ', dest]; | ||
log(...msg); | ||
return new CustomError(3, ...msg); | ||
} | ||
@@ -62,3 +66,3 @@ const destStats = await stat(dest); | ||
await moveFile(src, dest, opts.copy); | ||
return true; | ||
return; | ||
} | ||
@@ -74,4 +78,5 @@ | ||
if (!opts.overwrite) { | ||
log('Destination already exists: ', dest); | ||
return; | ||
msg = ['Destination already exists: ', dest]; | ||
log(...msg); | ||
return new CustomError(3, ...msg); | ||
} | ||
@@ -83,4 +88,5 @@ | ||
if (!opts.overwrite) { | ||
log('Destination is an existing file: ', dest); | ||
return; | ||
msg = ['Destination is an existing file: ', dest]; | ||
log(...msg); | ||
return new CustomError(4, ...msg); | ||
} else { | ||
@@ -105,3 +111,3 @@ await unlink(dest); | ||
if (!opts.copy) await rmdir(src); | ||
return true; | ||
return; | ||
}; | ||
@@ -127,2 +133,12 @@ | ||
class CustomError { | ||
constructor(code, m1, m2) { | ||
let str = ''; | ||
str += m1 || ''; | ||
str += m2 || ''; | ||
this.code = code; | ||
this.message = str; | ||
} | ||
} | ||
module.exports = mvdir; |
{ | ||
"name": "mvdir", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Move or copy files and directories. (async, recursive, across devices, copy & delete fallback, minimalistic with 0 dependencies)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,4 +19,8 @@ [](https://travis-ci.com/m-ahmadi/mvdir) | ||
mvdir('source/file.js', 'dest/file.js').then(success => { | ||
if (success) console.log('done.'); | ||
// returns undefined if successful, or an error object: | ||
const err = await mvdir('source/file.js', 'dest/file.js'); | ||
if (err) console.log(err.message); | ||
mvdir('source/file.js', 'dest/file.js').then(err => { | ||
if (!err) console.log('done.'); | ||
}); | ||
@@ -23,0 +27,0 @@ ``` |
@@ -73,4 +73,4 @@ const { mkdirSync, writeFileSync, rmdirSync, unlinkSync } = require('fs'); | ||
function log(m, stat) { | ||
console.log(`${m}[${stat ? '32' : '31'}m${stat ? 'passed ✔' : 'failed ✘'}[0m`); | ||
function log(m, err) { | ||
console.log(`${m}[${err ? '31' : '32'}m${err ? 'failed ✘' : 'passed ✔'}[0m`); | ||
} | ||
@@ -77,0 +77,0 @@ function text() { |
8763
191
38