-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 945 Bytes
/
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
'use strict';
const fs = require('fs');
const path = require('path');
const validFilename = require('valid-filename');
const rangify = require('rangify-string');
const defaults = {
fs,
destination: '',
append: '',
prepend: ''
};
module.exports = (file, range, opts) => {
if (!fs.existsSync(path.resolve(file))) {
const err = new Error(`File not found: ${file}`);
throw err;
}
range = rangify(range);
opts = Object.assign({}, defaults, opts);
for (let i = 0; i < range.length; i++) {
let dirname = opts.prepend + range[i] + opts.append;
if (validFilename(dirname)) {
try {
opts.fs.copyFileSync(path.resolve(file), path.resolve(opts.destination, `${dirname}/${file}`));
} catch (err) {
throw err;
}
} else {
throw new Error(`Invalid directory name: ${directory}`);
}
}
};