@phreshos/cli 0.1.4 → 0.1.5

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/dist/create.js CHANGED
@@ -1,83 +1,88 @@
1
- import { projectDependency, installProjectDependencies, projectPackageManager } from "./project-dependency.js";
2
- import questions from "./questions.js";
3
- import { dim, heading, line } from "./style.js";
1
+ import { projectDependency, installProjectDependencies, projectPackageManager, projectScript } from "./project-dependency.js";
2
+ import prompts from "./prompts.js";
3
+ import { accent, bold } from "./style.js";
4
4
  import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
5
5
  import { basename, dirname, extname, relative, resolve } from "node:path";
6
- /** Creates a complete Program from the bundled Get Started snapshot. */
6
+ /** Creates a complete Program from the bundled Phresh Program snapshot. */
7
7
  export default async function create(options = {}, directory = process.cwd()) {
8
- const prompts = questions();
8
+ const interaction = prompts();
9
+ interaction.begin("Create Program", "Phresh Program");
10
+ const requested = options.directory ?? (interaction.interactive
11
+ ? await interaction.ask("A new directory will contain the complete Server and Client project.", "Where should the Program be created?", "my-program")
12
+ : undefined);
13
+ if (!requested)
14
+ throw new Error("create needs a <directory> when no terminal is attached");
15
+ const target = resolve(directory, requested);
16
+ const identity = basename(target);
17
+ if (!programIdentity.test(identity))
18
+ throw new Error(`The directory name must be a kebab-case Program identity; received "${identity}"`);
19
+ if (existsSync(target))
20
+ throw new Error(`The destination already exists: ${target}`);
21
+ const name = options.name ?? (interaction.interactive
22
+ ? await interaction.ask("This name is shown to people; the directory name remains the Program identity.", "What name should people see?", title(identity))
23
+ : title(identity));
24
+ if (!name.trim())
25
+ throw new Error("--name must not be empty");
26
+ const install = options.install !== false;
27
+ const detected = projectPackageManager(directory).name;
28
+ const manager = options.packageManager ?? (install && interaction.interactive
29
+ ? packageManager(await interaction.choose("The generated project remains portable; this choice installs its dependencies now.", "Which package manager should be used?", packageManagers, detected))
30
+ : detected);
31
+ const parent = dirname(target);
32
+ mkdirSync(parent, { recursive: true });
33
+ const staging = mkdtempSync(resolve(parent, `.${identity}-`));
34
+ const bundled = template();
35
+ let placed = false;
9
36
  try {
10
- const requested = options.directory ?? (prompts.interactive
11
- ? await prompts.ask("A new directory will contain the complete Server and Client project.", "Where should the Program be created?", "my-program")
12
- : undefined);
13
- if (!requested)
14
- throw new Error("create needs a <directory> when no terminal is attached");
15
- const target = resolve(directory, requested);
16
- const identity = basename(target);
17
- if (!programIdentity.test(identity))
18
- throw new Error(`The directory name must be a kebab-case Program identity; received "${identity}"`);
19
- if (existsSync(target))
20
- throw new Error(`The destination already exists: ${target}`);
21
- const name = options.name ?? (prompts.interactive
22
- ? await prompts.ask("This name is shown to people; the directory name remains the Program identity.", "What name should people see?", title(identity))
23
- : title(identity));
24
- if (!name.trim())
25
- throw new Error("--name must not be empty");
26
- const install = options.install !== false;
27
- const detected = projectPackageManager(directory).name;
28
- const manager = options.packageManager ?? (install && prompts.interactive
29
- ? packageManager(await prompts.ask("The generated project remains portable; this choice installs its dependencies now.", "Which package manager should be used?", detected))
30
- : detected);
31
- heading("Create Program", name);
32
- line("identity", identity);
33
- line("directory", target);
34
- line("template", "Get Started");
37
+ cpSync(bundled.directory, staging, { recursive: true });
38
+ renameSync(resolve(staging, "gitignore"), resolve(staging, ".gitignore"));
39
+ customize(staging, target, identity, name, manager);
40
+ renameSync(staging, target);
41
+ placed = true;
42
+ // A project inside this repository becomes a real workspace member
43
+ // only at its final path. Install there so package selection sees the
44
+ // same project boundary that subsequent commands will see.
35
45
  if (install)
36
- line("packages", manager);
37
- console.log("");
38
- const parent = dirname(target);
39
- mkdirSync(parent, { recursive: true });
40
- const staging = mkdtempSync(resolve(parent, `.${identity}-`));
41
- try {
42
- cpSync(template(), staging, { recursive: true });
43
- renameSync(resolve(staging, "gitignore"), resolve(staging, ".gitignore"));
44
- customize(staging, target, identity, name, manager);
45
- if (install)
46
- await installProjectDependencies(staging, manager);
47
- renameSync(staging, target);
48
- }
49
- catch (error) {
50
- rmSync(staging, { recursive: true, force: true });
51
- throw error;
52
- }
53
- heading(name, "created");
54
- line("directory", target);
55
- line("packages", install ? "installed" : "not installed");
56
- console.log("");
57
- console.log(` ${dim("Next:")} cd ${relative(directory, target) || "."}`);
58
- if (!install)
59
- console.log(` ${manager} install`);
60
- console.log(" phresh dev");
61
- console.log("");
46
+ await interaction.progress("Installing dependencies", "Dependencies installed", () => installProjectDependencies(target, manager, interaction.interactive ? "capture" : "inherit"));
47
+ }
48
+ catch (error) {
49
+ rmSync(placed ? target : staging, { recursive: true, force: true });
50
+ throw error;
62
51
  }
63
- finally {
64
- prompts.close();
52
+ interaction.finish("Done");
53
+ console.log(bold("\nOpen the project"));
54
+ console.log(accent(`cd ${relative(directory, target) || "."}`));
55
+ if (!install) {
56
+ console.log(bold("\nInstall dependencies"));
57
+ console.log(accent(installCommand(manager)));
65
58
  }
59
+ const script = projectScript(target, manager, bundled.development ? "dev" : "start");
60
+ console.log(bold(`\n${bundled.development ? "Run Development Program" : "Run Program"}`));
61
+ console.log(accent(script));
62
+ console.log(bold("\nYou can now open the project and start building your Program"));
66
63
  }
67
64
  function template() {
68
65
  const candidates = [
69
66
  resolve(import.meta.dirname, "template"),
70
67
  resolve(import.meta.dirname, "..", "dist", "template")
71
68
  ];
72
- const found = candidates.find(existsSync);
73
- if (!found)
74
- throw new Error("The CLI template has not been built — run its build command and try again");
75
- return found;
69
+ for (const directory of candidates) {
70
+ if (!existsSync(directory))
71
+ continue;
72
+ const descriptionPath = resolve(dirname(directory), "template.json");
73
+ if (!existsSync(descriptionPath))
74
+ throw new Error("The CLI template description has not been built — run its build command and try again");
75
+ const description = JSON.parse(readFileSync(descriptionPath, "utf-8"));
76
+ if (typeof description.development !== "boolean")
77
+ throw new Error("The CLI template description is invalid — run its build command and try again");
78
+ return { directory, development: description.development };
79
+ }
80
+ throw new Error("The CLI template has not been built — run its build command and try again");
76
81
  }
77
82
  function customize(directory, finalDirectory, identity, name, manager) {
78
83
  for (const path of textFiles(directory)) {
79
84
  let content = readFileSync(path, "utf-8");
80
- content = content.replaceAll("get-started", identity).replaceAll("Get Started", name);
85
+ content = content.replaceAll("phresh-program", identity).replaceAll("Phresh Program", name);
81
86
  if (basename(path) === "README.md") {
82
87
  content = content
83
88
  .replaceAll("bun install", `${manager} install`)
@@ -125,6 +130,10 @@ function isProjectPackage(value) {
125
130
  function title(identity) {
126
131
  return identity.split("-").map(word => word[0].toUpperCase() + word.slice(1)).join(" ");
127
132
  }
133
+ function installCommand(manager) {
134
+ return `${manager} install`;
135
+ }
128
136
  const programIdentity = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
129
137
  const textExtensions = new Set([".css", ".html", ".json", ".md", ".ts", ".tsx"]);
130
138
  const textNames = new Set([".gitignore", "gitignore"]);
139
+ const packageManagers = ["bun", "npm", "pnpm", "yarn"];
package/dist/derive.js CHANGED
@@ -27,14 +27,14 @@ export default function derive(config, directory, which) {
27
27
  name: config.name,
28
28
  version: config.version,
29
29
  description: config.description,
30
- // Like icons, the document stays where the author put it for an
30
+ // Like the icon, the document stays where the author put it for an
31
31
  // attached run. Installation and packaging give it its canonical
32
32
  // name; the runtime receives an absolute source path here because a
33
33
  // derived description has no file beside which to resolve it.
34
34
  apiDocs: config.apiDocs && resolve(directory, config.apiDocs),
35
- // Where they already are. Unlike pack, which stages them, this
36
- // points into the authoring tree and leaves it alone.
37
- icons: config.icons && resolve(directory, config.icons),
35
+ // Where it already is. Unlike pack, which gives it its canonical
36
+ // name, this points into the authoring tree and leaves it alone.
37
+ icon: config.icon && resolve(directory, config.icon),
38
38
  // Beside the source, and said out loud. A program built from an
39
39
  // object resolves what it leaves unsaid against the *system's*
40
40
  // working directory — so silence here means a program keeps what
package/dist/init.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {} from "@phreshos/core";
2
2
  import { configFile, readManifest } from "./project.js";
3
- import { dim, heading, line } from "./style.js";
3
+ import { dim } from "./style.js";
4
4
  import ensureProjectDependency, { projectScript } from "./project-dependency.js";
5
- import questions from "./questions.js";
5
+ import prompts from "./prompts.js";
6
6
  import { existsSync, writeFileSync } from "node:fs";
7
7
  import { resolve } from "node:path";
8
8
  /**
@@ -16,139 +16,130 @@ import { resolve } from "node:path";
16
16
  export default async function init(options = {}, directory = process.cwd(), coreRange = "^0.1.0") {
17
17
  const manifest = await readManifest(directory);
18
18
  const path = resolve(directory, configFile);
19
- const prompts = questions();
20
- const { interactive, ask, yes } = prompts;
19
+ const interaction = prompts();
20
+ const { interactive, ask, yes } = interaction;
21
21
  const explicitShape = options.server === true || options.client === true;
22
22
  if (existsSync(path) && options.force !== true && !interactive)
23
23
  throw new Error(`${configFile} already exists — use --force to replace it`);
24
- try {
25
- if (existsSync(path) && options.force !== true) {
26
- heading(configFile, "already initialized");
27
- if (!await yes("The existing configuration must be replaced before this project can be initialized again.", "Replace it?", false)) {
28
- console.log(` ${dim("No changes made.")}\n`);
29
- return;
30
- }
31
- }
32
- heading("Initialize Program", manifest.name);
33
- line("identity", manifest.name, "package.json");
34
- if (manifest.version)
35
- line("version", manifest.version, "package.json");
36
- if (manifest.description)
37
- line("description", manifest.description, "package.json");
38
- console.log("");
39
- let serverSelected = options.server === true;
40
- let clientSelected = options.client === true;
41
- if (!explicitShape) {
42
- if (!interactive)
43
- throw new Error("Choose at least one half with --server, --client, or both");
44
- serverSelected = await yes("A Server runs on the host machine and can keep working without an open desktop.", "Does this Program have a Server?", true);
45
- clientSelected = !serverSelected || await yes("A Client runs through a desktop and provides the Program's visual interface.", "Does this Program have a Client?", true);
24
+ interaction.begin("Initialize Program", manifest.name);
25
+ if (existsSync(path) && options.force !== true) {
26
+ interaction.detail(configFile, "already initialized");
27
+ if (!await yes("The existing configuration must be replaced before this project can be initialized again.", "Replace it?", false)) {
28
+ interaction.finish("No changes made");
29
+ return;
46
30
  }
47
- if (!serverSelected && !clientSelected)
48
- throw new Error("A Program must have a server half, a client half, or both");
49
- const name = options.name ?? (interactive ? await ask("This name is shown to people; the package name remains the Program identity.", "What name should people see?", manifest.name) : undefined);
50
- if (name !== undefined && name.trim().length === 0)
51
- throw new Error("--name must not be empty");
52
- let apiDocs = options.apiDocs;
53
- if (interactive && apiDocs === undefined) {
54
- const suggested = ["api-docs.md", "README.md"].find(file => existsSync(resolve(directory, file)));
55
- if (await yes("API documentation explains only the services this Program itself provides.", "Does this Program provide API documentation?", Boolean(suggested))) {
56
- apiDocs = await ask("The path is resolved from the project root and becomes the official API entry point.", "Where is the API documentation file?", suggested);
57
- }
58
- }
59
- if (apiDocs !== undefined && apiDocs.trim().length === 0)
60
- throw new Error("An API documentation path must not be empty");
61
- let buildCommand = options.buildCommand;
62
- if (interactive && buildCommand === undefined) {
63
- const suggested = manifest.scripts?.build && projectScript(directory, manifest.packageManager, "build");
64
- const builds = await yes("Production operations need the Program's built files.", "Build before start, install, and pack?", Boolean(suggested));
65
- if (builds)
66
- buildCommand = await ask("This command produces the production Server and Client files.", "What command builds the Program?", suggested || undefined);
31
+ }
32
+ interaction.detail("identity", manifest.name, "package.json");
33
+ if (manifest.version)
34
+ interaction.detail("version", manifest.version, "package.json");
35
+ if (manifest.description)
36
+ interaction.detail("description", manifest.description, "package.json");
37
+ let serverSelected = options.server === true;
38
+ let clientSelected = options.client === true;
39
+ if (!explicitShape) {
40
+ if (!interactive)
41
+ throw new Error("Choose at least one half with --server, --client, or both");
42
+ serverSelected = await yes("A Server runs on the host machine and can keep working without an open desktop.", "Does this Program have a Server?", true);
43
+ clientSelected = !serverSelected || await yes("A Client runs through a desktop and provides the Program's visual interface.", "Does this Program have a Client?", true);
44
+ }
45
+ if (!serverSelected && !clientSelected)
46
+ throw new Error("A Program must have a server half, a client half, or both");
47
+ const name = options.name ?? (interactive ? await ask("This name is shown to people; the package name remains the Program identity.", "What name should people see?", manifest.name) : undefined);
48
+ if (name !== undefined && name.trim().length === 0)
49
+ throw new Error("--name must not be empty");
50
+ let apiDocs = options.apiDocs;
51
+ if (interactive && apiDocs === undefined) {
52
+ const suggested = ["api-docs.md", "README.md"].find(file => existsSync(resolve(directory, file)));
53
+ if (await yes("API documentation explains only the services this Program itself provides.", "Does this Program provide API documentation?", false)) {
54
+ apiDocs = await ask("The path is resolved from the project root and becomes the official API entry point.", "Where is the API documentation file?", suggested);
67
55
  }
68
- if (buildCommand !== undefined && buildCommand.trim().length === 0)
69
- throw new Error("A build command must not be empty");
70
- let server;
71
- if (serverSelected) {
72
- const location = options.serverLocation ?? (interactive ? await ask("The system runs the production Server from this project-relative directory.", "Where are the production Server files?", "build/server") : "");
73
- const startCommand = options.serverStartCommand ?? (interactive ? await ask("This command runs inside the production Server directory.", "What command starts the production Server?", "node main.js") : "");
74
- if (!location)
75
- throw new Error("--server-location is required without a terminal");
76
- if (!startCommand)
77
- throw new Error("--server-start-command is required without a terminal");
78
- let development = options.serverDevelopmentStartCommand;
79
- if (interactive && development === undefined) {
80
- const suggested = manifest.scripts?.dev && projectScript(directory, manifest.packageManager, "dev");
81
- if (await yes("Development mode can run the Server directly from the project source.", "Run the Server from source during development?", Boolean(suggested && !clientSelected))) {
82
- development = await ask("This command remains attached to the phresh dev session.", "What command starts the development Server?", suggested || undefined);
83
- }
56
+ }
57
+ if (apiDocs !== undefined && apiDocs.trim().length === 0)
58
+ throw new Error("An API documentation path must not be empty");
59
+ let buildCommand = options.buildCommand;
60
+ if (interactive && buildCommand === undefined) {
61
+ const suggested = manifest.scripts?.build && projectScript(directory, manifest.packageManager, "build");
62
+ const builds = await yes("Production operations need the Program's built files.", "Build before start, install, and pack?", Boolean(suggested));
63
+ if (builds)
64
+ buildCommand = await ask("This command produces the production Server and Client files.", "What command builds the Program?", suggested || undefined);
65
+ }
66
+ if (buildCommand !== undefined && buildCommand.trim().length === 0)
67
+ throw new Error("A build command must not be empty");
68
+ let server;
69
+ if (serverSelected) {
70
+ const location = options.serverLocation ?? (interactive ? await ask("The system runs the production Server from this project-relative directory.", "Where are the production Server files?", clientSelected ? "dist/server" : "dist") : "");
71
+ const startCommand = options.serverStartCommand ?? (interactive ? await ask("This command runs inside the production Server directory.", "What command starts the production Server?", "node main.js") : "");
72
+ if (!location)
73
+ throw new Error("--server-location is required without a terminal");
74
+ if (!startCommand)
75
+ throw new Error("--server-start-command is required without a terminal");
76
+ let development = options.serverDevelopmentStartCommand;
77
+ if (interactive && development === undefined) {
78
+ const suggested = manifest.scripts?.dev && projectScript(directory, manifest.packageManager, "dev");
79
+ if (await yes("Development mode can run the Server directly from the project source.", "Run the Server from source during development?", Boolean(suggested && !clientSelected))) {
80
+ development = await ask("This command remains attached to the phresh dev session.", "What command starts the development Server?", suggested || undefined);
84
81
  }
85
- if (development !== undefined && development.trim().length === 0)
86
- throw new Error("A server development command must not be empty");
87
- server = { location, startCommand, ...development && { development: { startCommand: development } } };
88
82
  }
89
- let client;
90
- if (clientSelected) {
91
- const location = options.clientLocation ?? (interactive ? await ask("The system serves the production Client from this project-relative directory.", "Where are the production Client files?", "dist") : "");
92
- if (!location)
93
- throw new Error("--client-location is required without a terminal");
94
- let developmentUrl = options.clientDevelopmentUrl;
95
- let developmentStartCommand = options.clientDevelopmentStartCommand;
96
- if (interactive && developmentUrl === undefined && developmentStartCommand === undefined) {
97
- const suggested = manifest.scripts?.dev && projectScript(directory, manifest.packageManager, "dev");
98
- if (await yes("A development server can provide live updates instead of built Client files.", "Use a Client development server?", Boolean(suggested))) {
99
- developmentUrl = await ask("The desktop opens this exact HTTP or HTTPS address during phresh dev.", "What URL serves the development Client?", "http://localhost:5173/");
100
- if (await yes("The CLI can own the development server and stop it when the session ends.", "Should phresh dev start the Client server?", Boolean(suggested))) {
101
- developmentStartCommand = await ask("This command remains attached to the phresh dev session.", "What command starts the Client development server?", suggested || undefined);
102
- }
83
+ if (development !== undefined && development.trim().length === 0)
84
+ throw new Error("A server development command must not be empty");
85
+ server = { location, startCommand, ...development && { development: { startCommand: development } } };
86
+ }
87
+ let client;
88
+ if (clientSelected) {
89
+ const location = options.clientLocation ?? (interactive ? await ask("The system serves the production Client from this project-relative directory.", "Where are the production Client files?", serverSelected ? "dist/client" : "dist") : "");
90
+ if (!location)
91
+ throw new Error("--client-location is required without a terminal");
92
+ let developmentUrl = options.clientDevelopmentUrl;
93
+ let developmentStartCommand = options.clientDevelopmentStartCommand;
94
+ if (interactive && developmentUrl === undefined && developmentStartCommand === undefined) {
95
+ const suggested = manifest.scripts?.dev && projectScript(directory, manifest.packageManager, "dev");
96
+ if (await yes("A development server can provide live updates instead of built Client files.", "Use a Client development server?", Boolean(suggested))) {
97
+ developmentUrl = await ask("The desktop opens this exact HTTP or HTTPS address during phresh dev.", "What URL serves the development Client?", "http://localhost:5173/");
98
+ if (await yes("The CLI can own the development server and stop it when the session ends.", "Should phresh dev start the Client server?", Boolean(suggested))) {
99
+ developmentStartCommand = await ask("This command remains attached to the phresh dev session.", "What command starts the Client development server?", suggested || undefined);
103
100
  }
104
101
  }
105
- if (developmentStartCommand !== undefined && developmentUrl === undefined)
106
- throw new Error("--client-development-url is required with --client-development-start-command");
107
- if (developmentUrl !== undefined && developmentUrl.trim().length === 0)
108
- throw new Error("A client development URL must not be empty");
109
- if (developmentUrl !== undefined && !httpUrl(developmentUrl))
110
- throw new Error("A client development URL must use HTTP or HTTPS");
111
- if (developmentStartCommand !== undefined && developmentStartCommand.trim().length === 0)
112
- throw new Error("A client development command must not be empty");
113
- client = {
114
- location,
115
- ...developmentUrl && { development: { url: developmentUrl, ...developmentStartCommand && { startCommand: developmentStartCommand } } }
116
- };
117
102
  }
118
- const described = {
119
- identity: manifest.name,
120
- name,
121
- version: manifest.version,
122
- description: manifest.description,
123
- apiDocs,
124
- buildCommand
103
+ if (developmentStartCommand !== undefined && developmentUrl === undefined)
104
+ throw new Error("--client-development-url is required with --client-development-start-command");
105
+ if (developmentUrl !== undefined && developmentUrl.trim().length === 0)
106
+ throw new Error("A client development URL must not be empty");
107
+ if (developmentUrl !== undefined && !httpUrl(developmentUrl))
108
+ throw new Error("A client development URL must use HTTP or HTTPS");
109
+ if (developmentStartCommand !== undefined && developmentStartCommand.trim().length === 0)
110
+ throw new Error("A client development command must not be empty");
111
+ client = {
112
+ location,
113
+ ...developmentUrl && { development: { url: developmentUrl, ...developmentStartCommand && { startCommand: developmentStartCommand } } }
125
114
  };
126
- const config = server
127
- ? { ...described, server, ...client && { client } }
128
- : { ...described, client: client };
129
- await ensureProjectDependency("@phreshos/core", coreRange, directory);
130
- writeFileSync(path, compose(config));
131
- heading(configFile, "created");
132
- if (config.server)
133
- line("server", config.server.startCommand, `./${config.server.location}`);
134
- if (config.client)
135
- line("client", `./${config.client.location}`);
136
- console.log("");
137
- const next = [config.server?.development || config.client?.development ? "phresh dev" : null, "phresh start", "phresh install"].filter(Boolean);
138
- console.log(` ${dim("Next:")} ${next.join(` ${dim("or")} `)}`);
139
- if (config.client) {
140
- heading("Client requirements");
141
- line("base URL", "./", "required for production assets");
142
- if (config.client.development)
143
- line("CORS", "enabled", "required on the development server");
144
- console.log("");
145
- }
146
- else
147
- console.log("");
148
115
  }
149
- finally {
150
- prompts.close();
116
+ const described = {
117
+ identity: manifest.name,
118
+ name,
119
+ version: manifest.version,
120
+ description: manifest.description,
121
+ apiDocs,
122
+ buildCommand
123
+ };
124
+ const config = server
125
+ ? { ...described, server, ...client && { client } }
126
+ : { ...described, client: client };
127
+ await ensureProjectDependency("@phreshos/core", coreRange, directory);
128
+ writeFileSync(path, compose(config));
129
+ if (config.server)
130
+ interaction.detail("server", config.server.startCommand, `./${config.server.location}`);
131
+ if (config.client)
132
+ interaction.detail("client", `./${config.client.location}`);
133
+ const next = [config.server?.development || config.client?.development ? "phresh dev" : null, "phresh start", "phresh install"].filter(Boolean);
134
+ interaction.message();
135
+ interaction.message(`${dim("Next:")} ${next.join(` ${dim("or")} `)}`);
136
+ if (config.client) {
137
+ interaction.message();
138
+ interaction.detail("base URL", "./", "required for production assets");
139
+ if (config.client.development)
140
+ interaction.detail("CORS", "enabled", "required on the development server");
151
141
  }
142
+ interaction.finish(`${configFile} created`);
152
143
  }
153
144
  function compose(config) {
154
145
  const blocks = [
package/dist/pack.js CHANGED
@@ -17,7 +17,7 @@ import build from "./build-command.js";
17
17
  * a declared half always has a location, even when the system chose it.
18
18
  *
19
19
  * There is **no wrapping directory**. `program.json`, `server/`,
20
- * `client/` and `icons/` sit at the package's root, and the directory
20
+ * `client/` and optional `icon.png` sit at the package's root, and the directory
21
21
  * the system installs into is named from the program's own `identity`.
22
22
  * The archive itself therefore needs no second naming layer.
23
23
  *
@@ -41,11 +41,8 @@ export default async function pack(directory = process.cwd()) {
41
41
  if (!zip.getEntry("client/index.html"))
42
42
  throw new Error(`The client files have no index.html — ${config.client.location} is not where a client half is`);
43
43
  }
44
- // Icons are the program's own affair rather than either half's: a
45
- // headless program is shown too. Packaged like a half, and like a
46
- // half their place is not the program's to choose.
47
- if (config.icons)
48
- place(zip, directory, config.icons, "icons");
44
+ if (config.icon)
45
+ file(zip, directory, config.icon, "icon.png", "Program icon");
49
46
  if (config.apiDocs)
50
47
  document(zip, directory, config.apiDocs);
51
48
  zip.addFile("program.json", Buffer.from(JSON.stringify(program(config, version), null, 4) + "\n"));
@@ -64,6 +61,7 @@ function program(config, version) {
64
61
  version,
65
62
  description: config.description,
66
63
  apiDocs: config.apiDocs ? "api-docs.md" : undefined,
64
+ icon: config.icon ? "icon.png" : undefined,
67
65
  ...config.server && { server: { location: "server", start: config.server.start, installCommand: config.server.installCommand, startCommand: config.server.startCommand } },
68
66
  ...config.client && { client: { location: "client", start: config.client.start, title: config.client.title, size: config.client.size, position: config.client.position, layer: config.client.layer, minimize: config.client.minimize } }
69
67
  };
@@ -77,8 +75,11 @@ function place(zip, directory, location, half) {
77
75
  zip.addLocalFolder(from, half);
78
76
  }
79
77
  function document(zip, directory, location) {
78
+ file(zip, directory, location, "api-docs.md", "API documentation file");
79
+ }
80
+ function file(zip, directory, location, target, label) {
80
81
  const from = resolve(directory, location);
81
82
  if (!existsSync(from) || !statSync(from).isFile())
82
- throw new Error(`The API documentation file is not at ${location}`);
83
- zip.addFile("api-docs.md", readFileSync(from));
83
+ throw new Error(`The ${label} is not at ${location}`);
84
+ zip.addFile(target, readFileSync(from));
84
85
  }
@@ -1,6 +1,6 @@
1
1
  import { connect } from "node:net";
2
2
  import { homedir } from "node:os";
3
- import { join } from "node:path";
3
+ import { isAbsolute, join } from "node:path";
4
4
  /**
5
5
  * The local Program intake, from the CLI's side.
6
6
  *
@@ -19,7 +19,15 @@ import { join } from "node:path";
19
19
  * the Program says, then how it ended. None pretends to be a remote method
20
20
  * returning through an unrelated transport.
21
21
  */
22
- export const socketPath = join(homedir(), ".phreshos", "intake.sock");
22
+ export function programIntakePath(environment = process.env, userHome = homedir()) {
23
+ const instanceHome = environment.PHRESHOS_HOME;
24
+ if (instanceHome === undefined)
25
+ return join(userHome, ".phreshos", "intake.sock");
26
+ if (!isAbsolute(instanceHome))
27
+ throw new Error("PHRESHOS_HOME must be an absolute filesystem path");
28
+ return join(instanceHome, "intake.sock");
29
+ }
30
+ export const socketPath = programIntakePath();
23
31
  export default function speak(question, heard, path = socketPath, signal) {
24
32
  return new Promise(function (settle, refuse) {
25
33
  const socket = connect(path);
@@ -1,7 +1,7 @@
1
1
  import { line } from "./style.js";
2
2
  import { spawn } from "node:child_process";
3
3
  import { existsSync, readFileSync } from "node:fs";
4
- import { relative, resolve } from "node:path";
4
+ import { isAbsolute, relative, resolve } from "node:path";
5
5
  /** Returns the package-manager command that runs one project script. */
6
6
  export function projectScript(directory, declared, script) {
7
7
  return `${projectPackageManager(directory, declared).name} run ${script}`;
@@ -9,19 +9,20 @@ export function projectScript(directory, declared, script) {
9
9
  /**
10
10
  * Resolves an SDK through the one source policy shared by `init` and `create`.
11
11
  *
12
- * A CLI running from this repository uses its sibling dev-kit. A distributed
13
- * CLI has no such sibling and keeps the published range embedded in its
14
- * generated template.
12
+ * A Program inside this repository's workspace uses its sibling dev-kit. A
13
+ * standalone project or distributed CLI keeps the published range embedded
14
+ * in the generated template. Local packages therefore never leak their own
15
+ * workspace-only development graph into an unrelated project.
15
16
  */
16
17
  export function projectDependency(name, range, directory) {
17
- const workspace = manifestAt(resolve(import.meta.dirname, "..", "..", ".."));
18
+ const workspaceDirectory = resolve(import.meta.dirname, "..", "..", "..");
19
+ const workspace = manifestAt(workspaceDirectory);
18
20
  const sourceDirectory = resolve(import.meta.dirname, "..", "..", localDirectories[name]);
19
21
  const manifest = manifestAt(sourceDirectory);
20
- if (workspace?.name === "@phreshos/workspace" && manifest?.name === name && manifest.version && accepts(range, manifest.version)) {
21
- const local = relative(directory, sourceDirectory).replaceAll("\\", "/") || ".";
22
+ if (workspace?.name === "@phreshos/workspace" && workspaceIncludes(workspaceDirectory, directory, workspace.workspaces) && manifest?.name === name && manifest.version && accepts(range, manifest.version)) {
22
23
  return {
23
- installSpecifier: sourceDirectory,
24
- manifestSpecifier: `file:${local}`,
24
+ installSpecifier: `${name}@workspace:*`,
25
+ manifestSpecifier: "workspace:*",
25
26
  local: true
26
27
  };
27
28
  }
@@ -37,10 +38,10 @@ export function projectPackageManager(directory, declared, preferred) {
37
38
  return managers[named ?? "npm"];
38
39
  }
39
40
  /** Installs the dependencies already declared by a generated project. */
40
- export async function installProjectDependencies(directory, preferred) {
41
+ export async function installProjectDependencies(directory, preferred, output = "inherit") {
41
42
  const manifest = manifestAt(directory);
42
43
  const manager = projectPackageManager(directory, manifest?.packageManager, preferred);
43
- await run(manager.name, manager.installArgs, directory);
44
+ await run(manager.name, manager.installArgs, directory, output);
44
45
  return manager.name;
45
46
  }
46
47
  /**
@@ -61,19 +62,24 @@ export default async function ensureProjectDependency(name, range, directory = p
61
62
  await run(manager.name, [...manager.addArgs(section), source.installSpecifier], directory);
62
63
  console.log("");
63
64
  }
64
- function run(command, args, directory) {
65
+ function run(command, args, directory, output = "inherit") {
65
66
  return new Promise(function (settle, refuse) {
67
+ let diagnostic = "";
66
68
  const child = spawn(command, args, {
67
69
  cwd: directory,
68
- stdio: "inherit",
70
+ stdio: output === "capture" ? ["ignore", "pipe", "pipe"] : "inherit",
69
71
  shell: process.platform === "win32"
70
72
  });
73
+ if (output === "capture") {
74
+ child.stdout?.on("data", chunk => { diagnostic += String(chunk); });
75
+ child.stderr?.on("data", chunk => { diagnostic += String(chunk); });
76
+ }
71
77
  child.once("error", error => refuse(new Error(`Could not run ${command}: ${error.message}`)));
72
78
  child.once("exit", function (code, signal) {
73
79
  if (signal)
74
80
  refuse(new Error(`${command} ended on ${signal}`));
75
81
  else if (code !== 0)
76
- refuse(new Error(`${command} exited with ${code ?? 0}`));
82
+ refuse(new Error(`${command} exited with ${code ?? 0}${diagnostic.trim() ? `\n\n${diagnostic.trim()}` : ""}`));
77
83
  else
78
84
  settle();
79
85
  });
@@ -82,6 +88,18 @@ function run(command, args, directory) {
82
88
  function accepts(range, version) {
83
89
  return range === "workspace:*" || range === version || range === `^${version}` || range === `~${version}`;
84
90
  }
91
+ function workspaceIncludes(root, directory, patterns) {
92
+ const local = relative(root, resolve(directory)).replaceAll("\\", "/");
93
+ if (!local || local === ".." || local.startsWith("../") || isAbsolute(local))
94
+ return false;
95
+ return patterns?.some(function (pattern) {
96
+ if (!pattern.endsWith("/*"))
97
+ return local === pattern;
98
+ const prefix = pattern.slice(0, -1);
99
+ const child = local.startsWith(prefix) ? local.slice(prefix.length) : "";
100
+ return Boolean(child) && !child.includes("/");
101
+ }) ?? false;
102
+ }
85
103
  function manifestAt(directory) {
86
104
  const path = resolve(directory, "package.json");
87
105
  if (!existsSync(path))