thedesk/node_modules/unused-filename/index.js
2018-01-28 21:22:43 +09:00

21 lines
548 B
JavaScript

'use strict';
const pathExists = require('path-exists');
const modifyFilename = require('modify-filename');
const incrementer = fp => {
let i = 0;
return () => modifyFilename(fp, (filename, ext) => `${filename} (${++i})${ext}`);
};
module.exports = fp => {
const getFp = incrementer(fp);
const find = newFp => pathExists(newFp).then(x => x ? find(getFp()) : newFp);
return find(fp);
};
module.exports.sync = fp => {
const getFp = incrementer(fp);
const find = newFp => pathExists.sync(newFp) ? find(getFp()) : newFp;
return find(fp);
};