howcode 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Igor Warzocha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # howcode
2
+
3
+ Launch the Howcode desktop app from npm.
4
+
5
+ ## Use
6
+
7
+ ```bash
8
+ npx howcode
9
+ # or
10
+ npm i -g howcode
11
+ howcode
12
+ ```
13
+
14
+ On first run, the launcher downloads the matching desktop build from GitHub Releases and caches it locally.
15
+
16
+ ## Cache location
17
+
18
+ - macOS: `~/Library/Caches/howcode`
19
+ - Linux: `$XDG_CACHE_HOME/howcode` or `~/.cache/howcode`
20
+ - Windows: `%LOCALAPPDATA%\howcode`
package/bin/howcode.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require("../lib/howcode.js").main();
package/lib/howcode.js ADDED
@@ -0,0 +1,259 @@
1
+ const fs = require("node:fs");
2
+ const fsp = require("node:fs/promises");
3
+ const os = require("node:os");
4
+ const path = require("node:path");
5
+ const { spawn, spawnSync } = require("node:child_process");
6
+ const { pipeline } = require("node:stream/promises");
7
+ const { Readable } = require("node:stream");
8
+
9
+ const packageJson = require("../package.json");
10
+
11
+ const APP_NAME = packageJson.howcode.appName;
12
+ const RELEASE_BASE_URL = process.env.HOWCODE_BASE_URL || packageJson.howcode.releaseBaseUrl;
13
+
14
+ const TARGETS = {
15
+ "darwin:arm64": {
16
+ os: "macos",
17
+ arch: "arm64",
18
+ executable: `${APP_NAME}.app/Contents/MacOS/launcher`,
19
+ },
20
+ "darwin:x64": {
21
+ os: "macos",
22
+ arch: "x64",
23
+ executable: `${APP_NAME}.app/Contents/MacOS/launcher`,
24
+ },
25
+ "linux:arm64": {
26
+ os: "linux",
27
+ arch: "arm64",
28
+ executable: `${APP_NAME}/bin/launcher`,
29
+ },
30
+ "linux:x64": {
31
+ os: "linux",
32
+ arch: "x64",
33
+ executable: `${APP_NAME}/bin/launcher`,
34
+ },
35
+ "win32:arm64": {
36
+ os: "win",
37
+ arch: "x64",
38
+ executable: `${APP_NAME}/bin/launcher.exe`,
39
+ },
40
+ "win32:x64": {
41
+ os: "win",
42
+ arch: "x64",
43
+ executable: `${APP_NAME}/bin/launcher.exe`,
44
+ },
45
+ };
46
+
47
+ function readJsonIfPresent(filePath) {
48
+ try {
49
+ return JSON.parse(fs.readFileSync(filePath, "utf8"));
50
+ } catch {
51
+ return null;
52
+ }
53
+ }
54
+
55
+ function getTarget() {
56
+ const key = `${process.platform}:${process.arch}`;
57
+ const target = TARGETS[key];
58
+ if (!target) {
59
+ throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
60
+ }
61
+ return target;
62
+ }
63
+
64
+ function getCacheRoot() {
65
+ if (process.env.HOWCODE_CACHE_DIR) {
66
+ return process.env.HOWCODE_CACHE_DIR;
67
+ }
68
+
69
+ if (process.platform === "win32") {
70
+ return path.join(
71
+ process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local"),
72
+ APP_NAME,
73
+ );
74
+ }
75
+
76
+ if (process.platform === "darwin") {
77
+ return path.join(os.homedir(), "Library", "Caches", APP_NAME);
78
+ }
79
+
80
+ return path.join(process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache"), APP_NAME);
81
+ }
82
+
83
+ function getPaths(target, releaseInfo) {
84
+ const cacheRoot = getCacheRoot();
85
+ const versionsRoot = path.join(cacheRoot, "versions");
86
+ const releaseKey = `${releaseInfo.version}-${releaseInfo.hash}`;
87
+ const installDir = path.join(versionsRoot, releaseKey);
88
+ return {
89
+ cacheRoot,
90
+ currentFile: path.join(cacheRoot, "current.json"),
91
+ installDir,
92
+ executablePath: path.join(installDir, target.executable),
93
+ };
94
+ }
95
+
96
+ async function fetchJson(url) {
97
+ const controller = new AbortController();
98
+ const timeout = setTimeout(() => controller.abort(), 5000);
99
+
100
+ try {
101
+ const response = await fetch(url, { signal: controller.signal });
102
+ if (!response.ok) {
103
+ throw new Error(`HTTP ${response.status} while fetching ${url}`);
104
+ }
105
+ return await response.json();
106
+ } finally {
107
+ clearTimeout(timeout);
108
+ }
109
+ }
110
+
111
+ async function downloadFile(url, filePath) {
112
+ const controller = new AbortController();
113
+ const timeout = setTimeout(() => controller.abort(), 60_000);
114
+
115
+ try {
116
+ const response = await fetch(url, { signal: controller.signal });
117
+ if (!response.ok || !response.body) {
118
+ throw new Error(`HTTP ${response.status} while downloading ${url}`);
119
+ }
120
+
121
+ await fsp.mkdir(path.dirname(filePath), { recursive: true });
122
+ await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(filePath));
123
+ } finally {
124
+ clearTimeout(timeout);
125
+ }
126
+ }
127
+
128
+ async function resolveLatestRelease(target) {
129
+ const updateUrl = `${RELEASE_BASE_URL}/stable-${target.os}-${target.arch}-update.json`;
130
+ const metadata = await fetchJson(updateUrl);
131
+ if (!metadata || typeof metadata.version !== "string" || typeof metadata.hash !== "string") {
132
+ throw new Error(`Invalid release metadata from ${updateUrl}`);
133
+ }
134
+
135
+ return {
136
+ version: metadata.version,
137
+ hash: metadata.hash,
138
+ assetUrl: `${RELEASE_BASE_URL}/${APP_NAME}-${target.os}-${target.arch}.tar.gz`,
139
+ };
140
+ }
141
+
142
+ async function installRelease(target, releaseInfo, paths) {
143
+ const tempRoot = path.join(paths.cacheRoot, `.tmp-${Date.now()}-${process.pid}`);
144
+ const tempInstallDir = `${paths.installDir}.partial`;
145
+ const archivePath = path.join(tempRoot, `${APP_NAME}-${target.os}-${target.arch}.tar.gz`);
146
+
147
+ console.log(`Downloading ${APP_NAME} ${releaseInfo.version} for ${target.os}-${target.arch}...`);
148
+
149
+ await fsp.rm(tempRoot, { recursive: true, force: true });
150
+ await fsp.rm(tempInstallDir, { recursive: true, force: true });
151
+ await fsp.mkdir(tempRoot, { recursive: true });
152
+ await fsp.mkdir(path.dirname(paths.installDir), { recursive: true });
153
+ await downloadFile(releaseInfo.assetUrl, archivePath);
154
+ await fsp.mkdir(tempInstallDir, { recursive: true });
155
+
156
+ const extract = spawnSync("tar", ["-xzf", archivePath, "-C", tempInstallDir], {
157
+ stdio: "inherit",
158
+ });
159
+
160
+ if (extract.status !== 0) {
161
+ throw new Error("Failed to extract downloaded archive with `tar -xzf`.");
162
+ }
163
+
164
+ if (!fs.existsSync(path.join(tempInstallDir, target.executable))) {
165
+ throw new Error(`Downloaded archive did not contain ${target.executable}.`);
166
+ }
167
+
168
+ await fsp.rm(paths.installDir, { recursive: true, force: true });
169
+ await fsp.rename(tempInstallDir, paths.installDir);
170
+ await fsp.rm(tempRoot, { recursive: true, force: true });
171
+
172
+ await fsp.writeFile(
173
+ paths.currentFile,
174
+ JSON.stringify(
175
+ {
176
+ version: releaseInfo.version,
177
+ hash: releaseInfo.hash,
178
+ installDir: paths.installDir,
179
+ executablePath: paths.executablePath,
180
+ },
181
+ null,
182
+ 2,
183
+ ),
184
+ );
185
+ }
186
+
187
+ async function pruneOldVersions(cacheRoot, keepDir) {
188
+ const versionsRoot = path.join(cacheRoot, "versions");
189
+ let entries = [];
190
+
191
+ try {
192
+ entries = await fsp.readdir(versionsRoot, { withFileTypes: true });
193
+ } catch {
194
+ return;
195
+ }
196
+
197
+ await Promise.all(
198
+ entries
199
+ .filter((entry) => entry.isDirectory())
200
+ .map((entry) => path.join(versionsRoot, entry.name))
201
+ .filter((dirPath) => dirPath !== keepDir)
202
+ .map((dirPath) => fsp.rm(dirPath, { recursive: true, force: true })),
203
+ );
204
+ }
205
+
206
+ function launch(executablePath) {
207
+ const child = spawn(executablePath, [], {
208
+ detached: true,
209
+ stdio: "ignore",
210
+ windowsHide: true,
211
+ cwd: path.dirname(executablePath),
212
+ });
213
+
214
+ child.unref();
215
+ }
216
+
217
+ async function main() {
218
+ const target = getTarget();
219
+ const cacheRoot = getCacheRoot();
220
+ await fsp.mkdir(cacheRoot, { recursive: true });
221
+
222
+ const current = readJsonIfPresent(path.join(cacheRoot, "current.json"));
223
+
224
+ let releaseInfo = null;
225
+ try {
226
+ releaseInfo = await resolveLatestRelease(target);
227
+ } catch (error) {
228
+ if (current?.executablePath && fs.existsSync(current.executablePath)) {
229
+ launch(current.executablePath);
230
+ return;
231
+ }
232
+
233
+ throw error;
234
+ }
235
+
236
+ const paths = getPaths(target, releaseInfo);
237
+ if (!fs.existsSync(paths.executablePath)) {
238
+ await installRelease(target, releaseInfo, paths);
239
+ }
240
+
241
+ await pruneOldVersions(cacheRoot, paths.installDir);
242
+ launch(paths.executablePath);
243
+ }
244
+
245
+ module.exports = {
246
+ main: async () => {
247
+ try {
248
+ await main();
249
+ } catch (error) {
250
+ const message = error instanceof Error ? error.message : String(error);
251
+ console.error(`howcode: ${message}`);
252
+ process.exit(1);
253
+ }
254
+ },
255
+ };
256
+
257
+ if (require.main === module) {
258
+ module.exports.main();
259
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "howcode",
3
+ "version": "0.1.0",
4
+ "description": "Launch the Howcode desktop app from npm or npx.",
5
+ "license": "MIT",
6
+ "author": "Igor Warzocha",
7
+ "homepage": "https://github.com/IgorWarzocha/howcode",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/IgorWarzocha/howcode.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/IgorWarzocha/howcode/issues"
14
+ },
15
+ "bin": {
16
+ "howcode": "bin/howcode.js"
17
+ },
18
+ "files": ["bin", "lib", "README.md", "LICENSE"],
19
+ "engines": {
20
+ "node": ">=18"
21
+ },
22
+ "keywords": ["desktop", "launcher", "pi", "coding"],
23
+ "howcode": {
24
+ "appName": "howcode",
25
+ "releaseBaseUrl": "https://github.com/IgorWarzocha/howcode/releases/latest/download"
26
+ }
27
+ }