-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
53 lines (48 loc) · 1.37 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
43
44
45
46
47
48
49
50
51
52
53
'use strict';
require('buffer-safe');
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const fileBytes = require('file-bytes');
const pify = require('pify');
const rimrafP = pify(require('rimraf'));
const floodFile = (file, iterations) => {
if (typeof iterations === 'undefined' || iterations < 0) {
iterations = 1;
}
return Promise.all(Array(iterations).fill(
fileBytes(file)
.then((size) => {
return new Promise(resolve => {
let wstream = fs.createWriteStream(file);
wstream.write(Buffer.alloc(size));
wstream.end();
wstream.on('finish', () => {
resolve(file);
});
});
}).catch(err => {
throw err;
})
));
};
module.exports = (pattern, opts) => {
// validate arguments
if (typeof pattern !== 'string' && !Array.isArray(pattern)) {
throw new TypeError(`Expected a string or array for pattern, got ${typeof pattern}`);
}
return globby(pattern, opts).then(function (files) {
return Promise.all(files.map(function (file) {
file = path.resolve(opts.cwd || '', file);
if (opts.dryRun) {
return file;
}
return floodFile(file, opts.iterations)
.then(rimrafP(file))
.then(function () {
return file;
});
}));
});
};
module.exports.floodFile = floodFile;