-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (33 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module.exports = function (target, opts) {
var fs = require('fs'),
path = require('path'),
isValid = path.isAbsolute,
format = ' ({n})'; // so if old name is file.png, the new name should become something like 'file (1).png'
var inc = 1; // start the increment from
format = (opts && opts.format) || format;
function fileExists(url) {
try {
fs.accessSync(url, fs.R_OK);
return true;
} catch(e) {
return false;
}
}
function newName() {
var parsed = path.parse(target); // always do it on original path
parsed.name += format.replace('{n}', inc++);
delete parsed.base; // needed for name + ext to take effect. https://nodejs.org/api/path.html#path_path_format_pathobject
return path.format(parsed);
}
function getNewName(file) {
if(fileExists(file)) {
return getNewName( newName() ); // recursive
} else {
return file;
}
}
if(!target || !isValid(target)) {
throw new Error('file-saveAble requires first argument to be a valid path.');
}
return getNewName(target);
};