-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
141 lines (125 loc) · 5.09 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
const fs = require("fs");
const {exec} = require("child_process");
const path = require("path");
const {stdout} = require("node:process");
const run = command => new Promise(r => {
const proc = exec(command);
proc.stdout.on("data", d => process.stdout.write(d));
proc.on("exit", r);
proc.on("disconnect", r);
});
const D = __dirname;
const S = path.sep;
const DS = D + S;
const ED = `cd "${DS}" && `;
(async () => {
let printer;
const request = async () => process.argv.includes("--yes-all") ? [stdout.write("y\n")] : (await printer.readLine()).toLowerCase() === "y";
if (!fs.existsSync("node_modules")) await run(ED + "npm install");
else {
printer = require("fancy-printer");
process.stdout.write("Want to update dependencies? (y/n) ");
if (await request()) await run(ED + "npm install");
}
printer = require("fancy-printer");
if (fs.existsSync(DS + "package-lock.json")) fs.rmSync(DS + "package-lock.json");
const files = [
"api.js",
"hizzy.js"
];
files.forEach(i => {
if (i.endsWith(".js")) {
const min = require("uglify-js").minify(fs.readFileSync(DS + i, "utf8"));
if (!min.code) throw min;
fs.writeFileSync(DS + i.replace(".js", ".min.js"), min.code);
}
});
const indexSpl = fs.readFileSync(DS + "hizzy.d.ts", "utf8")
.replaceAll("\"preact", "\"react")
.replaceAll("VNode", "ReactNode")
.replaceAll("/hooks/src/index", "")
.replaceAll("/hooks", "")
.replaceAll(/\/\/[^\n]+/g, "")
.split("/* ### TYPES ### */");
fs.writeFileSync(DS + "types/index.d.ts", `/*AUTO GENERATED FILE*/
${indexSpl[0]}
declare module "hizzy" {${indexSpl[1]}}`
.replaceAll("\n", "")
.replaceAll("\r", "")
);
const apiSpl = fs.readFileSync(DS + "api.d.ts", "utf8")
.replaceAll("\"preact", "\"react")
.replaceAll("VNode", "ReactNode")
.replaceAll(/\/\/[^\n]+/g, "")
.split("/* ### TYPES ### */");
fs.writeFileSync(DS + "types/api.d.ts", `/*AUTO GENERATED FILE*/
${apiSpl[0]}
declare module "hizzy/api" {${apiSpl[1]}}`
.replaceAll("\n", "")
.replaceAll("\r", "")
);
const j = require(DS + "types/package.json");
printer.dev = printer;
global.__PRODUCT__ = "hizzy";
global.__PRODUCT_U__ = "Hizzy";
global.__VERSION__ = require(DS + "package.json").version;
j.version = __VERSION__;
fs.writeFileSync(DS + "types/package.json", JSON.stringify(j, null, 2));
require(DS + "api.js");
fs.writeFileSync(DS + "tsconfig.json", fs.readFileSync(DS + "addons/hizzy-init/projects/ts/tsconfig.json"));
const readdirRecursive = (dir, sub = 0, sep = S, exc = []) => {
const obj = [];
fs.readdirSync(dir).forEach(i => {
if (exc.includes(i)) return;
i = dir + sep + i;
if (fs.statSync(i).isDirectory()) Object.assign(obj, readdirRecursive(i, sub, sep, exc));
else obj[i.substring(sub)] = fs.readFileSync(i);
});
return obj;
};
///////////////////////////
process.stdout.write("Want to publish create-app packages? (y/n) ");
const publishCreateApps = await request();
process.stdout.write("Want to publish addons? (y/n) ");
const publishAddons = await request();
const addonsToPublish = [];
if (publishAddons) for (const f of fs.readdirSync(DS + "addons")) {
if (f === "hizzy-init") continue;
process.stdout.write("Want to publish the addon at '/addons/" + f + "'? (y/n) ");
if (await request()) addonsToPublish.push(f);
}
process.stdout.write("Want to publish the package? (y/n) ");
const publishPackage = await request();
///////////////////////////
if (publishCreateApps) {
const Zip = require("jszip");
const doFiles = async name => {
const dir = DS + "addons/hizzy-init/projects/" + name;
const files = readdirRecursive(dir, dir.length + 1, "/", ["package.json", "package-lock.json", "node_modules"]);
const zip = new Zip;
for (const file in files) zip.file(file, files[file]);
fs.writeFileSync(DS + "addons/hizzy-init/dist/" + name + ".zip", await zip.generateAsync({
type: "nodebuffer"
}));
};
await doFiles("js");
await doFiles("ts");
const json = require(DS + "addons/hizzy-init/package.json");
json.version = __VERSION__;
for (const name of [
"create-hizzy-project",
"create-hizzy-app",
"create-hizzy"
]) {
json.name = name;
json.bin = {
[name]: "index.js"
};
fs.writeFileSync(DS + "addons/hizzy-init/package.json", JSON.stringify(json, null, 2));
await run(ED + "cd ./addons/hizzy-init && npm publish --access public");
}
}
for (const f of addonsToPublish)
await run(ED + "cd ./addons/" + f + " && npm publish --access public");
if (publishPackage) await run(ED + "npm publish --access public && cd types && npm publish --access public");
})();