@clonap4d/traceai 0.1.0-beta.1
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 +5 -0
- package/bin/traceai.js +8 -0
- package/lib/install.js +139 -0
- package/package.json +22 -0
- package/scripts/postinstall.js +15 -0
package/README.md
ADDED
package/bin/traceai.js
ADDED
package/lib/install.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const fsp = require("fs/promises");
|
|
3
|
+
const http = require("http");
|
|
4
|
+
const https = require("https");
|
|
5
|
+
const os = require("os");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const { pipeline } = require("stream/promises");
|
|
8
|
+
|
|
9
|
+
const pkg = require("../package.json");
|
|
10
|
+
|
|
11
|
+
const DEFAULT_BASE_URL = "https://github.com/MIK-HEAL/TraceAI/releases/download";
|
|
12
|
+
|
|
13
|
+
function currentVersion() {
|
|
14
|
+
return process.env.TRACEAI_VERSION || pkg.version;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizedPlatform(platform) {
|
|
18
|
+
switch (platform) {
|
|
19
|
+
case "win32":
|
|
20
|
+
return "windows";
|
|
21
|
+
case "darwin":
|
|
22
|
+
return "darwin";
|
|
23
|
+
default:
|
|
24
|
+
return "linux";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function normalizedArch(arch) {
|
|
29
|
+
switch (arch) {
|
|
30
|
+
case "x64":
|
|
31
|
+
return "amd64";
|
|
32
|
+
case "arm64":
|
|
33
|
+
return "arm64";
|
|
34
|
+
default:
|
|
35
|
+
return arch;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function binaryName() {
|
|
40
|
+
return process.platform === "win32" ? "traceai.exe" : "traceai";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function assetName() {
|
|
44
|
+
return `traceai-${normalizedPlatform(process.platform)}-${normalizedArch(process.arch)}${process.platform === "win32" ? ".exe" : ""}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function releaseBaseUrl() {
|
|
48
|
+
return process.env.TRACEAI_RELEASE_BASE_URL || DEFAULT_BASE_URL;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function binaryUrl() {
|
|
52
|
+
if (process.env.TRACEAI_BINARY_URL) {
|
|
53
|
+
return process.env.TRACEAI_BINARY_URL;
|
|
54
|
+
}
|
|
55
|
+
return `${releaseBaseUrl()}/v${currentVersion()}/${assetName()}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function cacheDir() {
|
|
59
|
+
return process.env.TRACEAI_CACHE_DIR || path.join(os.homedir(), ".traceai");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function binaryPath() {
|
|
63
|
+
return path.join(cacheDir(), "bin", currentVersion(), binaryName());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function ensureBinary() {
|
|
67
|
+
const target = binaryPath();
|
|
68
|
+
try {
|
|
69
|
+
await fsp.access(target, fs.constants.X_OK);
|
|
70
|
+
return target;
|
|
71
|
+
} catch (_) {
|
|
72
|
+
// download below
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
await fsp.mkdir(path.dirname(target), { recursive: true });
|
|
76
|
+
const tmp = `${target}.download`;
|
|
77
|
+
await download(binaryUrl(), tmp);
|
|
78
|
+
if (process.platform !== "win32") {
|
|
79
|
+
await fsp.chmod(tmp, 0o755);
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
await fsp.rename(tmp, target);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
await fsp.rm(target, { force: true });
|
|
85
|
+
await fsp.rename(tmp, target);
|
|
86
|
+
}
|
|
87
|
+
return target;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function download(url, destination) {
|
|
91
|
+
await new Promise((resolve, reject) => {
|
|
92
|
+
const client = url.startsWith("https:") ? https : http;
|
|
93
|
+
const request = client.get(url, (response) => {
|
|
94
|
+
const status = response.statusCode || 0;
|
|
95
|
+
if ([301, 302, 303, 307, 308].includes(status) && response.headers.location) {
|
|
96
|
+
response.resume();
|
|
97
|
+
const next = new URL(response.headers.location, url).toString();
|
|
98
|
+
download(next, destination).then(resolve, reject);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (status !== 200) {
|
|
102
|
+
response.resume();
|
|
103
|
+
reject(new Error(`download failed with status ${status} from ${url}`));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const file = fs.createWriteStream(destination);
|
|
107
|
+
pipeline(response, file).then(resolve, reject);
|
|
108
|
+
});
|
|
109
|
+
request.on("error", reject);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function runTraceAI(argv) {
|
|
114
|
+
const binary = await ensureBinary();
|
|
115
|
+
const child = require("child_process").spawn(binary, argv, {
|
|
116
|
+
stdio: "inherit",
|
|
117
|
+
});
|
|
118
|
+
child.on("exit", (code, signal) => {
|
|
119
|
+
if (signal) {
|
|
120
|
+
process.kill(process.pid, signal);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
process.exit(code ?? 0);
|
|
124
|
+
});
|
|
125
|
+
child.on("error", (err) => {
|
|
126
|
+
console.error(err.message);
|
|
127
|
+
process.exit(1);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = {
|
|
132
|
+
assetName,
|
|
133
|
+
binaryName,
|
|
134
|
+
binaryPath,
|
|
135
|
+
binaryUrl,
|
|
136
|
+
currentVersion,
|
|
137
|
+
ensureBinary,
|
|
138
|
+
runTraceAI,
|
|
139
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clonap4d/traceai",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "TraceAI CLI distribution wrapper",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"bin": {
|
|
7
|
+
"traceai": "bin/traceai.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"lib",
|
|
12
|
+
"scripts",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"postinstall": "node scripts/postinstall.js"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { ensureBinary } = require("../lib/install");
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
try {
|
|
7
|
+
await ensureBinary();
|
|
8
|
+
} catch (err) {
|
|
9
|
+
console.warn(`[traceai] postinstall skipped binary download: ${err.message}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
main().catch((err) => {
|
|
14
|
+
console.warn(`[traceai] postinstall warning: ${err.message}`);
|
|
15
|
+
});
|