@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/dist/context.js
CHANGED
|
@@ -4,17 +4,31 @@ export const DEFAULT_DESCRIPTION = "Describe this project.";
|
|
|
4
4
|
export const DEFAULT_BRANCH = "main";
|
|
5
5
|
const COMPONENT = /^[A-Za-z0-9_][A-Za-z0-9_.-]*$/;
|
|
6
6
|
const CODEOWNER = /^@[A-Za-z0-9_][A-Za-z0-9_.-]*(?:\/[A-Za-z0-9_][A-Za-z0-9_.-]*)*$/;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
/** Validate an owner, organization, or group path; only some platforms allow subgroups. */
|
|
8
|
+
export function checkNamespace(value, nestedAllowed) {
|
|
9
|
+
const namespace = textValue(value, "Namespace");
|
|
9
10
|
const components = namespace.split("/");
|
|
10
|
-
if (!
|
|
11
|
+
if (!nestedAllowed && components.length !== 1) {
|
|
11
12
|
throw new PrecastError("This platform does not support nested namespaces");
|
|
12
13
|
}
|
|
13
|
-
for (const component of
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
for (const component of components) {
|
|
15
|
+
checkComponent(component);
|
|
16
|
+
}
|
|
17
|
+
return namespace;
|
|
18
|
+
}
|
|
19
|
+
/** Validate a repository slug. */
|
|
20
|
+
export function checkRepo(value) {
|
|
21
|
+
checkComponent(value);
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function checkComponent(component) {
|
|
25
|
+
if (!COMPONENT.test(component) || component.endsWith(".git") || component.endsWith(".")) {
|
|
26
|
+
throw new PrecastError(`Invalid repository or namespace component: '${component}'`);
|
|
17
27
|
}
|
|
28
|
+
}
|
|
29
|
+
export function makeContext(options, platform) {
|
|
30
|
+
const namespace = checkNamespace(options.namespace, platform.nested_namespaces);
|
|
31
|
+
checkRepo(options.repo);
|
|
18
32
|
const server = serverUrl(options.serverUrl || platform.server_url);
|
|
19
33
|
const branch = defaultBranch(options.defaultBranch ?? DEFAULT_BRANCH);
|
|
20
34
|
const name = textValue(options.projectName || options.repo, "Project name");
|
|
@@ -36,10 +50,10 @@ export function makeContext(options, platform) {
|
|
|
36
50
|
AUTHOR: markdown(author),
|
|
37
51
|
DESCRIPTION: markdown(description),
|
|
38
52
|
DEFAULT_BRANCH: branch,
|
|
39
|
-
DEFAULT_BRANCH_YAML:
|
|
53
|
+
DEFAULT_BRANCH_YAML: yamlString(branch),
|
|
40
54
|
BRANCH_URL: quote(branch),
|
|
41
|
-
RUNNER_YAML:
|
|
42
|
-
RUNNER_TAGS_YAML: runner ? `[${
|
|
55
|
+
RUNNER_YAML: yamlString(runner ?? platform.runner),
|
|
56
|
+
RUNNER_TAGS_YAML: runner ? `[${yamlString(runner)}]` : "[]",
|
|
43
57
|
CODEOWNER: owner || "",
|
|
44
58
|
CODEOWNERS_RULE: owner
|
|
45
59
|
? `${platform.codeowners_pattern} ${owner}`
|
|
@@ -47,14 +61,32 @@ export function makeContext(options, platform) {
|
|
|
47
61
|
`# ${platform.codeowners_pattern} @username-or-team`,
|
|
48
62
|
};
|
|
49
63
|
// Descriptor entries are ordered so later values can refer to earlier URLs.
|
|
64
|
+
const onDefaultServer = sameServer(server, platform.server_url);
|
|
50
65
|
for (const [key, value] of Object.entries(platform.context)) {
|
|
51
66
|
if (Object.hasOwn(context, key)) {
|
|
52
67
|
throw new PrecastError(`Platform descriptor overrides reserved context key: ${key}`);
|
|
53
68
|
}
|
|
54
|
-
|
|
69
|
+
const hosted = platform.default_server_context;
|
|
70
|
+
const chosen = onDefaultServer && hosted && Object.hasOwn(hosted, key) ? hosted[key] : value;
|
|
71
|
+
context[key] = formatValue(chosen, context, key);
|
|
55
72
|
}
|
|
56
73
|
return context;
|
|
57
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Compare server URLs as the same origin and path: the URL parser lowercases
|
|
77
|
+
* the scheme and host and drops a default port, so `https://GitLab.com:443/`
|
|
78
|
+
* matches `https://gitlab.com`. Both URLs passed `serverUrl` first, but that
|
|
79
|
+
* check accepts some the URL parser rejects, such as an IPv6 address with a
|
|
80
|
+
* zone ID; such a server is never a hosted default, so it is not a match.
|
|
81
|
+
*/
|
|
82
|
+
function sameServer(left, right) {
|
|
83
|
+
const normalize = (url) => {
|
|
84
|
+
const parsed = URL.parse(url);
|
|
85
|
+
return parsed && `${parsed.protocol}//${parsed.host}${parsed.pathname.replace(/\/+$/, "")}`;
|
|
86
|
+
};
|
|
87
|
+
const a = normalize(left);
|
|
88
|
+
return a !== null && a === normalize(right);
|
|
89
|
+
}
|
|
58
90
|
/** Reject empty values, control characters, and anything that would read as a template token. */
|
|
59
91
|
export function textValue(value, label) {
|
|
60
92
|
if (!value || [...value].some(isControl)) {
|
|
@@ -130,11 +162,26 @@ export function quote(value, safe = "") {
|
|
|
130
162
|
return result;
|
|
131
163
|
}
|
|
132
164
|
/**
|
|
133
|
-
* Serialize a string as
|
|
134
|
-
*
|
|
165
|
+
* Serialize a string as an ASCII YAML double-quoted scalar. JSON's escapes for
|
|
166
|
+
* quotes, backslashes, and control characters are also YAML escapes, but JSON
|
|
167
|
+
* writes characters outside the Basic Multilingual Plane as two surrogate
|
|
168
|
+
* escapes, which YAML parsers reject. Those become one `\UXXXXXXXX` escape.
|
|
135
169
|
*/
|
|
136
|
-
export function
|
|
137
|
-
|
|
170
|
+
export function yamlString(value) {
|
|
171
|
+
let result = "";
|
|
172
|
+
for (const char of JSON.stringify(value)) {
|
|
173
|
+
const code = char.codePointAt(0) ?? 0;
|
|
174
|
+
if (code > 0xffff) {
|
|
175
|
+
result += `\\U${code.toString(16).padStart(8, "0")}`;
|
|
176
|
+
}
|
|
177
|
+
else if (code > 0x7f) {
|
|
178
|
+
result += `\\u${code.toString(16).padStart(4, "0")}`;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
result += char;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
138
185
|
}
|
|
139
186
|
/** Replace `{KEY}` with earlier context values; `{{` and `}}` are literal braces. */
|
|
140
187
|
export function formatValue(value, context, key) {
|
package/dist/errors.d.ts
CHANGED
|
@@ -6,3 +6,6 @@ export declare class PrecastError extends Error {
|
|
|
6
6
|
export declare function isSystemError(error: unknown): error is NodeJS.ErrnoException & {
|
|
7
7
|
code: string;
|
|
8
8
|
};
|
|
9
|
+
/** Thrown for unusable command lines, which exit with status 2 like other CLIs. */
|
|
10
|
+
export declare class UsageError extends Error {
|
|
11
|
+
}
|
package/dist/errors.js
CHANGED
|
@@ -6,3 +6,6 @@ export class PrecastError extends Error {
|
|
|
6
6
|
export function isSystemError(error) {
|
|
7
7
|
return error instanceof Error && typeof error.code === "string";
|
|
8
8
|
}
|
|
9
|
+
/** Thrown for unusable command lines, which exit with status 2 like other CLIs. */
|
|
10
|
+
export class UsageError extends Error {
|
|
11
|
+
}
|
package/dist/generate.d.ts
CHANGED
|
@@ -15,3 +15,9 @@ export interface GenerateResult {
|
|
|
15
15
|
files: string[];
|
|
16
16
|
}
|
|
17
17
|
export declare function generate(options: GenerateOptions, root?: string): GenerateResult;
|
|
18
|
+
/**
|
|
19
|
+
* Resolve the output to the canonical path that will be written and check it:
|
|
20
|
+
* it must not exist yet and must be outside the source directory. Prompts use
|
|
21
|
+
* the same check so an answer is refused before any generation starts.
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkOutput(output: string, root?: string): string;
|
package/dist/generate.js
CHANGED
|
@@ -13,15 +13,7 @@ export function generate(options, root = ROOT) {
|
|
|
13
13
|
const platform = readPlatform(root, options.platform);
|
|
14
14
|
const context = makeContext(options, platform);
|
|
15
15
|
const files = collectFiles(root, options.platform, context, options.tier ?? DEFAULT_TIER);
|
|
16
|
-
|
|
17
|
-
// case-only alias of the source directory cannot slip past the checks.
|
|
18
|
-
const destination = canonicalPath(options.output);
|
|
19
|
-
if (exists(destination)) {
|
|
20
|
-
throw new PrecastError(`Output already exists; choose a new directory: ${destination}`);
|
|
21
|
-
}
|
|
22
|
-
if (isInside(destination, realpathSync.native(root))) {
|
|
23
|
-
throw new PrecastError("Output must be outside the Precast source directory");
|
|
24
|
-
}
|
|
16
|
+
const destination = checkOutput(options.output, root);
|
|
25
17
|
const listing = [...files.keys()].sort(comparePaths);
|
|
26
18
|
if (options.dryRun) {
|
|
27
19
|
return { destination, files: listing };
|
|
@@ -48,6 +40,23 @@ export function generate(options, root = ROOT) {
|
|
|
48
40
|
}
|
|
49
41
|
return { destination, files: listing };
|
|
50
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the output to the canonical path that will be written and check it:
|
|
45
|
+
* it must not exist yet and must be outside the source directory. Prompts use
|
|
46
|
+
* the same check so an answer is refused before any generation starts.
|
|
47
|
+
*/
|
|
48
|
+
export function checkOutput(output, root = ROOT) {
|
|
49
|
+
// The same canonical path is checked and written, so `..` after a symlink or a
|
|
50
|
+
// case-only alias of the source directory cannot slip past the checks.
|
|
51
|
+
const destination = canonicalPath(output);
|
|
52
|
+
if (exists(destination)) {
|
|
53
|
+
throw new PrecastError(`Output already exists; choose a new directory: ${destination}`);
|
|
54
|
+
}
|
|
55
|
+
if (isInside(destination, realpathSync.native(root))) {
|
|
56
|
+
throw new PrecastError("Output must be outside the Precast source directory");
|
|
57
|
+
}
|
|
58
|
+
return destination;
|
|
59
|
+
}
|
|
51
60
|
/** True for anything at the path, including a dangling symlink. */
|
|
52
61
|
function exists(path) {
|
|
53
62
|
try {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export { type Context, type ContextOptions, makeContext } from "./context.ts";
|
|
|
3
3
|
export { PrecastError } from "./errors.ts";
|
|
4
4
|
export { type GenerateOptions, type GenerateResult, generate, ROOT } from "./generate.ts";
|
|
5
5
|
export { type PlatformDescriptor, platformNames, readPlatform } from "./platforms.ts";
|
|
6
|
+
export { defaultConfigPath, type Profile, readProfile } from "./profiles.ts";
|
|
6
7
|
export { DEFAULT_TIER, TIERS, type Tier } from "./tiers.ts";
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { makeContext } from "./context.js";
|
|
|
3
3
|
export { PrecastError } from "./errors.js";
|
|
4
4
|
export { generate, ROOT } from "./generate.js";
|
|
5
5
|
export { platformNames, readPlatform } from "./platforms.js";
|
|
6
|
+
export { defaultConfigPath, readProfile } from "./profiles.js";
|
|
6
7
|
export { DEFAULT_TIER, TIERS } from "./tiers.js";
|
package/dist/platforms.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ export interface PlatformDescriptor {
|
|
|
7
7
|
tiers?: Record<string, string[]>;
|
|
8
8
|
/** Ordered values; later entries may refer to earlier ones with `{KEY}`. */
|
|
9
9
|
context: Record<string, string>;
|
|
10
|
+
/**
|
|
11
|
+
* Replacements for `context` entries that apply only when generating for
|
|
12
|
+
* `server_url` itself, such as a hosted instance's default CI image.
|
|
13
|
+
*/
|
|
14
|
+
default_server_context?: Record<string, string>;
|
|
10
15
|
}
|
|
11
16
|
/** Platform IDs are the names of directories that contain a descriptor. */
|
|
12
17
|
export declare function platformNames(root: string): string[];
|
package/dist/platforms.js
CHANGED
|
@@ -3,7 +3,13 @@ import { join } from "node:path";
|
|
|
3
3
|
import { PrecastError } from "./errors.js";
|
|
4
4
|
import { isPlainObject } from "./tiers.js";
|
|
5
5
|
const STRING_FIELDS = ["server_url", "runner", "codeowners_pattern"];
|
|
6
|
-
const KNOWN_FIELDS = new Set([
|
|
6
|
+
const KNOWN_FIELDS = new Set([
|
|
7
|
+
...STRING_FIELDS,
|
|
8
|
+
"nested_namespaces",
|
|
9
|
+
"tiers",
|
|
10
|
+
"context",
|
|
11
|
+
"default_server_context",
|
|
12
|
+
]);
|
|
7
13
|
/** Platform IDs are the names of directories that contain a descriptor. */
|
|
8
14
|
export function platformNames(root) {
|
|
9
15
|
const directory = join(root, "platforms");
|
|
@@ -49,6 +55,19 @@ export function parseDescriptor(value, name) {
|
|
|
49
55
|
!Object.values(context).every((entry) => typeof entry === "string")) {
|
|
50
56
|
throw new PrecastError(`${label} needs a context object of strings`);
|
|
51
57
|
}
|
|
58
|
+
const hosted = value.default_server_context;
|
|
59
|
+
if (hosted !== undefined) {
|
|
60
|
+
if (!isPlainObject(hosted) ||
|
|
61
|
+
!Object.values(hosted).every((entry) => typeof entry === "string")) {
|
|
62
|
+
throw new PrecastError(`${label} needs a default_server_context object of strings`);
|
|
63
|
+
}
|
|
64
|
+
// Only replacements are allowed, so every server renders the same tokens.
|
|
65
|
+
for (const key of Object.keys(hosted)) {
|
|
66
|
+
if (!Object.hasOwn(context, key)) {
|
|
67
|
+
throw new PrecastError(`${label} default_server_context replaces no context key: ${key}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
52
71
|
return value;
|
|
53
72
|
}
|
|
54
73
|
export function readJson(path) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Terminal } from "./prompts.ts";
|
|
2
|
+
export declare function profileUsage(root: string): string;
|
|
3
|
+
/** `precast profile <list|add|remove>`; returns the exit status. */
|
|
4
|
+
export declare function profileCommand(argv: string[], root: string, terminal: Terminal): Promise<number>;
|
|
5
|
+
/** How to use a saved profile, naming the settings file when it is not the default. */
|
|
6
|
+
export declare function useHint(name: string, config: string | undefined): string;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { PrecastError, UsageError } from "./errors.js";
|
|
3
|
+
import { platformNames, readPlatform } from "./platforms.js";
|
|
4
|
+
import { checkNewProfileName, checkProfileName, defaultConfigPath, listProfiles, profileNames, removeProfile, saveProfile, validateProfile, } from "./profiles.js";
|
|
5
|
+
import { choosePlatform, chooseTier, questions, shellQuote } from "./prompts.js";
|
|
6
|
+
import { isTier, TIERS } from "./tiers.js";
|
|
7
|
+
const OPTIONS = {
|
|
8
|
+
platform: { type: "string" },
|
|
9
|
+
"server-url": { type: "string" },
|
|
10
|
+
namespace: { type: "string" },
|
|
11
|
+
author: { type: "string" },
|
|
12
|
+
description: { type: "string" },
|
|
13
|
+
"default-branch": { type: "string" },
|
|
14
|
+
codeowner: { type: "string" },
|
|
15
|
+
runner: { type: "string" },
|
|
16
|
+
tier: { type: "string" },
|
|
17
|
+
config: { type: "string" },
|
|
18
|
+
force: { type: "boolean" },
|
|
19
|
+
"no-input": { type: "boolean" },
|
|
20
|
+
help: { type: "boolean", short: "h" },
|
|
21
|
+
};
|
|
22
|
+
export function profileUsage(root) {
|
|
23
|
+
return `Usage:
|
|
24
|
+
precast profile list
|
|
25
|
+
precast profile add <name> --platform <id> [options]
|
|
26
|
+
precast profile add Ask for the name and settings (at a terminal)
|
|
27
|
+
precast profile remove <name>
|
|
28
|
+
|
|
29
|
+
Save the settings you use for a server, such as your own Forgejo or an
|
|
30
|
+
organization's GitLab, and use them with precast --profile <name>.
|
|
31
|
+
|
|
32
|
+
Options for add (each is optional except --platform):
|
|
33
|
+
--platform <id> ${platformNames(root).join(", ")}
|
|
34
|
+
--server-url <url> The server; defaults to the platform's public one
|
|
35
|
+
--namespace <owner> Owner, organization, or GitLab group/subgroup
|
|
36
|
+
--author <name> Display name
|
|
37
|
+
--description <text> Project description
|
|
38
|
+
--default-branch <name> Default branch
|
|
39
|
+
--codeowner <@owner> Code owner for every file
|
|
40
|
+
--runner <label> Actions runner label or GitLab runner tag
|
|
41
|
+
--tier <tier> ${TIERS.join(", ")}
|
|
42
|
+
--force Replace a profile that already exists
|
|
43
|
+
|
|
44
|
+
Options for every subcommand:
|
|
45
|
+
--config <file> Settings file (default: ${defaultConfigPath()})
|
|
46
|
+
--no-input Never ask questions; report missing options instead
|
|
47
|
+
-h, --help Show this help
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
function parse(argv) {
|
|
51
|
+
return parseArgs({ args: argv, options: OPTIONS, strict: true, allowPositionals: true });
|
|
52
|
+
}
|
|
53
|
+
/** `precast profile <list|add|remove>`; returns the exit status. */
|
|
54
|
+
export async function profileCommand(argv, root, terminal) {
|
|
55
|
+
const { values, positionals } = parse(argv);
|
|
56
|
+
const [action, ...names] = positionals;
|
|
57
|
+
if (values.help) {
|
|
58
|
+
process.stdout.write(profileUsage(root));
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
const file = values.config ?? defaultConfigPath();
|
|
62
|
+
const write = (text) => process.stdout.write(text);
|
|
63
|
+
if (names.length > 1) {
|
|
64
|
+
throw new UsageError(`expected one profile name, got: ${names.join(" ")}`);
|
|
65
|
+
}
|
|
66
|
+
const name = names[0];
|
|
67
|
+
if (action === "list") {
|
|
68
|
+
if (name !== undefined)
|
|
69
|
+
throw new UsageError("profile list takes no name");
|
|
70
|
+
const entries = listProfiles(file, root);
|
|
71
|
+
if (entries.length === 0) {
|
|
72
|
+
write(`No profiles in ${file}. Add one with: precast profile add <name>\n`);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
write(`Profiles in ${file}:\n`);
|
|
76
|
+
const width = Math.max(...entries.map((entry) => entry.name.length));
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
const detail = entry.profile ? describe(entry.profile, root) : `invalid: ${entry.error}`;
|
|
79
|
+
write(` ${entry.name.padEnd(width)} ${detail}\n`);
|
|
80
|
+
}
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
if (action === "remove") {
|
|
84
|
+
if (name === undefined)
|
|
85
|
+
throw new UsageError("profile remove needs a profile name");
|
|
86
|
+
removeProfile(file, name);
|
|
87
|
+
write(`Removed profile '${name}' from ${file}\n`);
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
if (action === "add") {
|
|
91
|
+
return add(name, values, file, root, terminal, write);
|
|
92
|
+
}
|
|
93
|
+
throw new UsageError(action === undefined
|
|
94
|
+
? "profile needs a subcommand: list, add, or remove"
|
|
95
|
+
: `unknown profile subcommand: ${action}; use list, add, or remove`);
|
|
96
|
+
}
|
|
97
|
+
async function add(given, values, file, root, terminal, write) {
|
|
98
|
+
const replace = values.force === true;
|
|
99
|
+
const existing = new Set(profileNames(file));
|
|
100
|
+
const available = (name) => {
|
|
101
|
+
// Replacing an existing name keeps older names usable; new ones follow the stricter rule.
|
|
102
|
+
if (existing.has(name) && replace)
|
|
103
|
+
return checkProfileName(name);
|
|
104
|
+
checkNewProfileName(name);
|
|
105
|
+
if (existing.has(name)) {
|
|
106
|
+
throw new PrecastError(`Profile '${name}' already exists in ${file}; use --force to replace it`);
|
|
107
|
+
}
|
|
108
|
+
return name;
|
|
109
|
+
};
|
|
110
|
+
if (values.tier !== undefined && !isTier(values.tier)) {
|
|
111
|
+
throw new UsageError(`--tier must be one of: ${TIERS.join(", ")}`);
|
|
112
|
+
}
|
|
113
|
+
if (values.platform !== undefined && !platformNames(root).includes(values.platform)) {
|
|
114
|
+
throw new UsageError(`--platform must be one of: ${platformNames(root).join(", ")}`);
|
|
115
|
+
}
|
|
116
|
+
// Check a given name before asking anything, so answers are not wasted.
|
|
117
|
+
if (given !== undefined)
|
|
118
|
+
available(given);
|
|
119
|
+
// A draft holds unset values as undefined until `toProfile` drops them.
|
|
120
|
+
const profile = {
|
|
121
|
+
platform: values.platform ?? "",
|
|
122
|
+
serverUrl: values["server-url"],
|
|
123
|
+
namespace: values.namespace,
|
|
124
|
+
author: values.author,
|
|
125
|
+
description: values.description,
|
|
126
|
+
defaultBranch: values["default-branch"],
|
|
127
|
+
codeowner: values.codeowner,
|
|
128
|
+
runner: values.runner,
|
|
129
|
+
tier: values.tier !== undefined && isTier(values.tier) ? values.tier : undefined,
|
|
130
|
+
};
|
|
131
|
+
let name = given;
|
|
132
|
+
if (name === undefined || values.platform === undefined) {
|
|
133
|
+
if (!terminal.interactive || values["no-input"]) {
|
|
134
|
+
const missing = [
|
|
135
|
+
name === undefined ? "a profile name" : "",
|
|
136
|
+
values.platform === undefined ? "--platform" : "",
|
|
137
|
+
]
|
|
138
|
+
.filter(Boolean)
|
|
139
|
+
.join(" and ");
|
|
140
|
+
throw new UsageError(`profile add needs ${missing}`);
|
|
141
|
+
}
|
|
142
|
+
await terminal.prompt(async (ask) => {
|
|
143
|
+
const q = questions(ask, write);
|
|
144
|
+
name ??= await q.answer("Profile name", "(letters, digits, - and _)", available);
|
|
145
|
+
// Each answer is checked by validating the profile with it, the same rules
|
|
146
|
+
// as command-line options; Enter leaves an optional value unset.
|
|
147
|
+
const check = (key) => (reply) => {
|
|
148
|
+
validateProfile(toProfile({ ...profile, [key]: reply || undefined }), root);
|
|
149
|
+
return reply;
|
|
150
|
+
};
|
|
151
|
+
if (values.platform === undefined) {
|
|
152
|
+
profile.platform = await choosePlatform(q, root);
|
|
153
|
+
const server = readPlatform(root, profile.platform).server_url;
|
|
154
|
+
profile.serverUrl ??=
|
|
155
|
+
(await q.answer("Server URL", `(Enter for ${server})`, check("serverUrl"))) || undefined;
|
|
156
|
+
profile.namespace ??=
|
|
157
|
+
(await q.answer("Namespace", "(Enter to ask each time)", check("namespace"))) ||
|
|
158
|
+
undefined;
|
|
159
|
+
profile.author ??=
|
|
160
|
+
(await q.answer("Author", "(Enter to use the namespace)", check("author"))) || undefined;
|
|
161
|
+
profile.runner ??=
|
|
162
|
+
(await q.answer("Runner label or tag", "(Enter for the platform's default)", check("runner"))) || undefined;
|
|
163
|
+
profile.tier ??= await chooseTier(q, { label: "none", value: undefined });
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const saved = validateProfile(toProfile(profile), root);
|
|
168
|
+
saveProfile(file, name, saved, { replace });
|
|
169
|
+
write(`${existing.has(name) ? "Replaced" : "Saved"} profile '${name}' in ${file}\n` +
|
|
170
|
+
`Use it with: ${useHint(name, values.config)}\n`);
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
function toProfile(draft) {
|
|
174
|
+
return Object.fromEntries(Object.entries(draft).filter(([, value]) => value !== undefined));
|
|
175
|
+
}
|
|
176
|
+
/** One line of `profile list`: platform, server, and the defaults it sets. */
|
|
177
|
+
function describe(profile, root) {
|
|
178
|
+
const known = platformNames(root).includes(profile.platform);
|
|
179
|
+
const server = profile.serverUrl ??
|
|
180
|
+
(known ? `${readPlatform(root, profile.platform).server_url} (default)` : "unknown platform");
|
|
181
|
+
const extras = [
|
|
182
|
+
profile.namespace && `namespace ${profile.namespace}`,
|
|
183
|
+
profile.author && `author ${profile.author}`,
|
|
184
|
+
profile.runner && `runner ${profile.runner}`,
|
|
185
|
+
profile.tier && `tier ${profile.tier}`,
|
|
186
|
+
].filter(Boolean);
|
|
187
|
+
return [profile.platform, server, ...extras].join(" · ");
|
|
188
|
+
}
|
|
189
|
+
/** How to use a saved profile, naming the settings file when it is not the default. */
|
|
190
|
+
export function useHint(name, config) {
|
|
191
|
+
return config === undefined
|
|
192
|
+
? `precast --profile ${name}`
|
|
193
|
+
: `precast ${shellQuote(`--config=${config}`)} --profile ${name}`;
|
|
194
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Tier } from "./tiers.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Saved defaults for one server, such as a self-hosted Forgejo or an internal
|
|
4
|
+
* GitLab. Values a command line gives explicitly win over the profile's.
|
|
5
|
+
*/
|
|
6
|
+
export interface Profile {
|
|
7
|
+
platform: string;
|
|
8
|
+
serverUrl?: string;
|
|
9
|
+
namespace?: string;
|
|
10
|
+
author?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
defaultBranch?: string;
|
|
13
|
+
codeowner?: string;
|
|
14
|
+
runner?: string;
|
|
15
|
+
tier?: Tier;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Where profiles live unless `--config` names another file: the XDG config
|
|
19
|
+
* directory on macOS and Linux, and the roaming application data folder on Windows.
|
|
20
|
+
*/
|
|
21
|
+
export declare function defaultConfigPath(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform, home?: string): string;
|
|
22
|
+
/** Profiles by name, in the order the settings file lists them. */
|
|
23
|
+
export type Profiles = Map<string, Profile>;
|
|
24
|
+
/** Reject names that would be awkward on a command line or in JSON. */
|
|
25
|
+
export declare function checkProfileName(name: string): string;
|
|
26
|
+
/** The stricter rule for a name being created: also not only digits. */
|
|
27
|
+
export declare function checkNewProfileName(name: string): string;
|
|
28
|
+
/** The names a settings file holds, valid or not. */
|
|
29
|
+
export declare function profileNames(file: string): string[];
|
|
30
|
+
/** One entry of a settings file: its profile, or why it is invalid. */
|
|
31
|
+
export type ProfileEntry = {
|
|
32
|
+
name: string;
|
|
33
|
+
profile: Profile;
|
|
34
|
+
error?: undefined;
|
|
35
|
+
} | {
|
|
36
|
+
name: string;
|
|
37
|
+
profile?: undefined;
|
|
38
|
+
error: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Every entry in file order, each checked on its own so one bad entry hides no
|
|
42
|
+
* others. With `root`, values are also checked as options would be, such as the
|
|
43
|
+
* platform name and server URL.
|
|
44
|
+
*/
|
|
45
|
+
export declare function listProfiles(file: string, root?: string): ProfileEntry[];
|
|
46
|
+
/** Read every profile; any invalid entry is an error. */
|
|
47
|
+
export declare function readProfiles(file: string): Profiles;
|
|
48
|
+
/** Read one named profile, validating only that entry. */
|
|
49
|
+
export declare function readProfile(file: string, name: string): Profile;
|
|
50
|
+
/**
|
|
51
|
+
* Check a profile's values with the same rules as command-line options, by
|
|
52
|
+
* building a project context from them with a placeholder repository.
|
|
53
|
+
*/
|
|
54
|
+
export declare function validateProfile(profile: Profile, root: string): Profile;
|
|
55
|
+
/**
|
|
56
|
+
* Add or replace one profile and write the file in one step, keeping the other
|
|
57
|
+
* entries as they were. Replacing an existing profile needs `replace`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function saveProfile(file: string, name: string, profile: Profile, { replace, lockWaitMs, beforeReplace, }?: {
|
|
60
|
+
replace?: boolean;
|
|
61
|
+
lockWaitMs?: number;
|
|
62
|
+
/** Called with the written temporary file before it replaces the original; for tests. */
|
|
63
|
+
beforeReplace?: (temporary: string) => void;
|
|
64
|
+
}): void;
|
|
65
|
+
/** Remove one profile; it must exist. */
|
|
66
|
+
export declare function removeProfile(file: string, name: string, { lockWaitMs }?: {
|
|
67
|
+
lockWaitMs?: number;
|
|
68
|
+
}): void;
|