-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (125 loc) · 4.08 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const fileUtils = require('./utils/fileUtils');
const objectUtils = require('./utils/objectUtils');
const path = require('path');
const fs = require('fs');
console.log(`Arguements: ${JSON.stringify(process.argv, null, 2)}`);
const map = async (startFilePath, outFilePath) => {
const results = {};
await new Promise(async resolve => {
await fileUtils.traverse({
filePath: startFilePath,
onFile: async (filePath) => {
const hash = await fileUtils.sha1(filePath);
results[hash] = results[hash] || [];
results[hash].push(filePath);
},
onDone: resolve
});
});
const s = JSON.stringify(results, null, 2);
if (!outFilePath) {
console.log(s);
} else {
await fileUtils.writeFile(outFilePath, s);
}
return results;
}
const diff = async (firstFile, secondFile) => {
if (!firstFile) {
console.error("diff requires a two files to read from, received none");
process.exit(1);
}
if (!secondFile) {
console.error("diff requires a two files to read from, received one");
process.exit(1);
}
const firstFileString = await fileUtils.readFile(firstFile);
const secondFileString = await fileUtils.readFile(secondFile);
const firstArray = Object.keys(JSON.parse(firstFileString));
const secondArray = Object.keys(JSON.parse(secondFileString));
const diff = objectUtils.diffArray({
firstArray,
secondArray
});
console.log(JSON.stringify(diff, null, 2));
}
const dups = async (inFilePath) => {
if (!inFilePath) {
console.error("dups requires a file path to read from");
process.exit(1);
}
const dupsMapString = await fileUtils.readFile(inFilePath);
const dupsMap = JSON.parse(dupsMapString);
const resultDupsMap = Object.keys(dupsMap)
.filter(key => dupsMap[key].length > 2)
.reduce((result, value) => {
result[value] = dupsMap[value]
return result;
}, {});
console.log(JSON.stringify(resultDupsMap, null, 2));
}
const sync = async (sourcePath, destinationPath) => {
const readOrGenerateMap = async (filePath, mapDest, mapName) => {
const stats = fs.lstatSync(filePath);
if(stats.isFile()) {
const fileContent = await fileUtils.readFile(filePath);
return JSON.parse(fileContent);
} else if(stats.isDirectory()) {
return map(filePath, path.join(mapDest, mapName));
} else {
throw new Error(`Cannot read for reading or generating map ${filePath}`);
}
}
let destinationDirectory = destinationPath;
let destStats = fs.lstatSync(destinationPath);
if(destStats.isFile()) {
destinationDirectory = path.dirname(destinationPath);
} else if (!destStats.isDirectory()) {
throw new Error(`Cannot read destination file ${destinationPath}`);
}
const destinationObj = await readOrGenerateMap(destinationPath, destinationDirectory, "destination.map.json");
const sourceObj = await readOrGenerateMap(sourcePath, destinationDirectory, "source.map.json");
const firstArray = Object.keys(sourceObj);
const secondArray = Object.keys(destinationObj);
const diff = objectUtils.diffArray({
firstArray,
secondArray
});
for(let i = 0; i < diff.firstArrayExclusive.length; i++) {
const item = diff.firstArrayExclusive[i];
const ext = path.extname(sourceObj[item][0]);
await fileUtils.copyFile(sourceObj[item][0], path.join(destinationDirectory, `${item}${ext}`));
}
}
// arg 0: node exe path
// arg 1: path to run script
// arg 2: command
(async () => {
const command = process.argv[2];
switch(command) {
case "map":
const startFilePath = process.argv[3] || ".";
const outFilePath = process.argv[4];
await map(startFilePath, outFilePath);
break;
case "diff":
const firstFile = process.argv[3];
const secondFile = process.argv[4];
await diff(firstFile, secondFile);
break;
case "dups":
const inFilePath = process.argv[3];
await dups(inFilePath);
break;
case "sync":
const inFilePathSync = process.argv[3];
const destinationPath = process.argv[4];
await sync(inFilePathSync, destinationPath);
break;
case "usage":
console.log("TODO");
break;
default:
console.log(`Invalid command "${command}"`)
}
})();