forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
65 lines (53 loc) · 1.76 KB
/
patch.js
File metadata and controls
65 lines (53 loc) · 1.76 KB
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
#!/usr/bin/env node
const crypto = require('crypto');
const path = require('path');
const cp = require('child_process');
const fsPromise = require('fs/promises');
const APP_ROOT = path.join(__dirname, '..');
const SRC_SOURCE = path.join(APP_ROOT, 'src');
const SRC_TARGET = path.join(APP_ROOT, 'lib/vscode/src');
let config = {};
let needUpdate = false;
async function patch(root = []) {
const files = await fsPromise.readdir(path.join(SRC_SOURCE, ...root));
for (const file of files) {
const filePath = path.join(SRC_SOURCE, ...root, file);
const fileStat = await fsPromise.stat(filePath);
if (fileStat.isFile()) {
const targetFile = path.join(SRC_TARGET, ...root, file);
const content = await fsPromise.readFile(targetFile);
const hashSum = crypto.createHash('sha256');
hashSum.update(content);
const hex = hashSum.digest('hex');
const key = `${root.join('/')}/${file}`;
console.log(hex, key);
if (config[key] !== hex) {
needUpdate = true;
cp.execSync(`code -r -w -d ${targetFile} ${filePath}`);
await updateConfig(key);
}
} else if (fileStat.isDirectory()) {
await patch([...root, file]);
}
}
}
async function getConfig() {
const config = await fsPromise.readFile(path.join(__dirname, '.patch')).catch(() => '{}');
return JSON.parse(config);
}
async function saveConfig() {
await fsPromise.writeFile(path.join(__dirname, '.patch'), JSON.stringify(config, null, 2));
}
async function updateConfig(file) {
const content = await fsPromise.readFile(path.join(SRC_TARGET, file));
const hashSum = crypto.createHash('sha256');
hashSum.update(content);
const hex = hashSum.digest('hex');
config[file] = hex;
}
const main = async () => {
config = await getConfig();
await patch();
needUpdate && (await saveConfig());
};
main();