@adunne09/waldo 0.0.0-dev-202603111643

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.
Files changed (3) hide show
  1. package/bin/waldo +256 -0
  2. package/package.json +18 -0
  3. package/postinstall.mjs +77 -0
package/bin/waldo ADDED
@@ -0,0 +1,256 @@
1
+ #!/usr/bin/env node
2
+
3
+ import childProcess from "node:child_process";
4
+ import fs from "node:fs";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ function run(target) {
10
+ const result = childProcess.spawnSync(target, process.argv.slice(2), {
11
+ stdio: "inherit",
12
+ });
13
+
14
+ if (result.error) {
15
+ console.error(result.error.message);
16
+ process.exit(1);
17
+ }
18
+
19
+ process.exit(typeof result.status === "number" ? result.status : 0);
20
+ }
21
+
22
+ const envPath = process.env.WALDO_BIN_PATH;
23
+ if (envPath) {
24
+ run(envPath);
25
+ }
26
+
27
+ const scriptPath = fs.realpathSync(fileURLToPath(import.meta.url));
28
+ const scriptDir = path.dirname(scriptPath);
29
+ const packageDir = path.dirname(scriptDir);
30
+ const packageJsonPath = path.join(packageDir, "package.json");
31
+ const configFileName = "config.env";
32
+ const autoUpdateConfigKey = "WALDO_AUTOUPDATE";
33
+ const defaultNpmRegistry = "https://registry.npmjs.org";
34
+ const publicPackageName = "@adunne09/waldo";
35
+
36
+ function resolveUserConfigRoot() {
37
+ const home = os.homedir();
38
+
39
+ if (os.platform() === "darwin") {
40
+ return path.join(home, "Library", "Application Support");
41
+ }
42
+
43
+ if (os.platform() === "win32") {
44
+ const appData = process.env.APPDATA && path.isAbsolute(process.env.APPDATA) ? process.env.APPDATA : undefined;
45
+ return appData || path.join(home, "AppData", "Roaming");
46
+ }
47
+
48
+ const xdgConfigHome =
49
+ process.env.XDG_CONFIG_HOME && path.isAbsolute(process.env.XDG_CONFIG_HOME)
50
+ ? process.env.XDG_CONFIG_HOME
51
+ : undefined;
52
+
53
+ return xdgConfigHome || path.join(home, ".config");
54
+ }
55
+
56
+ function parseDotEnv(text) {
57
+ const map = new Map();
58
+ for (const line of text.split(/\r?\n/)) {
59
+ const trimmed = line.trim();
60
+ if (!trimmed || trimmed.startsWith("#")) continue;
61
+
62
+ const separator = trimmed.indexOf("=");
63
+ if (separator <= 0) continue;
64
+
65
+ const key = trimmed.slice(0, separator).trim();
66
+ let value = trimmed.slice(separator + 1).trim();
67
+
68
+ if (
69
+ value.length >= 2 &&
70
+ ((value.startsWith("\"") && value.endsWith("\"")) || (value.startsWith("'") && value.endsWith("'")))
71
+ ) {
72
+ value = value.slice(1, -1);
73
+ }
74
+
75
+ map.set(key, value);
76
+ }
77
+
78
+ return map;
79
+ }
80
+
81
+ function parseBoolean(value) {
82
+ if (typeof value !== "string") return undefined;
83
+ const normalized = value.trim().toLowerCase();
84
+ if (!normalized) return undefined;
85
+ if (["1", "true", "yes", "on"].includes(normalized)) return true;
86
+ if (["0", "false", "no", "off"].includes(normalized)) return false;
87
+ return undefined;
88
+ }
89
+
90
+ function resolveAutoUpdateEnabled() {
91
+ const explicit = parseBoolean(process.env[autoUpdateConfigKey]);
92
+ if (explicit !== undefined) {
93
+ return explicit;
94
+ }
95
+
96
+ try {
97
+ const configPath = path.join(resolveUserConfigRoot(), "waldo", configFileName);
98
+ if (!fs.existsSync(configPath)) {
99
+ return true;
100
+ }
101
+
102
+ const parsed = parseDotEnv(fs.readFileSync(configPath, "utf8"));
103
+ return parseBoolean(parsed.get(autoUpdateConfigKey)) ?? true;
104
+ } catch {
105
+ return true;
106
+ }
107
+ }
108
+
109
+ function resolveNpmGlobalRoot() {
110
+ const result = childProcess.spawnSync("npm", ["root", "-g"], {
111
+ encoding: "utf8",
112
+ });
113
+
114
+ if (result.error || result.status !== 0) {
115
+ return undefined;
116
+ }
117
+
118
+ const root = result.stdout.trim();
119
+ return root || undefined;
120
+ }
121
+
122
+ function isGlobalNpmInstall() {
123
+ const globalRoot = resolveNpmGlobalRoot();
124
+ if (!globalRoot) {
125
+ return false;
126
+ }
127
+
128
+ return packageDir === globalRoot || packageDir.startsWith(`${globalRoot}/`) || packageDir.startsWith(`${globalRoot}\\`);
129
+ }
130
+
131
+ function resolveCurrentVersion() {
132
+ const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
133
+ return typeof pkg.version === "string" ? pkg.version : "0.0.0";
134
+ }
135
+
136
+ function resolveNpmRegistry() {
137
+ const result = childProcess.spawnSync("npm", ["config", "get", "registry"], {
138
+ encoding: "utf8",
139
+ });
140
+
141
+ if (result.error || result.status !== 0) {
142
+ return defaultNpmRegistry;
143
+ }
144
+
145
+ const registry = result.stdout.trim() || defaultNpmRegistry;
146
+ return registry.endsWith("/") ? registry.slice(0, -1) : registry;
147
+ }
148
+
149
+ async function resolveLatestVersion() {
150
+ const registry = resolveNpmRegistry();
151
+ const response = await fetch(`${registry}/${encodeURIComponent(publicPackageName)}/latest`);
152
+ if (!response.ok) {
153
+ throw new Error(`Failed to resolve latest Waldo version (${response.status} ${response.statusText})`);
154
+ }
155
+
156
+ const data = await response.json();
157
+ if (!data || typeof data.version !== "string" || !data.version.trim()) {
158
+ throw new Error("npm registry did not return a Waldo version");
159
+ }
160
+
161
+ return data.version.trim();
162
+ }
163
+
164
+ function runGlobalUpgrade(target) {
165
+ const result = childProcess.spawnSync("npm", ["install", "-g", `${publicPackageName}@${target}`], {
166
+ stdio: "inherit",
167
+ });
168
+
169
+ if (result.error) {
170
+ throw result.error;
171
+ }
172
+
173
+ if (result.status !== 0) {
174
+ throw new Error(`npm install -g ${publicPackageName}@${target} failed`);
175
+ }
176
+ }
177
+
178
+ const platformMap = {
179
+ darwin: "darwin",
180
+ linux: "linux",
181
+ win32: "windows",
182
+ };
183
+
184
+ const archMap = {
185
+ x64: "x64",
186
+ arm64: "arm64",
187
+ };
188
+
189
+ const platform = platformMap[os.platform()] || os.platform();
190
+ const arch = archMap[os.arch()] || os.arch();
191
+ const binaryName = platform === "windows" ? "waldo.exe" : "waldo";
192
+ const packageName = `${publicPackageName}-${platform}-${arch}`;
193
+
194
+ function findBinary(startDir) {
195
+ let current = startDir;
196
+
197
+ for (;;) {
198
+ const modules = path.join(current, "node_modules");
199
+ if (fs.existsSync(modules)) {
200
+ const candidate = path.join(modules, "@adunne09", `waldo-${platform}-${arch}`, "bin", binaryName);
201
+ if (fs.existsSync(candidate)) {
202
+ return candidate;
203
+ }
204
+ }
205
+
206
+ const parent = path.dirname(current);
207
+ if (parent === current) {
208
+ return undefined;
209
+ }
210
+
211
+ current = parent;
212
+ }
213
+ }
214
+
215
+ function resolveBinary() {
216
+ const cachedBinary = path.join(scriptDir, ".waldo");
217
+ if (fs.existsSync(cachedBinary)) {
218
+ return cachedBinary;
219
+ }
220
+
221
+ return findBinary(scriptDir);
222
+ }
223
+
224
+ async function maybeAutoUpdate() {
225
+ if (!resolveAutoUpdateEnabled()) {
226
+ return;
227
+ }
228
+
229
+ if (!isGlobalNpmInstall()) {
230
+ return;
231
+ }
232
+
233
+ try {
234
+ const current = resolveCurrentVersion();
235
+ const latest = await resolveLatestVersion();
236
+ if (current === latest) {
237
+ return;
238
+ }
239
+
240
+ runGlobalUpgrade(latest);
241
+ } catch {
242
+ // Ignore update failures and continue launching the installed binary.
243
+ }
244
+ }
245
+
246
+ await maybeAutoUpdate();
247
+
248
+ const resolved = resolveBinary();
249
+ if (!resolved) {
250
+ console.error(
251
+ `Could not find the installed Waldo binary for ${packageName}. Try reinstalling ${packageName}.`,
252
+ );
253
+ process.exit(1);
254
+ }
255
+
256
+ run(resolved);
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@adunne09/waldo",
3
+ "version": "0.0.0-dev-202603111643",
4
+ "type": "module",
5
+ "bin": {
6
+ "waldo": "./bin/waldo"
7
+ },
8
+ "scripts": {
9
+ "postinstall": "bun ./postinstall.mjs || node ./postinstall.mjs"
10
+ },
11
+ "optionalDependencies": {
12
+ "@adunne09/waldo-darwin-arm64": "0.0.0-dev-202603111643",
13
+ "@adunne09/waldo-darwin-x64": "0.0.0-dev-202603111643",
14
+ "@adunne09/waldo-linux-arm64": "0.0.0-dev-202603111643",
15
+ "@adunne09/waldo-linux-x64": "0.0.0-dev-202603111643",
16
+ "@adunne09/waldo-windows-x64": "0.0.0-dev-202603111643"
17
+ }
18
+ }
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { createRequire } from "node:module";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
+ const require = createRequire(import.meta.url);
11
+ const publicPackageName = "@adunne09/waldo";
12
+
13
+ const detectPlatformAndArch = () => {
14
+ const platform =
15
+ os.platform() === "win32"
16
+ ? "windows"
17
+ : os.platform() === "darwin"
18
+ ? "darwin"
19
+ : os.platform() === "linux"
20
+ ? "linux"
21
+ : os.platform();
22
+
23
+ const arch = os.arch() === "arm64" ? "arm64" : os.arch() === "x64" ? "x64" : os.arch();
24
+
25
+ return { platform, arch };
26
+ };
27
+
28
+ const findBinary = () => {
29
+ const { platform, arch } = detectPlatformAndArch();
30
+ const packageName = `${publicPackageName}-${platform}-${arch}`;
31
+ const packageJsonPath = require.resolve(`${packageName}/package.json`);
32
+ const packageDir = path.dirname(packageJsonPath);
33
+ const binaryName = platform === "windows" ? "waldo.exe" : "waldo";
34
+ const binaryPath = path.join(packageDir, "bin", binaryName);
35
+
36
+ if (!fs.existsSync(binaryPath)) {
37
+ throw new Error(`Binary not found at ${binaryPath}`);
38
+ }
39
+
40
+ return binaryPath;
41
+ };
42
+
43
+ const main = async () => {
44
+ try {
45
+ if (os.platform() === "win32") {
46
+ return;
47
+ }
48
+
49
+ const binaryPath = findBinary();
50
+ const target = path.join(__dirname, "bin", ".waldo");
51
+ if (fs.existsSync(target)) {
52
+ fs.unlinkSync(target);
53
+ }
54
+
55
+ try {
56
+ fs.linkSync(binaryPath, target);
57
+ } catch {
58
+ fs.copyFileSync(binaryPath, target);
59
+ }
60
+
61
+ fs.chmodSync(target, 0o755);
62
+ } catch (error) {
63
+ console.error(
64
+ `Failed to setup Waldo binary: ${error instanceof Error ? error.message : String(error)}`,
65
+ );
66
+ process.exit(1);
67
+ }
68
+ };
69
+
70
+ try {
71
+ await main();
72
+ } catch (error) {
73
+ console.error(
74
+ `Postinstall script error: ${error instanceof Error ? error.message : String(error)}`,
75
+ );
76
+ process.exit(0);
77
+ }