@flowscripter/example-cli 3.0.2 → 3.0.3

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/bun.lock CHANGED
@@ -5,7 +5,7 @@
5
5
  "": {
6
6
  "name": "@flowscripter/example-cli",
7
7
  "dependencies": {
8
- "@flowscripter/dynamic-cli-framework": "^7.0.7",
8
+ "@flowscripter/dynamic-cli-framework": "^7.0.8",
9
9
  "@flowscripter/dynamic-cli-framework-api": "^4.0.4",
10
10
  "@flowscripter/dynamic-plugin-framework": "2.2.14",
11
11
  },
@@ -21,7 +21,7 @@
21
21
  },
22
22
  },
23
23
  "packages": {
24
- "@flowscripter/dynamic-cli-framework": ["@flowscripter/dynamic-cli-framework@7.0.7", "", { "dependencies": { "@flowscripter/dynamic-cli-framework-api": "^4.0.4", "@flowscripter/dynamic-plugin-framework": "^2.2.14", "emphasize": "^7.0.0", "fastest-levenshtein": "^1.0.16", "figlet": "^1.12.0", "highlight.js": "^11.12.0", "prettier": "^3.9.9", "semver": "^7.8.5", "supports-color": "^11.0.0", "supports-terminal-graphics": "^0.1.0" }, "peerDependencies": { "typescript": "^7.0.2" } }, "sha512-7n99Cw6FP1rL8UbK5++TTf/+1dybRDCxm67VPV0tcVLLXIkxzVU/SKUB+kGzs3hkVVYyHBbWWRdQ8GHLTeE3pw=="],
24
+ "@flowscripter/dynamic-cli-framework": ["@flowscripter/dynamic-cli-framework@7.0.8", "", { "dependencies": { "@flowscripter/dynamic-cli-framework-api": "^4.0.4", "@flowscripter/dynamic-plugin-framework": "^2.2.14", "emphasize": "^7.0.0", "fastest-levenshtein": "^1.0.16", "figlet": "^1.12.0", "highlight.js": "^11.12.0", "prettier": "^3.9.9", "semver": "^7.8.5", "supports-color": "^11.0.0", "supports-terminal-graphics": "^0.1.0" }, "peerDependencies": { "typescript": "^7.0.2" } }, "sha512-wlIGkPWyzsmBCVeJlh2qqxX/KG3sfbz2GNpbG4KbV5hhS12Y7WqRB/77m6uf5xRQIAP6ivb6vcQbVu6QVnWUTA=="],
25
25
 
26
26
  "@flowscripter/dynamic-cli-framework-api": ["@flowscripter/dynamic-cli-framework-api@4.0.4", "", { "peerDependencies": { "typescript": "^7.0.2" } }, "sha512-Vb+dUEuQ48maXEUYJ906hrvcRxRtTkCXIJs6CEPDzgVZ7WVYgf1YXU/PNaxEhZ+On4RmMwvpej4Du6rci4Y48Q=="],
27
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowscripter/example-cli",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Simple example CLI using https://github.com/flowscripter/dynamic-cli-framework",
5
5
  "keywords": [
6
6
  "bun",
@@ -24,7 +24,7 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@flowscripter/dynamic-cli-framework": "^7.0.7",
27
+ "@flowscripter/dynamic-cli-framework": "^7.0.8",
28
28
  "@flowscripter/dynamic-cli-framework-api": "^4.0.4",
29
29
  "@flowscripter/dynamic-plugin-framework": "2.2.14"
30
30
  },
@@ -1,120 +1,91 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { createBannerStartupTask } from "@flowscripter/dynamic-cli-framework";
3
- import {
4
- ASCII_BANNER_GENERATOR_SERVICE_ID,
5
- KEY_VALUE_SERVICE_ID,
6
- PRINTER_SERVICE_ID,
7
- } from "@flowscripter/dynamic-cli-framework-api";
8
- import type { Context } from "@flowscripter/dynamic-cli-framework-api";
1
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import path from "node:path";
5
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
6
+ import packageJson from "../package.json";
9
7
 
10
- // Functional test for dynamic-cli-framework#141: the startup banner must print the
8
+ // Functional test for dynamic-cli-framework#141/#213: the startup banner must print the
11
9
  // upgrade-availability hint "(X.Y.Z available, run 'app upgrade')" on the SAME line as
12
- // "version: X.Y.Z", rather than as a separate line (the pre-fix behaviour).
10
+ // "version: X.Y.Z", sourced from the cached upgrade-check result.
13
11
  //
14
- // This drives the real, installed banner StartupTask (from createBannerStartupTask(50), the
15
- // factory example-cli's src/cli.ts now calls following the dynamic-cli-framework
16
- // service-lifecycle redesign) with a stub KeyValueService reporting a cached "upgrade available"
17
- // result.
18
- //
19
- // Following that redesign, the banner no longer calls a live UpgradeService at all - it does a
20
- // cheap KeyValueService read of the previous run's cached upgrade-check result (see
21
- // dynamic-cli-framework's bannerStartupTask.ts / UPGRADE_CHECK_CACHE_KEY - an internal cache key,
22
- // not part of the public API surface, hardcoded below as "upgrade-check-result"; if the
23
- // framework's internal key ever changes this test needs updating to match). This also removes the
24
- // original #141 test's race entirely - the opportunistic upgrade check used to run concurrently
25
- // with, and often lose to, the banner's own priority band - so there's no longer a reason this
26
- // couldn't also be exercised as a full black-box run; kept as a direct unit test here for speed
27
- // and because it isolates the formatting fix precisely.
28
- const UPGRADE_CHECK_CACHE_KEY = "upgrade-check-result";
12
+ // bannerStartupTask.ts only honours a cached result coming from the framework's own
13
+ // DefaultUpgradeService (via an `instanceof` check) - that class isn't part of the package's
14
+ // public exports, so it can't be stubbed from outside the framework. Instead this drives the
15
+ // real CLI end to end (same pattern as tests/version_command_test.ts), pre-seeding the on-disk
16
+ // config file's "services" key-value scope for UPGRADE_SERVICE_ID
17
+ // ("@flowscripter/dynamic-cli-framework/upgrade-service") with the cached
18
+ // "upgrade-check-result" entry that DefaultUpgradeService.getCachedUpgradeCheckResult() reads -
19
+ // the same entry refreshUpgradeCheckCache() would have written from a prior run.
20
+ const PROJECT_ROOT = path.join(import.meta.dir, "..");
21
+ const UPGRADE_SERVICE_ID = "@flowscripter/dynamic-cli-framework/upgrade-service";
29
22
 
30
- function createStubPrinterService(lines: string[]) {
31
- const identity = (message: string) => message;
32
- return {
33
- blue: identity,
34
- primary: identity,
35
- secondary: identity,
36
- info: (message: string) => {
37
- lines.push(message);
38
- return Promise.resolve();
39
- },
40
- };
41
- }
23
+ let homeDir: string;
42
24
 
43
- function createStubAsciiBannerGeneratorService() {
44
- return {
45
- registerFont: () => {},
46
- getRegisteredFonts: () => [],
47
- generate: () => Promise.resolve("EXAMPLE-CLI"),
48
- };
49
- }
25
+ beforeEach(async () => {
26
+ homeDir = await mkdtemp(join(tmpdir(), "example-cli-home-"));
27
+ });
50
28
 
51
- function createStubKeyValueService(updateAvailable: boolean, latestVersion: string) {
52
- const cachedResult = updateAvailable
53
- ? { status: "checked", updateAvailable: true, latestVersion }
54
- : { status: "checked", updateAvailable: false, latestVersion };
55
- return {
56
- has: (key: string) => Promise.resolve(key === UPGRADE_CHECK_CACHE_KEY),
57
- get: (key: string) =>
58
- key === UPGRADE_CHECK_CACHE_KEY
59
- ? Promise.resolve(cachedResult)
60
- : Promise.reject(new Error(`Attempt to access unknown key: ${key}`)),
61
- set: () => Promise.reject(new Error("not implemented")),
62
- delete: () => Promise.reject(new Error("not implemented")),
63
- };
64
- }
29
+ afterEach(async () => {
30
+ await rm(homeDir, { recursive: true, force: true });
31
+ });
65
32
 
66
- function createContext(
67
- printerService: unknown,
68
- asciiBannerGeneratorService: unknown,
69
- keyValueService: unknown,
70
- ): Context {
71
- const services: Record<string, unknown> = {
72
- [PRINTER_SERVICE_ID]: printerService,
73
- [ASCII_BANNER_GENERATOR_SERVICE_ID]: asciiBannerGeneratorService,
74
- [KEY_VALUE_SERVICE_ID]: keyValueService,
75
- };
76
- return {
77
- cliConfig: {
78
- name: "example-cli",
79
- description: "Simple example CLI using dynamic-cli-framework.",
80
- version: "1.8.2",
33
+ async function seedUpgradeCheckCache(
34
+ updateAvailable: boolean,
35
+ latestVersion: string,
36
+ ): Promise<void> {
37
+ const config = {
38
+ "key-values": {
39
+ services: {
40
+ [UPGRADE_SERVICE_ID]: {
41
+ "upgrade-check-result": {
42
+ status: "checked",
43
+ currentVersion: packageJson.version,
44
+ latestVersion,
45
+ updateAvailable,
46
+ os: "linux",
47
+ arch: "x64",
48
+ installMethod: "github-release",
49
+ },
50
+ },
51
+ },
81
52
  },
82
- getServiceById: (id: string) => services[id],
83
- doesServiceExist: (id: string) => id in services,
84
53
  };
54
+ await writeFile(join(homeDir, ".examplecli.json"), JSON.stringify(config));
55
+ }
56
+
57
+ function runCli(): { stderr: string } {
58
+ const result = Bun.spawnSync([process.execPath, "run", "index.ts"], {
59
+ cwd: PROJECT_ROOT,
60
+ env: { ...process.env, HOME: homeDir },
61
+ stdin: "ignore",
62
+ stdout: "pipe",
63
+ stderr: "pipe",
64
+ });
65
+ return { stderr: result.stderr.toString("utf-8") };
85
66
  }
86
67
 
87
68
  describe("Banner upgrade hint (dynamic-cli-framework #141)", () => {
88
69
  test("prints the upgrade hint on the same line as the version when an upgrade is available", async () => {
89
- const lines: string[] = [];
90
- const task = createBannerStartupTask(50);
91
- const context = createContext(
92
- createStubPrinterService(lines),
93
- createStubAsciiBannerGeneratorService(),
94
- createStubKeyValueService(true, "99.0.0"),
95
- );
70
+ await seedUpgradeCheckCache(true, "99.0.0");
96
71
 
97
- await task.run(context);
72
+ const { stderr } = runCli();
98
73
 
99
- const versionLine = lines.find((line) => line.includes("version:"));
74
+ const versionLine = stderr.split("\n").find((line) => line.includes("version:"));
100
75
  expect(versionLine).toBeDefined();
101
- expect(versionLine).toContain("version: 1.8.2 (99.0.0 available, run 'example-cli upgrade')");
76
+ expect(versionLine).toContain(
77
+ `version: ${packageJson.version} (99.0.0 available, run 'example-cli upgrade')`,
78
+ );
102
79
  });
103
80
 
104
81
  test("does not print a hint when no upgrade is available", async () => {
105
- const lines: string[] = [];
106
- const task = createBannerStartupTask(50);
107
- const context = createContext(
108
- createStubPrinterService(lines),
109
- createStubAsciiBannerGeneratorService(),
110
- createStubKeyValueService(false, "1.8.2"),
111
- );
82
+ await seedUpgradeCheckCache(false, packageJson.version);
112
83
 
113
- await task.run(context);
84
+ const { stderr } = runCli();
114
85
 
115
- const versionLine = lines.find((line) => line.includes("version:"));
86
+ const versionLine = stderr.split("\n").find((line) => line.includes("version:"));
116
87
  expect(versionLine).toBeDefined();
117
88
  expect(versionLine).not.toContain("available");
118
- expect(versionLine).toContain("version: 1.8.2");
89
+ expect(versionLine).toContain(`version: ${packageJson.version}`);
119
90
  });
120
91
  });