-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (68 loc) · 2.2 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
const contentsPath = "./Playground.playground/Contents.swift";
const tmpPath = "./tmp.swift";
function parseArguments() {
// 前两个参数为node程序位置(/usr/local/bin/node)和当前脚本文件名(.../index.js)
let argvs = {};
argvs.actionType = process.argv.length > 2 ? process.argv[2] : '';
argvs.filePath = process.argv.length > 3 ? process.argv[3] : tmpPath;
console.log(argvs)
return argvs;
}
function save(filePath) {
copyFile(contentsPath, filePath, true);
}
function load(filePath) {
copyFile(filePath, contentsPath, false);
}
function copyFile(fromPath, toPath, isSave) {
fs.readFile(fromPath, (err, data) => {
if (!err) {
if (isSave && toPath == tmpPath) { // 保存时未指定文件路径
let fulltext = String(data)
let res = fulltext.match('FILEPATH = "(.*?)"');
if (res && res.length > 1) {
toPath = res[1];
console.log(`success match file path: ${toPath}`)
}
}
if (fs.existsSync(toPath) == false) {
mkdir(toPath);
}
fs.writeFile(toPath, data, (err) => {
if (err) {
console.log('error: write file', err);
} else {
console.log(`success copy file from ${fromPath} to ${toPath}`);
}
})
} else {
console.log('error: read file', err);
}
})
}
function mkdir(filePath) {
const dirCache={};
const arr=filePath.split('/');
let dir=arr[0];
for(let i=1;i<arr.length;i++){
if(!dirCache[dir]&&!fs.existsSync(dir)){
dirCache[dir]=true;
fs.mkdirSync(dir);
console.log(`mkdir: ${dir}`);
}
dir=dir+'/'+arr[i];
}
fs.writeFileSync(filePath, '');
}
function main() {
let argvs = parseArguments();
if (argvs.actionType == 'save') {
save(argvs.filePath);
} else if (argvs.actionType == 'load') {
load(argvs.filePath);
} else {
console.log(`error: action type '${argvs.actionType}' is not exist, use 'save' or 'load'`);
}
}
main();