@theholocron/cli 5.0.0-alpha.120 → 5.0.0-alpha.121
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +85 -13
- package/dist/cli.mjs.map +1 -1
- package/package.json +4 -4
package/dist/cli.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
4
4
|
import path, { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
5
|
-
import { checkbox, input, select } from "@inquirer/prompts";
|
|
5
|
+
import { checkbox, confirm, input, select } from "@inquirer/prompts";
|
|
6
6
|
import { KNOWN_WORKFLOWS, createAstromech, deployOnRelease, extractPreviewConfig, readWorkspacePackages, reusableTemplates } from "@theholocron/astromech";
|
|
7
7
|
import { loadTasksConfig } from "@theholocron/astromech/config";
|
|
8
8
|
import yargs from "yargs";
|
|
@@ -1072,12 +1072,34 @@ async function runCleanupPreview(input) {
|
|
|
1072
1072
|
}
|
|
1073
1073
|
//#endregion
|
|
1074
1074
|
//#region src/commands/clone.ts
|
|
1075
|
+
/**
|
|
1076
|
+
* Accepts a bare org (`"theholocron"`) — clones every repo in it — or an
|
|
1077
|
+
* `org/repo` coordinate (`"theholocron/new-repo"`) — clones just that one
|
|
1078
|
+
* repo. The repo half is never silently discarded: if it's there, it's used.
|
|
1079
|
+
* A trailing slash with nothing after it (`"theholocron/"`) is treated the
|
|
1080
|
+
* same as a bare org.
|
|
1081
|
+
*/
|
|
1082
|
+
function parseCloneTarget(raw) {
|
|
1083
|
+
const trimmed = raw.trim();
|
|
1084
|
+
const slash = trimmed.indexOf("/");
|
|
1085
|
+
if (slash === -1) return { org: trimmed };
|
|
1086
|
+
const org = trimmed.slice(0, slash);
|
|
1087
|
+
const repo = trimmed.slice(slash + 1);
|
|
1088
|
+
return repo ? {
|
|
1089
|
+
org,
|
|
1090
|
+
repo
|
|
1091
|
+
} : { org };
|
|
1092
|
+
}
|
|
1075
1093
|
function encodeTokenForGitHttpAuth(token) {
|
|
1076
1094
|
const trimmed = token.trim();
|
|
1077
1095
|
if (!trimmed) throw new Error("empty token");
|
|
1078
1096
|
if (/[\u0000-\u001F\u007F\s]/.test(trimmed)) throw new Error("token contains whitespace or control characters");
|
|
1079
1097
|
return encodeURIComponent(trimmed);
|
|
1080
1098
|
}
|
|
1099
|
+
/** Strip a leading dot so a hidden repo (e.g. `.github`) is still visible in Finder once cloned. */
|
|
1100
|
+
function repoDirName(repo) {
|
|
1101
|
+
return repo.name.startsWith(".") ? repo.name.slice(1) : repo.name;
|
|
1102
|
+
}
|
|
1081
1103
|
async function listOrgRepos(org, token, fetchFn) {
|
|
1082
1104
|
const repos = [];
|
|
1083
1105
|
let url = `https://api.github.com/orgs/${org}/repos?per_page=100&type=all`;
|
|
@@ -1094,14 +1116,26 @@ async function listOrgRepos(org, token, fetchFn) {
|
|
|
1094
1116
|
}
|
|
1095
1117
|
return repos;
|
|
1096
1118
|
}
|
|
1119
|
+
async function getRepo(org, repo, token, fetchFn) {
|
|
1120
|
+
const res = await fetchFn(`https://api.github.com/repos/${org}/${repo}`, { headers: {
|
|
1121
|
+
Authorization: `Bearer ${token}`,
|
|
1122
|
+
Accept: "application/vnd.github+json",
|
|
1123
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
1124
|
+
} });
|
|
1125
|
+
if (res.status === 404) throw new Error(`${org}/${repo} not found — check the repo name and that the token can see it`);
|
|
1126
|
+
if (!res.ok) throw new Error(`GitHub API ${res.status}: ${res.statusText} — check that the token has repo:read scope`);
|
|
1127
|
+
return await res.json();
|
|
1128
|
+
}
|
|
1097
1129
|
async function runClone(input) {
|
|
1098
1130
|
const print = input.print ?? ((line) => console.log(line));
|
|
1099
1131
|
const logger = input.logger ?? getLogger();
|
|
1100
1132
|
const fetchFn = input.fetch ?? globalThis.fetch;
|
|
1101
1133
|
const dryRun = input.dryRun ?? false;
|
|
1102
1134
|
const targetDir = resolve(input.dir ?? join(homedir(), "Code", input.org));
|
|
1135
|
+
const label = input.repo ? `${input.org}/${input.repo}` : `org=${input.org}`;
|
|
1103
1136
|
logger.info({
|
|
1104
1137
|
org: input.org,
|
|
1138
|
+
repo: input.repo,
|
|
1105
1139
|
targetDir,
|
|
1106
1140
|
dryRun: dryRun || void 0
|
|
1107
1141
|
}, "clone: start");
|
|
@@ -1111,20 +1145,21 @@ async function runClone(input) {
|
|
|
1111
1145
|
stdio: "inherit"
|
|
1112
1146
|
}).status };
|
|
1113
1147
|
});
|
|
1114
|
-
print(style.header(`Holocron clone —
|
|
1148
|
+
print(style.header(`Holocron clone — ${label} → ${targetDir}${dryRun ? " (dry-run)" : ""}`));
|
|
1115
1149
|
if (!existsSync(targetDir)) {
|
|
1116
1150
|
if (dryRun) print(style.dim(` would create ${targetDir}`));
|
|
1117
1151
|
else mkdirSync(targetDir, { recursive: true });
|
|
1118
1152
|
}
|
|
1119
1153
|
let repos;
|
|
1120
1154
|
try {
|
|
1121
|
-
repos = await listOrgRepos(input.org, input.token, fetchFn);
|
|
1155
|
+
repos = input.repo ? [await getRepo(input.org, input.repo, input.token, fetchFn)] : await listOrgRepos(input.org, input.token, fetchFn);
|
|
1122
1156
|
} catch (err) {
|
|
1123
1157
|
const message = errorMessage(err);
|
|
1124
1158
|
logger.warn({
|
|
1125
1159
|
org: input.org,
|
|
1160
|
+
repo: input.repo,
|
|
1126
1161
|
reason: message
|
|
1127
|
-
}, "clone: failed to list
|
|
1162
|
+
}, "clone: failed to list repos");
|
|
1128
1163
|
return {
|
|
1129
1164
|
status: "fail",
|
|
1130
1165
|
cloned: 0,
|
|
@@ -1133,12 +1168,33 @@ async function runClone(input) {
|
|
|
1133
1168
|
message
|
|
1134
1169
|
};
|
|
1135
1170
|
}
|
|
1171
|
+
if (!input.repo && !dryRun && input.confirmWholeOrg) {
|
|
1172
|
+
const alreadyCloned = repos.filter((r) => existsSync(join(targetDir, repoDirName(r)))).length;
|
|
1173
|
+
const scope = {
|
|
1174
|
+
total: repos.length,
|
|
1175
|
+
alreadyCloned,
|
|
1176
|
+
toClone: repos.length - alreadyCloned
|
|
1177
|
+
};
|
|
1178
|
+
if (!await input.confirmWholeOrg(scope)) {
|
|
1179
|
+
print(style.dim(" aborted — nothing cloned"));
|
|
1180
|
+
logger.info({
|
|
1181
|
+
org: input.org,
|
|
1182
|
+
...scope
|
|
1183
|
+
}, "clone: aborted by user");
|
|
1184
|
+
return {
|
|
1185
|
+
status: "aborted",
|
|
1186
|
+
cloned: 0,
|
|
1187
|
+
skipped: 0,
|
|
1188
|
+
failed: 0,
|
|
1189
|
+
message: "aborted by user"
|
|
1190
|
+
};
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1136
1193
|
let cloned = 0;
|
|
1137
1194
|
let skipped = 0;
|
|
1138
1195
|
let failed = 0;
|
|
1139
1196
|
for (const repo of repos) {
|
|
1140
|
-
const
|
|
1141
|
-
const dest = join(targetDir, dirName);
|
|
1197
|
+
const dest = join(targetDir, repoDirName(repo));
|
|
1142
1198
|
if (existsSync(dest)) {
|
|
1143
1199
|
print(style.dim(` skip ${repo.full_name}`));
|
|
1144
1200
|
skipped++;
|
|
@@ -1182,6 +1238,7 @@ async function runClone(input) {
|
|
|
1182
1238
|
const status = dryRun ? "dry-run" : failed > 0 ? "fail" : "ok";
|
|
1183
1239
|
logger[failed > 0 ? "warn" : "info"]({
|
|
1184
1240
|
org: input.org,
|
|
1241
|
+
repo: input.repo,
|
|
1185
1242
|
status,
|
|
1186
1243
|
cloned,
|
|
1187
1244
|
skipped,
|
|
@@ -8009,8 +8066,13 @@ const numeric = (value) => /^\d+$/.test(value) ? true : "enter a number";
|
|
|
8009
8066
|
const COMMAND_REGISTRY = [
|
|
8010
8067
|
{
|
|
8011
8068
|
name: "clone",
|
|
8012
|
-
description: "Clone
|
|
8013
|
-
positionals: [
|
|
8069
|
+
description: "Clone a single repo (org/repo) or every repo in an org (org)",
|
|
8070
|
+
positionals: [{
|
|
8071
|
+
key: "target",
|
|
8072
|
+
message: "org/repo to clone (or a bare org to clone every repo in it):",
|
|
8073
|
+
type: "input",
|
|
8074
|
+
validate: (v) => v.trim().length > 0 || "org/repo or org is required"
|
|
8075
|
+
}]
|
|
8014
8076
|
},
|
|
8015
8077
|
{
|
|
8016
8078
|
name: "doctor",
|
|
@@ -8543,10 +8605,9 @@ try {
|
|
|
8543
8605
|
org: argv.org
|
|
8544
8606
|
});
|
|
8545
8607
|
finishCommand = startCommand(name);
|
|
8546
|
-
}).command("clone", "Clone
|
|
8608
|
+
}).command("clone [target]", "Clone a single repo (org/repo) or every repo in an org (org)", (y) => y.positional("target", {
|
|
8547
8609
|
type: "string",
|
|
8548
|
-
|
|
8549
|
-
describe: "GitHub org to clone (e.g., theholocron)"
|
|
8610
|
+
describe: "org/repo to clone, or a bare org to clone every repo in it (confirms first)."
|
|
8550
8611
|
}).option("dir", {
|
|
8551
8612
|
type: "string",
|
|
8552
8613
|
describe: "Parent directory to clone into (default: ~/Code/<org>)"
|
|
@@ -8561,11 +8622,22 @@ try {
|
|
|
8561
8622
|
process.exitCode = 1;
|
|
8562
8623
|
return;
|
|
8563
8624
|
}
|
|
8625
|
+
const [targetInput] = await promptForPositionals(getEntry("clone"), argv);
|
|
8626
|
+
const { org, repo } = parseCloneTarget(targetInput);
|
|
8564
8627
|
if ((await runClone({
|
|
8565
|
-
org
|
|
8628
|
+
org,
|
|
8629
|
+
...repo ? { repo } : {},
|
|
8566
8630
|
token,
|
|
8567
8631
|
dryRun: argv.dryRun,
|
|
8568
|
-
...argv.dir ? { dir: argv.dir } : {}
|
|
8632
|
+
...argv.dir ? { dir: argv.dir } : {},
|
|
8633
|
+
...!repo ? { confirmWholeOrg: async ({ total, alreadyCloned, toClone }) => {
|
|
8634
|
+
if (!process.stdin.isTTY) throw new NonInteractiveError(`clone: cloning every repo in "${org}" needs interactive confirmation — run this from a terminal, or pass a specific org/repo instead`);
|
|
8635
|
+
if (toClone === 0) return true;
|
|
8636
|
+
return confirm({
|
|
8637
|
+
message: `${org} has ${total} repos — ${alreadyCloned} already cloned locally, ${toClone} new. Clone them all?`,
|
|
8638
|
+
default: false
|
|
8639
|
+
});
|
|
8640
|
+
} } : {}
|
|
8569
8641
|
})).status === "fail") process.exitCode = 1;
|
|
8570
8642
|
}).command("doctor", "Load the config and run a smoke check against every provider", (y) => y.option("repo", {
|
|
8571
8643
|
type: "string",
|