@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.10
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/LICENSE +24 -0
- package/README.md +212 -2
- package/bin/sft.js +22 -0
- package/config/changeset-types.txt +350 -0
- package/config/install/gitmessage +6 -0
- package/config/install/release/README.md +26 -0
- package/config/install/release/deploy-target.txt +368 -0
- package/config/install/release/remove-target.txt +279 -0
- package/config/metadata-capability.txt +118 -0
- package/config/metadata-dirs.txt +79 -0
- package/config/metasync-detect.txt +52 -0
- package/config/metasync-extra.txt +30 -0
- package/config/metasync-fingerprint.txt +90 -0
- package/dist/cli.js +35 -0
- package/dist/commands/admin.js +46 -0
- package/dist/commands/check.js +20 -0
- package/dist/commands/ci.js +123 -0
- package/dist/commands/deploy.js +31 -0
- package/dist/commands/doctor.js +11 -0
- package/dist/commands/dryrun.js +17 -0
- package/dist/commands/hook.js +46 -0
- package/dist/commands/index.js +37 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +57 -0
- package/dist/commands/log.js +58 -0
- package/dist/commands/metasync.js +21 -0
- package/dist/commands/pr.js +32 -0
- package/dist/commands/project.js +102 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +67 -0
- package/dist/commands/switch-org.js +12 -0
- package/dist/commands/update-cli.js +11 -0
- package/dist/commands/version.js +18 -0
- package/dist/core/admin-clone.js +79 -0
- package/dist/core/ai-answers.js +40 -0
- package/dist/core/branches.js +88 -0
- package/dist/core/check-targets.js +170 -0
- package/dist/core/check.js +34 -0
- package/dist/core/ci-check-branch.js +150 -0
- package/dist/core/ci-login-steps.js +73 -0
- package/dist/core/ci-release-steps.js +193 -0
- package/dist/core/ci-sequence-steps.js +188 -0
- package/dist/core/ci-validate-steps.js +140 -0
- package/dist/core/cli-update-notice.js +47 -0
- package/dist/core/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +358 -0
- package/dist/core/create-state.js +82 -0
- package/dist/core/delegate.js +45 -0
- package/dist/core/deploy.js +66 -0
- package/dist/core/diag.js +200 -0
- package/dist/core/doctor.js +211 -0
- package/dist/core/eca.js +259 -0
- package/dist/core/exit-codes.js +12 -0
- package/dist/core/gh-identity.js +46 -0
- package/dist/core/hook-install.js +130 -0
- package/dist/core/hook.js +116 -0
- package/dist/core/identity.js +48 -0
- package/dist/core/job.js +243 -0
- package/dist/core/launcher-status.js +215 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +109 -0
- package/dist/core/log-collector.js +370 -0
- package/dist/core/log-format.js +44 -0
- package/dist/core/log-pull.js +565 -0
- package/dist/core/log-select.js +126 -0
- package/dist/core/log-session.js +362 -0
- package/dist/core/log-start.js +421 -0
- package/dist/core/log-status.js +223 -0
- package/dist/core/log-summarize.js +127 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/login-url.js +50 -0
- package/dist/core/manifest-build.js +118 -0
- package/dist/core/mask.js +70 -0
- package/dist/core/metadata-types.js +79 -0
- package/dist/core/metasync.js +684 -0
- package/dist/core/new-version.js +93 -0
- package/dist/core/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +217 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +246 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-upgrade.js +138 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +401 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +163 -0
- package/dist/core/release.js +256 -0
- package/dist/core/scoped-box.js +29 -0
- package/dist/core/start.js +155 -0
- package/dist/core/switch-org.js +49 -0
- package/dist/core/test-types.js +212 -0
- package/dist/core/toolbox.js +4 -0
- package/dist/core/update-cli.js +156 -0
- package/dist/core/update-secret.js +301 -0
- package/dist/core/update-workflows.js +132 -0
- package/dist/core/verify-gh.js +286 -0
- package/dist/core/verify-samples.js +152 -0
- package/dist/core/windows-shim.js +21 -0
- package/dist/io/capability-file.js +11 -0
- package/dist/io/create-deps.js +69 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +39 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +172 -0
- package/dist/io/log-deps.js +107 -0
- package/dist/io/metasync-deps.js +22 -0
- package/dist/io/open-browser.js +21 -0
- package/dist/io/package-version.js +10 -0
- package/dist/io/pinned-project.js +111 -0
- package/dist/io/promote-deps.js +28 -0
- package/dist/io/push-refs.js +20 -0
- package/dist/io/resolve.js +74 -0
- package/dist/io/run.js +201 -0
- package/dist/io/switch-org-deps.js +10 -0
- package/dist/io/temp-dir.js +24 -0
- package/dist/io/tool-versions.js +14 -0
- package/dist/io/toolbox.js +68 -0
- package/dist/io/update-cli-deps.js +75 -0
- package/dist/io/update-secret-deps.js +27 -0
- package/dist/launch.js +152 -0
- package/dist/ui/confirm.js +18 -0
- package/dist/ui/launcher.js +163 -0
- package/dist/ui/prompt.js +66 -0
- package/lib/apexlog.js +819 -0
- package/lib/metasync.js +1364 -0
- package/lib/package.json +4 -0
- package/lib/typecheck.js +547 -0
- package/package.json +44 -4
- package/templates/.forceignore +12 -0
- package/templates/.gitattributes +58 -0
- package/templates/.github/workflows/wf-metasync.yml +177 -0
- package/templates/.github/workflows/wf-release.yml +454 -0
- package/templates/.github/workflows/wf-sequence.yml +292 -0
- package/templates/.github/workflows/wf-validate.yml +389 -0
- package/templates/.prettierignore +11 -0
- package/templates/.prettierrc +17 -0
- package/templates/.vscode/extensions.json +9 -0
- package/templates/.vscode/launch.json +16 -0
- package/templates/.vscode/settings.json +39 -0
- package/templates/AGENTS.md +3 -0
- package/templates/CLAUDE.md +4 -0
- package/templates/README.md +11 -0
- package/templates/config/project-scratch-def.json +13 -0
- package/templates/eslint.config.js +55 -0
- package/templates/force-app/main/default/.gitkeep +0 -0
- package/templates/gitignore.template +62 -0
- package/templates/jest.config.js +6 -0
- package/templates/package.json +29 -0
- package/templates/scripts/apex/hello.apex +10 -0
- package/templates/scripts/soql/account.soql +6 -0
- package/templates/sf-tools/config/branches.txt +24 -0
- package/templates/sf-tools/config/metadata.txt +576 -0
- package/templates/sfdx-project.json +12 -0
- package/templates-ts/.github/workflows/wf-metasync.yml +109 -0
- package/templates-ts/.github/workflows/wf-release.yml +263 -0
- package/templates-ts/.github/workflows/wf-sequence.yml +153 -0
- package/templates-ts/.github/workflows/wf-validate.yml +185 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { EcaStop, registerJwtSecretEca } from "./eca.js";
|
|
2
|
+
import { originRepo } from "./origin.js";
|
|
3
|
+
import { tierSwitchWfProblems } from "./project-status.js";
|
|
4
|
+
import { ghOwnerProblem } from "./update-secret.js";
|
|
5
|
+
const BRANCHES_REL = "sf-tools/config/branches.txt";
|
|
6
|
+
const TIERS = {
|
|
7
|
+
staging: { tier: "staging", suffix: "STG", label: "ステージング組織", alias: "staging" },
|
|
8
|
+
develop: { tier: "develop", suffix: "DEV", label: "開発組織", alias: "develop" },
|
|
9
|
+
};
|
|
10
|
+
class Stop extends Error {
|
|
11
|
+
code;
|
|
12
|
+
constructor(code, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** シェルの $(...) と同じく、末尾の改行を除き、CR も除く */
|
|
18
|
+
const shellText = (s) => s.replace(/\r/g, "").replace(/\n+$/, "");
|
|
19
|
+
const trimmedLines = (text) => text.split("\n").map((l) => l.trim());
|
|
20
|
+
export const tierTextHasLine = (text, name) => trimmedLines(shellText(text)).includes(name);
|
|
21
|
+
/** text から name の行(前後の空白を除いて一致)だけを除く。ほかの行はそのまま(CR は除く)。各行に改行を付ける */
|
|
22
|
+
export const tierTextWithoutLine = (text, name) => shellText(text)
|
|
23
|
+
.split("\n")
|
|
24
|
+
.filter((l) => l.trim() !== name)
|
|
25
|
+
.map((l) => `${l}\n`)
|
|
26
|
+
.join("");
|
|
27
|
+
/** 末尾に name を足した内容(元が空なら name だけ) */
|
|
28
|
+
export const tierTextWithLine = (text, name) => {
|
|
29
|
+
const t = shellText(text);
|
|
30
|
+
return `${t !== "" ? `${t}\n` : ""}${name}\n`;
|
|
31
|
+
};
|
|
32
|
+
const stamp = (d) => {
|
|
33
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
34
|
+
return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
|
|
35
|
+
};
|
|
36
|
+
async function run(box, cmd, args, input) {
|
|
37
|
+
const r = await box.run(cmd, args, input !== undefined ? { input } : {}).catch(() => null);
|
|
38
|
+
return { ok: r !== null && r.code === 0, out: r?.stdout.replace(/\r/g, "") ?? "" };
|
|
39
|
+
}
|
|
40
|
+
async function must(box, cmd, args, message) {
|
|
41
|
+
const r = await run(box, cmd, args);
|
|
42
|
+
if (!r.ok)
|
|
43
|
+
throw new Stop(1, message);
|
|
44
|
+
return r.out;
|
|
45
|
+
}
|
|
46
|
+
async function confirm(ask, question) {
|
|
47
|
+
const a = await ask("", question);
|
|
48
|
+
if (a === "need")
|
|
49
|
+
throw new Stop(20, "");
|
|
50
|
+
if (a !== "yes")
|
|
51
|
+
throw new Stop(1, "中断しました。");
|
|
52
|
+
}
|
|
53
|
+
function warning(box, lines) {
|
|
54
|
+
for (const l of ["", ...lines, ""])
|
|
55
|
+
box.output.err(l);
|
|
56
|
+
}
|
|
57
|
+
async function ownerCheck(c) {
|
|
58
|
+
const p = await ghOwnerProblem(c.box, c.owner);
|
|
59
|
+
if (p !== null)
|
|
60
|
+
throw new Stop(1, p);
|
|
61
|
+
}
|
|
62
|
+
async function clean(box) {
|
|
63
|
+
const r = await run(box, "git", ["status", "--porcelain"]);
|
|
64
|
+
if (r.out.trim() !== "")
|
|
65
|
+
throw new Stop(1, "未コミットの変更があります。コミットまたは退避してから、実行してください。");
|
|
66
|
+
}
|
|
67
|
+
async function prepare(args, box, deps, ask, cmd) {
|
|
68
|
+
const rest = args.filter((a) => a !== "-v" && a !== "--verbose");
|
|
69
|
+
const name = rest.find((a) => !a.startsWith("-"));
|
|
70
|
+
const flags = rest.filter((a) => a.startsWith("-"));
|
|
71
|
+
const t = name === "staging" || name === "develop" ? TIERS[name] : undefined;
|
|
72
|
+
if (t === undefined || rest.filter((a) => !a.startsWith("-")).length !== 1)
|
|
73
|
+
throw new Stop(1, `${cmd} には staging または develop を指定してください。`);
|
|
74
|
+
if (!deps.folderName.startsWith("force-"))
|
|
75
|
+
throw new Stop(1, "force-* ディレクトリ内で実行してください。");
|
|
76
|
+
const origin = await originRepo(box);
|
|
77
|
+
if (origin.kind !== "ok")
|
|
78
|
+
throw new Stop(1, "origin から GitHub のリポジトリを特定できません。");
|
|
79
|
+
await must(box, "git", ["fetch", "origin", "main"], "origin/main の取得に失敗しました。");
|
|
80
|
+
const shown = await box.run("git", ["show", `origin/main:${BRANCHES_REL}`], { env: { MSYS_NO_PATHCONV: "1" } }).catch(() => null);
|
|
81
|
+
if (shown === null || shown.code !== 0)
|
|
82
|
+
throw new Stop(1, `origin/main の ${BRANCHES_REL} を読めません。`);
|
|
83
|
+
const repo = `${origin.owner}/${origin.repo}`;
|
|
84
|
+
return [{ box, deps, ask, repo, owner: origin.owner, repoName: origin.repo, t, main: shellText(shown.stdout) }, flags];
|
|
85
|
+
}
|
|
86
|
+
async function names(c, kind) {
|
|
87
|
+
const r = await run(c.box, "gh", [kind, "list", "-R", c.repo, "--json", "name", "--jq", ".[].name"]);
|
|
88
|
+
return r.ok ? r.out.split("\n").filter((l) => l !== "") : null;
|
|
89
|
+
}
|
|
90
|
+
/** 作業用ブランチで branches.txt を置き換え、PR を作って、実行前のブランチに戻る。戻り値: PR の URL */
|
|
91
|
+
async function branchesPr(c, kind, text, title, body) {
|
|
92
|
+
const { box } = c;
|
|
93
|
+
const prBranch = `system-${kind}-tier-${c.t.tier}-${stamp(box.clock.now())}`;
|
|
94
|
+
await must(box, "git", ["checkout", "-b", prBranch, "origin/main"], `作業用ブランチの作成に失敗しました: ${prBranch}`);
|
|
95
|
+
// シェル版は一時ファイル + mv。ここは Node で直接書く(Windows でも mv のコマンドに頼らない)
|
|
96
|
+
try {
|
|
97
|
+
box.files.writeText(BRANCHES_REL, text);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
throw new Stop(1, `branches.txt の更新に失敗しました: ${BRANCHES_REL}`);
|
|
101
|
+
}
|
|
102
|
+
box.output.err(kind === "add"
|
|
103
|
+
? `[SUCCESS] branches.txt に ${c.t.tier} を追記しました。`
|
|
104
|
+
: `[SUCCESS] branches.txt から ${c.t.tier} の行を消しました。`);
|
|
105
|
+
await must(box, "git", ["add", BRANCHES_REL], "git add に失敗しました。");
|
|
106
|
+
await must(box, "git", ["commit", "--no-verify", "-m", title], "git commit に失敗しました。");
|
|
107
|
+
// --no-verify: 作業用ブランチへの、管理者による自動の操作のため、pre-push フック(検証の実行)を省く
|
|
108
|
+
await must(box, "git", ["push", "--no-verify", "-u", "origin", prBranch], `git push に失敗しました(作業用ブランチ: ${prBranch}${kind === "add" ? "。Salesforce への接続は、完了しています" : ""})。`);
|
|
109
|
+
const url = await must(box, "gh", ["pr", "create", "--base", "main", "--head", prBranch, "-R", c.repo, "--title", title, "--body", body], "PR の作成に失敗しました。");
|
|
110
|
+
return url.trim().split("\n").pop() ?? "";
|
|
111
|
+
}
|
|
112
|
+
async function originalBranch(c) {
|
|
113
|
+
const r = await run(c.box, "git", ["symbolic-ref", "--short", "HEAD"]);
|
|
114
|
+
const b = r.ok && r.out.trim() !== "" ? r.out.trim() : "main";
|
|
115
|
+
c.box.output.err(`[INFO] 実行前のブランチ: ${b}(終わったら、ここへ戻ります)`);
|
|
116
|
+
return b;
|
|
117
|
+
}
|
|
118
|
+
async function addTier(args, box, deps, ask) {
|
|
119
|
+
const err = (l) => box.output.err(l);
|
|
120
|
+
const [c, flags] = await prepare(args, box, deps, ask, "add-tier");
|
|
121
|
+
if (flags.length > 0)
|
|
122
|
+
throw new Stop(1, `不明なオプションです: ${flags[0]}`);
|
|
123
|
+
const { t } = c;
|
|
124
|
+
err(`[HEADER] tier 追加: ${t.tier}`);
|
|
125
|
+
if (tierTextHasLine(c.main, t.tier))
|
|
126
|
+
throw new Stop(1, `${t.tier} はすでに branches.txt(origin/main)に登録されています。`);
|
|
127
|
+
if (t.tier === "develop" && !tierTextHasLine(c.main, "staging"))
|
|
128
|
+
throw new Stop(1, "develop を追加するには先に staging が必要です(origin/main の branches.txt にありません)。");
|
|
129
|
+
err(`[INFO] 対象リポジトリ: ${c.repo}`);
|
|
130
|
+
err(`[INFO] 追加 tier : ${t.tier}(${t.label})`);
|
|
131
|
+
warning(box, [
|
|
132
|
+
"!! このコマンドは管理者専用です",
|
|
133
|
+
" 段階(ブランチ)と、Salesforce 組織への接続を",
|
|
134
|
+
" 追加します。Secrets / Variables が書き換わります",
|
|
135
|
+
]);
|
|
136
|
+
err("[WARNING] 途中で失敗しても、Salesforce 側に作った外部クライアントアプリと、登録した Secrets / Variables と、作成したブランチは、自動では、元に戻しません。");
|
|
137
|
+
await ownerCheck(c);
|
|
138
|
+
await confirm(ask, " 続行しますか?");
|
|
139
|
+
await clean(box);
|
|
140
|
+
// 接続(先に行う: 本番組織は main にしかつながない。つなげなければ、ブランチは変えない)
|
|
141
|
+
err(`[HEADER] Step 1: ${t.label} を、Salesforce 組織に接続します。`);
|
|
142
|
+
let skip = false;
|
|
143
|
+
const secrets = (await names(c, "secret")) ?? [];
|
|
144
|
+
const vr = await run(box, "gh", ["variable", "list", "-R", c.repo, "--json", "name,value", "--jq", '.[] | "\\(.name)=\\(.value)"']);
|
|
145
|
+
const vars = vr.ok ? vr.out.split("\n") : [];
|
|
146
|
+
const hasVar = (n) => vars.some((l) => l.startsWith(`${n}=`));
|
|
147
|
+
if (secrets.includes(`SF_CONSUMER_KEY_${t.suffix}`) && hasVar(`SF_USERNAME_${t.suffix}`) && hasVar(`SF_INSTANCE_URL_${t.suffix}`)) {
|
|
148
|
+
err(`[INFO] ${t.tier} の接続情報が、GitHub に残っています(前に remove-tier で外した段階の可能性があります):`);
|
|
149
|
+
for (const k of ["SF_ORG_NAME", "SF_USERNAME", "SF_INSTANCE_URL", "SF_ORG_CHECKED"]) {
|
|
150
|
+
const v = vars.find((l) => l.startsWith(`${k}_${t.suffix}=`));
|
|
151
|
+
if (v !== undefined)
|
|
152
|
+
err(`[INFO] ${v}`);
|
|
153
|
+
}
|
|
154
|
+
err("[INFO] 残っている接続情報が今も使えるか(組織のアプリ・ユーザー)は、ここでは確かめられません。PR のマージ後の検証・リリースで分かります。");
|
|
155
|
+
const a = await ask("", " つなぎ直さずに、段階だけ戻しますか?(N: ブラウザでログインして、つなぎ直す)");
|
|
156
|
+
if (a === "need")
|
|
157
|
+
throw new Stop(20, "");
|
|
158
|
+
if (a === "yes") {
|
|
159
|
+
skip = true;
|
|
160
|
+
err("[INFO] つなぎ直さずに、段階だけ戻します(Salesforce 組織への接続は、行いません)。");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!skip) {
|
|
164
|
+
const jwtDir = `${deps.home}/.sf-jwt/${c.repoName}`;
|
|
165
|
+
if (!box.files.exists(`${jwtDir}/server.key`))
|
|
166
|
+
throw new Stop(1, `秘密鍵が見つかりません: ${jwtDir}/server.key\n sft project create(Phase 10)を先に実行してください。`);
|
|
167
|
+
if (!box.files.exists(`${jwtDir}/server.crt`))
|
|
168
|
+
throw new Stop(1, `証明書が見つかりません: ${jwtDir}/server.crt`);
|
|
169
|
+
await registerJwtSecretEca(box, deps, ask, { ...t, repo: c.repo, keyFile: `${jwtDir}/server.key`, certFile: `${jwtDir}/server.crt` });
|
|
170
|
+
}
|
|
171
|
+
// ブランチと branches.txt(main へは直接 push しない)
|
|
172
|
+
err(`[HEADER] Step 2: ${t.tier} ブランチを追加します。`);
|
|
173
|
+
const original = await originalBranch(c);
|
|
174
|
+
await must(box, "git", ["checkout", "main"], "main ブランチへの切り替えに失敗しました。");
|
|
175
|
+
// 新しいブランチは、手順 1 で取得した origin/main から作る(手元の main は古いことがあるため。取り直さない)
|
|
176
|
+
if ((await run(box, "git", ["ls-remote", "--exit-code", "--heads", "origin", t.tier])).ok) {
|
|
177
|
+
err(`[INFO] ${t.tier} ブランチはリモートに既に存在します(スキップ)。`);
|
|
178
|
+
// remove-tier で残したブランチは、main より遅れていることがある(main の変更は、環境のブランチへ伝播しない)
|
|
179
|
+
if ((await run(box, "git", ["fetch", "origin", t.tier])).ok) {
|
|
180
|
+
const behind = (await run(box, "git", ["rev-list", "--count", `origin/${t.tier}..origin/main`])).out.trim();
|
|
181
|
+
if (behind !== "" && behind !== "0")
|
|
182
|
+
err(`[WARNING] ${t.tier} ブランチは、main より ${behind} コミット遅れています。(main の変更は伝播しません。開発のブランチは sft push で main を取り込むため、その PR をマージすると追いつきます)`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// -B: 前回の失敗で、ローカルに同名のブランチが残っていても、作り直す
|
|
187
|
+
await must(box, "git", ["checkout", "-B", t.tier, "origin/main"], `${t.tier} ブランチの作成に失敗しました。`);
|
|
188
|
+
await must(box, "git", ["push", "--no-verify", "-u", "origin", t.tier], `${t.tier} ブランチのプッシュに失敗しました。`);
|
|
189
|
+
await must(box, "git", ["checkout", "main"], "main ブランチへの切り替えに失敗しました。");
|
|
190
|
+
err(`[SUCCESS] ${t.tier} ブランチを作成しました。`);
|
|
191
|
+
}
|
|
192
|
+
const url = await branchesPr(c, "add", tierTextWithLine(c.main, t.tier), `chore: ${t.tier} tier を追加`, `branches.txt に ${t.tier} を追加します(sft project add-tier ${t.tier} で作成)。マージすると、段階として有効になります。`);
|
|
193
|
+
await must(box, "git", ["checkout", original], `${original} ブランチへの切り替えに失敗しました。`);
|
|
194
|
+
err(`[SUCCESS] tier 追加の準備ができました: ${t.tier}(${t.label})`);
|
|
195
|
+
err("[INFO] 次のステップ:");
|
|
196
|
+
err(`[INFO] ・PR をマージしてください(マージすると、branches.txt が main に反映されます): ${url}`);
|
|
197
|
+
err(`[INFO] ・GitHub Actions(wf-release / wf-validate)が ${t.tier} ブランチに対して動作することを確認してください。`);
|
|
198
|
+
err("[INFO] ・いまの接続を見る: sft project status");
|
|
199
|
+
return 0;
|
|
200
|
+
}
|
|
201
|
+
const connNames = (s) => ({
|
|
202
|
+
secrets: [`SF_CONSUMER_KEY_${s}`],
|
|
203
|
+
variables: ["SF_USERNAME", "SF_INSTANCE_URL", "SF_ORG_ID", "SF_ORG_NAME", "SF_ORG_KIND", "SF_ORG_CHECKED"].map((n) => `${n}_${s}`),
|
|
204
|
+
});
|
|
205
|
+
async function purgeSecrets(c) {
|
|
206
|
+
const { box, t } = c;
|
|
207
|
+
const err = (l) => box.output.err(l);
|
|
208
|
+
if (tierTextHasLine(c.main, t.tier))
|
|
209
|
+
throw new Stop(1, `${t.tier} は、まだ branches.txt(origin/main)にあります。段階を外す PR のマージ前には、接続情報を消せません。`);
|
|
210
|
+
const es = await names(c, "secret");
|
|
211
|
+
if (es === null)
|
|
212
|
+
throw new Stop(1, "Secrets の一覧を取得できません。");
|
|
213
|
+
const ev = await names(c, "variable");
|
|
214
|
+
if (ev === null)
|
|
215
|
+
throw new Stop(1, "Variables の一覧を取得できません。");
|
|
216
|
+
const n = connNames(t.suffix);
|
|
217
|
+
const ts = n.secrets.filter((x) => es.includes(x));
|
|
218
|
+
const tv = n.variables.filter((x) => ev.includes(x));
|
|
219
|
+
if (ts.length + tv.length === 0) {
|
|
220
|
+
err(`[SUCCESS] ${t.tier} の接続情報(Secrets / Variables)は、ありません。消すものはありません。`);
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
warning(box, [
|
|
224
|
+
"!! このコマンドは管理者専用です",
|
|
225
|
+
` ${t.tier} の接続情報を消します。この段階の`,
|
|
226
|
+
" 組織に、CI からつながらなくなります",
|
|
227
|
+
]);
|
|
228
|
+
err(`[INFO] 消す Secrets : ${ts.join(" ") || "(なし)"}`);
|
|
229
|
+
err(`[INFO] 消す Variables: ${tv.join(" ") || "(なし)"}`);
|
|
230
|
+
err(`[WARNING] Secret の値は、消すと戻せません(戻すときは sft project add-tier ${t.tier} で、新しくつなぎ直します)。`);
|
|
231
|
+
await ownerCheck(c);
|
|
232
|
+
await confirm(c.ask, " 消しますか?");
|
|
233
|
+
let failed = 0;
|
|
234
|
+
for (const [kind, label, list] of [
|
|
235
|
+
["secret", "Secret", ts],
|
|
236
|
+
["variable", "Variable", tv],
|
|
237
|
+
])
|
|
238
|
+
for (const x of list) {
|
|
239
|
+
if ((await run(box, "gh", [kind, "delete", x, "-R", c.repo])).ok)
|
|
240
|
+
err(`[SUCCESS] 消しました(${label}): ${x}`);
|
|
241
|
+
else {
|
|
242
|
+
err(`[WARNING] 消せませんでした(${label}): ${x}`);
|
|
243
|
+
failed++;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (failed > 0) {
|
|
247
|
+
err(`[ERROR] ${failed} 個を消せませんでした。もう一度実行すると、残っているものだけを消します。`);
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
err(`[SUCCESS] ${t.tier} の接続情報を消しました。`);
|
|
251
|
+
return 0;
|
|
252
|
+
}
|
|
253
|
+
async function removeTier(args, box, deps, ask) {
|
|
254
|
+
const err = (l) => box.output.err(l);
|
|
255
|
+
const [c, flags] = await prepare(args, box, deps, ask, "remove-tier");
|
|
256
|
+
const bad = flags.find((f) => f !== "--purge-secrets");
|
|
257
|
+
if (bad !== undefined)
|
|
258
|
+
throw new Stop(1, `不明なオプションです: ${bad}`);
|
|
259
|
+
const purge = flags.includes("--purge-secrets");
|
|
260
|
+
const { t } = c;
|
|
261
|
+
err(`[HEADER] 段階を外す: ${t.tier}${purge ? "(接続情報を消す: --purge-secrets)" : ""}`);
|
|
262
|
+
err(`[INFO] 対象リポジトリ: ${c.repo}`);
|
|
263
|
+
err(`[INFO] 外す段階 : ${t.tier}`);
|
|
264
|
+
if (purge)
|
|
265
|
+
return purgeSecrets(c);
|
|
266
|
+
if (!tierTextHasLine(c.main, t.tier))
|
|
267
|
+
throw new Stop(1, `${t.tier} は branches.txt(origin/main)にありません(すでに外れています)。`);
|
|
268
|
+
if (t.tier === "staging" && tierTextHasLine(c.main, "develop"))
|
|
269
|
+
throw new Stop(1, "staging を外すには、先に develop を外す必要があります(origin/main の branches.txt に develop があります)。");
|
|
270
|
+
// origin/main の workflow が、段階の切り替えに対応しているか
|
|
271
|
+
const wf = await tierSwitchWfProblems(box, deps.templateDir, deps.shellTemplateDir);
|
|
272
|
+
if (wf.length > 0) {
|
|
273
|
+
err(`[INFO] origin/main の workflow が、段階の切り替えに対応していません(または、雛形と違います): ${wf.join(" ")}`);
|
|
274
|
+
err("[INFO] sf-tools を更新し、作業用のブランチで sft project update-workflows を実行して、workflow を main まで入れてから、実行してください。");
|
|
275
|
+
err("[INFO] (古い workflow のままでは、branches.txt から行を消しても、リリース・検証は止まりません。廃止した wf-propagate.yml は sft project update-workflows --remove で消します)");
|
|
276
|
+
throw new Stop(1, `origin/main の workflow が、段階の切り替えに対応していません: ${wf.join(" ")}`);
|
|
277
|
+
}
|
|
278
|
+
err("[SUCCESS] origin/main の workflow は、段階の切り替えに対応しています。");
|
|
279
|
+
// 影響(読み取りのみ。読めないものは WARNING で続ける)
|
|
280
|
+
err(`[HEADER] 段階を外したときの影響(${t.tier})`);
|
|
281
|
+
if ((await run(box, "git", ["fetch", "origin", t.tier])).ok) {
|
|
282
|
+
const cnt = await run(box, "git", ["rev-list", "--no-merges", "--count", `origin/main..origin/${t.tier}`]);
|
|
283
|
+
const n = cnt.ok ? cnt.out.trim() : "?";
|
|
284
|
+
err(`[INFO] a. ${t.tier} にだけあって、main にない変更: ${n} 件(ブランチは消さないので、失われません。ただし、リリースされなくなります)`);
|
|
285
|
+
if (n !== "0" && n !== "?") {
|
|
286
|
+
const log = await run(box, "git", ["log", "--no-merges", "--oneline", `origin/main..origin/${t.tier}`]);
|
|
287
|
+
for (const l of log.out
|
|
288
|
+
.split("\n")
|
|
289
|
+
.filter((x) => x !== "")
|
|
290
|
+
.slice(0, 20))
|
|
291
|
+
err(`[INFO] ${l}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else
|
|
295
|
+
err(`[WARNING] a. ${t.tier} ブランチを取得できません(リモートにない可能性があります)。`);
|
|
296
|
+
const prs = await run(box, "gh", [
|
|
297
|
+
"pr",
|
|
298
|
+
"list",
|
|
299
|
+
"-R",
|
|
300
|
+
c.repo,
|
|
301
|
+
"--base",
|
|
302
|
+
t.tier,
|
|
303
|
+
"--state",
|
|
304
|
+
"open",
|
|
305
|
+
"--json",
|
|
306
|
+
"number,title",
|
|
307
|
+
"--jq",
|
|
308
|
+
'.[] | "#\\(.number) \\(.title)"',
|
|
309
|
+
]);
|
|
310
|
+
if (prs.ok) {
|
|
311
|
+
const list = prs.out.split("\n").filter((l) => l !== "");
|
|
312
|
+
err(`[INFO] b. ${t.tier} をマージ先にする、開いている PR: ${list.length === 0 ? "なし" : ""}`);
|
|
313
|
+
for (const l of list)
|
|
314
|
+
err(`[INFO] ${l}(マージしても、リリース・検証されなくなります)`);
|
|
315
|
+
}
|
|
316
|
+
else
|
|
317
|
+
err("[WARNING] b. 開いている PR を取得できません。");
|
|
318
|
+
const existing = [...((await names(c, "secret")) ?? []), ...((await names(c, "variable")) ?? [])];
|
|
319
|
+
const n = connNames(t.suffix);
|
|
320
|
+
const have = [...n.secrets, ...n.variables].filter((x) => existing.includes(x));
|
|
321
|
+
err(`[INFO] c. ${t.tier} の接続情報(名前だけ): ${have.join(" ") || "なし"}`);
|
|
322
|
+
if (have.length > 0)
|
|
323
|
+
err(`[INFO] 残します(段階が無効のあいだは、使われません)。消すには、PR のマージ後に sft project remove-tier ${t.tier} --purge-secrets`);
|
|
324
|
+
const rs = await run(box, "gh", ["api", `repos/${c.repo}/rulesets`, "--jq", '.[] | "\\(.id) \\(.name)"']);
|
|
325
|
+
if (!rs.ok)
|
|
326
|
+
err("[WARNING] d. Ruleset の一覧を取得できません。");
|
|
327
|
+
else {
|
|
328
|
+
const hit = [];
|
|
329
|
+
for (const l of rs.out.split("\n").filter((x) => x !== "")) {
|
|
330
|
+
const sp = l.indexOf(" ");
|
|
331
|
+
const id = sp < 0 ? l : l.slice(0, sp);
|
|
332
|
+
const inc = await run(box, "gh", ["api", `repos/${c.repo}/rulesets/${id}`, "--jq", ".conditions.ref_name.include[]?"]);
|
|
333
|
+
if (inc.out.split("\n").includes(`refs/heads/${t.tier}`))
|
|
334
|
+
hit.push(sp < 0 ? "" : l.slice(sp + 1));
|
|
335
|
+
}
|
|
336
|
+
err(`[INFO] d. ${t.tier} を対象にする Ruleset: ${hit.join(" ") || "なし"}`);
|
|
337
|
+
if (hit.length > 0)
|
|
338
|
+
err(`[INFO] 残します。段階が無効のあいだは、${t.tier} への PR を出さないでください。`);
|
|
339
|
+
}
|
|
340
|
+
warning(box, [
|
|
341
|
+
"!! このコマンドは管理者専用です",
|
|
342
|
+
` 段階 ${t.tier} を外す PR を作ります`,
|
|
343
|
+
" (マージすると、リリース・検証が止まります)",
|
|
344
|
+
]);
|
|
345
|
+
err("[INFO] 変えるのは、branches.txt の PR だけです。ブランチ・Secrets / Variables・Ruleset・Salesforce 組織は、変えません。");
|
|
346
|
+
await ownerCheck(c);
|
|
347
|
+
await confirm(ask, " 続行しますか?");
|
|
348
|
+
await clean(box);
|
|
349
|
+
const original = await originalBranch(c);
|
|
350
|
+
const url = await branchesPr(c, "remove", tierTextWithoutLine(c.main, t.tier), `chore: ${t.tier} tier を外す`, `branches.txt から ${t.tier} を外します(sft project remove-tier ${t.tier} で作成)。マージすると、${t.tier} へのリリース・PR の検証が止まります。ブランチ・接続情報(Secrets / Variables)・Ruleset は残すので、sft project add-tier ${t.tier} で戻せます。`);
|
|
351
|
+
await must(box, "git", ["checkout", original], `${original} ブランチへの切り替えに失敗しました。`);
|
|
352
|
+
err(`[SUCCESS] 段階を外す PR を作りました: ${url}`);
|
|
353
|
+
err("[INFO] 次のステップ:");
|
|
354
|
+
err(`[INFO] 1. PR をマージすると、${t.tier} へのリリース・PR の検証が止まります`);
|
|
355
|
+
err(`[INFO] 2. 残したもの: ${t.tier} ブランチ・接続情報(Secrets / Variables)・Ruleset・組織の外部クライアントアプリ`);
|
|
356
|
+
err(`[INFO] 3. 戻すとき: sft project add-tier ${t.tier}(接続情報が残っていれば、つなぎ直さずに戻せます)`);
|
|
357
|
+
err(`[INFO] 4. 接続情報を消すとき: PR のマージ後に sft project remove-tier ${t.tier} --purge-secrets`);
|
|
358
|
+
err("[INFO] 5. ブランチを消すときは、手で(消す前に、上の a の「main にない変更」を確かめてください)");
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
async function guard(f, box) {
|
|
362
|
+
try {
|
|
363
|
+
return await f();
|
|
364
|
+
}
|
|
365
|
+
catch (e) {
|
|
366
|
+
if (!(e instanceof Stop) && !(e instanceof EcaStop))
|
|
367
|
+
throw e;
|
|
368
|
+
if (e.message !== "")
|
|
369
|
+
box.output.err(`[ERROR] ${e.message}`);
|
|
370
|
+
return e.code;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
export const runProjectAddTier = (args, box, deps, ask) => guard(() => addTier(args, box, deps, ask), box);
|
|
374
|
+
export const runProjectRemoveTier = (args, box, deps, ask) => guard(() => removeTier(args, box, deps, ask), box);
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { branchList, isProtectedBranch } from "./branches.js";
|
|
2
|
+
import { isNewer } from "./new-version.js";
|
|
3
|
+
const PACKAGE = "@tama-create/sf-tools";
|
|
4
|
+
const PKG = "sf-tools/package.json";
|
|
5
|
+
const LOCK = "sf-tools/package-lock.json";
|
|
6
|
+
const VERSION_RE = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
|
7
|
+
class Stop extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
async function npmVersionExists(box, version) {
|
|
15
|
+
const r = await box.run("npm", ["view", `${PACKAGE}@${version}`, "version"], { timeoutMs: 30_000 }).catch(() => null);
|
|
16
|
+
return r !== null && r.code === 0 && r.stdout.trim() === version;
|
|
17
|
+
}
|
|
18
|
+
async function upgrade(args, box, deps, ask) {
|
|
19
|
+
const { files, output } = box;
|
|
20
|
+
const err = (l) => output.err(l);
|
|
21
|
+
let version = "";
|
|
22
|
+
let check = false;
|
|
23
|
+
for (const a of args) {
|
|
24
|
+
if (a === "--check")
|
|
25
|
+
check = true;
|
|
26
|
+
else if (a === "-v" || a === "--verbose")
|
|
27
|
+
continue;
|
|
28
|
+
else if (a.startsWith("-"))
|
|
29
|
+
throw new Stop(1, `不明なオプションです: ${a}`);
|
|
30
|
+
else if (version === "")
|
|
31
|
+
version = a;
|
|
32
|
+
else
|
|
33
|
+
throw new Stop(1, "版は 1 つだけ指定してください。");
|
|
34
|
+
}
|
|
35
|
+
if (!check && version === "")
|
|
36
|
+
throw new Stop(1, "版を指定してください(例: sft project upgrade 1.4.0)。");
|
|
37
|
+
if (version !== "" && !VERSION_RE.test(version))
|
|
38
|
+
throw new Stop(1, `版の形が正しくありません(1.2.3 または 1.2.3-dev.4): ${version}`);
|
|
39
|
+
if (!deps.folderName.startsWith("force-"))
|
|
40
|
+
throw new Stop(1, "force-* ディレクトリの中ではありません。");
|
|
41
|
+
if (!files.exists(PKG))
|
|
42
|
+
throw new Stop(1, `${PKG} がありません(sft の版を固定していないプロジェクトです)。`);
|
|
43
|
+
let pkg;
|
|
44
|
+
try {
|
|
45
|
+
pkg = JSON.parse(files.readText(PKG));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new Stop(1, `${PKG} を読めません(JSON の形が正しくありません)。`);
|
|
49
|
+
}
|
|
50
|
+
if (typeof pkg !== "object" || pkg === null || Array.isArray(pkg))
|
|
51
|
+
throw new Stop(1, `${PKG} を読めません(JSON の形が正しくありません)。`);
|
|
52
|
+
const now = pkg.dependencies?.[PACKAGE];
|
|
53
|
+
if (typeof now !== "string" || !VERSION_RE.test(now))
|
|
54
|
+
throw new Stop(1, `${PKG} に、${PACKAGE} の固定した版(1.2.3 の形)がありません。`);
|
|
55
|
+
if (check) {
|
|
56
|
+
const r = await box.run("npm", ["view", PACKAGE, "dist-tags", "--json"], { timeoutMs: 30_000 }).catch(() => null);
|
|
57
|
+
err(`[INFO] 固定している版: ${now}`);
|
|
58
|
+
if (r === null || r.code !== 0)
|
|
59
|
+
throw new Stop(2, "npm の札の一覧を取得できません。");
|
|
60
|
+
err(`[INFO] npm の札: ${r.stdout.replace(/\s+/g, " ").trim()}`);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (version === now) {
|
|
64
|
+
err(`[SUCCESS] すでに ${now} に固定しています。`);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
const head = await box.run("git", ["symbolic-ref", "--short", "HEAD"]).catch(() => null);
|
|
68
|
+
const branch = head !== null && head.code === 0 ? head.stdout.trim() : "";
|
|
69
|
+
if (branch === "")
|
|
70
|
+
throw new Stop(1, "現在のブランチを取得できません。");
|
|
71
|
+
const cfg = "sf-tools/config/branches.txt";
|
|
72
|
+
const list = branchList(files.exists(cfg) ? files.readText(cfg) : null);
|
|
73
|
+
for (const w of list.warnings)
|
|
74
|
+
err(`[WARNING] ${w}`);
|
|
75
|
+
if (isProtectedBranch(branch, list.branches))
|
|
76
|
+
throw new Stop(1, `${branch} ブランチでは実行できません(環境ブランチです。作業用のブランチを切ってください)。`);
|
|
77
|
+
const st = await box.run("git", ["status", "--porcelain", "--untracked-files=no"]).catch(() => null);
|
|
78
|
+
if (st === null || st.code !== 0)
|
|
79
|
+
throw new Stop(1, "git status を取得できません。");
|
|
80
|
+
if (st.stdout.trim() !== "")
|
|
81
|
+
throw new Stop(1, "追跡しているファイルに、未コミットの変更があります。コミットするか退避してから、実行してください。");
|
|
82
|
+
// 存在の確認は読み取りのみ。質問の前に済ませる(ない版で、確認だけさせない)
|
|
83
|
+
if (!(await npmVersionExists(box, version)))
|
|
84
|
+
throw new Stop(1, `${PACKAGE}@${version} が、npm に見つかりません。`);
|
|
85
|
+
for (const l of [
|
|
86
|
+
"!! force-* に固定した sf-tools の版を上げます",
|
|
87
|
+
` ${now} → ${version}(package.json・package-lock.json を書き換えます)`,
|
|
88
|
+
...(isNewer(now, version) ? [" 注意: 今より古い版に下げます。段階ごとの版がそろわなくなるおそれがあります"] : []),
|
|
89
|
+
" CI/CD(リリース・検証)の動作が変わります。管理者以外は、実行しないでください",
|
|
90
|
+
])
|
|
91
|
+
err(l);
|
|
92
|
+
const a = await ask("", "続行しますか?");
|
|
93
|
+
if (a === "need")
|
|
94
|
+
throw new Stop(20, "");
|
|
95
|
+
if (a !== "yes")
|
|
96
|
+
throw new Stop(1, "中断しました。");
|
|
97
|
+
const before = files.readText(PKG);
|
|
98
|
+
const hadLock = files.exists(LOCK);
|
|
99
|
+
const beforeLock = hadLock ? files.readText(LOCK) : "";
|
|
100
|
+
const next = { ...pkg, dependencies: { ...pkg.dependencies, [PACKAGE]: version } };
|
|
101
|
+
// 失敗でも中断でも、書き換え前に戻す(ロックが元からなければ、作りかけを消す)
|
|
102
|
+
let r = null;
|
|
103
|
+
try {
|
|
104
|
+
files.writeText(PKG, `${JSON.stringify(next, null, 2)}\n`);
|
|
105
|
+
r = await box
|
|
106
|
+
.run("npm", ["install", "--package-lock-only", "--ignore-scripts", "--prefix", "sf-tools"], { timeoutMs: 300_000 })
|
|
107
|
+
.catch(() => null);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
if (r === null || r.code !== 0) {
|
|
111
|
+
files.writeText(PKG, before);
|
|
112
|
+
if (hadLock)
|
|
113
|
+
files.writeText(LOCK, beforeLock);
|
|
114
|
+
else if (files.exists(LOCK))
|
|
115
|
+
files.removeFile(LOCK);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (r === null || r.code !== 0)
|
|
119
|
+
throw new Stop(1, "ロックファイルの作成に失敗したため、元に戻しました(npm install --package-lock-only --prefix sf-tools)。");
|
|
120
|
+
err(`[SUCCESS] ${PACKAGE} の固定を ${now} → ${version} に書き換えました(${PKG}・${LOCK})。`);
|
|
121
|
+
err("[INFO] 続きの手順(ワークフローは、新しい版の雛形で書き換えます。コミット・プッシュは、行いません):");
|
|
122
|
+
err("[INFO] 1. npm ci --prefix sf-tools --ignore-scripts");
|
|
123
|
+
err("[INFO] 2. sft project update-workflows(新しい版の雛形と合わせる)");
|
|
124
|
+
err("[INFO] 3. sft push → PR(develop → staging → main の順に、同じコミットを出す。一方通行の規則)");
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
export async function runProjectUpgrade(args, box, deps, ask) {
|
|
128
|
+
try {
|
|
129
|
+
return await upgrade(args, box, deps, ask);
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
if (!(e instanceof Stop))
|
|
133
|
+
throw e;
|
|
134
|
+
if (e.message !== "")
|
|
135
|
+
box.output.err(`[ERROR] ${e.message}`);
|
|
136
|
+
return e.code;
|
|
137
|
+
}
|
|
138
|
+
}
|