index.js 548 B

123456789101112131415161718192021
  1. 'use strict';
  2. const pathExists = require('path-exists');
  3. const modifyFilename = require('modify-filename');
  4. const incrementer = fp => {
  5. let i = 0;
  6. return () => modifyFilename(fp, (filename, ext) => `${filename} (${++i})${ext}`);
  7. };
  8. module.exports = fp => {
  9. const getFp = incrementer(fp);
  10. const find = newFp => pathExists(newFp).then(x => x ? find(getFp()) : newFp);
  11. return find(fp);
  12. };
  13. module.exports.sync = fp => {
  14. const getFp = incrementer(fp);
  15. const find = newFp => pathExists.sync(newFp) ? find(getFp()) : newFp;
  16. return find(fp);
  17. };