forked from ludejun/Deep-Viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.js
70 lines (69 loc) · 2.04 KB
/
move.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs');
const path = require('path');
const filePath = path.resolve();
fs.readdir(filePath, (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach((filename) => {
fs.stat(path.join(filePath, filename), (er, stats) => {
if (er) throw er;
if (stats.isDirectory() && filename === 'src') {
const name = filename;
readFile(path.join(filePath, filename), name);
}
});
});
});
function getdir(url) {
const arr = url.split('.');
const len = arr.length;
return arr[len - 1];
}
function readFile(readurl, name) {
fs.readdir(readurl, (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach((filename) => {
fs.stat(path.join(readurl, filename), (er, stats) => {
if (er) throw er;
if (stats.isFile() && getdir(filename) !== 'js' && getdir(filename) !== 'DS_Store' && getdir(filename) !== 'md') {
const newUrl = `${filePath}/${name}/${filename}`;
const desUrl = newUrl.replace('src', 'lib');
const desArray = desUrl.split('lib');
const basefolder = `${desArray[0]}lib`;
const folders = desArray[1].split('/');
folders.shift();
const length = folders.length;
let tempName = basefolder;
for (let i = 0; i < length; i++) {
if (folders[i].indexOf('.') === -1) {
tempName += `/${folders[i]}`;
if (!fsExistsSync(tempName)) {
fs.mkdirSync(tempName);
}
}
}
const readable = fs.createReadStream(newUrl);
const writable = fs.createWriteStream(desUrl);
readable.pipe(writable);
console.log(`${filename}复制成功`);
} else if (stats.isDirectory()) {
const dirName = filename;
readFile(path.join(readurl, filename), `${name}/${dirName}`);
}
});
});
});
}
function fsExistsSync(paths) {
try {
fs.accessSync(paths, fs.F_OK);
} catch (e) {
return false;
}
return true;
}