@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.
Files changed (164) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +212 -2
  3. package/bin/sft.js +22 -0
  4. package/config/changeset-types.txt +350 -0
  5. package/config/install/gitmessage +6 -0
  6. package/config/install/release/README.md +26 -0
  7. package/config/install/release/deploy-target.txt +368 -0
  8. package/config/install/release/remove-target.txt +279 -0
  9. package/config/metadata-capability.txt +118 -0
  10. package/config/metadata-dirs.txt +79 -0
  11. package/config/metasync-detect.txt +52 -0
  12. package/config/metasync-extra.txt +30 -0
  13. package/config/metasync-fingerprint.txt +90 -0
  14. package/dist/cli.js +35 -0
  15. package/dist/commands/admin.js +46 -0
  16. package/dist/commands/check.js +20 -0
  17. package/dist/commands/ci.js +123 -0
  18. package/dist/commands/deploy.js +31 -0
  19. package/dist/commands/doctor.js +11 -0
  20. package/dist/commands/dryrun.js +17 -0
  21. package/dist/commands/hook.js +46 -0
  22. package/dist/commands/index.js +37 -0
  23. package/dist/commands/job.js +31 -0
  24. package/dist/commands/launcher.js +57 -0
  25. package/dist/commands/log.js +58 -0
  26. package/dist/commands/metasync.js +21 -0
  27. package/dist/commands/pr.js +32 -0
  28. package/dist/commands/project.js +102 -0
  29. package/dist/commands/push.js +16 -0
  30. package/dist/commands/release.js +19 -0
  31. package/dist/commands/start.js +67 -0
  32. package/dist/commands/switch-org.js +12 -0
  33. package/dist/commands/update-cli.js +11 -0
  34. package/dist/commands/version.js +18 -0
  35. package/dist/core/admin-clone.js +79 -0
  36. package/dist/core/ai-answers.js +40 -0
  37. package/dist/core/branches.js +88 -0
  38. package/dist/core/check-targets.js +170 -0
  39. package/dist/core/check.js +34 -0
  40. package/dist/core/ci-check-branch.js +150 -0
  41. package/dist/core/ci-login-steps.js +73 -0
  42. package/dist/core/ci-release-steps.js +193 -0
  43. package/dist/core/ci-sequence-steps.js +188 -0
  44. package/dist/core/ci-validate-steps.js +140 -0
  45. package/dist/core/cli-update-notice.js +47 -0
  46. package/dist/core/command.js +25 -0
  47. package/dist/core/create-connect.js +519 -0
  48. package/dist/core/create-setup.js +358 -0
  49. package/dist/core/create-state.js +82 -0
  50. package/dist/core/delegate.js +45 -0
  51. package/dist/core/deploy.js +66 -0
  52. package/dist/core/diag.js +200 -0
  53. package/dist/core/doctor.js +211 -0
  54. package/dist/core/eca.js +259 -0
  55. package/dist/core/exit-codes.js +12 -0
  56. package/dist/core/gh-identity.js +46 -0
  57. package/dist/core/hook-install.js +130 -0
  58. package/dist/core/hook.js +116 -0
  59. package/dist/core/identity.js +48 -0
  60. package/dist/core/job.js +243 -0
  61. package/dist/core/launcher-status.js +215 -0
  62. package/dist/core/lines.js +25 -0
  63. package/dist/core/log-auto.js +56 -0
  64. package/dist/core/log-box.js +109 -0
  65. package/dist/core/log-collector.js +370 -0
  66. package/dist/core/log-format.js +44 -0
  67. package/dist/core/log-pull.js +565 -0
  68. package/dist/core/log-select.js +126 -0
  69. package/dist/core/log-session.js +362 -0
  70. package/dist/core/log-start.js +421 -0
  71. package/dist/core/log-status.js +223 -0
  72. package/dist/core/log-summarize.js +127 -0
  73. package/dist/core/logger.js +71 -0
  74. package/dist/core/login-url.js +50 -0
  75. package/dist/core/manifest-build.js +118 -0
  76. package/dist/core/mask.js +70 -0
  77. package/dist/core/metadata-types.js +79 -0
  78. package/dist/core/metasync.js +684 -0
  79. package/dist/core/new-version.js +93 -0
  80. package/dist/core/org.js +115 -0
  81. package/dist/core/origin.js +62 -0
  82. package/dist/core/package-xml.js +157 -0
  83. package/dist/core/pr.js +217 -0
  84. package/dist/core/project-create.js +733 -0
  85. package/dist/core/project-reconnect.js +126 -0
  86. package/dist/core/project-status.js +246 -0
  87. package/dist/core/project-tier.js +374 -0
  88. package/dist/core/project-upgrade.js +138 -0
  89. package/dist/core/project-verify.js +517 -0
  90. package/dist/core/promote-check.js +401 -0
  91. package/dist/core/promote.js +141 -0
  92. package/dist/core/push.js +163 -0
  93. package/dist/core/release.js +256 -0
  94. package/dist/core/scoped-box.js +29 -0
  95. package/dist/core/start.js +155 -0
  96. package/dist/core/switch-org.js +49 -0
  97. package/dist/core/test-types.js +212 -0
  98. package/dist/core/toolbox.js +4 -0
  99. package/dist/core/update-cli.js +156 -0
  100. package/dist/core/update-secret.js +301 -0
  101. package/dist/core/update-workflows.js +132 -0
  102. package/dist/core/verify-gh.js +286 -0
  103. package/dist/core/verify-samples.js +152 -0
  104. package/dist/core/windows-shim.js +21 -0
  105. package/dist/io/capability-file.js +11 -0
  106. package/dist/io/create-deps.js +69 -0
  107. package/dist/io/doctor-deps.js +32 -0
  108. package/dist/io/hook-install-deps.js +11 -0
  109. package/dist/io/job-deps.js +39 -0
  110. package/dist/io/key-file.js +17 -0
  111. package/dist/io/keys.js +172 -0
  112. package/dist/io/log-deps.js +107 -0
  113. package/dist/io/metasync-deps.js +22 -0
  114. package/dist/io/open-browser.js +21 -0
  115. package/dist/io/package-version.js +10 -0
  116. package/dist/io/pinned-project.js +111 -0
  117. package/dist/io/promote-deps.js +28 -0
  118. package/dist/io/push-refs.js +20 -0
  119. package/dist/io/resolve.js +74 -0
  120. package/dist/io/run.js +201 -0
  121. package/dist/io/switch-org-deps.js +10 -0
  122. package/dist/io/temp-dir.js +24 -0
  123. package/dist/io/tool-versions.js +14 -0
  124. package/dist/io/toolbox.js +68 -0
  125. package/dist/io/update-cli-deps.js +75 -0
  126. package/dist/io/update-secret-deps.js +27 -0
  127. package/dist/launch.js +152 -0
  128. package/dist/ui/confirm.js +18 -0
  129. package/dist/ui/launcher.js +163 -0
  130. package/dist/ui/prompt.js +66 -0
  131. package/lib/apexlog.js +819 -0
  132. package/lib/metasync.js +1364 -0
  133. package/lib/package.json +4 -0
  134. package/lib/typecheck.js +547 -0
  135. package/package.json +44 -4
  136. package/templates/.forceignore +12 -0
  137. package/templates/.gitattributes +58 -0
  138. package/templates/.github/workflows/wf-metasync.yml +177 -0
  139. package/templates/.github/workflows/wf-release.yml +454 -0
  140. package/templates/.github/workflows/wf-sequence.yml +292 -0
  141. package/templates/.github/workflows/wf-validate.yml +389 -0
  142. package/templates/.prettierignore +11 -0
  143. package/templates/.prettierrc +17 -0
  144. package/templates/.vscode/extensions.json +9 -0
  145. package/templates/.vscode/launch.json +16 -0
  146. package/templates/.vscode/settings.json +39 -0
  147. package/templates/AGENTS.md +3 -0
  148. package/templates/CLAUDE.md +4 -0
  149. package/templates/README.md +11 -0
  150. package/templates/config/project-scratch-def.json +13 -0
  151. package/templates/eslint.config.js +55 -0
  152. package/templates/force-app/main/default/.gitkeep +0 -0
  153. package/templates/gitignore.template +62 -0
  154. package/templates/jest.config.js +6 -0
  155. package/templates/package.json +29 -0
  156. package/templates/scripts/apex/hello.apex +10 -0
  157. package/templates/scripts/soql/account.soql +6 -0
  158. package/templates/sf-tools/config/branches.txt +24 -0
  159. package/templates/sf-tools/config/metadata.txt +576 -0
  160. package/templates/sfdx-project.json +12 -0
  161. package/templates-ts/.github/workflows/wf-metasync.yml +109 -0
  162. package/templates-ts/.github/workflows/wf-release.yml +263 -0
  163. package/templates-ts/.github/workflows/wf-sequence.yml +153 -0
  164. package/templates-ts/.github/workflows/wf-validate.yml +185 -0
@@ -0,0 +1,126 @@
1
+ import { contentLines } from "./branches.js";
2
+ import { EcaStop, ecaName, registerJwtSecretEca } from "./eca.js";
3
+ import { originRepo } from "./origin.js";
4
+ import { orgKindLabel } from "./project-status.js";
5
+ import { ghOwnerProblem } from "./update-secret.js";
6
+ const TIERS = {
7
+ main: { tier: "main", suffix: "PROD", label: "メイン組織", alias: "prod" },
8
+ prod: { tier: "main", suffix: "PROD", label: "メイン組織", alias: "prod" },
9
+ staging: { tier: "staging", suffix: "STG", label: "ステージング組織", alias: "staging" },
10
+ develop: { tier: "develop", suffix: "DEV", label: "開発組織", alias: "develop" },
11
+ };
12
+ class Stop extends Error {
13
+ code;
14
+ constructor(code, message) {
15
+ super(message);
16
+ this.code = code;
17
+ }
18
+ }
19
+ async function reconnect(args, box, deps, ask) {
20
+ const err = (l) => box.output.err(l);
21
+ const rest = args.filter((a) => a !== "-v" && a !== "--verbose");
22
+ if (rest.length !== 1)
23
+ throw new Stop(1, "reconnect には、main・staging・develop のいずれかを 1 つ指定してください。");
24
+ const t = TIERS[rest[0] ?? ""];
25
+ if (t === undefined)
26
+ throw new Stop(1, "reconnect には、main・staging・develop のいずれかを指定してください。");
27
+ err(`[HEADER] 接続先の付け替え: ${rest[0]}`);
28
+ // 1. リポジトリ・段階の確認
29
+ if (!deps.folderName.startsWith("force-"))
30
+ throw new Stop(1, "force-* ディレクトリ内で実行してください。");
31
+ const origin = await originRepo(box);
32
+ if (origin.kind !== "ok")
33
+ throw new Stop(1, "origin から GitHub のリポジトリを特定できません。");
34
+ const repo = `${origin.owner}/${origin.repo}`;
35
+ if (t.tier !== "main") {
36
+ let lines = [];
37
+ try {
38
+ lines = contentLines(box.files.readText("sf-tools/config/branches.txt"));
39
+ }
40
+ catch {
41
+ lines = [];
42
+ }
43
+ if (!lines.includes(t.tier))
44
+ throw new Stop(1, `${t.tier} は、branches.txt にありません(段階を増やすときは add-tier ${t.tier})。`);
45
+ }
46
+ // 2. いまの接続を表示し、確認する
47
+ const variable = async (name) => {
48
+ const r = await box.run("gh", ["variable", "get", name, "-R", repo]).catch(() => null);
49
+ return r !== null && r.code === 0 ? r.stdout.replace(/\r/g, "").trim() : "";
50
+ };
51
+ const oldUrl = await variable(`SF_INSTANCE_URL_${t.suffix}`);
52
+ const oldUser = await variable(`SF_USERNAME_${t.suffix}`);
53
+ const oldKind = await variable(`SF_ORG_KIND_${t.suffix}`);
54
+ const oldId = await variable(`SF_ORG_ID_${t.suffix}`);
55
+ const oldName = await variable(`SF_ORG_NAME_${t.suffix}`);
56
+ err("");
57
+ err(` リポジトリ : ${repo}`);
58
+ err(` 段階 : ${t.tier}`);
59
+ err(" いまの接続 :");
60
+ if (oldUrl !== "" || oldUser !== "") {
61
+ err(` ログイン先 : ${oldUrl || "(未設定)"}`);
62
+ err(` 種類 : ${orgKindLabel(oldKind || "unknown")}`);
63
+ err(` ユーザー : ${oldUser || "(未設定)"}`);
64
+ err(` 組織 : ${oldName || "(未確認)"}${oldId !== "" ? `(ID ${oldId})` : ""}`);
65
+ }
66
+ else
67
+ err(" (未接続)");
68
+ err("");
69
+ // 管理者向けの警告と確認(CLAUDE.md 2.4)。Actions の上でも省かない(Actions で動かす想定がない)
70
+ for (const l of [
71
+ "!! このコマンドは管理者専用です",
72
+ " 接続先(Secrets / Variables)が書き換わります",
73
+ " 管理者以外は、実行しないでください",
74
+ "",
75
+ ])
76
+ err(l);
77
+ err(`[WARNING] この段階(${t.tier})の接続先を、付け替えます。Secrets / Variables が、まとめて更新されます。`);
78
+ const owner = await ghOwnerProblem(box, origin.owner);
79
+ if (owner !== null)
80
+ throw new Stop(1, owner);
81
+ const a = await ask("", " 付け替えますか?");
82
+ if (a === "need")
83
+ throw new Stop(20, "");
84
+ if (a !== "yes")
85
+ throw new Stop(1, "中断しました。");
86
+ // 3. 鍵と証明書
87
+ const jwtDir = `${deps.home}/.sf-jwt/${origin.repo}`;
88
+ for (const [file, what] of [
89
+ ["server.key", "秘密鍵"],
90
+ ["server.crt", "証明書"],
91
+ ])
92
+ if (!box.files.exists(`${jwtDir}/${file}`))
93
+ throw new Stop(1, `${what}が見つかりません: ${jwtDir}/${file}`);
94
+ // 4. ログイン → 種類の確認 → アプリの作成 → 登録
95
+ const info = await registerJwtSecretEca(box, deps, ask, {
96
+ ...t,
97
+ repo,
98
+ keyFile: `${jwtDir}/server.key`,
99
+ certFile: `${jwtDir}/server.crt`,
100
+ });
101
+ // 5. 古い組織に残るもの(自動では消さない)
102
+ err("");
103
+ const app = ecaName(origin.repo);
104
+ const newId = info?.id ?? "";
105
+ if (oldId !== "" && newId !== "" && oldId !== newId) {
106
+ err(`[WARNING] 古い組織(${oldName || "名前なし"} / ID ${oldId})には、以前の接続に使った外部クライアントアプリ「${app}」が、残っています(自動では、削除しません)。`);
107
+ err("[INFO] 不要であれば、その組織の「設定」→「外部クライアントアプリ マネージャ」で、削除してください。");
108
+ }
109
+ else if (oldId === "")
110
+ err(`[INFO] 以前の接続先の組織 ID は、保存されていませんでした。古い組織に、以前の接続に使った外部クライアントアプリ「${app}」が残っていないか、必要なら、確認してください。`);
111
+ err(`[SUCCESS] 付け替えが完了しました: ${t.tier}`);
112
+ err("[INFO] いまの接続を見る: sft project status");
113
+ return 0;
114
+ }
115
+ export async function runProjectReconnect(args, box, deps, ask) {
116
+ try {
117
+ return await reconnect(args, box, deps, ask);
118
+ }
119
+ catch (e) {
120
+ if (!(e instanceof Stop) && !(e instanceof EcaStop))
121
+ throw e;
122
+ if (e.message !== "")
123
+ box.output.err(`[ERROR] ${e.message}`);
124
+ return e.code;
125
+ }
126
+ }
@@ -0,0 +1,246 @@
1
+ // sft project status: セットアップの診断と、段階ごとの Salesforce の接続先の一覧(phases/init/status.sh の移し先)。
2
+ // force-* の中で動かす。何も変更しない(読み取りのみ。管理者向けの警告・確認は出さない)。
3
+ // 流れ:
4
+ // 1. フォルダ名が force- で始まらなければ止める。origin から owner/repo を求める(GitHub でなければ止める)
5
+ // 2. セットアップの診断(diag.ts の runDiag)。対象の段階は main と、手元の sf-tools/config/branches.txt にある staging / develop
6
+ // (tierList の規則: 段階の名前でない行は無視・develop は staging もあるときだけ)
7
+ // 3. 段階ごとの接続先: Variables(SF_INSTANCE_URL_* / SF_USERNAME_* / SF_ORG_KIND_* / SF_ORG_ID_* / SF_ORG_NAME_* / SF_ORG_CHECKED_*)。
8
+ // URL もユーザーもない段階は「(未接続)」。同じ組織 ID が前の段階にあれば WARNING(循環)。
9
+ // 保存された種類が本番で、main 以外なら ERROR(本番は main のみ: F8。終了コード 1)。どの段階にも接続がなければ WARNING
10
+ // 4. 段階の切り替え(origin/main の branches.txt。doc/tier-scaling-design.md 7 章): git fetch origin main(失敗は WARNING で省く)のあと、
11
+ // 有効な段階の表示・名前でない行の WARNING・staging のない develop は ERROR(終了コード 1)。
12
+ // 無効の段階に、ブランチ・接続情報・その段階を対象にする Ruleset が残っていれば INFO。
13
+ // origin/main の workflow が段階の切り替えに対応していなければ WARNING(tierSwitchWfProblems)。
14
+ // Variable SF_TOOLS_BRANCH が、未設定・main・development のどれでもなければ WARNING。origin/main に branches.txt がなければ WARNING
15
+ // 表示する組織の情報は「GitHub に保存された値」で、いまログインして確かめた値ではない。
16
+ // 終了コード: 0 = 不足なし・違反なし / 1 = 診断で不足がある・本番が main 以外にある・staging のない develop がある・止めたとき
17
+ import { tierList } from "./branches.js";
18
+ import { runDiag, tierSuffix } from "./diag.js";
19
+ import { originRepo } from "./origin.js";
20
+ const ORG_KIND_LABEL = {
21
+ production: "本番",
22
+ sandbox: "Sandbox",
23
+ developer: "Developer Edition",
24
+ trial: "Trial",
25
+ unknown: "種類を判定できません",
26
+ };
27
+ /** 保存された組織の種類の表示名(lib/org.sh の org_kind_label と同じ。知らない値は「種類を判定できません」) */
28
+ export function orgKindLabel(kind) {
29
+ return ORG_KIND_LABEL[kind] ?? ORG_KIND_LABEL.unknown;
30
+ }
31
+ /** 段階の切り替えの判定の部分(# >>> sf-tools: tier-switch v1 から <<< までの行。行頭の空白は除く)。印がなければ空 */
32
+ export function tierWfBlock(text) {
33
+ const out = [];
34
+ let inBlock = false;
35
+ for (const raw of text.split("\n")) {
36
+ if (raw.includes("# >>> sf-tools: tier-switch v1"))
37
+ inBlock = true;
38
+ if (!inBlock)
39
+ continue;
40
+ out.push(raw.replace(/^\s+/, ""));
41
+ if (raw.includes("# <<< sf-tools: tier-switch v1"))
42
+ break;
43
+ }
44
+ return out.join("\n");
45
+ }
46
+ /** origin/main のファイルの中身。ないときは null(git fetch は、呼ぶ側が済ませておく) */
47
+ async function showOriginMain(box, path) {
48
+ try {
49
+ const r = await box.run("git", ["show", `origin/main:${path}`], { env: { MSYS_NO_PATHCONV: "1" } });
50
+ return r.code === 0 ? r.stdout : null;
51
+ }
52
+ catch {
53
+ return null;
54
+ }
55
+ }
56
+ /**
57
+ * origin/main の workflow が、段階の切り替えに対応していないファイル名(init-common.sh の tier_switch_wf_problems と同じ判定)。
58
+ * wf-release.yml・wf-validate.yml: 判定の部分が雛形と同じで、# sf-tools: tier-check の印がある。
59
+ * wf-propagate.yml: 残っていれば、対応していない扱い(廃止)。wf-metasync.yml: sf-metasync.sh を呼び、コメント以外に git push がない
60
+ * sft 版(templates-ts/)のワークフローは、段階の判定を sft の中(sft ci check-branch)に持つため、yml に印はない:
61
+ * wf-release.yml・wf-validate.yml は `ci check-branch` を呼んでいれば、対応している扱い。wf-metasync.yml は `sft metasync`(`$SFT metasync`)を呼び、
62
+ * コメント以外に git push がなければ、対応している扱い。シェル版(印のある雛形)も、これまでどおり対応している扱い
63
+ */
64
+ export async function tierSwitchWfProblems(box, templateDir, shellTemplateDir) {
65
+ const bad = [];
66
+ for (const wf of ["wf-release.yml", "wf-validate.yml"]) {
67
+ const remote = (await showOriginMain(box, `.github/workflows/${wf}`)) ?? "";
68
+ const tmplPath = `${shellTemplateDir ?? templateDir}/${wf}`;
69
+ const tmpl = box.files.exists(tmplPath) ? box.files.readText(tmplPath) : "";
70
+ const rb = tierWfBlock(remote);
71
+ const shellOk = rb !== "" && rb === tierWfBlock(tmpl) && remote.includes("# sf-tools: tier-check");
72
+ // sft 版: 対象の確認のステップ(run: の行)が、sft ci check-branch を呼んでいる
73
+ const sftOk = remote.split("\n").some((l) => /^\s*(-\s+)?run:.*\bci check-branch\b/.test(l));
74
+ if (!shellOk && !sftOk)
75
+ bad.push(wf);
76
+ }
77
+ if ((await showOriginMain(box, ".github/workflows/wf-propagate.yml")) !== null)
78
+ bad.push("wf-propagate.yml");
79
+ const ms = (await showOriginMain(box, ".github/workflows/wf-metasync.yml")) ?? "";
80
+ const pushes = ms.split("\n").some((l) => !/^\s*#/.test(l) && l.includes("git push"));
81
+ const msCode = ms
82
+ .split("\n")
83
+ .filter((l) => !/^\s*#/.test(l))
84
+ .join("\n");
85
+ const callsMetasync = ms.includes("sf-metasync.sh") || /\bSFT metasync\b|\bsft(\.js)? metasync\b/.test(msCode);
86
+ if (!callsMetasync || pushes)
87
+ bad.push("wf-metasync.yml");
88
+ return bad;
89
+ }
90
+ async function ghText(box, args) {
91
+ try {
92
+ const r = await box.run("gh", args);
93
+ return r.code === 0 ? r.stdout.replace(/\r/g, "").trim() : "";
94
+ }
95
+ catch {
96
+ return "";
97
+ }
98
+ }
99
+ export async function runProjectStatus(args, box, deps) {
100
+ const err = (l) => box.output.err(l);
101
+ for (const a of args) {
102
+ if (a !== "-v" && a !== "--verbose") {
103
+ err(`[ERROR] 不明なオプションです: ${a}`);
104
+ return 1;
105
+ }
106
+ }
107
+ err("=== セットアップの診断と、Salesforce 接続(段階ごと) ===");
108
+ if (!deps.folderName.startsWith("force-")) {
109
+ err("[FATAL] force-* ディレクトリ内で実行してください。");
110
+ return 1;
111
+ }
112
+ const origin = await originRepo(box);
113
+ if (origin.kind !== "ok") {
114
+ err(origin.kind === "none"
115
+ ? "[FATAL] git remote(origin)の URL を取得できません。"
116
+ : "[FATAL] origin が GitHub のリポジトリではありません。");
117
+ return 1;
118
+ }
119
+ const repo = `${origin.owner}/${origin.repo}`;
120
+ const variable = (name) => ghText(box, ["variable", "get", name, "-R", repo]);
121
+ err("");
122
+ err(` リポジトリ: ${repo}`);
123
+ err("");
124
+ // 2. セットアップの診断(ここでの段階の WARNING は、4 で origin/main について出すため、出さない)
125
+ const cfg = "sf-tools/config/branches.txt";
126
+ const local = tierList(box.files.exists(cfg) ? box.files.readText(cfg) : null);
127
+ const tiers = local.ok ? local.tiers : ["main"];
128
+ const diagNg = await runDiag(box, repo, tiers);
129
+ // 3. 段階ごとの接続先
130
+ err("");
131
+ err(" 【Salesforce の接続(段階ごと)】");
132
+ err("");
133
+ let found = false;
134
+ let violation = false;
135
+ const seen = [];
136
+ for (const tier of tiers) {
137
+ const sfx = tierSuffix(tier);
138
+ const url = await variable(`SF_INSTANCE_URL_${sfx}`);
139
+ const user = await variable(`SF_USERNAME_${sfx}`);
140
+ const kind = await variable(`SF_ORG_KIND_${sfx}`);
141
+ const oid = await variable(`SF_ORG_ID_${sfx}`);
142
+ const oname = await variable(`SF_ORG_NAME_${sfx}`);
143
+ const chk = await variable(`SF_ORG_CHECKED_${sfx}`);
144
+ if (url === "" && user === "") {
145
+ err(` ${tier.padEnd(8)} → (未接続)`);
146
+ continue;
147
+ }
148
+ found = true;
149
+ err(` ${tier.padEnd(8)} → ${url || "(ログイン先が未設定)"}`);
150
+ err(` 種類: ${orgKindLabel(kind || "unknown")} / ユーザー: ${user || "(未設定)"}`);
151
+ if (oid !== "") {
152
+ err(` 組織: ${oname || "(名前なし)"}(ID ${oid})/ 最終確認: ${chk || "不明"}`);
153
+ // 同じ組織 ID が前の段階にあれば警告する(複数あるときは、あとの段階を出す。シェル版と同じ)
154
+ const same = seen.filter((s) => s.id === oid).pop();
155
+ if (same)
156
+ err(`[WARNING] ${tier} は、${same.tier} と、同じ組織につながっています(デプロイが、同じ組織の中で、循環します)。`);
157
+ seen.push({ id: oid, tier });
158
+ }
159
+ else {
160
+ err(" 組織: 未確認(組織の情報が、保存されていません)");
161
+ }
162
+ if (kind === "production" && tier !== "main") {
163
+ err(`[ERROR] ${tier} が、Salesforce の本番組織につながっています(本番組織は main のみです)。`);
164
+ violation = true;
165
+ }
166
+ }
167
+ err("");
168
+ if (!found)
169
+ err("[WARNING] 接続情報が、見つかりませんでした(sft project create が、完了していない可能性があります)。");
170
+ // 4. 段階の切り替え(origin/main の branches.txt)
171
+ err("");
172
+ err(" 【段階の切り替え(origin/main の branches.txt)】");
173
+ err("");
174
+ const fetched = await box.run("git", ["fetch", "origin", "main"]).catch(() => null);
175
+ if (fetched === null || fetched.code !== 0) {
176
+ err("[WARNING] origin/main を取得できないため、段階の切り替えの診断を省きます。");
177
+ }
178
+ else {
179
+ const mainTxt = await showOriginMain(box, cfg);
180
+ if (mainTxt !== null) {
181
+ const names = mainTxt
182
+ .split("\n")
183
+ .map((l) => l.replace(/\r$/, "").trim())
184
+ .filter((l) => l !== "" && !l.startsWith("#"));
185
+ for (const l of names) {
186
+ if (l !== "main" && l !== "staging" && l !== "develop") {
187
+ err(`[WARNING] branches.txt の「${l}」は、段階の名前(main / staging / develop)ではないため、段階としては無視されます。`);
188
+ }
189
+ }
190
+ const enabled = ["main"];
191
+ if (names.includes("staging"))
192
+ enabled.push("staging");
193
+ if (names.includes("develop")) {
194
+ if (names.includes("staging"))
195
+ enabled.push("develop");
196
+ else {
197
+ err("[ERROR] branches.txt に develop がありますが、staging がないため、develop は段階として無効です(develop には staging が必要です)。");
198
+ violation = true;
199
+ }
200
+ }
201
+ err(`[INFO] 有効な段階: ${enabled.join(" ")}`);
202
+ // 無効の段階に、ブランチ・接続情報・Ruleset が残っていれば、知らせる(使われない。戻すときに使う)
203
+ const secretNames = (await ghText(box, ["secret", "list", "-R", repo, "--json", "name", "--jq", ".[].name"])).split("\n");
204
+ const varNames = (await ghText(box, ["variable", "list", "-R", repo, "--json", "name", "--jq", ".[].name"])).split("\n");
205
+ const rulesets = (await ghText(box, ["api", `repos/${repo}/rulesets`, "--jq", '.[] | "\\(.id) \\(.name)"']))
206
+ .split("\n")
207
+ .filter((l) => l !== "");
208
+ for (const t of ["staging", "develop"]) {
209
+ if (enabled.includes(t))
210
+ continue;
211
+ const sfx = tierSuffix(t);
212
+ const ls = await box.run("git", ["ls-remote", "--exit-code", "--heads", "origin", t]).catch(() => null);
213
+ if (ls !== null && ls.code === 0)
214
+ err(`[INFO] ${t} ブランチは残っていますが、段階としては無効です(branches.txt にない)。`);
215
+ const keys = [`SF_CONSUMER_KEY_${sfx}`, `SF_USERNAME_${sfx}`, `SF_INSTANCE_URL_${sfx}`];
216
+ if ([...secretNames, ...varNames].some((n) => keys.includes(n))) {
217
+ err(`[INFO] ${t} の接続情報が残っています(使われません。消すには sft project remove-tier ${t} --purge-secrets)。`);
218
+ }
219
+ for (const r of rulesets) {
220
+ const sp = r.indexOf(" ");
221
+ const id = sp < 0 ? r : r.slice(0, sp);
222
+ const name = sp < 0 ? "" : r.slice(sp + 1);
223
+ const inc = (await ghText(box, ["api", `repos/${repo}/rulesets/${id}`, "--jq", ".conditions.ref_name.include[]?"])).split("\n");
224
+ if (inc.includes(`refs/heads/${t}`)) {
225
+ err(`[INFO] ${t} を対象にする Ruleset が残っています: ${name}(段階が無効のあいだは、${t} への PR を出さないでください)。`);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ else {
231
+ err("[WARNING] origin/main に sf-tools/config/branches.txt がありません(workflow は、段階の切り替えを使わず、今までの動きになります)。");
232
+ }
233
+ const bad = await tierSwitchWfProblems(box, deps.templateDir, deps.shellTemplateDir);
234
+ if (bad.length > 0) {
235
+ err(`[WARNING] workflow が、段階の切り替えに対応していません(または、雛形と違います): ${bad.join(" ")}`);
236
+ err("[WARNING] branches.txt から行を消しても、リリース・検証は止まりません。作業用のブランチで sft project update-workflows を実行し、main まで入れてください(廃止した wf-propagate.yml は、sft project update-workflows --remove wf-propagate.yml で消します)。");
237
+ }
238
+ const stb = await variable("SF_TOOLS_BRANCH");
239
+ if (stb !== "" && stb !== "main" && stb !== "development") {
240
+ err(`[WARNING] Variable SF_TOOLS_BRANCH が「${stb}」です(想定は、未設定・main・development。wf-metasync が、想定しない版の sf-metasync.sh を使います)。`);
241
+ }
242
+ }
243
+ err("");
244
+ err("[INFO] 接続先を変えるとき: sft project reconnect <main|staging|develop>");
245
+ return violation || diagNg > 0 ? 1 : 0;
246
+ }