-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (43 loc) · 1.34 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
var path = require( 'path' ),
sander = require( 'sander' ),
path = require( 'path' ),
fs = require('graceful-fs'),
symlinkOrCopy = require( 'symlink-or-copy' ).sync;
module.exports = rename;
function rename(inputdir, outputdir, options, callback) {
function processdir (dir, cb) {
fs.readdir(dir, function (err, files) {
var remaining = files.length, result = [], check;
if (err) return cb(err);
// Empty dir?
if (!remaining) { cb(null, result); }
check = function () {
if (!--remaining) { cb(null, result); }
};
function handleResult (err) {
if (err) return cb(err);
check();
}
files.forEach(function (filename) {
var filepath = dir + path.sep + filename;
var destpath = filepath.replace(inputdir, outputdir)
.replace(options.from, options.to);
fs.stat(filepath, function (err, stats) {
if (err) return cb( err );
if ( stats.isDirectory() ) {
processdir(filepath, handleResult);
} else {
sander.mkdirSync(path.dirname(destpath));
try {
symlinkOrCopy(filepath, destpath);
check();
} catch (e) {
cb(e);
}
}
});
})
});
};
processdir(inputdir, callback);
}