@pipelex/create-method-app 0.71.1 → 0.74.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 CHANGED
@@ -16,7 +16,7 @@ The first command writes the [`webapp-js`](https://github.com/Pipelex/pipelex-sd
16
16
  ## What it needs
17
17
 
18
18
  - **Node.js** at or above the template's floor (22.13 for `webapp-js`) with its npm, and `git` unless you pass `--no-git`. The initializer runs npm itself, the one that ran it under `npm create`, or, on Windows, the one the Node installer puts beside `node.exe`; `make` is not needed.
19
- - **`PIPELEX_API_KEY`** in the environment, since a fresh copy has no `.env.local` for `npm run create` to read it from. `npm run create` runs with it and never copies it into the project: the project's `.env.local` gets an empty `PIPELEX_API_KEY` line, where you put your own key before `npm run dev`. The initializer only checks that it is set, and never prints it.
19
+ - **A key**: `PIPELEX_API_KEY` in the environment, or else the one the Pipelex login saved. A fresh copy has no `.env.local` or `.env` for `npm run create` to read it from, so these are the two sources. The login, `npx -y @pipelex/mcp@latest login` (`npx.cmd` on Windows, which Windows PowerShell 5.1 runs whatever its execution policy), saves the key as `PIPELEX_API_KEY` in `~/.pipelex/.env`, or in the `.env` of the directory `PIPELEX_HOME` names, and the Pipelex plugin's tools read the same file, so one login serves both, including in an app such as Claude's desktop app, where a variable set in a terminal window never reaches the agent. The file is read only when the environment holds no key; `PIPELEX_HOME` must be absolute or start with `~/`, and a relative one is refused rather than read. `npm run create` runs with the key and never copies it into the project: the project's `.env.local` gets an empty `PIPELEX_API_KEY` line, where you put your own key before `npm run dev`, since the app itself does not read the login's file. When the login's key is the one it ran with, `npm run create` says so in a warning the initializer repeats at the end, so put your key in `.env.local`, or start `npm run serve` from a shell that exports it, before running a method. The initializer only checks that a key is there, and never prints it; when there is none, `refused: no-key` names the login first, with the file it looked in. `npm run create` reads the login's file again itself under the same rules, which both suites hold to one table of cases, the family's `initializers/login-key.json`.
20
20
  - **A directory that is missing, empty, or holds only `.git`.** Anything else, `.DS_Store` included, is refused, and nothing is written.
21
21
 
22
22
  The package has no dependency. It carries the template inside it, packed from the family's repository at the commit it was published from, so `@X.Y.Z` writes exactly the X.Y.Z template and nothing is fetched from GitHub.
package/lib/login.mjs ADDED
@@ -0,0 +1,144 @@
1
+ /**
2
+ * The key the Pipelex login saved, read as the last source of the key.
3
+ *
4
+ * `npx -y @pipelex/mcp@latest login` saves its key as `PIPELEX_API_KEY` in
5
+ * `~/.pipelex/.env`, or in the `.env` of `PIPELEX_HOME` when that is set, and
6
+ * the Pipelex plugin's tools read that file on every call. The initializer's
7
+ * preflight reads it too, after the environment, so one login serves the tools
8
+ * and the method app. The copy's `npm run create` reads the same file under
9
+ * the same rules, which the template's `scripts/lib/create.mts` restates
10
+ * because a template may import nothing from outside its directory; both
11
+ * suites execute the family's `initializers/login-key.json` to keep the two
12
+ * readings identical, and both follow `pipelex-mcp`'s `src/credentials.ts`,
13
+ * which owns the file.
14
+ *
15
+ * The rules:
16
+ *
17
+ * - **The file is read only when the environment holds no key**, and the key
18
+ * is never copied anywhere, nor printed.
19
+ * - **Where it is**: `PIPELEX_HOME`, read from the environment alone, `~` or a
20
+ * leading `~/` (`~\` on Windows) expanded against the user's home directory;
21
+ * else `.pipelex` under the user's home directory. A `PIPELEX_HOME` that is
22
+ * neither absolute nor `~`-prefixed, `~user/…` included, is refused rather
23
+ * than resolved against the working directory, where a cloned repository
24
+ * could put a `.env` of its own.
25
+ * - **What counts as the file**: a regular file of at most
26
+ * {@link MAX_LOGIN_FILE_BYTES}, a symlink followed to its target, parsed
27
+ * with Node's own `.env` parser, a leading byte-order mark dropped.
28
+ * - **An empty value is no value**, for `PIPELEX_API_KEY`, `PIPELEX_BASE_URL`
29
+ * and `PIPELEX_HOME` alike.
30
+ * - **The file's `PIPELEX_BASE_URL` comes with the file's key, and with it
31
+ * alone**, so that a development key saved beside a development base URL is
32
+ * not sent to production. The initializer makes no request and reads it only
33
+ * for the shared cases; `npm run create` pairs it with the key.
34
+ */
35
+
36
+ import fs from "node:fs";
37
+ import os from "node:os";
38
+ import path from "node:path";
39
+ import process from "node:process";
40
+ // The module, not the named export: `parseEnv` arrived in Node 20.12, and a
41
+ // named import of it would fail to link on an older Node before the
42
+ // initializer could print its `refused: node-too-old`.
43
+ import util from "node:util";
44
+
45
+ /** The home directory's name under the user's home, when `PIPELEX_HOME` is not set. */
46
+ export const HOME_DIR_NAME = ".pipelex";
47
+
48
+ /** The file in the home directory that holds the saved key. */
49
+ export const LOGIN_FILENAME = ".env";
50
+
51
+ /** A file past this is not the credentials file, and counts as unreadable. */
52
+ export const MAX_LOGIN_FILE_BYTES = 1024 * 1024;
53
+
54
+ /**
55
+ * The command that saves a key, as the person types it on this system: on
56
+ * Windows, `npx.cmd`, which runs in Windows PowerShell 5.1 whatever its
57
+ * execution policy, where a typed `npx` may be refused.
58
+ */
59
+ export function loginCommand(platform = process.platform) {
60
+ return `${platform === "win32" ? "npx.cmd" : "npx"} -y @pipelex/mcp@latest login`;
61
+ }
62
+
63
+ /**
64
+ * The directory that holds the login's file, or `null` for a `PIPELEX_HOME`
65
+ * this reader refuses.
66
+ */
67
+ export function loginHome(env, homedir = os.homedir) {
68
+ const configured = env.PIPELEX_HOME;
69
+ if (!configured) return path.join(homedir(), HOME_DIR_NAME);
70
+ if (configured === "~") return path.normalize(homedir());
71
+ if (configured.startsWith("~/") || (path.sep === "\\" && configured.startsWith("~\\"))) {
72
+ return path.join(homedir(), configured.slice(2));
73
+ }
74
+ if (path.isAbsolute(configured)) return path.normalize(configured);
75
+ return null;
76
+ }
77
+
78
+ /**
79
+ * What reading the login's file found. Never throws.
80
+ *
81
+ * { status: "key", file, key, baseUrl } a key, with the file's base URL or undefined
82
+ * { status: "no-key", file } a readable file that holds no key
83
+ * { status: "missing", file } no file there
84
+ * { status: "unreadable", file } a file that cannot be read as the credentials file
85
+ * { status: "relative-home" } a PIPELEX_HOME this reader refuses
86
+ */
87
+ export function readLoginKey(env, { homedir = os.homedir } = {}) {
88
+ let home;
89
+ try {
90
+ home = loginHome(env, homedir);
91
+ } catch {
92
+ // `os.homedir()` throws only when the user has no home directory at all.
93
+ return { status: "unreadable", file: path.join("~", HOME_DIR_NAME, LOGIN_FILENAME) };
94
+ }
95
+ if (home === null) return { status: "relative-home" };
96
+ const file = path.join(home, LOGIN_FILENAME);
97
+ let text;
98
+ try {
99
+ const stats = fs.statSync(file);
100
+ if (!stats.isFile() || stats.size > MAX_LOGIN_FILE_BYTES) return { status: "unreadable", file };
101
+ text = fs.readFileSync(file, "utf8");
102
+ } catch (error) {
103
+ return { status: error?.code === "ENOENT" ? "missing" : "unreadable", file };
104
+ }
105
+ let values;
106
+ try {
107
+ values = util.parseEnv(text.replace(/^\uFEFF/, ""));
108
+ } catch {
109
+ return { status: "unreadable", file };
110
+ }
111
+ const key = values.PIPELEX_API_KEY || undefined;
112
+ if (key === undefined) return { status: "no-key", file };
113
+ return { status: "key", file, key, baseUrl: values.PIPELEX_BASE_URL || undefined };
114
+ }
115
+
116
+ /** A path as a message shows it: `~/…` under the user's home directory, absolute otherwise. */
117
+ export function displayPath(file, homedir = os.homedir) {
118
+ try {
119
+ const relative = path.relative(homedir(), file);
120
+ const outside = relative === ".." || relative.startsWith(`..${path.sep}`);
121
+ if (relative !== "" && !outside && !path.isAbsolute(relative)) return `~${path.sep}${relative}`;
122
+ } catch {
123
+ // No home directory to abbreviate against.
124
+ }
125
+ return file;
126
+ }
127
+
128
+ /**
129
+ * Why no key was found, for the `refused: no-key` verdict: the login first,
130
+ * then the shell, then writing `.env.local` by hand.
131
+ */
132
+ export function noKeyDetail(read, { homedir = os.homedir, platform = process.platform } = {}) {
133
+ const login = loginCommand(platform);
134
+ const rest =
135
+ "export PIPELEX_API_KEY in this shell, or pass --no-create and write .env.local yourself before running npm run create; npm run create never copies the key into the project";
136
+ if (read.status === "relative-home") {
137
+ return `PIPELEX_API_KEY is not set, and the login's saved key is not read because PIPELEX_HOME is neither an absolute path nor one starting with ~/: fix PIPELEX_HOME and run ${login}, ${rest}`;
138
+ }
139
+ const shown = displayPath(read.file, homedir);
140
+ if (read.status === "unreadable") {
141
+ return `PIPELEX_API_KEY is not set, and ${shown}, where the login saves it, cannot be read: make it a file you can read and run ${login}, ${rest}`;
142
+ }
143
+ return `PIPELEX_API_KEY is not set, and no key is saved in ${shown}: run ${login} to save one there, ${rest}`;
144
+ }
package/lib/main.mjs CHANGED
@@ -8,9 +8,10 @@
8
8
  *
9
9
  * 1. **A preflight that leaves nothing behind.** The arguments and the
10
10
  * template; Node at or above the template's `engines` floor; npm unless
11
- * `--no-create`, and `git` unless `--no-git`; `--method` and
12
- * `PIPELEX_API_KEY` unless `--no-create`, the key tested for presence and
13
- * never printed; the destination rule; git's reading of the destination,
11
+ * `--no-create`, and `git` unless `--no-git`; `--method` and a key
12
+ * unless `--no-create`, `PIPELEX_API_KEY` from the environment or else the
13
+ * one the Pipelex login saved (`login.mjs`), tested for presence and never
14
+ * printed; the destination rule; git's reading of the destination,
14
15
  * and an identity when a commit will be made. It writes nothing but a
15
16
  * missing destination inside another repository's work tree, made so that
16
17
  * git reads it as the directory it will be, and the throwaway repository
@@ -47,6 +48,7 @@ import {
47
48
  readGit,
48
49
  templateOrigin,
49
50
  } from "./git.mjs";
51
+ import { noKeyDetail, readLoginKey } from "./login.mjs";
50
52
  import {
51
53
  createWords,
52
54
  extractWarnings,
@@ -120,9 +122,11 @@ export function resolveDeps(deps = {}) {
120
122
  return {
121
123
  cwd: deps.cwd ?? process.cwd(),
122
124
  env,
125
+ homedir: deps.homedir ?? os.homedir,
123
126
  npm: deps.npm ?? npmInvocation(env),
124
127
  out: deps.out ?? process.stdout,
125
128
  packDir: deps.packDir ?? PACKS_DIR,
129
+ platform: deps.platform ?? process.platform,
126
130
  nodeVersion: deps.nodeVersion ?? process.versions.node,
127
131
  signal: deps.signal,
128
132
  tmpDir: deps.tmpDir ?? os.tmpdir(),
@@ -300,10 +304,16 @@ async function create(argv, d, say, state) {
300
304
  );
301
305
  }
302
306
  if (!args.noCreate && !d.env.PIPELEX_API_KEY?.trim()) {
303
- throw Verdict.refused(
304
- "no-key",
305
- "PIPELEX_API_KEY is not set: export it in this shell for npm run create, which never copies it into the project, or pass --no-create and write .env.local yourself before running npm run create",
306
- );
307
+ // The copy is new, so it has no .env.local or .env: after the environment,
308
+ // the login's file is the one source left, and npm run create reads it
309
+ // again itself, under the same rules.
310
+ const login = readLoginKey(d.env, { homedir: d.homedir });
311
+ if (login.status !== "key") {
312
+ throw Verdict.refused(
313
+ "no-key",
314
+ noKeyDetail(login, { homedir: d.homedir, platform: d.platform }),
315
+ );
316
+ }
307
317
  }
308
318
 
309
319
  const dest = path.resolve(d.cwd, args.dir);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipelex/create-method-app",
3
- "version": "0.71.1",
3
+ "version": "0.74.0",
4
4
  "description": "Start a Pipelex method app: npm create @pipelex/method-app@latest my-app -- --method <bundle | mt_… | address>",
5
5
  "keywords": [
6
6
  "pipelex",
Binary file