everkm-publish 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,10 @@
1
+ ## 使用方法
2
+
3
+ 请移步至[官网](https://publish.everkm.cn)。
4
+
5
+ ## 中国用户安装
6
+
7
+ ```bash
8
+ EVERKM_PUBLISH_BINARY=https://assets.daobox.cc/everkm-publish/stable/ npm add everkm-publish
9
+
10
+ ```
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ var os = require("os");
5
+ var path = require("path");
6
+
7
+ // console.log('args', process.argv.slice(2))
8
+
9
+ function pkgAndSubpathForCurrentPlatform() {
10
+ let pkg;
11
+ let binName;
12
+ let platformKey = `${process.platform} ${os.arch()}`;
13
+ switch (process.platform) {
14
+ case "win32":
15
+ binName = "everkm-publish.exe";
16
+ break;
17
+ case "darwin":
18
+ binName = "everkm-publish.bin";
19
+ break;
20
+ case "linux":
21
+ binName = "everkm-publish.bin";
22
+ break;
23
+ default:
24
+ throw new Error(`Unsupported platform: ${platformKey}`);
25
+ }
26
+ return { pkg, binName };
27
+ }
28
+
29
+ const binPath = path.join(__dirname, pkgAndSubpathForCurrentPlatform().binName);
30
+
31
+ try{
32
+ require("child_process").execFileSync(binPath, process.argv.slice(2), {
33
+ stdio: "inherit",
34
+ });
35
+ } catch(ex){
36
+ console.error('error: ', ex.toString())
37
+ process.exit(1)
38
+ }
package/install.js ADDED
@@ -0,0 +1,336 @@
1
+ "use strict";
2
+ const Downloader = require("nodejs-file-downloader");
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if ((from && typeof from === "object") || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
17
+ });
18
+ }
19
+ return to;
20
+ };
21
+
22
+ // lib/npm/node-platform.ts
23
+ var fs = require("fs");
24
+ var os = require("os");
25
+ var path = require("path");
26
+
27
+ var knownWindowsPackages = {
28
+ // "win32 arm64": "@esbuild/win32-arm64",
29
+ // "win32 ia32": "@esbuild/win32-ia32",
30
+ "win32 x64": "windows-amd64.tar.gz",
31
+ };
32
+ var knownUnixlikePackages = {
33
+ // "android arm64": "@esbuild/android-arm64",
34
+ // "darwin arm64": "@esbuild/darwin-arm64",
35
+ "darwin x64": "macOS-amd64.tar.gz",
36
+ // "freebsd arm64": "@esbuild/freebsd-arm64",
37
+ // "freebsd x64": "@esbuild/freebsd-x64",
38
+ // "linux arm": "@esbuild/linux-arm",
39
+ // "linux arm64": "@esbuild/linux-arm64",
40
+ // "linux ia32": "@esbuild/linux-ia32",
41
+ // "linux mips64el": "@esbuild/linux-mips64el",
42
+ // "linux ppc64": "@esbuild/linux-ppc64",
43
+ // "linux riscv64": "@esbuild/linux-riscv64",
44
+ // "linux s390x BE": "@esbuild/linux-s390x",
45
+ "linux x64": "linux-amd64.tar.gz",
46
+ // "linux loong64": "@esbuild/linux-loong64",
47
+ // "netbsd x64": "@esbuild/netbsd-x64",
48
+ // "openbsd x64": "@esbuild/openbsd-x64",
49
+ // "sunos x64": "@esbuild/sunos-x64",
50
+ };
51
+
52
+ // 返回可执行文件信息
53
+ function pkgAndSubpathForCurrentPlatform() {
54
+ let pkg;
55
+ let binName;
56
+ let platformKey = `${process.platform} ${os.arch()}`;
57
+ if (platformKey in knownWindowsPackages) {
58
+ pkg = knownWindowsPackages[platformKey];
59
+ binName = "everkm-publish.exe";
60
+ } else if (platformKey in knownUnixlikePackages) {
61
+ pkg = knownUnixlikePackages[platformKey];
62
+ binName = "everkm-publish.bin";
63
+ } else {
64
+ throw new Error(`Unsupported platform: ${platformKey}`);
65
+ }
66
+ return { pkg, binName };
67
+ }
68
+
69
+ // lib/npm/node-install.ts
70
+ var fs2 = require("fs");
71
+ var os2 = require("os");
72
+ var path2 = require("path");
73
+ var zlib = require("zlib");
74
+ var AdmZip = require("adm-zip");
75
+ const tar = require("tar");
76
+ var https = require("https");
77
+ var http = require("http");
78
+ var child_process = require("child_process");
79
+ var versionFromPackageJSON = require(path2.join(
80
+ __dirname,
81
+ "package.json"
82
+ )).version;
83
+ var toPath = path2.join(
84
+ __dirname,
85
+ "bin",
86
+ pkgAndSubpathForCurrentPlatform().binName
87
+ );
88
+
89
+ function validateBinaryVersion(...command) {
90
+ command.push("--version");
91
+ let stdout;
92
+ try {
93
+ stdout = child_process
94
+ .execFileSync(command.shift(), command, {
95
+ // Without this, this install script strangely crashes with the error
96
+ // "EACCES: permission denied, write" but only on Ubuntu Linux when node is
97
+ // installed from the Snap Store. This is not a problem when you download
98
+ // the official version of node. The problem appears to be that stderr
99
+ // (i.e. file descriptor 2) isn't writable?
100
+ //
101
+ // More info:
102
+ // - https://snapcraft.io/ (what the Snap Store is)
103
+ // - https://nodejs.org/dist/ (download the official version of node)
104
+ // - https://github.com/evanw/esbuild/issues/1711#issuecomment-1027554035
105
+ //
106
+ stdio: "pipe",
107
+ })
108
+ .toString()
109
+ .trim()
110
+ .split(" ")[1];
111
+ } catch (err) {
112
+ if (
113
+ os2.platform() === "darwin" &&
114
+ /_SecTrustEvaluateWithError/.test(err + "")
115
+ ) {
116
+ let os3 = "this version of macOS";
117
+ try {
118
+ os3 =
119
+ "macOS " +
120
+ child_process
121
+ .execFileSync("sw_vers", ["-productVersion"])
122
+ .toString()
123
+ .trim();
124
+ } catch {}
125
+ throw new Error(`The "everkm-publish" package cannot be installed because ${os3} is too outdated.
126
+
127
+ The "everkm-publish" binary executable can't be run.
128
+ `);
129
+ }
130
+ throw err;
131
+ }
132
+
133
+ if (stdout !== versionFromPackageJSON) {
134
+ throw new Error(
135
+ `Expected ${JSON.stringify(
136
+ versionFromPackageJSON
137
+ )} but got ${JSON.stringify(stdout)}`
138
+ );
139
+ }
140
+ }
141
+
142
+ function isYarn() {
143
+ const { npm_config_user_agent } = process.env;
144
+ if (npm_config_user_agent) {
145
+ return /\byarn\//.test(npm_config_user_agent);
146
+ }
147
+ return false;
148
+ }
149
+
150
+ function deleteDirectory(path) {
151
+ if (fs.existsSync(path)) {
152
+ fs.readdirSync(path).forEach((file, index) => {
153
+ const currentPath = path + "/" + file;
154
+ if (fs.lstatSync(currentPath).isDirectory()) {
155
+ // 递归删除子目录
156
+ deleteDirectory(currentPath);
157
+ } else {
158
+ // 删除文件
159
+ fs.unlinkSync(currentPath);
160
+ }
161
+ });
162
+ // 删除目录本身
163
+ fs.rmdirSync(path);
164
+ console.log(`Successfully removed directory ${path}`);
165
+ } else {
166
+ console.log(`Directory ${path} does not exist`);
167
+ }
168
+ }
169
+
170
+ // 对指定目录进行遍历
171
+ function traverseDirectory(directory) {
172
+ const fileList = [];
173
+ const files = fs.readdirSync(directory);
174
+ for (let i = 0; i < files.length; i++) {
175
+ const file = files[i];
176
+ const filePath = path.join(directory, file);
177
+ const stat = fs.statSync(filePath);
178
+ if (stat.isDirectory()) {
179
+ fileList.push(...traverseDirectory(filePath));
180
+ } else {
181
+ fileList.push(filePath);
182
+ }
183
+ }
184
+ return fileList;
185
+ }
186
+
187
+ async function downloadBinary(pkg, binName) {
188
+ const binaryDist = (process.env.EVERKM_PUBLISH_BINARY || '').replace(/\/$/, '')
189
+ let fileUrl = `https://github.com/everkm/publish/releases/download/everkm-publish%40v${versionFromPackageJSON}/EverkmPublish_${versionFromPackageJSON}_${pkg}`;
190
+ if (binaryDist){
191
+ // fileUrl = `https://assets.daobox.cc/everkm-publish/stable/${versionFromPackageJSON}/EverkmPublish_${versionFromPackageJSON}_${pkg}`;
192
+ fileUrl = `${binaryDist}/${versionFromPackageJSON}/EverkmPublish_${versionFromPackageJSON}_${pkg}`;
193
+ }
194
+ // const fileUrl = "http://localhost:8000/daobox/everkm-publish.tar.gz";
195
+ const filename = path.join(__dirname, "bin", pkg);
196
+ const dest = path.dirname(filename);
197
+ console.log("download everkm publish binary:", fileUrl);
198
+
199
+ const proxy =
200
+ process.env.https_proxy ||
201
+ process.env.HTTPS_PROXY ||
202
+ process.env.http_proxy ||
203
+ process.env.HTTP_PROXY;
204
+ console.log("use proxy", proxy);
205
+
206
+ const params = {
207
+ url: fileUrl,
208
+ directory: dest,
209
+ cloneFiles: false
210
+ };
211
+ if (proxy) {
212
+ params.proxy = proxy;
213
+ }
214
+ const downloader = new Downloader(params);
215
+
216
+ try {
217
+ const { filePath, downloadStatus } = await downloader.download(); //Downloader.download() resolves with some useful properties.
218
+ const stats = fs.statSync(filePath);
219
+
220
+ console.log(`File saved as ${filePath}, size: ${stats.size} bytes`);
221
+ const filename = filePath;
222
+
223
+ return new Promise((resolve, reject) => {
224
+ const extractDir = path.join(dest, "download");
225
+ // 判断目录是否存在,不存在则创建
226
+ if (!fs.existsSync(extractDir)) {
227
+ fs.mkdirSync(extractDir, { recursive: true }, function (err) {
228
+ if (err) {
229
+ return reject(err);
230
+ }
231
+ });
232
+ }
233
+
234
+ const extractFinish = () => {
235
+ // 最终BIN文件名
236
+ const binFile = path.join(dest, binName);
237
+ // console.log("final bin name", binFile);
238
+ // console.log("extract dir", extractDir);
239
+
240
+ // 迁移bin文件到可执行目录
241
+ const files = traverseDirectory(extractDir);
242
+
243
+ // 打印所有文件
244
+ // console.log("files", files);
245
+
246
+ files.some(function (file) {
247
+ const arr = file.split("/");
248
+ if (!/^everkm\-publish/.test(arr[arr.length - 1])) {
249
+ return false;
250
+ }
251
+
252
+ // 使用 fs.rename 方法将文件从源路径移动到目标路径
253
+ fs.renameSync(file, binFile, function (err) {
254
+ if (err) {
255
+ throw err;
256
+ }
257
+ });
258
+
259
+ return true;
260
+ });
261
+
262
+ deleteDirectory(extractDir);
263
+ fs2.unlinkSync(filename);
264
+
265
+ fs2.chmodSync(binFile, 493);
266
+ };
267
+
268
+ // 解压缩
269
+ if (/\.tar\.gz$/.test(filename)) {
270
+ const readStream = fs.createReadStream(filename);
271
+ const unzip = zlib.createGunzip(); // 创建 gunzip 解压缩流
272
+ const untar = tar.x({
273
+ sync: true,
274
+ C: extractDir, // alias for cwd:'some-dir', also ok
275
+ }); // 创建 tar 解压缩流
276
+
277
+ readStream
278
+ .pipe(unzip) // 使用 gunzip 解压缩流
279
+ .pipe(untar) // 使用 tar 解压缩流
280
+ .on("error", (err) => {
281
+ console.error(err);
282
+ })
283
+ .on("finish", () => {
284
+ // console.log("解压缩完成", filename, extractDir);
285
+
286
+ try {
287
+ extractFinish();
288
+ resolve();
289
+ } catch (err) {
290
+ reject(err);
291
+ }
292
+ });
293
+ } else if (/\.zip$/.test(filename)) {
294
+ const zip = new AdmZip(filename); // 指定 ZIP 文件路径
295
+ zip.extractAllTo(extractDir, true); // 解压 ZIP 文件到指定目录
296
+ try {
297
+ extractFinish();
298
+ resolve();
299
+ } catch (err) {
300
+ reject(err);
301
+ }
302
+ } else {
303
+ reject(`not support archive package: ${pkg}`);
304
+ }
305
+ });
306
+ } catch (error) {
307
+ //IMPORTANT: Handle a possible error. An error is thrown in case of network errors, or status codes of 400 and above.
308
+ //Note that if the maxAttempts is set to higher than 1, the error is thrown only if all attempts fail.
309
+ console.error("download failed", error);
310
+ }
311
+
312
+ return new Promise((resolve, reject) => {
313
+ https.get(fileUrl, (response) => {
314
+ const fileStream = fs.createWriteStream(filename);
315
+ response.pipe(fileStream);
316
+ fileStream.on("finish", () => {});
317
+ fileStream.on("error", (e) => {
318
+ reject(e);
319
+ });
320
+ });
321
+ });
322
+ }
323
+
324
+ async function checkAndPreparePackage() {
325
+ const { pkg, binName } = pkgAndSubpathForCurrentPlatform();
326
+ try {
327
+ await downloadBinary(pkg, binName);
328
+ } catch (e3) {
329
+ console.error("error", e3);
330
+ throw new Error(`Failed to install package "${pkg}"`);
331
+ }
332
+ }
333
+
334
+ checkAndPreparePackage().then(() => {
335
+ validateBinaryVersion(toPath);
336
+ });
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "everkm-publish",
3
+ "version": "0.10.2",
4
+ "description": "",
5
+ "scripts": {
6
+ "postinstall": "node install.js",
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "dayu",
11
+ "license": "BSD",
12
+ "engines": {
13
+ "node": ">=12"
14
+ },
15
+ "bin": {
16
+ "everkm-publish": "bin/everkm-publish"
17
+ },
18
+ "dependencies": {
19
+ "adm-zip": "^0.5.10",
20
+ "nodejs-file-downloader": "^4.12.1",
21
+ "tar": "^6.1.13"
22
+ }
23
+ }