@seangalie/precast 0.1.0 → 0.3.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/CHANGELOG.md +55 -0
- package/README.md +80 -7
- package/dist/bin.js +1 -1
- package/dist/cli.d.ts +3 -1
- package/dist/cli.js +263 -53
- package/dist/context.d.ts +9 -3
- package/dist/context.js +62 -15
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +3 -0
- package/dist/generate.d.ts +6 -0
- package/dist/generate.js +18 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/platforms.d.ts +5 -0
- package/dist/platforms.js +20 -1
- package/dist/profile-command.d.ts +6 -0
- package/dist/profile-command.js +194 -0
- package/dist/profiles.d.ts +68 -0
- package/dist/profiles.js +261 -0
- package/dist/prompts.d.ts +54 -0
- package/dist/prompts.js +117 -0
- package/dist/style.d.ts +14 -0
- package/dist/style.js +34 -0
- package/package.json +3 -3
- package/platforms/forgejo/files/.forgejo/SETUP.md.tmpl +11 -2
- package/platforms/forgejo/files/.forgejo/default_merge_message/MERGE_TEMPLATE.md +2 -0
- package/platforms/forgejo/platform.json +2 -1
- package/platforms/gitlab/files/.gitlab/SETUP.md.tmpl +13 -10
- package/platforms/gitlab/files/.gitlab-ci.yml.tmpl +4 -2
- package/platforms/gitlab/platform.json +7 -3
- package/template/.pre-commit-config.yaml.tmpl +13 -1
- package/template/.repo/ci-checks.sh +2 -0
- package/template/AGENTS.md.tmpl +3 -0
- package/template/SETUP.md.tmpl +12 -3
- package/template/docs/CONTRIBUTING.md.tmpl +11 -6
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,61 @@ and [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [0.3.0] - 2026-11-18
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `precast profile add`, `list`, and `remove` manage saved profiles without
|
|
13
|
+
editing the settings file. At a terminal, `profile add` asks for the name,
|
|
14
|
+
platform, server, and optional defaults.
|
|
15
|
+
- A prompted run offers your saved profiles as its first question, and after a
|
|
16
|
+
run that used `--server-url` without a profile it offers to save that server
|
|
17
|
+
as a new profile.
|
|
18
|
+
- Regular and full Forgejo projects get
|
|
19
|
+
`.forgejo/default_merge_message/MERGE_TEMPLATE.md`, so merge commits start
|
|
20
|
+
with the PR's title and reference and have an empty body.
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- `--config` no longer requires `--profile`: it also sets the settings file that
|
|
25
|
+
prompts read and save profiles in.
|
|
26
|
+
|
|
27
|
+
## [0.2.0] - 2026-11-18
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
|
|
31
|
+
- Interactive prompts: run in a terminal with required options missing, Precast
|
|
32
|
+
asks for the platform, tier, namespace, repository, and output folder,
|
|
33
|
+
checks each answer, and prints the equivalent command, under a small ASCII
|
|
34
|
+
header and with color where the terminal supports it (`NO_COLOR` turns color
|
|
35
|
+
off). `--no-input` turns the questions off; scripts, CI, and anything
|
|
36
|
+
without a terminal still get an error.
|
|
37
|
+
- Named profiles in a user settings file, chosen with `--profile`, save a
|
|
38
|
+
platform, server, namespace, and other defaults for servers you use often.
|
|
39
|
+
`--config` reads profiles from another file.
|
|
40
|
+
- Regular and full projects get a `commit-msg` hook that enforces Conventional
|
|
41
|
+
Commit messages and a `no-commit-to-branch` guard for their default branch,
|
|
42
|
+
which also covers local merge commits. `pre-commit install` installs both,
|
|
43
|
+
generated CI skips the guard, and `SETUP.md` has the first commit made before
|
|
44
|
+
the hooks are installed.
|
|
45
|
+
- GitLab projects generated for GitLab.com set `image: python:3.14-trixie` in
|
|
46
|
+
`.gitlab-ci.yml`, because the hosted runners' default image lacks Python's
|
|
47
|
+
`venv`. Other GitLab servers keep their runners' image.
|
|
48
|
+
|
|
49
|
+
### Changed
|
|
50
|
+
|
|
51
|
+
- **Breaking:** Forgejo projects default to `https://codeberg.org` and GitLab
|
|
52
|
+
projects to `https://gitlab.com`, instead of my own Forgejo and PPPL's GitLab.
|
|
53
|
+
Use `--server-url` or a profile for any other server.
|
|
54
|
+
- GitLab and Forgejo setup checklists no longer refer to PPPL, and the Forgejo
|
|
55
|
+
checklist explains choosing a hosted runner's label with `--runner`.
|
|
56
|
+
|
|
57
|
+
### Fixed
|
|
58
|
+
|
|
59
|
+
- A default branch or runner containing a character outside the Basic
|
|
60
|
+
Multilingual Plane, such as an emoji, now renders as valid YAML in generated
|
|
61
|
+
workflows and hook settings.
|
|
62
|
+
|
|
8
63
|
## [0.1.0] - 2026-11-17
|
|
9
64
|
|
|
10
65
|
### Added
|
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ Start a new project with its documentation, checks, and forge settings already i
|
|
|
19
19
|
- [Generate a project](#generate-a-project)
|
|
20
20
|
- [Publish it](#publish-it)
|
|
21
21
|
- [Options](#options)
|
|
22
|
+
- [Save your servers as profiles](#save-your-servers-as-profiles)
|
|
22
23
|
- [What Precast will not do](#what-precast-will-not-do)
|
|
23
24
|
- [Roadmap](#roadmap)
|
|
24
25
|
- [Changelog](#changelog)
|
|
@@ -60,13 +61,14 @@ extra files later.
|
|
|
60
61
|
|
|
61
62
|
| Platform | Default server | Platform files |
|
|
62
63
|
| --- | --- | --- |
|
|
63
|
-
| `forgejo` | `https://
|
|
64
|
+
| `forgejo` | `https://codeberg.org` | `.forgejo/`, plus `renovate.json` in full |
|
|
64
65
|
| `github` | `https://github.com` | `.github/` |
|
|
65
|
-
| `gitlab` | `https://
|
|
66
|
+
| `gitlab` | `https://gitlab.com` | `.gitlab/`, plus `.gitlab-ci.yml` in regular and full |
|
|
66
67
|
|
|
67
68
|
Each platform keeps its own native formats: Forgejo and GitHub use issue forms
|
|
68
69
|
and Actions workflows, and GitLab uses Markdown templates and GitLab CI. Any of
|
|
69
|
-
them can point at a different server
|
|
70
|
+
them can point at a different server, such as your own Forgejo or your
|
|
71
|
+
organization's GitLab, with `--server-url` or a [profile](#save-your-servers-as-profiles).
|
|
70
72
|
|
|
71
73
|
## Getting Started
|
|
72
74
|
|
|
@@ -75,8 +77,8 @@ them can point at a different server with `--server-url`.
|
|
|
75
77
|
- Node.js 22.18 or newer, which includes npm. Precast has no other runtime
|
|
76
78
|
dependencies.
|
|
77
79
|
- Git, to publish the project you generate.
|
|
78
|
-
- An account on the forge where the new project will live. For
|
|
79
|
-
you also need
|
|
80
|
+
- An account on the forge where the new project will live. For a server inside
|
|
81
|
+
an organization's network, you may also need its VPN when you push.
|
|
80
82
|
|
|
81
83
|
### Installation
|
|
82
84
|
|
|
@@ -99,8 +101,17 @@ The examples below use `precast`. If you did not install it, use
|
|
|
99
101
|
|
|
100
102
|
### Generate a project
|
|
101
103
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
The quickest start is to run `precast` on its own in a terminal. It asks for the
|
|
105
|
+
platform, tier, namespace, repository name, and output folder, checks each
|
|
106
|
+
answer, and then prints the full command so you can skip the questions next
|
|
107
|
+
time. Pressing Enter at the tier question keeps `regular`. Press
|
|
108
|
+
Ctrl+C to stop without writing anything. The questions use color in terminals
|
|
109
|
+
that support it; set `NO_COLOR=1` to turn it off. Commands that give every
|
|
110
|
+
required option ask nothing and print plain text.
|
|
111
|
+
|
|
112
|
+
You can also give everything as options. Run `precast` from the folder where you
|
|
113
|
+
keep your projects. This creates a `regular` GitHub project for the account
|
|
114
|
+
`my-name` in a new folder:
|
|
104
115
|
|
|
105
116
|
```sh
|
|
106
117
|
precast --platform github --namespace my-name \
|
|
@@ -165,10 +176,72 @@ If you used `--default-branch`, use that name in place of `main` above.
|
|
|
165
176
|
| `--codeowner` | A user or team to own every file, such as `@my-name`; without it, the code owners file holds a commented example |
|
|
166
177
|
| `--runner` | The Actions runner label or GitLab runner tag for CI |
|
|
167
178
|
| `--server-url` | A different server than the platform's default |
|
|
179
|
+
| `--profile` | Use the saved settings with this name; see below |
|
|
180
|
+
| `--config` | The settings file to read profiles from, if not the usual one |
|
|
168
181
|
| `--dry-run` | Check everything and list the files without writing them |
|
|
182
|
+
| `--no-input` | Never ask questions; report missing options instead, as scripts need |
|
|
169
183
|
|
|
170
184
|
`precast --help` prints the same list, and `precast --version` prints the version.
|
|
171
185
|
|
|
186
|
+
### Save your servers as profiles
|
|
187
|
+
|
|
188
|
+
If you use your own Forgejo, an organization's GitLab, or the same settings
|
|
189
|
+
every time, save them as a profile instead of typing them for each project. The
|
|
190
|
+
easiest way is to let Precast ask:
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
precast profile add
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
It asks for a name, the platform, the server, and optional defaults such as the
|
|
197
|
+
namespace, author, runner, and tier. You can also give them as options:
|
|
198
|
+
|
|
199
|
+
```sh
|
|
200
|
+
precast profile add work --platform gitlab \
|
|
201
|
+
--server-url https://gitlab.example.com --runner linux
|
|
202
|
+
precast profile list
|
|
203
|
+
precast profile remove work
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Then use a profile with `--profile`. Anything you type on the command line wins
|
|
207
|
+
over the profile, and Precast asks for anything still missing, such as the
|
|
208
|
+
repository name:
|
|
209
|
+
|
|
210
|
+
```sh
|
|
211
|
+
precast --profile work --namespace group/subgroup --repo my-project --output my-project
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
When you run `precast` on its own and have saved profiles, the first question
|
|
215
|
+
offers them; press Enter to continue without one. After a run that used
|
|
216
|
+
`--server-url` without a profile, Precast offers to save that server as a new
|
|
217
|
+
profile.
|
|
218
|
+
|
|
219
|
+
Profiles live in a settings file:
|
|
220
|
+
|
|
221
|
+
- macOS and Linux: `~/.config/precast/config.json`, or `$XDG_CONFIG_HOME/precast/config.json`
|
|
222
|
+
- Windows: `%APPDATA%\precast\config.json`
|
|
223
|
+
|
|
224
|
+
You can edit it by hand too. Profile names use letters, digits, hyphens, and
|
|
225
|
+
underscores; a new name can't be only digits. Each profile names a `platform`
|
|
226
|
+
and may set `serverUrl`, `namespace`, `author`, `description`, `defaultBranch`,
|
|
227
|
+
`codeowner`, `runner`, and `tier`:
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
{
|
|
231
|
+
"profiles": {
|
|
232
|
+
"work": {
|
|
233
|
+
"platform": "gitlab",
|
|
234
|
+
"serverUrl": "https://gitlab.example.com",
|
|
235
|
+
"runner": "linux"
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Commands that give every required option never read the settings file unless
|
|
242
|
+
you name a profile, so they always behave the same way. `--config` points
|
|
243
|
+
Precast at a different settings file.
|
|
244
|
+
|
|
172
245
|
### What Precast will not do
|
|
173
246
|
|
|
174
247
|
Precast never contacts a forge, runs Git, or asks for passwords or tokens. It
|
package/dist/bin.js
CHANGED
package/dist/cli.d.ts
CHANGED
package/dist/cli.js
CHANGED
|
@@ -2,9 +2,13 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
4
|
import { DEFAULT_BRANCH, DEFAULT_DESCRIPTION } from "./context.js";
|
|
5
|
-
import { isSystemError, PrecastError } from "./errors.js";
|
|
5
|
+
import { isSystemError, PrecastError, UsageError } from "./errors.js";
|
|
6
6
|
import { generate, ROOT } from "./generate.js";
|
|
7
7
|
import { platformNames } from "./platforms.js";
|
|
8
|
+
import { profileCommand, useHint } from "./profile-command.js";
|
|
9
|
+
import { checkNewProfileName, defaultConfigPath, listProfiles, profileNames, readProfile, saveProfile, } from "./profiles.js";
|
|
10
|
+
import { Cancelled, promptForMissing, questions, shellQuote, withTerminal, } from "./prompts.js";
|
|
11
|
+
import { header, paint } from "./style.js";
|
|
8
12
|
import { DEFAULT_TIER, isTier, TIERS } from "./tiers.js";
|
|
9
13
|
const OPTIONS = {
|
|
10
14
|
platform: { type: "string" },
|
|
@@ -19,122 +23,328 @@ const OPTIONS = {
|
|
|
19
23
|
"default-branch": { type: "string" },
|
|
20
24
|
codeowner: { type: "string" },
|
|
21
25
|
runner: { type: "string" },
|
|
26
|
+
profile: { type: "string" },
|
|
27
|
+
config: { type: "string" },
|
|
22
28
|
"dry-run": { type: "boolean" },
|
|
29
|
+
"no-input": { type: "boolean" },
|
|
23
30
|
help: { type: "boolean", short: "h" },
|
|
24
31
|
version: { type: "boolean", short: "v" },
|
|
25
32
|
};
|
|
26
|
-
const REQUIRED = ["platform", "namespace", "repo", "output"];
|
|
27
33
|
function usage(root) {
|
|
28
|
-
return `Usage:
|
|
34
|
+
return `Usage:
|
|
35
|
+
precast Ask for everything needed (at a terminal)
|
|
36
|
+
precast --platform <id> --namespace <owner> --repo <name> --output <dir> [options]
|
|
37
|
+
precast --profile <name> --repo <name> --output <dir> [options]
|
|
38
|
+
precast profile list|add|remove Manage saved profiles (see precast profile --help)
|
|
29
39
|
|
|
30
40
|
Render shared files and one platform's native files into a new project directory.
|
|
31
41
|
|
|
32
|
-
Required:
|
|
42
|
+
Required. At a terminal, Precast asks for any that are missing:
|
|
33
43
|
--platform <id> ${platformNames(root).join(", ")}
|
|
34
44
|
--namespace <owner> Owner, organization, or GitLab group/subgroup
|
|
35
45
|
--repo <name> Repository slug
|
|
36
46
|
--output <dir> New directory outside the Precast source directory
|
|
37
47
|
|
|
38
|
-
|
|
39
|
-
--tier <tier> ${TIERS.join(", ")}; each
|
|
40
|
-
|
|
48
|
+
Optional. When Precast is asking, it also asks for these (defaults shown):
|
|
49
|
+
--tier <tier> ${TIERS.join(", ")}; each adds to the one before (default: ${DEFAULT_TIER})
|
|
50
|
+
|
|
51
|
+
Never asked; set these as options:
|
|
41
52
|
--project-name <name> Human-readable title; defaults to the repository slug
|
|
42
53
|
--author <name> Display name; defaults to the namespace
|
|
43
54
|
--description <text> Defaults to "${DEFAULT_DESCRIPTION}"
|
|
44
55
|
--default-branch <name> Defaults to ${DEFAULT_BRANCH}
|
|
45
56
|
--codeowner <@owner> Explicit @user or @namespace/team; omitted means a commented example
|
|
57
|
+
--server-url <url> Override the platform's default instance URL
|
|
46
58
|
--runner <label> Actions runner label or GitLab runner tag
|
|
59
|
+
|
|
60
|
+
Profiles (a profile can set everything above except the repository, output, and title):
|
|
61
|
+
--profile <name> Use saved settings, such as a self-hosted server; options given here win
|
|
62
|
+
--config <file> Settings file for profiles (default: ${defaultConfigPath()})
|
|
63
|
+
|
|
64
|
+
Other options:
|
|
47
65
|
--dry-run Validate and list files without writing
|
|
66
|
+
--no-input Never ask questions; report missing options instead
|
|
48
67
|
-h, --help Show this help
|
|
49
68
|
-v, --version Show the version
|
|
50
69
|
`;
|
|
51
70
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
71
|
+
const nodeTerminal = {
|
|
72
|
+
interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
|
|
73
|
+
prompt: (use) => withTerminal(process.stdin, process.stdout, use),
|
|
74
|
+
};
|
|
75
|
+
export async function main(argv, root = ROOT, terminal = nodeTerminal) {
|
|
76
|
+
if (argv[0] === "profile") {
|
|
77
|
+
try {
|
|
78
|
+
return await profileCommand(argv.slice(1), root, terminal);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error instanceof Cancelled) {
|
|
82
|
+
process.stderr.write("\nCancelled; no profile was saved\n");
|
|
83
|
+
return 130;
|
|
84
|
+
}
|
|
85
|
+
if (error instanceof UsageError ||
|
|
86
|
+
(isSystemError(error) && error.code.startsWith("ERR_PARSE_ARGS"))) {
|
|
87
|
+
process.stderr.write(`error: ${error.message}\nRun precast profile --help for usage.\n`);
|
|
88
|
+
return 2;
|
|
89
|
+
}
|
|
90
|
+
return failure(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
56
93
|
let values;
|
|
57
94
|
try {
|
|
58
|
-
values = parse(argv
|
|
95
|
+
values = parse(argv);
|
|
59
96
|
}
|
|
60
97
|
catch (error) {
|
|
61
|
-
|
|
62
|
-
(isSystemError(error) && error.code.startsWith("ERR_PARSE_ARGS"))) {
|
|
63
|
-
process.stderr.write(`error: ${error.message}\nRun precast --help for usage.\n`);
|
|
64
|
-
return 2;
|
|
65
|
-
}
|
|
66
|
-
throw error;
|
|
98
|
+
return usageFailure(error);
|
|
67
99
|
}
|
|
68
100
|
if (values.help) {
|
|
69
101
|
process.stdout.write(usage(root));
|
|
70
102
|
return 0;
|
|
71
103
|
}
|
|
72
104
|
if (values.version) {
|
|
73
|
-
|
|
74
|
-
process.stdout.write(`${manifest.version}\n`);
|
|
105
|
+
process.stdout.write(`${readVersion(root)}\n`);
|
|
75
106
|
return 0;
|
|
76
107
|
}
|
|
77
|
-
const tier = values.tier ?? DEFAULT_TIER;
|
|
78
|
-
if (!isTier(tier)) {
|
|
79
|
-
throw new Error("unreachable: tier is validated during parsing");
|
|
80
|
-
}
|
|
81
108
|
try {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
109
|
+
const settings = values.config ?? defaultConfigPath();
|
|
110
|
+
let profile;
|
|
111
|
+
let pickedProfile;
|
|
112
|
+
try {
|
|
113
|
+
// Compare with undefined so an empty `--profile=` is rejected, not ignored.
|
|
114
|
+
profile = values.profile !== undefined ? readProfile(settings, values.profile) : undefined;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return failure(error);
|
|
118
|
+
}
|
|
119
|
+
let options;
|
|
120
|
+
let nextTime;
|
|
121
|
+
let tier;
|
|
122
|
+
try {
|
|
123
|
+
const required = merge(values, profile);
|
|
124
|
+
// Reject a bad platform or tier before asking anything about it.
|
|
125
|
+
checkChoices(required.platform, values.tier ?? profile?.tier, root);
|
|
126
|
+
let missing = missingNames(required);
|
|
127
|
+
if (missing.length > 0 && terminal.interactive && !values["no-input"]) {
|
|
128
|
+
process.stdout.write(header(readVersion(root)));
|
|
129
|
+
const write = (text) => process.stdout.write(text);
|
|
130
|
+
const answers = await terminal.prompt(async (ask) => {
|
|
131
|
+
// With no profile or platform chosen yet, saved profiles come first.
|
|
132
|
+
const saved = new Map();
|
|
133
|
+
if (values.profile === undefined && values.platform === undefined) {
|
|
134
|
+
for (const entry of listProfiles(settings, root)) {
|
|
135
|
+
if (entry.profile) {
|
|
136
|
+
saved.set(entry.name, entry.profile);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// One broken entry should not hide the others or stop the run.
|
|
140
|
+
write(`${paint("yellow", `! Skipping profile '${entry.name}': ${entry.error}`)}\n`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (saved.size > 0) {
|
|
145
|
+
const rows = [...saved].map(([name, settings]) => [name, summarize(settings)]);
|
|
146
|
+
pickedProfile = await questions(ask, write).choose(`${paint("bold", "Profiles")}\n`, rows, "Profile", { label: "none", value: undefined });
|
|
147
|
+
if (pickedProfile !== undefined) {
|
|
148
|
+
profile = saved.get(pickedProfile);
|
|
149
|
+
Object.assign(required, merge(values, profile));
|
|
150
|
+
checkChoices(required.platform, values.tier ?? profile?.tier, root);
|
|
151
|
+
missing = missingNames(required);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// A session that is already asking also offers the tier, unless one was chosen.
|
|
155
|
+
const askTier = (values.tier ?? profile?.tier) === undefined;
|
|
156
|
+
return promptForMissing(required, ask, write, root, { askTier });
|
|
157
|
+
});
|
|
158
|
+
// `--key=value` keeps a value such as `-draft` from reading as an option,
|
|
159
|
+
// and the new options go before any `--` that ended the original options.
|
|
160
|
+
const added = missing.map((name) => `--${name}=${answers[name]}`);
|
|
161
|
+
if (pickedProfile !== undefined)
|
|
162
|
+
added.unshift(`--profile=${pickedProfile}`);
|
|
163
|
+
if (answers.tier !== undefined)
|
|
164
|
+
added.push(`--tier=${answers.tier}`);
|
|
165
|
+
tier = answers.tier;
|
|
166
|
+
const end = argv.indexOf("--");
|
|
167
|
+
const replay = end < 0 ? [...argv, ...added] : [...argv.slice(0, end), ...added, ...argv.slice(end)];
|
|
168
|
+
nextTime = `precast ${replay.map(shellQuote).join(" ")}`;
|
|
169
|
+
Object.assign(required, {
|
|
170
|
+
platform: answers.platform,
|
|
171
|
+
namespace: answers.namespace,
|
|
172
|
+
repo: answers.repo,
|
|
173
|
+
output: answers.output,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
options = resolve(values, profile, required, root, tier);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (error instanceof Cancelled) {
|
|
180
|
+
process.stderr.write(`\n${error.message}\n`);
|
|
181
|
+
return 130;
|
|
182
|
+
}
|
|
183
|
+
return usageFailure(error);
|
|
184
|
+
}
|
|
185
|
+
const { destination, files } = generate(options, root);
|
|
186
|
+
const verb = options.dryRun ? "Would generate" : "Generated";
|
|
98
187
|
const lines = [
|
|
99
|
-
`${verb} ${files.length} files for ${
|
|
188
|
+
`${verb} ${files.length} files for ${options.platform} (${options.tier} tier): ${destination}`,
|
|
100
189
|
];
|
|
101
|
-
if (
|
|
190
|
+
if (options.dryRun) {
|
|
102
191
|
lines.push(...files.map((file) => ` ${file}`));
|
|
103
192
|
}
|
|
104
193
|
else {
|
|
105
194
|
lines.push("Review SETUP.md before creating the destination repository and pushing.");
|
|
106
195
|
}
|
|
196
|
+
if (nextTime) {
|
|
197
|
+
// A prompted session marks success and shows how to skip the questions.
|
|
198
|
+
// Only a prompted run is styled; complete commands print plain text.
|
|
199
|
+
lines[0] = `\n${paint("green", `✓ ${lines[0]}`)}`;
|
|
200
|
+
lines.push("", paint("dim", "Next time, run:"), ` ${paint("cyan", nextTime)}`);
|
|
201
|
+
}
|
|
107
202
|
process.stdout.write(`${lines.join("\n")}\n`);
|
|
203
|
+
// After a prompted run on a server of your own, offer to save it as a profile.
|
|
204
|
+
if (nextTime &&
|
|
205
|
+
profile === undefined &&
|
|
206
|
+
values["server-url"] !== undefined &&
|
|
207
|
+
!options.dryRun) {
|
|
208
|
+
// Saving is optional: a problem with it is reported, but the project was
|
|
209
|
+
// generated, so the run still succeeds.
|
|
210
|
+
try {
|
|
211
|
+
await offerToSave(options, settings, values.config, terminal);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
if (error instanceof Cancelled) {
|
|
215
|
+
process.stderr.write("\nNo profile was saved\n");
|
|
216
|
+
}
|
|
217
|
+
else if (error instanceof PrecastError || isSystemError(error)) {
|
|
218
|
+
process.stderr.write(`warning: the profile was not saved: ${error.message}\n`);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
108
225
|
return 0;
|
|
109
226
|
}
|
|
110
227
|
catch (error) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
228
|
+
return failure(error);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** Ask for a profile name and save this run's server and settings under it. */
|
|
232
|
+
async function offerToSave(options, settings, config, terminal) {
|
|
233
|
+
const existing = new Set(profileNames(settings));
|
|
234
|
+
const write = (text) => process.stdout.write(text);
|
|
235
|
+
const name = await terminal.prompt((ask) => questions(ask, write).answer("Save this server as a profile?", "(a name, or Enter to skip)", (reply) => {
|
|
236
|
+
if (reply === "")
|
|
237
|
+
return reply;
|
|
238
|
+
checkNewProfileName(reply);
|
|
239
|
+
if (existing.has(reply)) {
|
|
240
|
+
throw new PrecastError(`Profile '${reply}' already exists; choose another name`);
|
|
114
241
|
}
|
|
115
|
-
|
|
242
|
+
return reply;
|
|
243
|
+
}));
|
|
244
|
+
if (name === "")
|
|
245
|
+
return;
|
|
246
|
+
const profile = {
|
|
247
|
+
platform: options.platform,
|
|
248
|
+
namespace: options.namespace,
|
|
249
|
+
tier: options.tier,
|
|
250
|
+
};
|
|
251
|
+
for (const [key, value] of Object.entries({
|
|
252
|
+
serverUrl: options.serverUrl,
|
|
253
|
+
author: options.author,
|
|
254
|
+
description: options.description,
|
|
255
|
+
defaultBranch: options.defaultBranch,
|
|
256
|
+
codeowner: options.codeowner,
|
|
257
|
+
runner: options.runner,
|
|
258
|
+
})) {
|
|
259
|
+
if (value !== undefined)
|
|
260
|
+
profile[key] = value;
|
|
116
261
|
}
|
|
262
|
+
saveProfile(settings, name, profile);
|
|
263
|
+
write(`${paint("green", `✓ Saved profile '${name}'`)} in ${settings}\n` +
|
|
264
|
+
` ${paint("dim", "Use it with:")} ${paint("cyan", useHint(name, config))}\n`);
|
|
117
265
|
}
|
|
118
|
-
|
|
266
|
+
/** A profile's platform and server, for choosing among saved profiles. */
|
|
267
|
+
function summarize(profile) {
|
|
268
|
+
return [profile.platform, profile.serverUrl, profile.namespace].filter(Boolean).join(" · ");
|
|
269
|
+
}
|
|
270
|
+
/** Report an expected failure without a stack trace; rethrow anything else. */
|
|
271
|
+
function failure(error) {
|
|
272
|
+
if (error instanceof PrecastError || isSystemError(error)) {
|
|
273
|
+
process.stderr.write(`error: ${error.message}\n`);
|
|
274
|
+
return 1;
|
|
275
|
+
}
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
function usageFailure(error) {
|
|
279
|
+
if (error instanceof UsageError ||
|
|
280
|
+
(isSystemError(error) && error.code.startsWith("ERR_PARSE_ARGS"))) {
|
|
281
|
+
process.stderr.write(`error: ${error.message}\nRun precast --help for usage.\n`);
|
|
282
|
+
return 2;
|
|
283
|
+
}
|
|
284
|
+
throw error;
|
|
285
|
+
}
|
|
286
|
+
function parse(argv) {
|
|
119
287
|
const { values } = parseArgs({
|
|
120
288
|
args: argv,
|
|
121
289
|
options: OPTIONS,
|
|
122
290
|
strict: true,
|
|
123
291
|
allowPositionals: false,
|
|
124
292
|
});
|
|
125
|
-
|
|
126
|
-
|
|
293
|
+
return values;
|
|
294
|
+
}
|
|
295
|
+
/** The required values from the command line and profile; options given explicitly win. */
|
|
296
|
+
function merge(values, profile) {
|
|
297
|
+
if (profile && values.platform !== undefined && values.platform !== profile.platform) {
|
|
298
|
+
throw new UsageError(`--platform ${values.platform} conflicts with profile ${values.profile}, which uses ${profile.platform}`);
|
|
127
299
|
}
|
|
128
|
-
|
|
300
|
+
return {
|
|
301
|
+
platform: values.platform ?? profile?.platform,
|
|
302
|
+
namespace: values.namespace ?? profile?.namespace,
|
|
303
|
+
repo: values.repo,
|
|
304
|
+
output: values.output,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function missingNames(required) {
|
|
308
|
+
return ["platform", "namespace", "repo", "output"].filter((name) => required[name] === undefined);
|
|
309
|
+
}
|
|
310
|
+
/** Validate the platform and tier and fill in the optional values. */
|
|
311
|
+
function resolve(values, profile, required, root, answeredTier) {
|
|
312
|
+
const missing = missingNames(required);
|
|
129
313
|
if (missing.length > 0) {
|
|
130
314
|
throw new UsageError(`missing required options: ${missing.map((name) => `--${name}`).join(", ")}`);
|
|
131
315
|
}
|
|
316
|
+
const tier = values.tier ?? profile?.tier ?? answeredTier ?? DEFAULT_TIER;
|
|
317
|
+
checkChoices(required.platform, tier, root);
|
|
318
|
+
if (!isTier(tier)) {
|
|
319
|
+
throw new Error("unreachable: checkChoices validates the tier");
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
platform: required.platform,
|
|
323
|
+
namespace: required.namespace,
|
|
324
|
+
repo: required.repo,
|
|
325
|
+
output: required.output,
|
|
326
|
+
tier,
|
|
327
|
+
serverUrl: values["server-url"] ?? profile?.serverUrl,
|
|
328
|
+
projectName: values["project-name"],
|
|
329
|
+
author: values.author ?? profile?.author,
|
|
330
|
+
description: values.description ?? profile?.description,
|
|
331
|
+
defaultBranch: values["default-branch"] ?? profile?.defaultBranch,
|
|
332
|
+
codeowner: values.codeowner ?? profile?.codeowner,
|
|
333
|
+
runner: values.runner ?? profile?.runner,
|
|
334
|
+
dryRun: values["dry-run"],
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/** Validate a platform and tier when they are known; either may still be missing. */
|
|
338
|
+
function checkChoices(platform, tier, root) {
|
|
132
339
|
const platforms = platformNames(root);
|
|
133
|
-
if (!platforms.includes(
|
|
340
|
+
if (platform !== undefined && !platforms.includes(platform)) {
|
|
134
341
|
throw new UsageError(`--platform must be one of: ${platforms.join(", ")}`);
|
|
135
342
|
}
|
|
136
|
-
if (
|
|
343
|
+
if (tier !== undefined && !isTier(tier)) {
|
|
137
344
|
throw new UsageError(`--tier must be one of: ${TIERS.join(", ")}`);
|
|
138
345
|
}
|
|
139
|
-
|
|
346
|
+
}
|
|
347
|
+
function readVersion(root) {
|
|
348
|
+
const manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
349
|
+
return manifest.version;
|
|
140
350
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -16,6 +16,10 @@ export interface ContextOptions {
|
|
|
16
16
|
}
|
|
17
17
|
export declare const DEFAULT_DESCRIPTION = "Describe this project.";
|
|
18
18
|
export declare const DEFAULT_BRANCH = "main";
|
|
19
|
+
/** Validate an owner, organization, or group path; only some platforms allow subgroups. */
|
|
20
|
+
export declare function checkNamespace(value: string, nestedAllowed: boolean): string;
|
|
21
|
+
/** Validate a repository slug. */
|
|
22
|
+
export declare function checkRepo(value: string): string;
|
|
19
23
|
export declare function makeContext(options: ContextOptions, platform: PlatformDescriptor): Context;
|
|
20
24
|
/** Reject empty values, control characters, and anything that would read as a template token. */
|
|
21
25
|
export declare function textValue(value: string | undefined, label: string): string;
|
|
@@ -24,9 +28,11 @@ export declare function markdown(value: string): string;
|
|
|
24
28
|
/** Percent-encode UTF-8 bytes outside unreserved characters and `safe`. */
|
|
25
29
|
export declare function quote(value: string, safe?: string): string;
|
|
26
30
|
/**
|
|
27
|
-
* Serialize a string as
|
|
28
|
-
*
|
|
31
|
+
* Serialize a string as an ASCII YAML double-quoted scalar. JSON's escapes for
|
|
32
|
+
* quotes, backslashes, and control characters are also YAML escapes, but JSON
|
|
33
|
+
* writes characters outside the Basic Multilingual Plane as two surrogate
|
|
34
|
+
* escapes, which YAML parsers reject. Those become one `\UXXXXXXXX` escape.
|
|
29
35
|
*/
|
|
30
|
-
export declare function
|
|
36
|
+
export declare function yamlString(value: string): string;
|
|
31
37
|
/** Replace `{KEY}` with earlier context values; `{{` and `}}` are literal braces. */
|
|
32
38
|
export declare function formatValue(value: string, context: Context, key: string): string;
|