@pipelex/create-method-app 0.71.1 → 0.74.1
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 +24 -24
- package/lib/login.mjs +146 -0
- package/lib/main.mjs +130 -15
- package/lib/verdict.mjs +15 -1
- package/package.json +1 -1
- package/templates/webapp-js.pack +0 -0
package/README.md
CHANGED
|
@@ -9,31 +9,31 @@ cd my-app
|
|
|
9
9
|
npm run serve
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
The first command writes the [`webapp-js`](https://github.com/Pipelex/pipelex-sdk/tree/main/method-apps/webapp-js) template of the [method-app family](https://github.com/Pipelex/pipelex-sdk/tree/main/method-apps), the `method-apps/` directory of `Pipelex/pipelex-sdk`, into `my-app/`, commits it as it came, and runs the copy's own `npm run create`, after the `npm install` it needs, which scaffolds the method, names the project after it and runs `npm run all`. The second starts the dev server in the background, proves that the page answers, and prints its URL; `npm run serve -- stop` stops it. `npm run dev` runs the same server in the foreground instead. None of it needs `make` or a POSIX shell, so the same commands run on macOS, Linux and Windows, in PowerShell and `cmd` as in any POSIX shell.
|
|
12
|
+
The first command writes the [`webapp-js`](https://github.com/Pipelex/pipelex-sdk/tree/main/method-apps/webapp-js) template of the [method-app family](https://github.com/Pipelex/pipelex-sdk/tree/main/method-apps), the `method-apps/` directory of `Pipelex/pipelex-sdk`, into `my-app/`, commits it as it came, and runs the copy's own `npm run create`, after the `npm install` it needs, which scaffolds the method, names the project after it and runs `npm run all`. The second starts the dev server in the background, proves that the page answers, and prints its URL; `npm run serve -- stop` stops it. `npm run dev` runs the same server in the foreground instead. None of it needs `make` or a POSIX shell, so the same commands run on macOS, Linux and Windows, in PowerShell and `cmd` as in any POSIX shell. In Windows PowerShell 5.1, whose default execution policy refuses a typed `npm`, type `npm.cmd` in its place, as in `npm.cmd create @pipelex/method-app@latest` and `npm.cmd run serve`, which run in every Windows shell; on Windows, the next steps a verdict names are spelled that way.
|
|
13
13
|
|
|
14
14
|
`--method` takes a `.mthds` file or a directory of them, a method id from your organization's catalog (`mt_…`, or `mt_…@<version>` and `mt_…@draft` to pin a version or run the draft), or a published package address (`github.com/Pipelex/methods/text_stats@v0.2.0`). A path is read from where you typed the command.
|
|
15
15
|
|
|
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
|
-
-
|
|
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, for a key of your own. While that line stays empty and your shell exports no key, the app's local dev server, `npm run dev` or `npm run serve`, reads the login's file again when it starts and runs on that key; a production build (`npm run build`, then `npm run start`) and a deployment never read it. 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: the dev server runs on the login's key, and production needs a `PIPELEX_API_KEY` of its own. 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.
|
|
23
23
|
|
|
24
24
|
## Options
|
|
25
25
|
|
|
26
|
-
| Option | What it does
|
|
27
|
-
| -------------------------------------------------------------------------------------------------- |
|
|
28
|
-
| `--method <m>` | The method the app runs. Required unless `--no-create`.
|
|
29
|
-
| `--name`, `--title`, `--description` | The project's package name, title and description, which `npm run create` otherwise derives from the method.
|
|
30
|
-
| `--pipe`, `--method-name`, `--label` | The pipe to run, the method's directory name and its tab label, when the method does not settle them.
|
|
31
|
-
| `--author-name`, `--author-email`, `--repo-url`, `--license`, `--license-holder`, `--license-year` | What the project says about who made it. Nothing is invented.
|
|
32
|
-
| `--dry-run` | `npm run create` plans and prints the identity without changing a file. The copy and
|
|
33
|
-
| `--no-create` | Write the template and commit it, then stop. The verdict names the `npm install` and `npm run create` to run, after writing `.env.local` yourself, for instance.
|
|
34
|
-
| `--no-git` | Make no repository and no commit.
|
|
35
|
-
| `--quiet` | Write the output of `npm install` and `npm run create` to a log and print its path, so the summary stays in view.
|
|
36
|
-
| `--template <name>` | The template to write. `webapp-js` is the default and the only one.
|
|
26
|
+
| Option | What it does |
|
|
27
|
+
| -------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
28
|
+
| `--method <m>` | The method the app runs. Required unless `--no-create`. |
|
|
29
|
+
| `--name`, `--title`, `--description` | The project's package name, title and description, which `npm run create` otherwise derives from the method. |
|
|
30
|
+
| `--pipe`, `--method-name`, `--label` | The pipe to run, the method's directory name and its tab label, when the method does not settle them. |
|
|
31
|
+
| `--author-name`, `--author-email`, `--repo-url`, `--license`, `--license-holder`, `--license-year` | What the project says about who made it. Nothing is invented. |
|
|
32
|
+
| `--dry-run` | Only `npm run create` is dry: it plans and prints the identity without changing a file. The copy, the dependencies `npm install` wrote into `node_modules` and the pristine commit stand, and the verdict says so, names the commands that discard them all and leave the destination as it stood, and names the real `npm run create` to run. |
|
|
33
|
+
| `--no-create` | Write the template and commit it, then stop. The verdict names the `npm install` and `npm run create` to run, after writing `.env.local` yourself, for instance. |
|
|
34
|
+
| `--no-git` | Make no repository and no commit. |
|
|
35
|
+
| `--quiet` | Write the output of `npm install` and `npm run create` to a log and print its path, so the summary stays in view. |
|
|
36
|
+
| `--template <name>` | The template to write. `webapp-js` is the default and the only one. |
|
|
37
37
|
|
|
38
38
|
Each create option is named after the variable of the family's create contract it sets (`--author-email` is `AUTHOR_EMAIL`), which is also the flag of the template's own `npm run create`, and reaches it as one argument, `--method` as the first, so no value needs quoting beyond what your shell needs. A blank value is not passed on. Nothing is ever asked: a missing value is a refusal naming the option.
|
|
39
39
|
|
|
@@ -61,17 +61,17 @@ The pristine commit reads `Start from Pipelex/pipelex-sdk/method-apps/webapp-js
|
|
|
61
61
|
|
|
62
62
|
A run ends with one verdict line, the last line it prints on standard output, whose first word is stable:
|
|
63
63
|
|
|
64
|
-
| Verdict | Meaning
|
|
65
|
-
| --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
66
|
-
| ``created <dir>; next: `cd <dir>`, then `npm run serve`
|
|
67
|
-
| `copied <dir>; next: …` | With `--no-create` or `--dry-run`: the copy and the git outcome, and the `npm run create` to run next, after `npm install` when it has not run
|
|
68
|
-
| `refused: not-empty` | The destination holds something other than a lone `.git`; nothing was written
|
|
69
|
-
| `refused: unusable-destination` | The destination cannot be read: a file stands where a directory of its path should be, or a directory on it cannot be read; nothing was written
|
|
70
|
-
| `refused: repository-has-history`, `refused: repository-has-staged-files`, `refused: inside-template-checkout` | The git reading above; nothing was written
|
|
71
|
-
| `refused: no-method`, `no-key`, `missing-tool`, `node-too-old`, `no-git-identity`, `unknown-option`, `unknown-template`, `other-ecosystem`, `usage` | The preflight; nothing was written, and the line names the fix
|
|
72
|
-
| `failed: write` | The copy failed or was interrupted, and everything the initializer had created was removed; nothing else was touched
|
|
73
|
-
| `failed: commit` | Git refused the pristine commit, for instance through a global hook; the copy stands, and the line says how to commit it and what to run next
|
|
74
|
-
| `failed: create` | `npm install` or `npm run create` did not succeed; the copy and its commit stand. After a failed install, the line names both commands to run once npm's message is dealt with; after a failed `npm run create`, its own message says what to run next: a refusal before it wrote anything can be fixed and run again, and a failure after it cannot
|
|
64
|
+
| Verdict | Meaning |
|
|
65
|
+
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
66
|
+
| ``created <dir>; next: `cd <dir>`, then `npm run serve` ``, with `npm.cmd` for `npm` on Windows | The copy, the git outcome, and a green `npm run create` |
|
|
67
|
+
| `copied <dir>; next: …` | With `--no-create` or `--dry-run`: the copy and the git outcome, and the `npm run create` to run next, after `npm install` when it has not run. After `--dry-run`, in parentheses before `next:`, what stands (its files, the dependencies `npm install` wrote into `node_modules`, and the pristine commit when one was made) and the commands that discard it and leave the destination as it stood, the same in every shell: `node -e "fs.rmSync(…)" <dir>` from outside a directory the run made, everything in it for an empty directory that was there, and for a lone `.git` everything but `.git`, then `git update-ref -d HEAD` and `git read-tree --empty` when the pristine commit went into that repository, and `git config --unset core.hooksPath .husky/_`, the hooks path the template's husky set; a missing destination is deleted from the highest directory the run created on the way to it |
|
|
68
|
+
| `refused: not-empty` | The destination holds something other than a lone `.git`; nothing was written |
|
|
69
|
+
| `refused: unusable-destination` | The destination cannot be read: a file stands where a directory of its path should be, or a directory on it cannot be read; nothing was written |
|
|
70
|
+
| `refused: repository-has-history`, `refused: repository-has-staged-files`, `refused: inside-template-checkout` | The git reading above; nothing was written |
|
|
71
|
+
| `refused: no-method`, `no-key`, `missing-tool`, `node-too-old`, `no-git-identity`, `unknown-option`, `unknown-template`, `other-ecosystem`, `usage` | The preflight; nothing was written, and the line names the fix |
|
|
72
|
+
| `failed: write` | The copy failed or was interrupted, and everything the initializer had created was removed; nothing else was touched |
|
|
73
|
+
| `failed: commit` | Git refused the pristine commit, for instance through a global hook; the copy stands, and the line says how to commit it and what to run next |
|
|
74
|
+
| `failed: create` | `npm install` or `npm run create` did not succeed; the copy and its commit stand. After a failed install, the line names both commands to run once npm's message is dealt with; after a failed `npm run create`, its own message says what to run next: a refusal before it wrote anything can be fixed and run again, and a failure after it cannot |
|
|
75
75
|
|
|
76
76
|
Before the verdict come the warnings `npm run create` printed, each once, and the git outcome. A `next:` names its commands one after the other, each between backquotes, to run in turn: no separator reads the same in every shell, since Windows PowerShell 5.1 has no `&&` and `cmd` no `;`. Each word is quoted for the system the initializer ran on: in single quotes for a POSIX shell, and on Windows in double quotes, which PowerShell and `cmd` both read, or in PowerShell's single quotes for a word holding a `$` or a backquote. The exit code is 0 for `created` and `copied` and 1 otherwise, but the verdict is the line to read.
|
|
77
77
|
|
package/lib/login.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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`, and its local dev server,
|
|
9
|
+
* read the same file under the same rules, which the template's
|
|
10
|
+
* `scripts/lib/login.mts` restates because a template may import nothing from
|
|
11
|
+
* outside its directory; both
|
|
12
|
+
* suites execute the family's `initializers/login-key.json` to keep the two
|
|
13
|
+
* readings identical, and both follow `pipelex-mcp`'s `src/credentials.ts`,
|
|
14
|
+
* which owns the file.
|
|
15
|
+
*
|
|
16
|
+
* The rules:
|
|
17
|
+
*
|
|
18
|
+
* - **The file is read only when the environment holds no key**, and the key
|
|
19
|
+
* is never copied anywhere, nor printed.
|
|
20
|
+
* - **Where it is**: `PIPELEX_HOME`, read from the environment alone, `~` or a
|
|
21
|
+
* leading `~/` (`~\` on Windows) expanded against the user's home directory;
|
|
22
|
+
* else `.pipelex` under the user's home directory. A `PIPELEX_HOME` that is
|
|
23
|
+
* neither absolute nor `~`-prefixed, `~user/…` included, is refused rather
|
|
24
|
+
* than resolved against the working directory, where a cloned repository
|
|
25
|
+
* could put a `.env` of its own.
|
|
26
|
+
* - **What counts as the file**: a regular file of at most
|
|
27
|
+
* {@link MAX_LOGIN_FILE_BYTES}, a symlink followed to its target, parsed
|
|
28
|
+
* with Node's own `.env` parser, a leading byte-order mark dropped.
|
|
29
|
+
* - **An empty value is no value**, for `PIPELEX_API_KEY`, `PIPELEX_BASE_URL`
|
|
30
|
+
* and `PIPELEX_HOME` alike.
|
|
31
|
+
* - **The file's `PIPELEX_BASE_URL` comes with the file's key, and with it
|
|
32
|
+
* alone**, so that a development key saved beside a development base URL is
|
|
33
|
+
* not sent to production. The initializer makes no request and reads it only
|
|
34
|
+
* for the shared cases; `npm run create` pairs it with the key.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import fs from "node:fs";
|
|
38
|
+
import os from "node:os";
|
|
39
|
+
import path from "node:path";
|
|
40
|
+
import process from "node:process";
|
|
41
|
+
// The module, not the named export: `parseEnv` arrived in Node 20.12, and a
|
|
42
|
+
// named import of it would fail to link on an older Node before the
|
|
43
|
+
// initializer could print its `refused: node-too-old`.
|
|
44
|
+
import util from "node:util";
|
|
45
|
+
|
|
46
|
+
import { npmCommand } from "./verdict.mjs";
|
|
47
|
+
|
|
48
|
+
/** The home directory's name under the user's home, when `PIPELEX_HOME` is not set. */
|
|
49
|
+
export const HOME_DIR_NAME = ".pipelex";
|
|
50
|
+
|
|
51
|
+
/** The file in the home directory that holds the saved key. */
|
|
52
|
+
export const LOGIN_FILENAME = ".env";
|
|
53
|
+
|
|
54
|
+
/** A file past this is not the credentials file, and counts as unreadable. */
|
|
55
|
+
export const MAX_LOGIN_FILE_BYTES = 1024 * 1024;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The command that saves a key, as the person types it on this system: on
|
|
59
|
+
* Windows, `npx.cmd`, which runs in Windows PowerShell 5.1 whatever its
|
|
60
|
+
* execution policy, where a typed `npx` may be refused.
|
|
61
|
+
*/
|
|
62
|
+
export function loginCommand(platform = process.platform) {
|
|
63
|
+
return `${platform === "win32" ? "npx.cmd" : "npx"} -y @pipelex/mcp@latest login`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The directory that holds the login's file, or `null` for a `PIPELEX_HOME`
|
|
68
|
+
* this reader refuses.
|
|
69
|
+
*/
|
|
70
|
+
export function loginHome(env, homedir = os.homedir) {
|
|
71
|
+
const configured = env.PIPELEX_HOME;
|
|
72
|
+
if (!configured) return path.join(homedir(), HOME_DIR_NAME);
|
|
73
|
+
if (configured === "~") return path.normalize(homedir());
|
|
74
|
+
if (configured.startsWith("~/") || (path.sep === "\\" && configured.startsWith("~\\"))) {
|
|
75
|
+
return path.join(homedir(), configured.slice(2));
|
|
76
|
+
}
|
|
77
|
+
if (path.isAbsolute(configured)) return path.normalize(configured);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What reading the login's file found. Never throws.
|
|
83
|
+
*
|
|
84
|
+
* { status: "key", file, key, baseUrl } a key, with the file's base URL or undefined
|
|
85
|
+
* { status: "no-key", file } a readable file that holds no key
|
|
86
|
+
* { status: "missing", file } no file there
|
|
87
|
+
* { status: "unreadable", file } a file that cannot be read as the credentials file
|
|
88
|
+
* { status: "relative-home" } a PIPELEX_HOME this reader refuses
|
|
89
|
+
*/
|
|
90
|
+
export function readLoginKey(env, { homedir = os.homedir } = {}) {
|
|
91
|
+
let home;
|
|
92
|
+
try {
|
|
93
|
+
home = loginHome(env, homedir);
|
|
94
|
+
} catch {
|
|
95
|
+
// `os.homedir()` throws only when the user has no home directory at all.
|
|
96
|
+
return { status: "unreadable", file: path.join("~", HOME_DIR_NAME, LOGIN_FILENAME) };
|
|
97
|
+
}
|
|
98
|
+
if (home === null) return { status: "relative-home" };
|
|
99
|
+
const file = path.join(home, LOGIN_FILENAME);
|
|
100
|
+
let text;
|
|
101
|
+
try {
|
|
102
|
+
const stats = fs.statSync(file);
|
|
103
|
+
if (!stats.isFile() || stats.size > MAX_LOGIN_FILE_BYTES) return { status: "unreadable", file };
|
|
104
|
+
text = fs.readFileSync(file, "utf8");
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return { status: error?.code === "ENOENT" ? "missing" : "unreadable", file };
|
|
107
|
+
}
|
|
108
|
+
let values;
|
|
109
|
+
try {
|
|
110
|
+
values = util.parseEnv(text.replace(/^\uFEFF/, ""));
|
|
111
|
+
} catch {
|
|
112
|
+
return { status: "unreadable", file };
|
|
113
|
+
}
|
|
114
|
+
const key = values.PIPELEX_API_KEY || undefined;
|
|
115
|
+
if (key === undefined) return { status: "no-key", file };
|
|
116
|
+
return { status: "key", file, key, baseUrl: values.PIPELEX_BASE_URL || undefined };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A path as a message shows it: `~/…` under the user's home directory, absolute otherwise. */
|
|
120
|
+
export function displayPath(file, homedir = os.homedir) {
|
|
121
|
+
try {
|
|
122
|
+
const relative = path.relative(homedir(), file);
|
|
123
|
+
const outside = relative === ".." || relative.startsWith(`..${path.sep}`);
|
|
124
|
+
if (relative !== "" && !outside && !path.isAbsolute(relative)) return `~${path.sep}${relative}`;
|
|
125
|
+
} catch {
|
|
126
|
+
// No home directory to abbreviate against.
|
|
127
|
+
}
|
|
128
|
+
return file;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Why no key was found, for the `refused: no-key` verdict: the login first,
|
|
133
|
+
* then the shell, then writing `.env.local` by hand.
|
|
134
|
+
*/
|
|
135
|
+
export function noKeyDetail(read, { homedir = os.homedir, platform = process.platform } = {}) {
|
|
136
|
+
const login = loginCommand(platform);
|
|
137
|
+
const rest = `export PIPELEX_API_KEY in this shell, or pass --no-create and write .env.local yourself before running ${npmCommand(platform)} run create; npm run create never copies the key into the project`;
|
|
138
|
+
if (read.status === "relative-home") {
|
|
139
|
+
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}`;
|
|
140
|
+
}
|
|
141
|
+
const shown = displayPath(read.file, homedir);
|
|
142
|
+
if (read.status === "unreadable") {
|
|
143
|
+
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}`;
|
|
144
|
+
}
|
|
145
|
+
return `PIPELEX_API_KEY is not set, and no key is saved in ${shown}: run ${login} to save one there, ${rest}`;
|
|
146
|
+
}
|
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
|
-
*
|
|
13
|
-
*
|
|
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,
|
|
@@ -58,7 +60,7 @@ import {
|
|
|
58
60
|
} from "./npm.mjs";
|
|
59
61
|
import { decodePack, PackError } from "./pack.mjs";
|
|
60
62
|
import { loadTable, PACKAGE_ROOT } from "./templates.mjs";
|
|
61
|
-
import { EXIT_OK, inTurn, shellQuote, Verdict } from "./verdict.mjs";
|
|
63
|
+
import { EXIT_OK, inTurn, npmCommand, shellQuote, Verdict } from "./verdict.mjs";
|
|
62
64
|
import { DestinationChanged, Interrupted, writeTree } from "./write.mjs";
|
|
63
65
|
|
|
64
66
|
export const PACKS_DIR = path.join(PACKAGE_ROOT, "templates");
|
|
@@ -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(),
|
|
@@ -248,9 +252,16 @@ function planGit(args, dest, found, env) {
|
|
|
248
252
|
/**
|
|
249
253
|
* The commands to run next, without --dry-run, quoted for this system's shell
|
|
250
254
|
* and in order: `cd` into the copy, then `npm run create`, after the
|
|
251
|
-
* `npm install` it needs unless `installed
|
|
255
|
+
* `npm install` it needs unless `installed`, npm spelled as a person types it
|
|
256
|
+
* there (`npmCommand`).
|
|
252
257
|
*/
|
|
253
|
-
function nextCreate(
|
|
258
|
+
export function nextCreate(
|
|
259
|
+
dest,
|
|
260
|
+
table,
|
|
261
|
+
template,
|
|
262
|
+
values,
|
|
263
|
+
{ installed = false, platform = process.platform } = {},
|
|
264
|
+
) {
|
|
254
265
|
const forwarded = { ...values };
|
|
255
266
|
delete forwarded.DRY_RUN;
|
|
256
267
|
const words = createWords(
|
|
@@ -258,10 +269,106 @@ function nextCreate(dest, table, template, values, { installed = false } = {}) {
|
|
|
258
269
|
forwarded,
|
|
259
270
|
table.switches,
|
|
260
271
|
table.required,
|
|
261
|
-
).map((word) => shellQuote(word));
|
|
272
|
+
).map((word) => shellQuote(word, platform));
|
|
262
273
|
if (values[table.required] === undefined) words.unshift("<method>");
|
|
263
|
-
const
|
|
264
|
-
|
|
274
|
+
const npm = npmCommand(platform);
|
|
275
|
+
const create = `${npm} run create -- ${words.join(" ")}`;
|
|
276
|
+
return inTurn([
|
|
277
|
+
`cd ${shellQuote(dest, platform)}`,
|
|
278
|
+
...(installed ? [] : [`${npm} install`]),
|
|
279
|
+
create,
|
|
280
|
+
]);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The highest of `dest` and its ancestors that does not exist yet. */
|
|
284
|
+
function highestMissing(dest) {
|
|
285
|
+
const existing = nearestExisting(dest);
|
|
286
|
+
return path.join(existing, path.relative(existing, dest).split(path.sep)[0]);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Node's recursive removal of `process.argv[1]`, or of one entry in it, as a script for `node -e`. */
|
|
290
|
+
const REMOVE = (target) => `fs.rmSync(${target}, { recursive: true, force: true })`;
|
|
291
|
+
const IN_DEST = "path.join(process.argv[1], entry)";
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The commands that discard what a dry run left and put the destination back
|
|
295
|
+
* as it stood before, the same on every system: Node, which every run of the
|
|
296
|
+
* initializer has, and no `rm -rf`, which is no PowerShell command, and git
|
|
297
|
+
* for the one repository that was there before. What was there decides them
|
|
298
|
+
* (`existed`, `readDestination`'s kind):
|
|
299
|
+
*
|
|
300
|
+
* - `missing`: the directory, deleted whole from outside it, since Windows
|
|
301
|
+
* refuses to delete the directory a process stands in; `made`, the
|
|
302
|
+
* highest directory the run created on the way to it, is the one deleted,
|
|
303
|
+
* so no empty parent stays behind;
|
|
304
|
+
* - `empty`: everything in it, a repository the run made included, the
|
|
305
|
+
* directory itself kept;
|
|
306
|
+
* - `lone-git`: everything in it but `.git`, and, when the pristine commit
|
|
307
|
+
* went into that repository, the commit and the index it recorded, which
|
|
308
|
+
* leaves the repository with no commit and nothing staged, as it was; and
|
|
309
|
+
* the hooks path the template's husky set in that repository's config when
|
|
310
|
+
* npm install ran, which would otherwise disable its own hooks.
|
|
311
|
+
*
|
|
312
|
+
* No script holds a `!`, which an interactive bash would expand, nor a `$` or
|
|
313
|
+
* a backtick, which PowerShell would.
|
|
314
|
+
*/
|
|
315
|
+
export function discardCommands({
|
|
316
|
+
dest,
|
|
317
|
+
made = dest,
|
|
318
|
+
existed,
|
|
319
|
+
committed,
|
|
320
|
+
platform = process.platform,
|
|
321
|
+
}) {
|
|
322
|
+
const quoted = shellQuote(dest, platform);
|
|
323
|
+
if (existed === "missing") {
|
|
324
|
+
return [`node -e "${REMOVE("process.argv[1]")}" ${shellQuote(made, platform)}`];
|
|
325
|
+
}
|
|
326
|
+
const keep = existed === "lone-git" ? "entry === '.git' || " : "";
|
|
327
|
+
const commands = [
|
|
328
|
+
`node -e "for (const entry of fs.readdirSync(process.argv[1])) ${keep}${REMOVE(IN_DEST)}" ${quoted}`,
|
|
329
|
+
];
|
|
330
|
+
if (existed === "lone-git" && committed) {
|
|
331
|
+
commands.push(`git -C ${quoted} update-ref -d HEAD`, `git -C ${quoted} read-tree --empty`);
|
|
332
|
+
}
|
|
333
|
+
if (existed === "lone-git") {
|
|
334
|
+
commands.push(
|
|
335
|
+
`git -C ${quoted} config --unset core.hooksPath ${shellQuote(".husky/_", platform)}`,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
return commands;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* What a dry run's `copied` verdict says between its parentheses: that only
|
|
343
|
+
* `npm run create` was dry, what stands (the files, the dependencies npm
|
|
344
|
+
* install wrote, and the pristine commit when one was made), and the commands
|
|
345
|
+
* that discard it all and leave the destination as it was (`discardCommands`).
|
|
346
|
+
*/
|
|
347
|
+
export function dryRunStands({
|
|
348
|
+
dest,
|
|
349
|
+
made = dest,
|
|
350
|
+
existed,
|
|
351
|
+
committed,
|
|
352
|
+
platform = process.platform,
|
|
353
|
+
}) {
|
|
354
|
+
const commit =
|
|
355
|
+
existed === "lone-git"
|
|
356
|
+
? `, and the pristine commit, the first in the repository ${shellQuote(dest, platform)} held`
|
|
357
|
+
: ", and the pristine commit";
|
|
358
|
+
const stands = committed
|
|
359
|
+
? `its files, the dependencies npm install wrote into node_modules${commit}`
|
|
360
|
+
: "its files and the dependencies npm install wrote into node_modules";
|
|
361
|
+
const commands = inTurn(discardCommands({ dest, made, existed, committed, platform }));
|
|
362
|
+
const how =
|
|
363
|
+
existed === "missing"
|
|
364
|
+
? `to discard it, run ${commands} from outside ${shellQuote(made, platform)}`
|
|
365
|
+
: `to discard it and leave ${shellQuote(dest, platform)} as it was before, run ${commands}`;
|
|
366
|
+
return `only npm run create was dry and changed nothing; the copy stands, with ${stands}; ${how}`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** The commands to run once the app is created: `cd` into it, then `npm run serve`. */
|
|
370
|
+
export function nextServe(dest, platform = process.platform) {
|
|
371
|
+
return inTurn([`cd ${shellQuote(dest, platform)}`, `${npmCommand(platform)} run serve`]);
|
|
265
372
|
}
|
|
266
373
|
|
|
267
374
|
async function create(argv, d, say, state) {
|
|
@@ -300,10 +407,16 @@ async function create(argv, d, say, state) {
|
|
|
300
407
|
);
|
|
301
408
|
}
|
|
302
409
|
if (!args.noCreate && !d.env.PIPELEX_API_KEY?.trim()) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
);
|
|
410
|
+
// The copy is new, so it has no .env.local or .env: after the environment,
|
|
411
|
+
// the login's file is the one source left, and npm run create reads it
|
|
412
|
+
// again itself, under the same rules.
|
|
413
|
+
const login = readLoginKey(d.env, { homedir: d.homedir });
|
|
414
|
+
if (login.status !== "key") {
|
|
415
|
+
throw Verdict.refused(
|
|
416
|
+
"no-key",
|
|
417
|
+
noKeyDetail(login, { homedir: d.homedir, platform: d.platform }),
|
|
418
|
+
);
|
|
419
|
+
}
|
|
307
420
|
}
|
|
308
421
|
|
|
309
422
|
const dest = path.resolve(d.cwd, args.dir);
|
|
@@ -322,6 +435,8 @@ async function create(argv, d, say, state) {
|
|
|
322
435
|
const problem = destinationProblem(dest, found);
|
|
323
436
|
if (problem !== null) throw Verdict.refused("not-empty", problem);
|
|
324
437
|
const git = planGit(args, dest, found, d.env);
|
|
438
|
+
// The highest directory the write will create, which a dry run's discard deletes.
|
|
439
|
+
const made = found.kind === "missing" ? highestMissing(dest) : dest;
|
|
325
440
|
|
|
326
441
|
const values = { ...args.values };
|
|
327
442
|
if (values[table.required] !== undefined) {
|
|
@@ -467,11 +582,11 @@ async function create(argv, d, say, state) {
|
|
|
467
582
|
}
|
|
468
583
|
if (values.DRY_RUN) {
|
|
469
584
|
return {
|
|
470
|
-
line: `copied ${dest} (
|
|
585
|
+
line: `copied ${dest} (${dryRunStands({ dest, made, existed: found.kind, committed: git.commit })}); next: ${nextCreate(dest, table, template, values, { installed: true })}`,
|
|
471
586
|
};
|
|
472
587
|
}
|
|
473
588
|
stampInstall(dest);
|
|
474
|
-
return { line: `created ${dest}; next: ${
|
|
589
|
+
return { line: `created ${dest}; next: ${nextServe(dest)}` };
|
|
475
590
|
}
|
|
476
591
|
|
|
477
592
|
/**
|
package/lib/verdict.mjs
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
* is presentation: 0 for `created` and `copied`, 1 otherwise.
|
|
8
8
|
*
|
|
9
9
|
* created <dir> the copy, the git outcome, and a green npm run create
|
|
10
|
-
* copied <dir> --no-create or --dry-run: the copy and the git outcome only
|
|
10
|
+
* copied <dir> --no-create or --dry-run: the copy and the git outcome only; after
|
|
11
|
+
* --dry-run it names what stands (the files, the installed
|
|
12
|
+
* dependencies, the commit) and the commands that discard it, leaving
|
|
13
|
+
* the destination as it stood before the run
|
|
11
14
|
* refused: <why> the preflight stopped, and nothing was written
|
|
12
15
|
* failed: write the copy failed or was interrupted, and what it created was removed
|
|
13
16
|
* failed: commit git refused the pristine commit; the copy stands
|
|
@@ -73,3 +76,14 @@ export function shellQuote(word, platform = process.platform) {
|
|
|
73
76
|
export function inTurn(commands) {
|
|
74
77
|
return commands.map((command) => `\`${command}\``).join(", then ");
|
|
75
78
|
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The npm a person types on this system, as the verdicts name it: `npm.cmd` on
|
|
82
|
+
* Windows, `npm` elsewhere. Windows PowerShell 5.1, under the execution policy
|
|
83
|
+
* Windows sets by default and an IT department may pin, runs a typed `npm` as
|
|
84
|
+
* `npm.ps1` and refuses it, while `npm.cmd` runs there, in PowerShell 7 and in
|
|
85
|
+
* `cmd` alike. The template's `scripts/lib/npm.mts` names it the same way.
|
|
86
|
+
*/
|
|
87
|
+
export function npmCommand(platform = process.platform) {
|
|
88
|
+
return platform === "win32" ? "npm.cmd" : "npm";
|
|
89
|
+
}
|
package/package.json
CHANGED
package/templates/webapp-js.pack
CHANGED
|
Binary file
|