Skip to content

Commit 0dfaf92

Browse files
committed
refactor: postinstall script
1 parent 6bc54a6 commit 0dfaf92

File tree

5 files changed

+16
-280
lines changed

5 files changed

+16
-280
lines changed

scripts/postinstall.cjs

Lines changed: 6 additions & 275 deletions
Original file line numberDiff line numberDiff line change
@@ -23,286 +23,17 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
2323
));
2424

2525
// scripts/postinstall.ts
26-
var import_node_fs3 = require("fs");
27-
var import_node_process5 = __toESM(require("process"), 1);
28-
29-
// src/utils.ts
30-
var import_node_process = __toESM(require("process"), 1);
31-
var import_node_path = __toESM(require("path"), 1);
32-
var import_promises = require("fs/promises");
33-
var import_node_child_process = require("child_process");
34-
var import_consola = require("consola");
35-
var consola = (0, import_consola.createConsola)({
36-
level: 3
37-
}).withTag("reverse-proxy");
38-
function once(fn) {
39-
let called = false;
40-
return (...args) => {
41-
if (called)
42-
return;
43-
called = true;
44-
return fn(...args);
45-
};
46-
}
47-
48-
// src/caddy/index.ts
49-
var import_node_os2 = require("os");
50-
var import_node_process4 = __toESM(require("process"), 1);
51-
var import_node_fs2 = require("fs");
52-
var import_node_child_process4 = require("child_process");
53-
var import_promises2 = require("fs/promises");
54-
var import_got_cjs = require("got-cjs");
55-
var import_http_proxy_agent = require("http-proxy-agent");
56-
var import_https_proxy_agent = require("https-proxy-agent");
57-
var lockfile = __toESM(require("proper-lockfile"), 1);
58-
59-
// src/host/index.ts
60-
var import_node_process2 = __toESM(require("process"), 1);
61-
var import_node_child_process2 = require("child_process");
6226
var import_node_fs = require("fs");
63-
var import_node_util = require("util");
64-
var import_hostile = __toESM(require("hostile"), 1);
65-
var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
66-
var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
67-
68-
// src/caddy/constants.ts
69-
var import_node_path2 = require("path");
70-
var import_node_os = __toESM(require("os"), 1);
71-
var import_node_process3 = __toESM(require("process"), 1);
72-
var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
73-
import_dotenv_flow.default.config({
74-
silent: true
75-
});
76-
function resolvePath(path2) {
77-
if (path2.startsWith("~"))
78-
return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
79-
return path2;
80-
}
81-
var TEMP_DIR = resolvePath(import_node_process3.default.env.UHRP_CADDY_PATH || import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp");
82-
var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
83-
var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
84-
var caddyLockFilePath = (0, import_node_path2.join)(TEMP_DIR, "caddylock");
85-
var supportList = [
86-
{
87-
platform: "dragonfly",
88-
arch: "amd64"
89-
},
90-
{
91-
platform: "freebsd",
92-
arch: "amd64"
93-
},
94-
{
95-
platform: "freebsd",
96-
arch: "arm-6"
97-
},
98-
{
99-
platform: "freebsd",
100-
arch: "arm-7"
101-
},
102-
{
103-
platform: "freebsd",
104-
arch: "arm64"
105-
},
106-
{
107-
platform: "linux",
108-
arch: "amd64"
109-
},
110-
{
111-
platform: "linux",
112-
arch: "arm-5"
113-
},
114-
{
115-
platform: "linux",
116-
arch: "arm-6"
117-
},
118-
{
119-
platform: "linux",
120-
arch: "arm-7"
121-
},
122-
{
123-
platform: "linux",
124-
arch: "arm64"
125-
},
126-
{
127-
platform: "linux",
128-
arch: "mips"
129-
},
130-
{
131-
platform: "linux",
132-
arch: "mips64"
133-
},
134-
{
135-
platform: "linux",
136-
arch: "mips64le"
137-
},
138-
{
139-
platform: "linux",
140-
arch: "mipsle"
141-
},
142-
{
143-
platform: "linux",
144-
arch: "ppc64"
145-
},
146-
{
147-
platform: "linux",
148-
arch: "ppc64le"
149-
},
150-
{
151-
platform: "linux",
152-
arch: "s390x"
153-
},
154-
{
155-
platform: "darwin",
156-
arch: "amd64"
157-
},
158-
{
159-
platform: "darwin",
160-
arch: "arm64"
161-
},
162-
{
163-
platform: "openbsd",
164-
arch: "amd64"
165-
},
166-
{
167-
platform: "openbsd",
168-
arch: "arm-6"
169-
},
170-
{
171-
platform: "openbsd",
172-
arch: "arm-7"
173-
},
174-
{
175-
platform: "openbsd",
176-
arch: "arm64"
177-
},
178-
{
179-
platform: "windows",
180-
arch: "amd64"
181-
},
182-
{
183-
platform: "windows",
184-
arch: "arm-6"
185-
},
186-
{
187-
platform: "windows",
188-
arch: "arm-7"
189-
},
190-
{
191-
platform: "windows",
192-
arch: "arm64"
193-
}
194-
];
195-
196-
// src/caddy/utils.ts
197-
var import_node_net = require("net");
198-
var import_node_child_process3 = require("child_process");
199-
var import_ora = __toESM(require("ora"), 1);
200-
var spinner;
201-
function getNumber(number) {
202-
if (number <= 0)
203-
return "0%";
204-
else if (number < 100)
205-
return `${number.toFixed(2)}%`;
206-
else
207-
return "100%";
208-
}
209-
function logProgress(percent) {
210-
if (!spinner) {
211-
consola.log("");
212-
spinner = (0, import_ora.default)({
213-
text: "Start Download Caddy: 0%",
214-
indent: 2
215-
});
216-
spinner.start();
217-
} else {
218-
spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
219-
}
220-
}
221-
function logProgressOver() {
222-
if (!spinner) {
223-
spinner = (0, import_ora.default)({
224-
text: "Start Download Caddy: 100%",
225-
indent: 2
226-
});
227-
spinner.start();
228-
} else {
229-
spinner.succeed();
230-
consola.log("");
231-
}
232-
}
233-
234-
// src/caddy/index.ts
235-
async function download() {
236-
if (await testCaddy())
237-
return caddyPath;
238-
return new Promise((resolve, reject) => {
239-
if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
240-
(0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
241-
(0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
242-
}
243-
const file = (0, import_node_fs2.createWriteStream)(caddyPath);
244-
let _platform = (0, import_node_os2.platform)().toString();
245-
_platform === "win32" && (_platform = "windows");
246-
let _arch = import_node_process4.default.arch.toString();
247-
_arch === "x64" && (_arch = "amd64");
248-
const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
249-
if (!support)
250-
return reject(new Error("not support"));
251-
const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
252-
(0, import_node_fs2.existsSync)(caddyPath) && (0, import_node_fs2.unlinkSync)(caddyPath);
253-
const httpProxy = import_node_process4.default.env.HTTP_PROXY || import_node_process4.default.env.http_proxy;
254-
const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
255-
const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
256-
const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
257-
const chmodCaddyOnce = once(() => {
258-
(0, import_node_fs2.chmodSync)(caddyPath, 511);
259-
});
260-
import_got_cjs.got.stream(dowmloadLink, {
261-
agent: {
262-
http: httpAgent,
263-
https: httpsAgent
264-
}
265-
}).on("downloadProgress", (progress) => {
266-
logProgress(progress.percent);
267-
chmodCaddyOnce();
268-
}).pipe(file).on("finish", () => {
269-
logProgressOver();
270-
if (import_node_process4.default.platform === "win32")
271-
return resolve(caddyPath);
272-
(0, import_node_fs2.chmodSync)(caddyPath, 511);
273-
resolve(caddyPath);
274-
}).on("error", (err) => {
275-
reject(err);
276-
});
277-
});
278-
}
279-
function testCaddy() {
280-
return new Promise((resolve, reject) => {
281-
if (!(0, import_node_fs2.existsSync)(caddyPath))
282-
return resolve(false);
283-
(0, import_node_fs2.chmodSync)(caddyPath, 511);
284-
const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
285-
child.on("close", () => {
286-
return resolve(false);
287-
});
288-
child.on("error", (err) => {
289-
return reject(err);
290-
});
291-
child.stdout.on("data", (_data) => {
292-
return resolve(true);
293-
});
294-
});
295-
}
296-
297-
// scripts/postinstall.ts
27+
var import_node_process = __toESM(require("process"), 1);
28+
var import_unplugin_https_reverse_proxy = require("unplugin-https-reverse-proxy");
29829
async function run() {
29930
try {
300-
if (import_node_process5.default.env.UHRP_AUTO_INSTALL_CADDY) {
301-
(0, import_node_fs3.existsSync)(caddyPath) && (0, import_node_fs3.unlinkSync)(caddyPath);
302-
await download();
31+
if (import_node_process.default.env.UHRP_AUTO_INSTALL_CADDY) {
32+
(0, import_node_fs.existsSync)(import_unplugin_https_reverse_proxy.caddyPath) && (0, import_node_fs.unlinkSync)(import_unplugin_https_reverse_proxy.caddyPath);
33+
await (0, import_unplugin_https_reverse_proxy.download)();
30334
}
30435
} catch (e) {
305-
consola.error(e.code);
36+
import_unplugin_https_reverse_proxy.consola.error(e.code);
30637
}
30738
}
30839
run();

scripts/postinstall.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { existsSync, unlinkSync } from 'node:fs'
22
import process from 'node:process'
3-
import { consola } from '../src/utils'
4-
import { download } from '../src/caddy'
5-
import { caddyPath } from '../src/caddy/constants'
3+
import { caddyPath, consola, download } from 'unplugin-https-reverse-proxy'
64

75
async function run() {
86
try {

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,7 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
193193
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
194194

195195
export default unplugin
196+
197+
export { consola }
198+
export { download } from './caddy'
199+
export { caddyPath } from './caddy/constants'

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"esModuleInterop": true,
88
"strict": true,
99
"strictNullChecks": true,
10-
"resolveJsonModule": true
10+
"resolveJsonModule": true,
11+
"paths": {
12+
"unplugin-https-reverse-proxy": ["./src"]
13+
}
1114
},
1215
"exclude": ["node_modules", "examples/vuecli4"]
1316
}

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export default [
2222
outDir: 'scripts',
2323
format: ['cjs'],
2424
onSuccess: 'npm run build:fix',
25-
external: ['proper-lockfile'],
25+
external: ['proper-lockfile', 'unplugin-https-reverse-proxy'],
2626
},
2727
] satisfies Options[]

0 commit comments

Comments
 (0)