moondust 0.1.0 → 0.2.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/bin/moondust.js CHANGED
@@ -1,21 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * Moondust npm entry: ensure cached release binary exists, then exec with argv forwarded.
4
- *
5
- * Expects GitHub Releases with assets named:
6
- * moondust_<semver>_linux_amd64.tar.gz
7
- * moondust_<semver>_darwin_arm64.tar.gz
8
- * moondust_<semver>_windows_amd64.zip
9
- * (see RELEASE_ASSETS.md in repo root)
3
+ * Interactive version pick, then ensure cached release binary exists and exec.
4
+ * Releases: https://github.com/zackarysantana/moondust — assets moondust_<semver>_<os>_<arch>.{tar.gz,zip}.
10
5
  */
11
6
  import { spawn } from "node:child_process";
12
- import { readFileSync, existsSync, mkdirSync, copyFileSync, unlinkSync, rmSync } from "node:fs";
7
+ import { existsSync, mkdirSync, copyFileSync, unlinkSync, rmSync } from "node:fs";
13
8
  import path from "node:path";
14
- import { fileURLToPath } from "node:url";
15
9
  import os from "node:os";
10
+ import { stdin, stdout } from "node:process";
11
+ import { createInterface } from "node:readline/promises";
16
12
 
17
13
  import { getTargetTriple, assetBasename } from "../lib/platform.js";
18
- import { getReleaseAssetUrl } from "../lib/github.js";
14
+ import {
15
+ getLatestReleaseTag,
16
+ listRecentReleases,
17
+ releaseTagExists,
18
+ getReleaseAssetUrl,
19
+ } from "../lib/github.js";
19
20
  import {
20
21
  downloadToFile,
21
22
  extractArchive,
@@ -24,27 +25,22 @@ import {
24
25
  ensureExecutable,
25
26
  } from "../lib/download.js";
26
27
 
27
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
28
- const pkg = JSON.parse(readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
28
+ /** GitHub owner/repo for releases (API + assets). */
29
+ const GITHUB_REPO = "zackarysantana/moondust";
29
30
 
30
- function githubRepoFromPackage(p) {
31
- const url = p.repository?.url;
32
- if (!url) return null;
33
- const m = String(url)
34
- .replace(/\.git$/, "")
35
- .match(/github\.com\/([^/]+)\/([^/]+)/i);
36
- return m ? `${m[1]}/${m[2]}` : null;
31
+ function semverFromTag(tag) {
32
+ return tag.replace(/^v/i, "").trim();
37
33
  }
38
34
 
39
- const REPO = process.env.MOONDUST_GITHUB_REPO ?? githubRepoFromPackage(pkg);
40
-
41
- const GITHUB_TOKEN = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN;
35
+ /** User input like "0.1.0" or "v0.1.0" → "v0.1.0" */
36
+ function tagFromUserVersion(raw) {
37
+ const t = raw.trim();
38
+ if (!t) return null;
39
+ return t.startsWith("v") ? t : `v${t}`;
40
+ }
42
41
 
43
42
  function cacheDir(version) {
44
- const base =
45
- process.env.MOONDUST_CACHE_DIR ??
46
- path.join(os.homedir(), ".cache", "moondust", version);
47
- return base;
43
+ return path.join(os.homedir(), ".cache", "moondust", version);
48
44
  }
49
45
 
50
46
  function binaryName(ext) {
@@ -63,18 +59,12 @@ async function ensureBinary(version, triple) {
63
59
  return cached;
64
60
  }
65
61
 
66
- if (!REPO || !REPO.includes("/")) {
67
- throw new Error(
68
- "Set MOONDUST_GITHUB_REPO to owner/repo (e.g. myorg/moondust) or add repository.url to packages/cli/package.json",
69
- );
70
- }
71
-
72
- const url = await getReleaseAssetUrl(REPO, tag, archiveName, GITHUB_TOKEN);
62
+ const url = await getReleaseAssetUrl(GITHUB_REPO, tag, archiveName);
73
63
  const tmpArchive = tempDownloadPath("moondust") + (isZip ? ".zip" : ".tar.gz");
74
64
  const extractRoot = tempDownloadPath("moondust-extract");
75
65
 
76
66
  try {
77
- await downloadToFile(url, tmpArchive, GITHUB_TOKEN);
67
+ await downloadToFile(url, tmpArchive);
78
68
  await extractArchive(tmpArchive, extractRoot, isZip);
79
69
  const found = await findBinaryInDir(extractRoot, "moondust", triple.ext);
80
70
  mkdirSync(destDir, { recursive: true });
@@ -96,8 +86,68 @@ async function ensureBinary(version, triple) {
96
86
  return cached;
97
87
  }
98
88
 
89
+ async function chooseVersionInteractive() {
90
+ const rl = createInterface({ input: stdin, output: stdout });
91
+ try {
92
+ const line = await rl.question("Use latest version? [Y/n] ");
93
+ const useLatest = line.trim() === "" || /^y(es)?$/i.test(line.trim());
94
+
95
+ if (useLatest) {
96
+ const tag = await getLatestReleaseTag(GITHUB_REPO);
97
+ return semverFromTag(tag);
98
+ }
99
+
100
+ const releases = await listRecentReleases(GITHUB_REPO, 5);
101
+ if (releases.length === 0) {
102
+ throw new Error(`No releases found for ${GITHUB_REPO}.`);
103
+ }
104
+
105
+ console.log("\nRecent releases:");
106
+ for (let i = 0; i < releases.length; i++) {
107
+ const r = releases[i];
108
+ const title = r.name || r.tag_name;
109
+ const when = r.published_at
110
+ ? new Date(r.published_at).toISOString().slice(0, 10)
111
+ : "";
112
+ console.log(
113
+ ` ${i + 1}. ${r.tag_name} — ${title}${when ? ` — ${when}` : ""}`,
114
+ );
115
+ }
116
+ console.log(" … (Older releases may exist on GitHub.)\n");
117
+
118
+ for (;;) {
119
+ const raw = await rl.question(
120
+ "Enter version to install (e.g. 0.1.0): ",
121
+ );
122
+ const tag = tagFromUserVersion(raw);
123
+ if (!tag) {
124
+ console.error("Please enter a version.");
125
+ continue;
126
+ }
127
+ const exists = await releaseTagExists(GITHUB_REPO, tag);
128
+ if (!exists) {
129
+ console.error(
130
+ `No release found for ${tag}. Check the tag and try again.`,
131
+ );
132
+ continue;
133
+ }
134
+ return semverFromTag(tag);
135
+ }
136
+ } finally {
137
+ rl.close();
138
+ }
139
+ }
140
+
141
+ async function resolveVersion() {
142
+ if (!stdin.isTTY) {
143
+ const tag = await getLatestReleaseTag(GITHUB_REPO);
144
+ return semverFromTag(tag);
145
+ }
146
+ return chooseVersionInteractive();
147
+ }
148
+
99
149
  async function main() {
100
- const version = pkg.version;
150
+ const version = await resolveVersion();
101
151
  const triple = getTargetTriple();
102
152
  const binPath = await ensureBinary(version, triple);
103
153
 
package/lib/download.js CHANGED
@@ -7,10 +7,10 @@ import { randomBytes } from "node:crypto";
7
7
  import extractZip from "extract-zip";
8
8
  import { x as extractTar } from "tar";
9
9
 
10
- export async function downloadToFile(url, destPath, token) {
11
- const headers = { "User-Agent": "moondust-npm-cli" };
12
- if (token) headers.Authorization = `Bearer ${token}`;
13
- const res = await fetch(url, { headers });
10
+ export async function downloadToFile(url, destPath) {
11
+ const res = await fetch(url, {
12
+ headers: { "User-Agent": "moondust-npm-cli" },
13
+ });
14
14
  if (!res.ok) {
15
15
  const text = await res.text();
16
16
  throw new Error(`Download failed ${res.status}: ${text.slice(0, 300)}`);
package/lib/github.js CHANGED
@@ -1,16 +1,64 @@
1
+ const HEADERS = {
2
+ Accept: "application/vnd.github+json",
3
+ "X-GitHub-Api-Version": "2022-11-28",
4
+ "User-Agent": "moondust-npm-cli",
5
+ };
6
+
7
+ async function githubJson(url) {
8
+ const res = await fetch(url, { headers: HEADERS });
9
+ const text = await res.text();
10
+ if (!res.ok) {
11
+ throw new Error(`GitHub API ${res.status}: ${text.slice(0, 500)}`);
12
+ }
13
+ return text ? JSON.parse(text) : null;
14
+ }
15
+
16
+ /**
17
+ * Latest non-prerelease release tag, or newest tag if /latest is missing.
18
+ */
19
+ export async function getLatestReleaseTag(repo) {
20
+ const latestUrl = `https://api.github.com/repos/${repo}/releases/latest`;
21
+ const res = await fetch(latestUrl, { headers: HEADERS });
22
+ if (res.ok) {
23
+ const data = await res.json();
24
+ return data.tag_name;
25
+ }
26
+ if (res.status === 404) {
27
+ const arr = await githubJson(
28
+ `https://api.github.com/repos/${repo}/releases?per_page=1`,
29
+ );
30
+ if (!Array.isArray(arr) || arr.length === 0) {
31
+ throw new Error(`No releases found for ${repo}.`);
32
+ }
33
+ return arr[0].tag_name;
34
+ }
35
+ const text = await res.text();
36
+ throw new Error(`GitHub API ${res.status}: ${text.slice(0, 500)}`);
37
+ }
38
+
39
+ /** Most recent releases (newest first), up to `limit`. */
40
+ export async function listRecentReleases(repo, limit) {
41
+ const data = await githubJson(
42
+ `https://api.github.com/repos/${repo}/releases?per_page=${limit}`,
43
+ );
44
+ return Array.isArray(data) ? data : [];
45
+ }
46
+
1
47
  /**
2
- * Resolve download URL for a release asset from GitHub's API.
48
+ * True if a release exists for this tag (e.g. v0.1.0).
3
49
  */
4
- export async function getReleaseAssetUrl(repo, tag, assetName, token) {
50
+ export async function releaseTagExists(repo, tag) {
5
51
  const url = `https://api.github.com/repos/${repo}/releases/tags/${encodeURIComponent(tag)}`;
6
- const headers = {
7
- Accept: "application/vnd.github+json",
8
- "X-GitHub-Api-Version": "2022-11-28",
9
- "User-Agent": "moondust-npm-cli",
10
- };
11
- if (token) headers.Authorization = `Bearer ${token}`;
52
+ const res = await fetch(url, { headers: HEADERS });
53
+ return res.ok;
54
+ }
12
55
 
13
- const res = await fetch(url, { headers });
56
+ /**
57
+ * Resolve download URL for a release asset from GitHub's API (public repos).
58
+ */
59
+ export async function getReleaseAssetUrl(repo, tag, assetName) {
60
+ const url = `https://api.github.com/repos/${repo}/releases/tags/${encodeURIComponent(tag)}`;
61
+ const res = await fetch(url, { headers: HEADERS });
14
62
  if (res.status === 404) {
15
63
  throw new Error(
16
64
  `Release ${tag} not found for ${repo}. Publish a GitHub Release with that tag and matching assets.`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moondust",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Download and run the Moondust desktop app from GitHub Releases",
5
5
  "type": "module",
6
6
  "bin": {
@@ -23,6 +23,9 @@
23
23
  "launcher"
24
24
  ],
25
25
  "license": "MIT",
26
+ "devDependencies": {
27
+ "@types/node": "^22.10.0"
28
+ },
26
29
  "dependencies": {
27
30
  "extract-zip": "^2.0.1",
28
31
  "tar": "^7.4.3"