summary refs log tree commit diff
path: root/resources/app/node_modules/unused-filename/index.js
blob: cdb127ce5f510083439f77987a2fd184769e2776 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'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);
};