mafit 1.0.0 → 1.0.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.
@@ -1,22 +1,51 @@
1
1
  #!/usr/bin/env node
2
2
  const fs = require("node:fs");
3
+ const os = require("node:os");
3
4
  const path = require("node:path");
5
+ const http = require("node:http");
6
+ const https = require("node:https");
4
7
  const { spawnSync } = require("node:child_process");
5
8
 
6
9
  const binaryByPlatform = {
7
- "linux-x64": "mafit-linux-x64",
8
- "darwin-x64": "mafit-darwin-x64",
9
- "darwin-arm64": "mafit-darwin-arm64",
10
- "win32-x64": "mafit-win32-x64.exe"
10
+ "linux-x64": {
11
+ "id": "linux-x64",
12
+ "platform": "linux",
13
+ "arch": "x64",
14
+ "filename": "mafit-linux-x64"
15
+ },
16
+ "darwin-x64": {
17
+ "id": "darwin-x64",
18
+ "platform": "darwin",
19
+ "arch": "x64",
20
+ "filename": "mafit-darwin-x64"
21
+ },
22
+ "darwin-arm64": {
23
+ "id": "darwin-arm64",
24
+ "platform": "darwin",
25
+ "arch": "arm64",
26
+ "filename": "mafit-darwin-arm64"
27
+ },
28
+ "win32-x64": {
29
+ "id": "win32-x64",
30
+ "platform": "win32",
31
+ "arch": "x64",
32
+ "filename": "mafit-win32-x64.exe"
33
+ }
11
34
  };
12
35
  const fallbackByPlatform = {
13
36
  "win32-arm64": "win32-x64"
14
37
  };
38
+ const version = "1.0.1";
39
+ const binaryVersion = "1.0.1-bin.0";
40
+ const defaultUrlTemplate = "https://cdn.jsdelivr.net/npm/mafit@{binaryVersion}/dist/bin/{filename}";
15
41
  const key = `${process.platform}-${process.arch}`;
16
42
  const resolvedKey = binaryByPlatform[key] ? key : (fallbackByPlatform[key] || "");
17
- const binaryName = binaryByPlatform[resolvedKey];
43
+ const target = binaryByPlatform[resolvedKey];
44
+ const binaryName = target && target.filename;
45
+ const ensureOnly = process.argv[2] === "--mafit-internal-ensure";
46
+ const cliArgs = ensureOnly ? [] : process.argv.slice(2);
18
47
 
19
- if (!binaryName) {
48
+ if (!target || !binaryName) {
20
49
  console.error(`mafit: unsupported platform ${key}`);
21
50
  console.error("supported: linux-x64, darwin-x64, darwin-arm64, win32-x64");
22
51
  process.exit(1);
@@ -26,29 +55,145 @@ if (resolvedKey && resolvedKey !== key) {
26
55
  console.error(`mafit: ${key} fallback to ${resolvedKey}`);
27
56
  }
28
57
 
29
- const binaryPath = path.join(__dirname, binaryName);
30
- if (!fs.existsSync(binaryPath)) {
31
- console.error(`mafit: missing binary for ${key}`);
32
- console.error(`expected file: ${binaryPath}`);
33
- process.exit(1);
34
- }
58
+ const getCacheRoot = () => {
59
+ if (process.env.MAFIT_BINARY_CACHE_DIR) {
60
+ return process.env.MAFIT_BINARY_CACHE_DIR;
61
+ }
62
+ if (process.platform === "win32") {
63
+ const localAppData = process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local");
64
+ return path.join(localAppData, "mafit", "bin");
65
+ }
66
+ const xdg = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
67
+ return path.join(xdg, "mafit", "bin");
68
+ };
69
+
70
+ const applyTemplate = (template) =>
71
+ template
72
+ .replaceAll("{version}", version)
73
+ .replaceAll("{binaryVersion}", binaryVersion)
74
+ .replaceAll("{id}", target.id)
75
+ .replaceAll("{platform}", target.platform)
76
+ .replaceAll("{arch}", target.arch)
77
+ .replaceAll("{filename}", target.filename);
35
78
 
36
- if (process.platform !== "win32") {
79
+ const urlTemplate = process.env.MAFIT_BINARY_URL_TEMPLATE || defaultUrlTemplate;
80
+ const downloadUrl = applyTemplate(urlTemplate);
81
+ const packagedBinaryPath = path.join(__dirname, binaryName);
82
+ const cacheBinaryPath = path.join(getCacheRoot(), version, binaryName);
83
+
84
+ const ensureExecutable = (filePath) => {
85
+ if (process.platform === "win32") {
86
+ return;
87
+ }
37
88
  try {
38
- fs.chmodSync(binaryPath, 0o755);
89
+ fs.chmodSync(filePath, 0o755);
39
90
  } catch {}
40
- }
91
+ };
41
92
 
42
- const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
93
+ const makeRequest = (urlString, redirectsLeft = 5) =>
94
+ new Promise((resolve, reject) => {
95
+ const lib = urlString.startsWith("https://") ? https : http;
96
+ const req = lib.get(
97
+ urlString,
98
+ {
99
+ headers: {
100
+ "User-Agent": `mafit/${version}`,
101
+ },
102
+ },
103
+ (res) => {
104
+ const status = res.statusCode || 0;
105
+ const location = res.headers.location;
43
106
 
44
- if (result.error) {
45
- console.error(`mafit: failed to start binary: ${binaryPath}`);
46
- console.error(result.error.message);
47
- process.exit(1);
48
- }
107
+ if (status >= 300 && status < 400 && location) {
108
+ if (redirectsLeft <= 0) {
109
+ res.resume();
110
+ reject(new Error(`too many redirects: ${urlString}`));
111
+ return;
112
+ }
113
+ const nextUrl = new URL(location, urlString).toString();
114
+ res.resume();
115
+ resolve(makeRequest(nextUrl, redirectsLeft - 1));
116
+ return;
117
+ }
49
118
 
50
- if (typeof result.status === "number") {
51
- process.exit(result.status);
52
- }
119
+ if (status !== 200) {
120
+ res.resume();
121
+ reject(new Error(`download failed with status ${status}: ${urlString}`));
122
+ return;
123
+ }
124
+
125
+ resolve(res);
126
+ }
127
+ );
128
+
129
+ req.on("error", reject);
130
+ req.setTimeout(30000, () => req.destroy(new Error("download timed out")));
131
+ });
53
132
 
54
- process.exit(1);
133
+ const downloadBinary = async (targetPath) => {
134
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
135
+ const tempPath = `${targetPath}.part-${process.pid}`;
136
+ const stream = fs.createWriteStream(tempPath, { mode: 0o755 });
137
+ const response = await makeRequest(downloadUrl);
138
+
139
+ await new Promise((resolve, reject) => {
140
+ response.pipe(stream);
141
+ response.on("error", reject);
142
+ stream.on("error", reject);
143
+ stream.on("finish", resolve);
144
+ });
145
+
146
+ fs.renameSync(tempPath, targetPath);
147
+ ensureExecutable(targetPath);
148
+ };
149
+
150
+ const resolveBinaryPath = async () => {
151
+ if (fs.existsSync(packagedBinaryPath)) {
152
+ ensureExecutable(packagedBinaryPath);
153
+ return packagedBinaryPath;
154
+ }
155
+
156
+ if (fs.existsSync(cacheBinaryPath)) {
157
+ ensureExecutable(cacheBinaryPath);
158
+ return cacheBinaryPath;
159
+ }
160
+
161
+ console.error(`mafit: downloading ${target.id} binary...`);
162
+ try {
163
+ await downloadBinary(cacheBinaryPath);
164
+ } catch (error) {
165
+ console.error(`mafit: failed to download binary for ${target.id}`);
166
+ console.error(`url: ${downloadUrl}`);
167
+ console.error(`set MAFIT_BINARY_URL_TEMPLATE to your mirror if needed`);
168
+ console.error(error && error.message ? error.message : String(error));
169
+ process.exit(1);
170
+ }
171
+ return cacheBinaryPath;
172
+ };
173
+
174
+ const run = async () => {
175
+ const binaryPath = await resolveBinaryPath();
176
+ if (ensureOnly) {
177
+ return;
178
+ }
179
+
180
+ const result = spawnSync(binaryPath, cliArgs, { stdio: "inherit" });
181
+
182
+ if (result.error) {
183
+ console.error(`mafit: failed to start binary: ${binaryPath}`);
184
+ console.error(result.error.message);
185
+ process.exit(1);
186
+ }
187
+
188
+ if (typeof result.status === "number") {
189
+ process.exit(result.status);
190
+ }
191
+
192
+ process.exit(1);
193
+ };
194
+
195
+ run().catch((error) => {
196
+ console.error("mafit: unexpected launcher error");
197
+ console.error(error && error.message ? error.message : String(error));
198
+ process.exit(1);
199
+ });
package/package.json CHANGED
@@ -1,18 +1,23 @@
1
1
  {
2
2
  "name": "mafit",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "mafit tui app",
5
+ "mafitBinaryPackage": "mafit",
6
+ "mafitBinaryVersionSuffix": "-bin.0",
5
7
  "bin": {
6
8
  "mafit": "dist/bin/mafit.cjs"
7
9
  },
8
10
  "files": [
9
- "dist/bin/**"
11
+ "dist/bin/mafit.cjs"
10
12
  ],
11
13
  "scripts": {
12
14
  "sync:rust-version": "node scripts/sync-rust-version.cjs",
13
15
  "clean": "node scripts/clean-dist.cjs",
14
16
  "build:rust": "node scripts/build-rust.cjs",
15
17
  "build:launcher": "node scripts/build-launcher.cjs",
18
+ "prepare:binaries:pkg": "node scripts/prepare-binaries-package.cjs",
19
+ "pack:binaries:pkg": "npm run prepare:binaries:pkg && npm pack --dry-run .release/mafit-binaries",
20
+ "publish:binaries:pkg": "npm run prepare:binaries:pkg && npm publish .release/mafit-binaries --access public --tag binaries",
16
21
  "stage:binary": "node scripts/stage-binary.cjs",
17
22
  "verify:binaries": "node scripts/verify-release-binaries.cjs",
18
23
  "verify:binaries:core": "node scripts/verify-release-binaries.cjs --required linux-x64,win32-x64",
@@ -20,7 +25,7 @@
20
25
  "build:target": "npm run sync:rust-version && npm run build:rust && npm run build:launcher",
21
26
  "start": "node scripts/run-bin.cjs",
22
27
  "dev": "npm run build && npm run start",
23
- "prepack": "npm run build:launcher && npm run verify:binaries:core"
28
+ "prepack": "npm run build:launcher"
24
29
  },
25
30
  "license": "MIT"
26
31
  }
Binary file
Binary file