forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepublish.js
executable file
·117 lines (106 loc) · 2.56 KB
/
prepublish.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
#!/usr/bin/env node
//@ts-check
var p = require("child_process");
var path = require("path");
var fs = require("fs");
var assert = require("assert");
var root = path.join(__dirname, "..");
try {
// npmignore is broken let's do it
let file = path.join(root, process.platform, "bsc");
console.log(`try removing ${file}`);
fs.unlinkSync(file);
} catch (e) {}
var output = p.spawnSync(`npm pack --dry-run`, {
cwd: root,
encoding: "utf8",
shell: true,
// stdio: [0, 1, 2]
});
/**
*
* @param {string} output
*/
function parseOutput(output) {
var publishedFiles = output
.slice(
output.indexOf("Tarball Contents"),
output.lastIndexOf("npm notice === Tarball Details ===")
)
.split("\n")
.map((x) => x.split(" ").filter(Boolean))
.filter((x) => x[0] === "npm" && x[1] === "notice")
.map((x) => x[x.length - 1]);
return publishedFiles;
}
var packedFiles = parseOutput(output.stderr);
/**
*
* @param {string[]} files
*/
function stat(files) {
/**
* @type Map<string,Set<string> >
*/
var map = new Map();
for (let f of files) {
let p = path.parse(f);
if (map.get(p.dir) === undefined) {
map.set(p.dir, new Set([p.base]));
} else {
map.get(p.dir).add(p.base);
}
}
// for (let [k, v] of map) {
// console.log(`dir: ${k} \t=>\t ${v.size}`);
// }
return map;
}
/**
*
* @param {Map<string, Set<string> >} map
*/
function check(map) {
// we don't need check artifacts any more
// since it's already snapshot
// make sure the remote and current are on the same commit
var currentBranch = (p.execSync(`git branch --show-current`) + "").trim();
var command = `git fetch origin && git diff ${currentBranch} origin/${currentBranch}`;
console.log(`Running '${command}'`);
var remoteDiffs = p.execSync(command) + "";
if (remoteDiffs) {
console.warn(`diffs with remote`);
console.log(remoteDiffs);
} else {
console.log(`remote diffs looking good`);
}
}
var map = stat(packedFiles);
/**
*
* @param {Map<string,Set<string> >} map
*/
function toJSON(map) {
var o = {};
var keys = [...map.keys()].sort();
for (let k of keys) {
// @ts-ignore
o[k] = [...map.get(k)];
}
return o;
}
fs.writeFileSync(
path.join(__dirname, "..", "jscomp", "artifacts.json"),
JSON.stringify(toJSON(map), undefined, 2),
"utf8"
);
if (!process.argv.includes("-nocheck")) {
check(map);
}
var output = p.spawnSync(`git diff jscomp/artifacts.json`, {
cwd: root,
encoding: "utf8",
shell: true,
});
assert(output.status === 0);
console.log("The diff of artifacts", output.stdout);