@glassly/cli 2.0.12 → 2.0.13-beta.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/README.md +6 -2
- package/bin/glassly.js +39 -5
- package/package.json +2 -2
- package/src/index.ts +42 -6
- package/src/update.test.ts +89 -0
- package/src/update.ts +326 -0
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
# @glassly/cli
|
|
2
2
|
|
|
3
3
|
The `glassly` command: the one CLI for developing, releasing, and publishing
|
|
4
|
-
Glassly miniapps.
|
|
5
|
-
|
|
4
|
+
Glassly miniapps. Install it globally (`bun add -g @glassly/cli`). Scaffolded
|
|
5
|
+
projects also carry it as a dev dependency, and inside a project the global
|
|
6
|
+
`glassly` hands off to that copy, so each project runs the version it pins.
|
|
7
|
+
`glassly update` updates both the project's Glassly packages and the global
|
|
8
|
+
command, and the CLI prints a notice once a day when either is behind
|
|
9
|
+
(`GLASSLY_NO_UPDATE_CHECK=1` turns that off).
|
|
6
10
|
|
|
7
11
|
The project-side commands (`dev`, `build`, `pack`, `release`, `manifest`,
|
|
8
12
|
`permission`, `hardware`, `schema`) are implemented in
|
package/bin/glassly.js
CHANGED
|
@@ -2,15 +2,20 @@
|
|
|
2
2
|
// The CLI runs under Bun: the dev server, bundler plugins, and credential
|
|
3
3
|
// store use Bun APIs. This launcher lets `npx @glassly/cli` and plain `node`
|
|
4
4
|
// still work when Bun is installed, and explains what to do when it is not.
|
|
5
|
+
//
|
|
6
|
+
// A global `glassly` run inside a project hands off to the project's own
|
|
7
|
+
// @glassly/cli, so each project runs the tooling its package.json pins.
|
|
8
|
+
// `glassly update` never hands off: it has to work when the local copy is old.
|
|
5
9
|
import {spawnSync} from "node:child_process";
|
|
10
|
+
import {existsSync, readFileSync, realpathSync} from "node:fs";
|
|
11
|
+
import {dirname, join, resolve} from "node:path";
|
|
6
12
|
import {fileURLToPath} from "node:url";
|
|
7
13
|
|
|
8
|
-
const
|
|
14
|
+
const self = fileURLToPath(import.meta.url);
|
|
15
|
+
const packageDir = resolve(dirname(self), "..");
|
|
9
16
|
|
|
10
|
-
if (process.versions.bun) {
|
|
11
|
-
|
|
12
|
-
} else {
|
|
13
|
-
const result = spawnSync("bun", [entry, ...process.argv.slice(2)], {stdio: "inherit"});
|
|
17
|
+
if (!process.versions.bun) {
|
|
18
|
+
const result = spawnSync("bun", [self, ...process.argv.slice(2)], {stdio: "inherit"});
|
|
14
19
|
if (result.error && result.error.code === "ENOENT") {
|
|
15
20
|
console.error("glassly runs on Bun, which is not installed or not on PATH.");
|
|
16
21
|
console.error("Install it from https://bun.sh, then run: bunx @glassly/cli --help");
|
|
@@ -18,3 +23,32 @@ if (process.versions.bun) {
|
|
|
18
23
|
}
|
|
19
24
|
process.exit(result.status ?? 1);
|
|
20
25
|
}
|
|
26
|
+
|
|
27
|
+
function projectCli(cwd) {
|
|
28
|
+
for (let dir = cwd; ; dir = dirname(dir)) {
|
|
29
|
+
const candidate = join(dir, "node_modules", "@glassly", "cli");
|
|
30
|
+
if (existsSync(join(candidate, "package.json"))) {
|
|
31
|
+
if (realpathSync(candidate) === realpathSync(packageDir)) return null;
|
|
32
|
+
const pkg = JSON.parse(readFileSync(join(candidate, "package.json"), "utf8"));
|
|
33
|
+
const bin = typeof pkg.bin === "string" ? pkg.bin : pkg.bin?.glassly;
|
|
34
|
+
return bin ? join(candidate, bin) : null;
|
|
35
|
+
}
|
|
36
|
+
if (dirname(dir) === dir) return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const args = process.argv.slice(2);
|
|
41
|
+
const local = args[0] === "update" || process.env.GLASSLY_CLI_HANDOFF ? null : projectCli(process.cwd());
|
|
42
|
+
|
|
43
|
+
if (local) {
|
|
44
|
+
const {printUpdateNotice} = await import("../src/update.ts");
|
|
45
|
+
const {version} = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8"));
|
|
46
|
+
await printUpdateNotice({args, cwd: process.cwd(), cliVersion: version, cliDir: packageDir});
|
|
47
|
+
const result = spawnSync(process.execPath, [local, ...args], {
|
|
48
|
+
stdio: "inherit",
|
|
49
|
+
env: {...process.env, GLASSLY_CLI_HANDOFF: "1", GLASSLY_NO_UPDATE_CHECK: "1"},
|
|
50
|
+
});
|
|
51
|
+
process.exit(result.status ?? 1);
|
|
52
|
+
} else {
|
|
53
|
+
await import("../src/index.ts");
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glassly/cli",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.13-beta.0",
|
|
4
4
|
"description": "The glassly command: develop, pack, release, and publish Glassly miniapps. Bun-only.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"typecheck": "tsc --noEmit"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@glassly/miniapp-cli": "0.1.
|
|
25
|
+
"@glassly/miniapp-cli": "0.1.13-beta.0",
|
|
26
26
|
"commander": "^15.1.0",
|
|
27
27
|
"qrcode": "^1.5.4"
|
|
28
28
|
},
|
package/src/index.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
} from "@glassly/miniapp-cli";
|
|
30
30
|
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
31
31
|
import { basename, extname, join, resolve } from "node:path";
|
|
32
|
+
import { fileURLToPath } from "node:url";
|
|
32
33
|
import {
|
|
33
34
|
addMedia,
|
|
34
35
|
createAdminRegistryRevision,
|
|
@@ -65,6 +66,7 @@ import { clearCredentials, loadCredentials, saveCredentials, type CliCredentials
|
|
|
65
66
|
import { collectFeedbackContext } from "./feedback";
|
|
66
67
|
import { openBrowser } from "./open-browser";
|
|
67
68
|
import { encodeDevAttestation, ensureSigningKey, signBundleMetadata, signDevAttestation } from "./signing";
|
|
69
|
+
import { parseChannel, printUpdateNotice, runUpdate } from "./update";
|
|
68
70
|
|
|
69
71
|
const cliVersion = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version as string;
|
|
70
72
|
|
|
@@ -634,6 +636,27 @@ preinstall
|
|
|
634
636
|
}
|
|
635
637
|
});
|
|
636
638
|
|
|
639
|
+
program
|
|
640
|
+
.command("update")
|
|
641
|
+
.description("Update this project's Glassly SDK and tools, and the global glassly command, to the newest release")
|
|
642
|
+
.option("--cwd <path>", "miniapp project directory", process.cwd())
|
|
643
|
+
.option("--channel <channel>", "release channel: latest|beta|dev (default: the one the project is on)")
|
|
644
|
+
.option("--no-global", "leave the global glassly command alone")
|
|
645
|
+
.option("--no-project", "only update the global glassly command")
|
|
646
|
+
.action(async (options: { cwd: string; channel?: string; global: boolean; project: boolean }) => {
|
|
647
|
+
try {
|
|
648
|
+
await runUpdate({
|
|
649
|
+
cwd: resolve(options.cwd),
|
|
650
|
+
cliVersion,
|
|
651
|
+
channel: options.channel ? parseChannel(options.channel) : undefined,
|
|
652
|
+
global: options.global,
|
|
653
|
+
project: options.project,
|
|
654
|
+
});
|
|
655
|
+
} catch (error) {
|
|
656
|
+
fail(error);
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
|
|
637
660
|
program
|
|
638
661
|
.command("dev")
|
|
639
662
|
.description("Start the local miniapp dev server with signed Cloud identity when logged in")
|
|
@@ -786,12 +809,11 @@ const SIM_GESTURES: Record<string, [string, string]> = {
|
|
|
786
809
|
};
|
|
787
810
|
|
|
788
811
|
for (const [name, [gesture, description]] of Object.entries(SIM_GESTURES)) {
|
|
789
|
-
simulator
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
}));
|
|
812
|
+
const command = simulator.command(name).description(description);
|
|
813
|
+
if (name === "tap") command.option("--b", "tap B: the second finger (index 1) instead of the first");
|
|
814
|
+
command.action(simCommand(async (target, options: { b?: boolean }) => {
|
|
815
|
+
await simPost(target, "touch", name === "tap" ? { gesture, index: options.b ? 1 : 0 } : { gesture });
|
|
816
|
+
}));
|
|
795
817
|
}
|
|
796
818
|
|
|
797
819
|
simulator
|
|
@@ -850,6 +872,14 @@ simulator
|
|
|
850
872
|
await simPost(target, "battery", { target: options.phone ? "phone" : "glasses", level: Number(level), charging: options.charging === true });
|
|
851
873
|
}));
|
|
852
874
|
|
|
875
|
+
simulator
|
|
876
|
+
.command("phone <device>")
|
|
877
|
+
.description("Show the phone page on an iphone or a folded iPhone Duo (duo), with that device's safe area and capsule")
|
|
878
|
+
.action(simCommand(async (target, device: string) => {
|
|
879
|
+
if (device !== "iphone" && device !== "duo") throw new Error(`Unknown phone "${device}" (expected iphone or duo)`);
|
|
880
|
+
await simPost(target, "phone", { phone: device });
|
|
881
|
+
}));
|
|
882
|
+
|
|
853
883
|
simulator
|
|
854
884
|
.command("disconnect")
|
|
855
885
|
.description("Disconnect the glasses (the host pauses the miniapp)")
|
|
@@ -1224,6 +1254,12 @@ program
|
|
|
1224
1254
|
console.log("Logged out");
|
|
1225
1255
|
});
|
|
1226
1256
|
|
|
1257
|
+
await printUpdateNotice({
|
|
1258
|
+
args: process.argv.slice(2),
|
|
1259
|
+
cwd: process.cwd(),
|
|
1260
|
+
cliVersion,
|
|
1261
|
+
cliDir: fileURLToPath(new URL("..", import.meta.url)),
|
|
1262
|
+
});
|
|
1227
1263
|
program.parse();
|
|
1228
1264
|
|
|
1229
1265
|
async function requireCredentials(): Promise<CliCredentials | null> {
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { channelOf, compareVersions, updateProject, wantsUpdateNotice } from "./update";
|
|
6
|
+
|
|
7
|
+
describe("compareVersions", () => {
|
|
8
|
+
test("orders releases, prereleases, and build numbers", () => {
|
|
9
|
+
expect(compareVersions("0.4.2", "0.4.1")).toBeGreaterThan(0);
|
|
10
|
+
expect(compareVersions("0.4.1", "0.4.1-beta.1173")).toBeGreaterThan(0);
|
|
11
|
+
expect(compareVersions("0.4.1-dev.1172", "0.4.1-dev.999")).toBeGreaterThan(0);
|
|
12
|
+
expect(compareVersions("0.4.2-dev.1", "0.4.1-dev.1172")).toBeGreaterThan(0);
|
|
13
|
+
expect(compareVersions("2.0.12", "2.0.12")).toBe(0);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("channelOf reads the prerelease tag", () => {
|
|
18
|
+
expect(channelOf("0.4.1")).toBe("latest");
|
|
19
|
+
expect(channelOf("^0.4.1")).toBe("latest");
|
|
20
|
+
expect(channelOf("0.4.1-beta.1173")).toBe("beta");
|
|
21
|
+
expect(channelOf("0.4.1-dev.1172")).toBe("dev");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("simulator control subcommands skip the notice", () => {
|
|
25
|
+
const saved = { CI: process.env.CI, GLASSLY_NO_UPDATE_CHECK: process.env.GLASSLY_NO_UPDATE_CHECK };
|
|
26
|
+
delete process.env.CI;
|
|
27
|
+
delete process.env.GLASSLY_NO_UPDATE_CHECK;
|
|
28
|
+
try {
|
|
29
|
+
expect(wantsUpdateNotice(["dev"])).toBe(true);
|
|
30
|
+
expect(wantsUpdateNotice(["sim"])).toBe(true);
|
|
31
|
+
expect(wantsUpdateNotice(["sim", "--detach"])).toBe(true);
|
|
32
|
+
expect(wantsUpdateNotice(["sim", "screen"])).toBe(false);
|
|
33
|
+
expect(wantsUpdateNotice(["update"])).toBe(false);
|
|
34
|
+
expect(wantsUpdateNotice(["--version"])).toBe(false);
|
|
35
|
+
} finally {
|
|
36
|
+
Object.assign(process.env, saved);
|
|
37
|
+
for (const [key, value] of Object.entries(saved)) if (value === undefined) delete process.env[key];
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("updateProject", () => {
|
|
42
|
+
let dir: string;
|
|
43
|
+
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
|
44
|
+
|
|
45
|
+
function project(): string {
|
|
46
|
+
dir = mkdtempSync(join(tmpdir(), "glassly-update-"));
|
|
47
|
+
writeFileSync(
|
|
48
|
+
join(dir, "package.json"),
|
|
49
|
+
JSON.stringify({
|
|
50
|
+
dependencies: { "@glassly/miniapp": "^0.4.1", react: "^19.0.0" },
|
|
51
|
+
devDependencies: { "@glassly/cli": "^2.0.0", "@glassly/miniapp-cli": "^0.1.0" },
|
|
52
|
+
}, null, 2),
|
|
53
|
+
);
|
|
54
|
+
writeFileSync(join(dir, "miniapp.json"), `{\n "sdkVersion": "0.4.1",\n "minHostVersion": "0.4.1"\n}\n`);
|
|
55
|
+
return dir;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const tags = {
|
|
59
|
+
"@glassly/miniapp": { latest: "0.4.2", dev: "0.4.2-dev.7" },
|
|
60
|
+
"@glassly/cli": { latest: "2.0.13", dev: "2.0.13-dev.0" },
|
|
61
|
+
"@glassly/miniapp-cli": { latest: "0.1.13", dev: "0.1.13-dev.0" },
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
test("stable channel gets caret pins and a new sdkVersion, minHostVersion untouched", () => {
|
|
65
|
+
const root = project();
|
|
66
|
+
expect(updateProject(root, tags, "latest")).toBe(true);
|
|
67
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
68
|
+
expect(pkg.dependencies["@glassly/miniapp"]).toBe("^0.4.2");
|
|
69
|
+
expect(pkg.dependencies.react).toBe("^19.0.0");
|
|
70
|
+
expect(pkg.devDependencies["@glassly/cli"]).toBe("^2.0.13");
|
|
71
|
+
expect(pkg.devDependencies["@glassly/miniapp-cli"]).toBe("^0.1.13");
|
|
72
|
+
const manifest = JSON.parse(readFileSync(join(root, "miniapp.json"), "utf8"));
|
|
73
|
+
expect(manifest).toEqual({ sdkVersion: "0.4.2", minHostVersion: "0.4.1" });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("prerelease channel pins the SDK exactly", () => {
|
|
77
|
+
const root = project();
|
|
78
|
+
updateProject(root, tags, "dev");
|
|
79
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
80
|
+
expect(pkg.dependencies["@glassly/miniapp"]).toBe("0.4.2-dev.7");
|
|
81
|
+
expect(pkg.devDependencies["@glassly/cli"]).toBe("^2.0.13-dev.0");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("an up-to-date project is left alone", () => {
|
|
85
|
+
const root = project();
|
|
86
|
+
updateProject(root, tags, "latest");
|
|
87
|
+
expect(updateProject(root, tags, "latest")).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
});
|
package/src/update.ts
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { delimiter, dirname, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
|
|
6
|
+
export const SDK_PACKAGE = "@glassly/miniapp";
|
|
7
|
+
export const CLI_PACKAGE = "@glassly/cli";
|
|
8
|
+
export const TOOLS_PACKAGE = "@glassly/miniapp-cli";
|
|
9
|
+
const PROJECT_PACKAGES = [SDK_PACKAGE, CLI_PACKAGE, TOOLS_PACKAGE] as const;
|
|
10
|
+
|
|
11
|
+
export type Channel = "latest" | "beta" | "dev";
|
|
12
|
+
type DistTags = Partial<Record<Channel, string>>;
|
|
13
|
+
|
|
14
|
+
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
15
|
+
const CACHE_PATH = join(homedir(), ".glassly", "cli", "update-check.json");
|
|
16
|
+
const REGISTRY = (process.env.GLASSLY_NPM_REGISTRY || "https://registry.npmjs.org").replace(/\/+$/, "");
|
|
17
|
+
|
|
18
|
+
export function channelOf(version: string): Channel {
|
|
19
|
+
if (/-dev\b/.test(version)) return "dev";
|
|
20
|
+
if (/-beta\b/.test(version)) return "beta";
|
|
21
|
+
return "latest";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseChannel(raw: string): Channel {
|
|
25
|
+
const value = raw === "stable" ? "latest" : raw;
|
|
26
|
+
if (value === "latest" || value === "beta" || value === "dev") return value;
|
|
27
|
+
throw new Error(`Invalid --channel "${raw}" (expected latest|beta|dev)`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function compareVersions(a: string, b: string): number {
|
|
31
|
+
const [aCore = "", ...aPre] = a.split("-");
|
|
32
|
+
const [bCore = "", ...bPre] = b.split("-");
|
|
33
|
+
const aParts = aCore.split(".").map(Number);
|
|
34
|
+
const bParts = bCore.split(".").map(Number);
|
|
35
|
+
for (let i = 0; i < 3; i++) {
|
|
36
|
+
const diff = (aParts[i] ?? 0) - (bParts[i] ?? 0);
|
|
37
|
+
if (diff !== 0) return diff;
|
|
38
|
+
}
|
|
39
|
+
if (!aPre.length || !bPre.length) return aPre.length ? -1 : bPre.length ? 1 : 0;
|
|
40
|
+
const aIds = aPre.join("-").split(".");
|
|
41
|
+
const bIds = bPre.join("-").split(".");
|
|
42
|
+
for (let i = 0; i < Math.max(aIds.length, bIds.length); i++) {
|
|
43
|
+
const x = aIds[i];
|
|
44
|
+
const y = bIds[i];
|
|
45
|
+
if (x === undefined) return -1;
|
|
46
|
+
if (y === undefined) return 1;
|
|
47
|
+
const xNum = /^\d+$/.test(x);
|
|
48
|
+
const yNum = /^\d+$/.test(y);
|
|
49
|
+
if (xNum && yNum && Number(x) !== Number(y)) return Number(x) - Number(y);
|
|
50
|
+
if (xNum !== yNum) return xNum ? -1 : 1;
|
|
51
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
52
|
+
}
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function fetchDistTags(name: string, timeoutMs: number): Promise<DistTags> {
|
|
57
|
+
const response = await fetch(`${REGISTRY}/-/package/${name.replace("/", "%2f")}/dist-tags`, {
|
|
58
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
59
|
+
});
|
|
60
|
+
if (!response.ok) throw new Error(`npm registry answered ${response.status} for ${name}`);
|
|
61
|
+
return (await response.json()) as DistTags;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function fetchAllDistTags(timeoutMs: number): Promise<Record<string, DistTags>> {
|
|
65
|
+
const entries = await Promise.all(PROJECT_PACKAGES.map(async (name) => [name, await fetchDistTags(name, timeoutMs)] as const));
|
|
66
|
+
return Object.fromEntries(entries);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface CacheFile {
|
|
70
|
+
checkedAt: number;
|
|
71
|
+
tags: Record<string, DistTags>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readCache(): CacheFile | null {
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(readFileSync(CACHE_PATH, "utf8")) as CacheFile;
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function writeCache(tags: Record<string, DistTags>): void {
|
|
83
|
+
try {
|
|
84
|
+
mkdirSync(dirname(CACHE_PATH), { recursive: true });
|
|
85
|
+
writeFileSync(CACHE_PATH, JSON.stringify({ checkedAt: Date.now(), tags } satisfies CacheFile));
|
|
86
|
+
} catch {
|
|
87
|
+
/* a read-only home only loses the cache */
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function readJson(path: string): Record<string, unknown> | null {
|
|
92
|
+
try {
|
|
93
|
+
return JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
|
|
94
|
+
} catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function findProjectDir(cwd: string): string | null {
|
|
100
|
+
for (let dir = resolve(cwd); ; dir = dirname(dir)) {
|
|
101
|
+
if (existsSync(join(dir, "miniapp.json")) && existsSync(join(dir, "package.json"))) return dir;
|
|
102
|
+
if (dirname(dir) === dir) return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Workspace links resolve outside any node_modules: those are monorepo
|
|
107
|
+
// checkouts, not installs that npm can update.
|
|
108
|
+
function installedVersion(projectDir: string, name: string): string | null {
|
|
109
|
+
for (let dir = projectDir; ; dir = dirname(dir)) {
|
|
110
|
+
const pkgPath = join(dir, "node_modules", name, "package.json");
|
|
111
|
+
if (existsSync(pkgPath)) {
|
|
112
|
+
const real = realpathSync(pkgPath);
|
|
113
|
+
if (!real.split(sep).includes("node_modules")) return null;
|
|
114
|
+
const version = readJson(pkgPath)?.version;
|
|
115
|
+
return typeof version === "string" ? version : null;
|
|
116
|
+
}
|
|
117
|
+
if (dirname(dir) === dir) return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface Outdated {
|
|
122
|
+
name: string;
|
|
123
|
+
current: string;
|
|
124
|
+
latest: string;
|
|
125
|
+
where: "project" | "cli";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function outdatedPackages(input: {
|
|
129
|
+
tags: Record<string, DistTags>;
|
|
130
|
+
cliVersion: string;
|
|
131
|
+
cliIsWorkspace: boolean;
|
|
132
|
+
projectDir: string | null;
|
|
133
|
+
}): Outdated[] {
|
|
134
|
+
const result: Outdated[] = [];
|
|
135
|
+
const consider = (name: string, current: string | null, where: Outdated["where"]) => {
|
|
136
|
+
if (!current) return;
|
|
137
|
+
const latest = input.tags[name]?.[channelOf(current)];
|
|
138
|
+
if (latest && compareVersions(latest, current) > 0) result.push({ name, current, latest, where });
|
|
139
|
+
};
|
|
140
|
+
if (input.projectDir) {
|
|
141
|
+
for (const name of PROJECT_PACKAGES) consider(name, installedVersion(input.projectDir, name), "project");
|
|
142
|
+
}
|
|
143
|
+
if (!input.cliIsWorkspace && !result.some((entry) => entry.name === CLI_PACKAGE)) {
|
|
144
|
+
consider(CLI_PACKAGE, input.cliVersion, "cli");
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function isWorkspaceCli(cliDir: string): boolean {
|
|
150
|
+
return !realpathSync(cliDir).split(sep).includes("node_modules");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// `glassly sim <action>` runs in scripts and agent loops; the simulator start
|
|
154
|
+
// already warned, so its control subcommands stay quiet.
|
|
155
|
+
export function wantsUpdateNotice(args: string[]): boolean {
|
|
156
|
+
if (process.env.CI || process.env.GLASSLY_NO_UPDATE_CHECK) return false;
|
|
157
|
+
const [command, sub] = args;
|
|
158
|
+
if (!command || command === "update" || command === "help" || command.startsWith("-")) return false;
|
|
159
|
+
return !(command === "sim" && sub !== undefined && !sub.startsWith("-"));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Refreshes the registry at most once a day and never fails a command: a slow
|
|
163
|
+
// or offline registry just means no notice this time.
|
|
164
|
+
export async function printUpdateNotice(input: {
|
|
165
|
+
args: string[];
|
|
166
|
+
cwd: string;
|
|
167
|
+
cliVersion: string;
|
|
168
|
+
cliDir: string;
|
|
169
|
+
}): Promise<void> {
|
|
170
|
+
if (!wantsUpdateNotice(input.args)) return;
|
|
171
|
+
try {
|
|
172
|
+
let cache = readCache();
|
|
173
|
+
if (!cache || Date.now() - cache.checkedAt > CHECK_INTERVAL_MS) {
|
|
174
|
+
const tags = await fetchAllDistTags(1500).catch(() => null);
|
|
175
|
+
if (tags) {
|
|
176
|
+
writeCache(tags);
|
|
177
|
+
cache = { checkedAt: Date.now(), tags };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (!cache) return;
|
|
181
|
+
const outdated = outdatedPackages({
|
|
182
|
+
tags: cache.tags,
|
|
183
|
+
cliVersion: input.cliVersion,
|
|
184
|
+
cliIsWorkspace: isWorkspaceCli(input.cliDir),
|
|
185
|
+
projectDir: findProjectDir(input.cwd),
|
|
186
|
+
});
|
|
187
|
+
if (!outdated.length) return;
|
|
188
|
+
const list = outdated.map((entry) => `${entry.name} ${entry.current} → ${entry.latest}`).join(", ");
|
|
189
|
+
console.error(`Update available: ${list}. Run \`glassly update\`.`);
|
|
190
|
+
} catch {
|
|
191
|
+
/* never block a command on the update check */
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function pinFor(name: string, version: string): string {
|
|
196
|
+
return name === SDK_PACKAGE && version.includes("-") ? version : `^${version}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const DEPENDENCY_FIELDS = ["dependencies", "devDependencies"] as const;
|
|
200
|
+
|
|
201
|
+
function projectChannel(projectDir: string, cliVersion: string): Channel {
|
|
202
|
+
const sdk = installedVersion(projectDir, SDK_PACKAGE);
|
|
203
|
+
if (sdk) return channelOf(sdk);
|
|
204
|
+
const pinned = readJson(join(projectDir, "package.json"))?.dependencies as Record<string, string> | undefined;
|
|
205
|
+
const range = pinned?.[SDK_PACKAGE];
|
|
206
|
+
return range ? channelOf(range) : channelOf(cliVersion);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function updateProject(projectDir: string, tags: Record<string, DistTags>, channel: Channel): boolean {
|
|
210
|
+
const pkgPath = join(projectDir, "package.json");
|
|
211
|
+
const raw = readFileSync(pkgPath, "utf8");
|
|
212
|
+
const pkg = JSON.parse(raw) as Record<string, Record<string, string> | undefined>;
|
|
213
|
+
let changed = false;
|
|
214
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
215
|
+
const deps = pkg[field];
|
|
216
|
+
if (!deps) continue;
|
|
217
|
+
for (const name of PROJECT_PACKAGES) {
|
|
218
|
+
if (deps[name] === undefined) continue;
|
|
219
|
+
if (deps[name]!.startsWith("workspace:") || deps[name]!.startsWith("file:")) continue;
|
|
220
|
+
const version = tags[name]?.[channel];
|
|
221
|
+
if (!version) throw new Error(`${name} has no ${channel} release on npm`);
|
|
222
|
+
const pin = pinFor(name, version);
|
|
223
|
+
if (deps[name] === pin) continue;
|
|
224
|
+
console.log(` ${name} ${deps[name]} → ${pin}`);
|
|
225
|
+
deps[name] = pin;
|
|
226
|
+
changed = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (changed) {
|
|
230
|
+
const indent = /^\s+/m.exec(raw.split("\n")[1] ?? "")?.[0] ?? " ";
|
|
231
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, indent)}\n`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const sdk = tags[SDK_PACKAGE]?.[channel];
|
|
235
|
+
const manifestPath = join(projectDir, "miniapp.json");
|
|
236
|
+
if (sdk && existsSync(manifestPath)) {
|
|
237
|
+
const release = sdk.split("-")[0]!;
|
|
238
|
+
const manifest = readFileSync(manifestPath, "utf8");
|
|
239
|
+
const stamped = manifest.replace(/("sdkVersion"\s*:\s*)"[^"]*"/, `$1"${release}"`);
|
|
240
|
+
if (stamped !== manifest) {
|
|
241
|
+
writeFileSync(manifestPath, stamped);
|
|
242
|
+
console.log(` miniapp.json sdkVersion → ${release}`);
|
|
243
|
+
changed = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return changed;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
interface GlobalInstall {
|
|
250
|
+
dir: string;
|
|
251
|
+
version: string;
|
|
252
|
+
manager: "bun" | "npm";
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function cliPackageDir(file: string): string | null {
|
|
256
|
+
for (let dir = dirname(file); dirname(dir) !== dir; dir = dirname(dir)) {
|
|
257
|
+
if (readJson(join(dir, "package.json"))?.name === CLI_PACKAGE) return dir;
|
|
258
|
+
}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// PATH entries inside a node_modules/.bin belong to `bun run` and point at a
|
|
263
|
+
// project's own CLI; the global one is the first `glassly` outside them.
|
|
264
|
+
function findGlobalInstall(): GlobalInstall | null {
|
|
265
|
+
for (const entry of (process.env.PATH ?? "").split(delimiter)) {
|
|
266
|
+
if (!entry || entry.split(sep).includes("node_modules")) continue;
|
|
267
|
+
const bin = join(entry, process.platform === "win32" ? "glassly.exe" : "glassly");
|
|
268
|
+
try {
|
|
269
|
+
if (!statSync(bin).isFile()) continue;
|
|
270
|
+
} catch {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const dir = cliPackageDir(realpathSync(bin));
|
|
274
|
+
if (!dir || isWorkspaceCli(dir)) return null;
|
|
275
|
+
const version = readJson(join(dir, "package.json"))?.version;
|
|
276
|
+
if (typeof version !== "string") return null;
|
|
277
|
+
return { dir, version, manager: dir.split(sep).includes(".bun") ? "bun" : "npm" };
|
|
278
|
+
}
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function run(command: string, args: string[], cwd?: string): void {
|
|
283
|
+
console.log(`$ ${[command, ...args].join(" ")}`);
|
|
284
|
+
const result = spawnSync(command, args, { cwd, stdio: "inherit" });
|
|
285
|
+
if (result.error) throw new Error(`Could not run ${command}: ${result.error.message}`);
|
|
286
|
+
if (result.status !== 0) throw new Error(`${command} ${args.join(" ")} exited with code ${result.status}`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export async function runUpdate(options: {
|
|
290
|
+
cwd: string;
|
|
291
|
+
cliVersion: string;
|
|
292
|
+
channel?: Channel;
|
|
293
|
+
global: boolean;
|
|
294
|
+
project: boolean;
|
|
295
|
+
}): Promise<void> {
|
|
296
|
+
const tags = await fetchAllDistTags(10_000);
|
|
297
|
+
writeCache(tags);
|
|
298
|
+
|
|
299
|
+
const projectDir = options.project ? findProjectDir(options.cwd) : null;
|
|
300
|
+
const channel = options.channel ?? (projectDir ? projectChannel(projectDir, options.cliVersion) : channelOf(options.cliVersion));
|
|
301
|
+
console.log(`Channel: ${channel}`);
|
|
302
|
+
|
|
303
|
+
if (projectDir) {
|
|
304
|
+
const label = relative(options.cwd, projectDir) || ".";
|
|
305
|
+
console.log(`Project ${label}:`);
|
|
306
|
+
const changed = updateProject(projectDir, tags, channel);
|
|
307
|
+
if (!changed) console.log(" already on the newest Glassly packages");
|
|
308
|
+
run("bun", ["install"], projectDir);
|
|
309
|
+
} else if (options.project) {
|
|
310
|
+
console.log("No miniapp project here (no miniapp.json with a package.json); skipping project packages.");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (!options.global) return;
|
|
314
|
+
const target = tags[CLI_PACKAGE]?.[channel];
|
|
315
|
+
const install = findGlobalInstall();
|
|
316
|
+
if (!install) {
|
|
317
|
+
console.log(`No global glassly found. Install one with: bun add -g ${CLI_PACKAGE}${channel === "latest" ? "" : `@${channel}`}`);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (!target || compareVersions(target, install.version) <= 0) {
|
|
321
|
+
console.log(`Global glassly ${install.version} is up to date.`);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (install.manager === "bun") run("bun", ["add", "-g", `${CLI_PACKAGE}@${target}`]);
|
|
325
|
+
else run("npm", ["install", "-g", `${CLI_PACKAGE}@${target}`]);
|
|
326
|
+
}
|