@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
package/dist/io/run.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// 外のコマンドを動かす、ただ 1 つの場所(doc/rebuild-design.md 7.1)。
|
|
2
|
+
// 非同期だけを使う(ほかのコマンドの実行中も、デバッグログの取得の繰り返しが動くように: 6.3)。シェルは通さない
|
|
3
|
+
// - 時間切れは終了コード 124、中断は 130
|
|
4
|
+
// - 止めるときは、子の子も止める(Windows は taskkill で木ごと。ほかは ps で子孫を調べて、TERM のあと KILL)。
|
|
5
|
+
// プロセスのグループは分けない(端末の Ctrl+C が、子にもそのまま届くように)
|
|
6
|
+
// - 子の子が出力の口を持ったままでも、本体が終われば、少し待って戻る
|
|
7
|
+
// - まとめた出力(output)は、標準出力と標準エラーを行の単位でつなぐ(行の途中に割り込ませない)
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { EXIT } from "../core/exit-codes.js";
|
|
11
|
+
import { isUnsafeForCmd } from "../core/windows-shim.js";
|
|
12
|
+
import { envValue, resolveCommand } from "./resolve.js";
|
|
13
|
+
const TIMED_OUT = 124;
|
|
14
|
+
/** 止める合図のあと、強制で止めるまで */
|
|
15
|
+
const KILL_AFTER_MS = 2_000;
|
|
16
|
+
/** 本体が終わったあと、出力の残りを待つ時間 */
|
|
17
|
+
const DRAIN_MS = 2_000;
|
|
18
|
+
export function mergeEnv(base, extra, platform) {
|
|
19
|
+
const env = { ...base };
|
|
20
|
+
for (const [name, value] of Object.entries(extra ?? {})) {
|
|
21
|
+
const same = (key) => (platform === "win32" ? key.toLowerCase() === name.toLowerCase() : key === name);
|
|
22
|
+
for (const key of Object.keys(env).filter(same))
|
|
23
|
+
delete env[key];
|
|
24
|
+
if (value !== undefined)
|
|
25
|
+
env[name] = value;
|
|
26
|
+
}
|
|
27
|
+
return env;
|
|
28
|
+
}
|
|
29
|
+
const systemFile = (env, name) => join(envValue(env, "SystemRoot") ?? "C:\\Windows", "System32", name);
|
|
30
|
+
function start(resolved, args, cwd, env) {
|
|
31
|
+
if (!resolved.viaCmd)
|
|
32
|
+
return spawn(resolved.file, [...resolved.prefixArgs, ...args], { cwd, env, windowsHide: true });
|
|
33
|
+
const unsafe = [resolved.file, ...args].find(isUnsafeForCmd);
|
|
34
|
+
if (unsafe !== undefined)
|
|
35
|
+
throw new Error(`cmd.exe を通す起動に、渡せない値があります: ${unsafe}`);
|
|
36
|
+
const line = [resolved.file, ...args].map((a) => `"${a}"`).join(" ");
|
|
37
|
+
// cmd.exe は、環境変数(ComSpec)に頼らず、Windows の中のものを完全な場所で指す。
|
|
38
|
+
// NoDefaultCurrentDirectoryInExePath: .cmd の中で名前だけで呼ぶコマンドも、今のフォルダから動かさない
|
|
39
|
+
return spawn(systemFile(env, "cmd.exe"), ["/d", "/s", "/c", `"${line}"`], {
|
|
40
|
+
cwd,
|
|
41
|
+
env: mergeEnv(env, { NoDefaultCurrentDirectoryInExePath: "1" }, "win32"),
|
|
42
|
+
windowsHide: true,
|
|
43
|
+
windowsVerbatimArguments: true,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/** macOS・Linux: root の子孫の番号。調べられないときは、空 */
|
|
47
|
+
function descendants(root) {
|
|
48
|
+
return new Promise((done) => {
|
|
49
|
+
const ps = spawn("ps", ["-A", "-o", "pid=,ppid="], { stdio: ["ignore", "pipe", "ignore"] });
|
|
50
|
+
let text = "";
|
|
51
|
+
ps.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
52
|
+
text += chunk;
|
|
53
|
+
});
|
|
54
|
+
ps.on("error", () => done([]));
|
|
55
|
+
ps.on("close", () => {
|
|
56
|
+
const rows = text
|
|
57
|
+
.trim()
|
|
58
|
+
.split("\n")
|
|
59
|
+
.map((row) => row.trim().split(/\s+/).map(Number));
|
|
60
|
+
const found = [];
|
|
61
|
+
for (let parents = [root]; parents.length > 0;) {
|
|
62
|
+
parents = rows.filter(([, parent]) => parent !== undefined && parents.includes(parent)).map(([pid]) => pid);
|
|
63
|
+
found.push(...parents);
|
|
64
|
+
}
|
|
65
|
+
done(found);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function signalAll(pids, signal) {
|
|
70
|
+
for (const pid of pids) {
|
|
71
|
+
try {
|
|
72
|
+
process.kill(pid, signal);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// もう終わっている
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function stopTree(child, env) {
|
|
80
|
+
const pid = child.pid;
|
|
81
|
+
if (pid === undefined)
|
|
82
|
+
return;
|
|
83
|
+
if (process.platform === "win32") {
|
|
84
|
+
const killer = spawn(systemFile(env, "taskkill.exe"), ["/T", "/F", "/PID", String(pid)], { windowsHide: true, stdio: "ignore" });
|
|
85
|
+
killer.on("error", () => child.kill("SIGKILL"));
|
|
86
|
+
killer.on("close", (code) => {
|
|
87
|
+
if (code !== 0)
|
|
88
|
+
child.kill("SIGKILL");
|
|
89
|
+
});
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// 子孫の番号は、親を止める前に調べる(親が先に終わると、たどれなくなる)
|
|
93
|
+
void descendants(pid).then((found) => {
|
|
94
|
+
signalAll(found, "SIGTERM");
|
|
95
|
+
child.kill("SIGTERM");
|
|
96
|
+
// 本体が先に終わっても、残った子孫は KILL する
|
|
97
|
+
setTimeout(() => {
|
|
98
|
+
void descendants(pid).then((later) => {
|
|
99
|
+
signalAll([...found, ...later], "SIGKILL");
|
|
100
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
101
|
+
child.kill("SIGKILL");
|
|
102
|
+
});
|
|
103
|
+
}, KILL_AFTER_MS);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export const run = async (command, args, options = {}) => {
|
|
107
|
+
const platform = process.platform;
|
|
108
|
+
const env = mergeEnv(process.env, options.env, platform);
|
|
109
|
+
const cwd = options.cwd ?? process.cwd();
|
|
110
|
+
const resolved = await resolveCommand(command, {
|
|
111
|
+
platform,
|
|
112
|
+
env,
|
|
113
|
+
cwd,
|
|
114
|
+
allowCmdShell: options.allowCmdShell ?? false,
|
|
115
|
+
nodeOutput: async (script) => (await run(process.execPath, [script], { cwd, env: options.env, timeoutMs: 30_000 })).stdout,
|
|
116
|
+
});
|
|
117
|
+
if (options.signal?.aborted) {
|
|
118
|
+
return { code: EXIT.interrupted, stdout: "", stderr: "", output: "", timedOut: false, aborted: true };
|
|
119
|
+
}
|
|
120
|
+
return new Promise((done, fail) => {
|
|
121
|
+
const child = start(resolved, args, cwd, env);
|
|
122
|
+
const result = { stdout: "", stderr: "", output: "" };
|
|
123
|
+
const partial = { stdout: "", stderr: "" };
|
|
124
|
+
let timedOut = false;
|
|
125
|
+
let aborted = false;
|
|
126
|
+
let stopping = false;
|
|
127
|
+
let finished = false;
|
|
128
|
+
let limit;
|
|
129
|
+
let drain;
|
|
130
|
+
let giveUp;
|
|
131
|
+
const cleanup = () => {
|
|
132
|
+
finished = true;
|
|
133
|
+
clearTimeout(limit);
|
|
134
|
+
clearTimeout(drain);
|
|
135
|
+
clearTimeout(giveUp);
|
|
136
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
137
|
+
};
|
|
138
|
+
const finish = (exitCode) => {
|
|
139
|
+
if (finished)
|
|
140
|
+
return;
|
|
141
|
+
cleanup();
|
|
142
|
+
child.stdout?.destroy();
|
|
143
|
+
child.stderr?.destroy();
|
|
144
|
+
for (const rest of [partial.stdout, partial.stderr]) {
|
|
145
|
+
if (rest === "")
|
|
146
|
+
continue;
|
|
147
|
+
if (result.output !== "" && !result.output.endsWith("\n"))
|
|
148
|
+
result.output += "\n";
|
|
149
|
+
result.output += rest;
|
|
150
|
+
}
|
|
151
|
+
const code = timedOut ? TIMED_OUT : aborted ? EXIT.interrupted : (exitCode ?? EXIT.failure);
|
|
152
|
+
done({ code, ...result, timedOut, aborted });
|
|
153
|
+
};
|
|
154
|
+
const stop = () => {
|
|
155
|
+
if (stopping)
|
|
156
|
+
return;
|
|
157
|
+
stopping = true;
|
|
158
|
+
stopTree(child, env);
|
|
159
|
+
// 止める手段が失敗しても、呼び出し元は必ず戻す
|
|
160
|
+
giveUp = setTimeout(() => {
|
|
161
|
+
child.kill("SIGKILL");
|
|
162
|
+
finish(null);
|
|
163
|
+
}, KILL_AFTER_MS + DRAIN_MS * 2);
|
|
164
|
+
};
|
|
165
|
+
const onAbort = () => {
|
|
166
|
+
aborted = true;
|
|
167
|
+
stop();
|
|
168
|
+
};
|
|
169
|
+
for (const stream of ["stdout", "stderr"]) {
|
|
170
|
+
// 文字の途中で切れないように、ストリームの側で UTF-8 にする
|
|
171
|
+
child[stream]?.setEncoding("utf8").on("data", (text) => {
|
|
172
|
+
result[stream] += text;
|
|
173
|
+
const joined = partial[stream] + text;
|
|
174
|
+
const end = joined.lastIndexOf("\n") + 1;
|
|
175
|
+
result.output += joined.slice(0, end);
|
|
176
|
+
partial[stream] = joined.slice(end);
|
|
177
|
+
options.onChunk?.(text, stream);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
child.on("error", (error) => {
|
|
181
|
+
if (finished)
|
|
182
|
+
return;
|
|
183
|
+
cleanup();
|
|
184
|
+
fail(new Error(`コマンドを起動できません: ${command}(${error.message})`));
|
|
185
|
+
});
|
|
186
|
+
child.on("exit", (code) => {
|
|
187
|
+
drain = setTimeout(() => finish(code), DRAIN_MS);
|
|
188
|
+
});
|
|
189
|
+
child.on("close", (code) => finish(code));
|
|
190
|
+
// 子が入力を読まずに終わると、書き込みが失敗する。結果は終了コードで分かるので、ここでは何もしない
|
|
191
|
+
child.stdin?.on("error", () => { });
|
|
192
|
+
child.stdin?.end(options.input ?? "");
|
|
193
|
+
if (options.timeoutMs !== undefined) {
|
|
194
|
+
limit = setTimeout(() => {
|
|
195
|
+
timedOut = true;
|
|
196
|
+
stop();
|
|
197
|
+
}, options.timeoutMs);
|
|
198
|
+
}
|
|
199
|
+
options.signal?.addEventListener("abort", onAbort);
|
|
200
|
+
});
|
|
201
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// sft switch-org の外の部品の本物。ログインのやり直しは、sft の起動の準備と同じ loginOrg(src/core/start.ts)で行う
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { loginOrg } from "../core/start.js";
|
|
4
|
+
import { loginPrompts } from "../ui/prompt.js";
|
|
5
|
+
export function realSwitchOrgDeps(box) {
|
|
6
|
+
return {
|
|
7
|
+
folderName: basename(process.cwd()),
|
|
8
|
+
relogin: async () => ((await loginOrg(box, { prompts: loginPrompts(box.input) })) === null ? 1 : 0),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// 一時フォルダの本物(作る・消す)。テストは、偽物を渡す
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
/**
|
|
6
|
+
* フォルダを消す。Windows では、ウイルス対策・インデクサー・終わったばかりの子プロセスが、フォルダをつかんでいて、EPERM・EBUSY で
|
|
7
|
+
* 消せないことがある(ときどき起きる)ため、再試行する(maxRetries・retryDelay)。それでも消せなければ、止めずに、消せなかった場所を
|
|
8
|
+
* 警告に出す(中に、アクセストークンなどが残ることがある。手で消してもらう)。呼ぶ側の処理を、後始末の失敗で、失敗にしない
|
|
9
|
+
*/
|
|
10
|
+
export function removeTree(path) {
|
|
11
|
+
try {
|
|
12
|
+
rmSync(path, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
const code = e.code ?? "";
|
|
16
|
+
process.stderr.write(`[WARNING] 一時フォルダを消せませんでした(${code})。中に、秘密の値が残っているかもしれません。手で消してください: ${path}\n`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function realTempDeps(prefix) {
|
|
20
|
+
return {
|
|
21
|
+
makeTempDir: () => mkdtempSync(join(process.env.TMPDIR || tmpdir(), prefix)),
|
|
22
|
+
removeDir: removeTree,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readVersion } from "./package-version.js";
|
|
2
|
+
export async function toolVersions(run) {
|
|
3
|
+
const first = async (command) => {
|
|
4
|
+
try {
|
|
5
|
+
const result = await run(command, ["--version"], { timeoutMs: 20_000 });
|
|
6
|
+
return result.code === 0 ? (result.stdout.trim().split("\n")[0] ?? "不明") : "不明";
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return "なし";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const [git, sf] = await Promise.all([first("git"), first("sf")]);
|
|
13
|
+
return `git ${git.replace(/^git version /, "")} | node ${process.version} | sf ${sf} | sft ${readVersion()}`;
|
|
14
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// 本物の道具の箱(doc/rebuild-design.md 7.2)
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { terminalInput } from "./keys.js";
|
|
4
|
+
import { run } from "./run.js";
|
|
5
|
+
export function realToolbox() {
|
|
6
|
+
return {
|
|
7
|
+
run,
|
|
8
|
+
env: (name) => process.env[name],
|
|
9
|
+
// ログは書いた順を保つため、同期で書く(同期にしてはいけないのは、コマンドの起動: 6.3)
|
|
10
|
+
files: {
|
|
11
|
+
readText: (path) => readFileSync(path, "utf8"),
|
|
12
|
+
writeText: (path, text) => writeFileSync(path, text, "utf8"),
|
|
13
|
+
appendText: (path, text) => appendFileSync(path, text, "utf8"),
|
|
14
|
+
exists: (path) => existsSync(path),
|
|
15
|
+
makeDir: (path) => {
|
|
16
|
+
mkdirSync(path, { recursive: true });
|
|
17
|
+
},
|
|
18
|
+
isDir: (path) => {
|
|
19
|
+
try {
|
|
20
|
+
return statSync(path).isDirectory();
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
walkFiles: (root) => {
|
|
27
|
+
const found = [];
|
|
28
|
+
const walk = (rel) => {
|
|
29
|
+
let entries;
|
|
30
|
+
try {
|
|
31
|
+
entries = readdirSync(rel === "" ? root : `${root}/${rel}`, { withFileTypes: true });
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
for (const e of entries) {
|
|
37
|
+
if (e.name === ".git" || e.name === "node_modules")
|
|
38
|
+
continue;
|
|
39
|
+
const child = rel === "" ? e.name : `${rel}/${e.name}`;
|
|
40
|
+
if (e.isDirectory())
|
|
41
|
+
walk(child);
|
|
42
|
+
else
|
|
43
|
+
found.push(child);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
walk("");
|
|
47
|
+
return found.sort();
|
|
48
|
+
},
|
|
49
|
+
removeFile: (path) => {
|
|
50
|
+
rmSync(path, { force: true });
|
|
51
|
+
},
|
|
52
|
+
listDir: (path) => {
|
|
53
|
+
try {
|
|
54
|
+
return readdirSync(path);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
clock: { now: () => new Date(), sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)) },
|
|
62
|
+
output: {
|
|
63
|
+
out: (line) => console.log(line),
|
|
64
|
+
err: (line) => console.error(line),
|
|
65
|
+
},
|
|
66
|
+
input: terminalInput(process.stdin, process.stderr, { stdinSecret: process.env.SFT_SECRET_STDIN === "1" }),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// sft update-cli の外の部品の本物(sf の更新のロック・書き込みの確かめ)。ロックの場所と形は、シェル版(sf-update-cli.sh・sf-log.sh)と同じ:
|
|
2
|
+
// ~/.sf-tools-upgrade.lock(SF_TOOLS_UPGRADE_LOCK で差し替え可)に PID=<数字> の 1 行。ないときだけ作る(O_EXCL)。
|
|
3
|
+
// 持ち主が動いている = PID が生きていて、ロックが 2 時間より新しい。動いていなければ、途中で切れたものとして消し、もう一度だけ作る
|
|
4
|
+
import { closeSync, openSync, readFileSync, rmSync, statSync, writeSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
const STALE_MS = 2 * 60 * 60 * 1000;
|
|
8
|
+
function holderAlive(path) {
|
|
9
|
+
let pid;
|
|
10
|
+
try {
|
|
11
|
+
pid = /^PID=([0-9]+)$/m.exec(readFileSync(path, "utf8"))?.[1];
|
|
12
|
+
if (pid === undefined || pid === String(process.pid))
|
|
13
|
+
return null;
|
|
14
|
+
if (Date.now() - statSync(path).mtimeMs >= STALE_MS)
|
|
15
|
+
return null;
|
|
16
|
+
process.kill(Number(pid), 0);
|
|
17
|
+
return pid;
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
// EPERM は、別のユーザーの生きたプロセス(ロックを奪わない)
|
|
21
|
+
return e.code === "EPERM" && pid !== undefined ? pid : null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function tryCreate(path) {
|
|
25
|
+
try {
|
|
26
|
+
const fd = openSync(path, "wx");
|
|
27
|
+
writeSync(fd, `PID=${process.pid}\n`);
|
|
28
|
+
closeSync(fd);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** sf の更新のロックの場所(sf-update-cli.sh と同じ) */
|
|
36
|
+
export const upgradeLockPath = (box) => box.env("SF_TOOLS_UPGRADE_LOCK") || join(homedir(), ".sf-tools-upgrade.lock");
|
|
37
|
+
/** ほかのプロセスが、sf を更新しているか(ロックの PID が生きていて、古くない) */
|
|
38
|
+
export function upgradeRunning(box) {
|
|
39
|
+
return holderAlive(upgradeLockPath(box)) !== null;
|
|
40
|
+
}
|
|
41
|
+
export function realUpdateCliDeps(box) {
|
|
42
|
+
const lock = upgradeLockPath(box);
|
|
43
|
+
return {
|
|
44
|
+
acquireLock: () => {
|
|
45
|
+
if (tryCreate(lock))
|
|
46
|
+
return { kind: "ok" };
|
|
47
|
+
const pid = holderAlive(lock);
|
|
48
|
+
if (pid !== null)
|
|
49
|
+
return { kind: "busy", pid };
|
|
50
|
+
rmSync(lock, { force: true });
|
|
51
|
+
if (tryCreate(lock))
|
|
52
|
+
return { kind: "ok" };
|
|
53
|
+
const again = holderAlive(lock);
|
|
54
|
+
return again !== null ? { kind: "busy", pid: again } : { kind: "unwritable" };
|
|
55
|
+
},
|
|
56
|
+
releaseLock: () => {
|
|
57
|
+
try {
|
|
58
|
+
if (/^PID=([0-9]+)$/m.exec(readFileSync(lock, "utf8"))?.[1] === String(process.pid))
|
|
59
|
+
rmSync(lock, { force: true });
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// 自分のロックでなければ・読めなければ、触らない
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
dirWritable: (path) => {
|
|
66
|
+
const t = join(path, `.sf-tools-write-test-${process.pid}`);
|
|
67
|
+
if (!tryCreate(t))
|
|
68
|
+
return false;
|
|
69
|
+
rmSync(t, { force: true });
|
|
70
|
+
return true;
|
|
71
|
+
},
|
|
72
|
+
sfBrokenMark: box.env("SF_TOOLS_SF_BROKEN_MARK") || join(homedir(), ".sf-tools-sf-broken"),
|
|
73
|
+
platform: process.platform,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// sft project update-secret の外の部品の本物(入力の画面・ホーム・一時フォルダ・OS・今の日時)。テストは、偽物を渡す(src/core/update-secret.ts)
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { chooseNumber, readOrQuit } from "../ui/prompt.js";
|
|
4
|
+
import { realTempDeps } from "./temp-dir.js";
|
|
5
|
+
export function realUpdateSecretDeps(box) {
|
|
6
|
+
return {
|
|
7
|
+
...realTempDeps("sf-tools-home."),
|
|
8
|
+
prompts: {
|
|
9
|
+
text: async (prompt) => {
|
|
10
|
+
const r = await readOrQuit(box.input, prompt);
|
|
11
|
+
return r.kind === "text" ? r.value : "quit";
|
|
12
|
+
},
|
|
13
|
+
line: (prompt) => box.input.readLine(prompt),
|
|
14
|
+
choose: async (title, items) => {
|
|
15
|
+
const r = await chooseNumber(box.input, box.output, title, items);
|
|
16
|
+
return r.kind === "chosen" ? r.index : "quit";
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
home: homedir(),
|
|
20
|
+
platform: process.platform,
|
|
21
|
+
nowText: () => {
|
|
22
|
+
const d = box.clock.now();
|
|
23
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
24
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
package/dist/launch.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
// 入口(bin/sft.js)から呼ぶ、起動の最初の手順(doc/rebuild-design.md 4.2):
|
|
10
|
+
// 版を固定したプロジェクトの中なら、そろっているかを確かめて、中の版へ渡す。固定がなければ、自分で動く。
|
|
11
|
+
//
|
|
12
|
+
// 全体の sft と中の版の受け渡しの約束(約束の版 1。凍結する: 4.7)
|
|
13
|
+
// - 中の版の入口 sf-tools/node_modules/@tama-create/sf-tools/bin/sft.js を、同じプロセスで読み込む
|
|
14
|
+
// - 読み込まれた入口は、process.argv の引数で動き、終了コードを process.exitCode に入れてから、読み込みを終える
|
|
15
|
+
// - 渡す側は、環境変数 SFT_DELEGATED に「約束の版|渡す先の入口」を入れる
|
|
16
|
+
// - 受けた側は、この環境変数をすぐ消し(子のプロセスが、版の確認を飛ばさないように)、もう一度は渡さない。
|
|
17
|
+
// 宛先が自分でなければ、止まる(同じ所へ、何度も渡さないように)
|
|
18
|
+
//
|
|
19
|
+
// 中の版を直接起動して版が違うとき(Actions・フックの呼び方)は、聞かず・入れずに止まる。
|
|
20
|
+
// 入れ直しても、読み込み済みの古いコードが動き続けるため
|
|
21
|
+
import { realpathSync } from "node:fs";
|
|
22
|
+
import { isAbsolute, relative } from "node:path";
|
|
23
|
+
import { pathToFileURL } from "node:url";
|
|
24
|
+
import { main } from "./cli.js";
|
|
25
|
+
import { decidePlan, PACKAGE_NAME } from "./core/delegate.js";
|
|
26
|
+
import { EXIT } from "./core/exit-codes.js";
|
|
27
|
+
import { createLineWriter } from "./core/lines.js";
|
|
28
|
+
import { maskSecrets } from "./core/mask.js";
|
|
29
|
+
import { findPinnedProject } from "./io/pinned-project.js";
|
|
30
|
+
import { realToolbox } from "./io/toolbox.js";
|
|
31
|
+
import { askYesNo } from "./ui/confirm.js";
|
|
32
|
+
export const DELEGATED = "SFT_DELEGATED";
|
|
33
|
+
const CONTRACT = 1;
|
|
34
|
+
const INSTALL_LIMIT_MS = 10 * 60_000;
|
|
35
|
+
async function loadEntry(entry) {
|
|
36
|
+
// Windows の C:\ のパスは、そのままでは import() に渡せないので、file URL にする
|
|
37
|
+
await import(__rewriteRelativeImportExtension(pathToFileURL(entry).href));
|
|
38
|
+
return Number(process.exitCode ?? EXIT.ok);
|
|
39
|
+
}
|
|
40
|
+
/** 実際の場所(ジャンクションやリンクをたどる)。なければ、そのまま */
|
|
41
|
+
function realPathOf(path) {
|
|
42
|
+
try {
|
|
43
|
+
return realpathSync.native(path);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return path;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** 同じファイルを指すか。Windows は、パスの大文字・小文字を区別しない */
|
|
50
|
+
function samePath(a, b) {
|
|
51
|
+
const [x, y] = [realPathOf(a), realPathOf(b)];
|
|
52
|
+
return process.platform === "win32" ? x.toLowerCase() === y.toLowerCase() : x === y;
|
|
53
|
+
}
|
|
54
|
+
function isInside(folder, file) {
|
|
55
|
+
const way = relative(realPathOf(folder), realPathOf(file));
|
|
56
|
+
return way !== "" && !way.startsWith("..") && !isAbsolute(way);
|
|
57
|
+
}
|
|
58
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
59
|
+
async function planAt(cwd) {
|
|
60
|
+
try {
|
|
61
|
+
return decidePlan(await findPinnedProject(cwd));
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return { kind: "broken", reason: messageOf(error) };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** 固定した版を入れる。そろったら null、止まるときは終了コード */
|
|
68
|
+
async function install(plan, context, box) {
|
|
69
|
+
const { output } = box;
|
|
70
|
+
const fact = `固定した版の ${PACKAGE_NAME} が入っていません(固定: ${plan.locked} / 入っている版: ${plan.installed ?? "なし"})`;
|
|
71
|
+
const question = `npm ci --prefix "${plan.folder}" で、固定した版を入れますか?`;
|
|
72
|
+
if (context.env.GITHUB_ACTIONS === "true" || isInside(plan.folder, context.selfEntry)) {
|
|
73
|
+
output.err(`[ERROR] ${fact}`);
|
|
74
|
+
return EXIT.failure;
|
|
75
|
+
}
|
|
76
|
+
output.err(`[WARNING] ${fact}`);
|
|
77
|
+
if (context.argv.includes("--ai") || context.env.SF_TOOLS_AI === "1") {
|
|
78
|
+
// 名前のない質問は、AI からは答えられない(人に頼む合図)
|
|
79
|
+
output.err(`[確認待ち] (名前なし): ${question}`);
|
|
80
|
+
return EXIT.needAnswer;
|
|
81
|
+
}
|
|
82
|
+
const answer = await askYesNo(box.input, question);
|
|
83
|
+
if (answer === "interrupted")
|
|
84
|
+
return EXIT.interrupted;
|
|
85
|
+
if (answer !== "yes")
|
|
86
|
+
return EXIT.failure;
|
|
87
|
+
const writers = { stdout: createLineWriter((line) => output.err(line)), stderr: createLineWriter((line) => output.err(line)) };
|
|
88
|
+
const result = await box.run("npm", ["ci", "--prefix", plan.folder, "--ignore-scripts", "--no-audit", "--no-fund"], {
|
|
89
|
+
timeoutMs: INSTALL_LIMIT_MS,
|
|
90
|
+
onChunk: (chunk, stream) => writers[stream].push(chunk),
|
|
91
|
+
});
|
|
92
|
+
writers.stdout.flush();
|
|
93
|
+
writers.stderr.flush();
|
|
94
|
+
if (result.code !== 0) {
|
|
95
|
+
output.err("[ERROR] npm ci が失敗しました");
|
|
96
|
+
return EXIT.failure;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
async function start(context, box) {
|
|
101
|
+
const received = context.env[DELEGATED];
|
|
102
|
+
delete context.env[DELEGATED];
|
|
103
|
+
if (received !== undefined && received !== "") {
|
|
104
|
+
// 約束の版が新しくなっても、宛先が自分なら受ける(新しい版は、足すだけにする: 4.7)
|
|
105
|
+
const target = /^\d+\|(.*)$/s.exec(received)?.[1];
|
|
106
|
+
if (target !== undefined && samePath(target, context.selfEntry))
|
|
107
|
+
return main(context.argv, box);
|
|
108
|
+
box.output.err(`[ERROR] 受け渡しの印(${DELEGATED})の宛先が、この sft ではありません: ${maskSecrets(received)}`);
|
|
109
|
+
return EXIT.failure;
|
|
110
|
+
}
|
|
111
|
+
let plan = await planAt(context.cwd);
|
|
112
|
+
let installedNow = false;
|
|
113
|
+
if (plan.kind === "install") {
|
|
114
|
+
const stopped = await install(plan, context, box);
|
|
115
|
+
if (stopped !== null)
|
|
116
|
+
return stopped;
|
|
117
|
+
installedNow = true;
|
|
118
|
+
plan = await planAt(context.cwd);
|
|
119
|
+
if (plan.kind === "install") {
|
|
120
|
+
box.output.err("[ERROR] 入れたあとも、固定した版がそろいません");
|
|
121
|
+
return EXIT.failure;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (plan.kind === "broken") {
|
|
125
|
+
box.output.err(`[ERROR] ${maskSecrets(plan.reason)}`);
|
|
126
|
+
return EXIT.failure;
|
|
127
|
+
}
|
|
128
|
+
if (plan.kind === "self")
|
|
129
|
+
return main(context.argv, box);
|
|
130
|
+
if (samePath(plan.entry, context.selfEntry)) {
|
|
131
|
+
if (!installedNow)
|
|
132
|
+
return main(context.argv, box);
|
|
133
|
+
// どう呼ばれても、入れ直した回に、読み込み済みの古いコードは動かさない
|
|
134
|
+
box.output.err("[ERROR] 固定した版を入れ直しました。動いている sft は入れ直す前の版なので、ここで止まります");
|
|
135
|
+
return EXIT.failure;
|
|
136
|
+
}
|
|
137
|
+
if (context.env.SFT_DEBUG === "1")
|
|
138
|
+
box.output.err(`[受け渡し] ${context.selfEntry} → ${plan.entry}`);
|
|
139
|
+
context.env[DELEGATED] = `${CONTRACT}|${plan.entry}`;
|
|
140
|
+
return (context.load ?? loadEntry)(plan.entry);
|
|
141
|
+
}
|
|
142
|
+
export async function launch(context) {
|
|
143
|
+
const box = context.box ?? realToolbox();
|
|
144
|
+
try {
|
|
145
|
+
return await start(context, box);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// どこで失敗しても、伏せ字にした 1 行で出す(生の文や、呼び出しの経路を出さない)
|
|
149
|
+
box.output.err(`[ERROR] ${maskSecrets(messageOf(error))}`);
|
|
150
|
+
return EXIT.failure;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const ctrlC = String.fromCharCode(3);
|
|
2
|
+
export async function askYesNo(input, question) {
|
|
3
|
+
for (;;) {
|
|
4
|
+
const key = await input.readKey(` ${question} [Y/N/q]: `);
|
|
5
|
+
// 入力が終わった(答える人がいない)ときは、中断にする
|
|
6
|
+
if (key === null)
|
|
7
|
+
return "quit";
|
|
8
|
+
if (key === ctrlC)
|
|
9
|
+
return "interrupted";
|
|
10
|
+
const lower = key.toLowerCase();
|
|
11
|
+
if (lower === "y")
|
|
12
|
+
return "yes";
|
|
13
|
+
if (lower === "n")
|
|
14
|
+
return "no";
|
|
15
|
+
if (lower === "q")
|
|
16
|
+
return "quit";
|
|
17
|
+
}
|
|
18
|
+
}
|