-
Notifications
You must be signed in to change notification settings - Fork 6
/
repack.js
60 lines (52 loc) · 1.31 KB
/
repack.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
const {
existsSync,
readFileSync,
unlinkSync,
writeFileSync
} = require('node:fs');
const decompress = require('decompress');
const { execSync } = require('node:child_process');
const source = process.argv[2];
const tempDir = '__temp';
const timeFile = 'commit-time.txt';
const command = (input, options) => {
try {
return execSync(input, options);
} catch (error) {
if (error.stdout) {
throw new Error(error.stdout.toString());
}
throw error;
}
};
decompress(source, tempDir).then(files => {
const dest = process.argv[3] || source;
if (existsSync(dest)) {
unlinkSync(dest);
}
if (!existsSync(timeFile)) {
writeFileSync(timeFile, `${(Number(process.env.COMMIT_TIME) * 1000) || Date.now()}`);
}
const time = Number(readFileSync(timeFile)) || Date.now();
const date = new Date(time);
command(`find . -exec touch -t "${date.toISOString().substring(0, 16).replace(/\D/g, '')}" '{}' +`, {
cwd: tempDir,
env: {
...process.env,
TZ: 'UTC',
},
});
const filePaths = [];
files.forEach(file => {
if (file.type === 'file') {
filePaths.push(file.path);
}
});
filePaths.sort();
filePaths.forEach(filePath => {
command(`zip -rq -J -D -X -9 --compression-method deflate ../${dest} ${filePath}`, {cwd: tempDir});
});
command(`rm -rf ${tempDir}`);
}).catch(error => {
console.error(error);
});