-
Notifications
You must be signed in to change notification settings - Fork 16
/
publish.js
161 lines (138 loc) · 3.72 KB
/
publish.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const exec = (cmd, opts = {}) => {
console.log("$", cmd);
return execSync(cmd, {
...opts,
env: { CI: "true", ...process.env, ...(opts.env || {}) },
});
};
const DRY_RUN = !!process.env.DRY_RUN;
var count = 0;
const examplesFolderEntries = fs.readdirSync(path.join(process.cwd(), "."), {
withFileTypes: true,
});
const packageNames = [];
for (let folder of examplesFolderEntries) {
if (!folder.isDirectory()) continue;
const absolute = path.resolve(process.cwd(), ".", folder.name);
let packageJSONText;
try {
packageJSONText = fs.readFileSync(
path.join(absolute, "package.json"),
"utf8"
);
} catch {
continue;
}
let packageJSON = JSON.parse(packageJSONText);
if (!packageJSON.name) continue;
if (!packageJSON.name.startsWith("@bun-examples")) continue;
var version = "0.0.1";
try {
const _versions = exec(`npm view ${packageJSON.name} versions --json`)
.toString()
.trim();
if (_versions.length > 0) {
const versionsArray = JSON.parse(_versions);
version = versionsArray[versionsArray.length - 1];
}
} catch (exception) {
console.error(exception);
}
var retryCount = 5;
// Never commit lockfiles
try {
fs.rmSync(path.join(absolute, "package-lock.json"));
} catch (exception) {}
try {
fs.rmSync(path.join(absolute, "yarn.lock"));
} catch (exception) {}
try {
fs.rmSync(path.join(absolute, "pnpm-lock.yaml"));
} catch (exception) {}
try {
fs.copyFileSync(
path.join(absolute, ".gitignore"),
path.join(absolute, "gitignore")
);
} catch (exception) {}
restart: while (retryCount-- > 0) {
packageJSON.version = require("semver").inc(packageJSON.version, "patch");
if ("private" in packageJSON) delete packageJSON.private;
if ("license" in packageJSON) delete packageJSON.license;
if ("main" in packageJSON && !("module" in packageJSON)) {
packageJSON.module = packageJSON.main;
delete packageJSON.main;
}
fs.writeFileSync(
path.join(absolute, "package.json"),
JSON.stringify(packageJSON, null, 2)
);
try {
exec(`npm version patch --force --no-commit-hooks --no-git-tag-version`, {
cwd: absolute,
});
packageJSON = JSON.parse(
fs.readFileSync(path.join(absolute, "package.json"), "utf8")
);
version = packageJSON.version;
} catch (e) {
if (e.code !== "E404") {
throw e;
}
}
try {
exec(
`npm publish ${
DRY_RUN ? "--dry-run" : ""
} --access public --registry https://registry.npmjs.org/`,
{ cwd: absolute }
);
packageNames.push([
packageJSON.name,
{
version: packageJSON.version,
description: packageJSON.description || "",
},
]);
count++;
break;
} catch (exception) {
continue restart;
}
}
}
if (packageNames.length > 0) {
const packageJSON = {
name: "bun-examples-all",
private: false,
version: `0.0.${Date.now()}`,
description: "All bun-examples",
examples: Object.fromEntries(packageNames),
};
const dir = path.join(process.cwd(), "./bun-examples-all");
try {
fs.rmSync(dir, {
recursive: true,
force: true,
});
} catch (exception) {}
try {
fs.mkdirSync(dir, {
recursive: true,
});
} catch (exception) {}
fs.writeFileSync(
path.join(dir, "package.json"),
JSON.stringify(packageJSON, null, 2)
);
exec(
`npm publish ${
DRY_RUN ? "--dry-run" : ""
} --access public --registry https://registry.npmjs.org/`,
{ cwd: dir }
);
}
console.log(`Published ${count} packages`);