@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.2
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/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 +36 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +40 -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 +93 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +19 -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/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +314 -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 +184 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +85 -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/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +199 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +235 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +380 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +152 -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 +68 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +31 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +161 -0
- package/dist/io/log-deps.js +106 -0
- package/dist/io/metasync-deps.js +21 -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 +27 -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 +10 -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 +132 -0
- package/dist/ui/prompt.js +66 -0
- package/package.json +37 -4
package/dist/io/keys.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// キー入力(doc/rebuild-design.md 6.2)。
|
|
2
|
+
// - 端末のとき: キー 1 つ(Enter は要らない)。↑ などは、まとまった 1 回分の文字になる
|
|
3
|
+
// - 端末でないとき(Node から端末に見えない端末・パイプ): 打って Enter の 1 行を、1 回分にする
|
|
4
|
+
// - 1 行の入力(readLine): 端末でも、Enter までの 1 行
|
|
5
|
+
// - 秘密の入力(readSecret): 端末のときだけ。Enter まで raw モードのまま読み、打った文字の代わりに * を出す(設計 5.5)
|
|
6
|
+
// 読むときだけ標準入力を開き、読み終えたら閉じる(プロセスが終われるように。子のプロセスに入力が渡るように)
|
|
7
|
+
import { StringDecoder } from "node:string_decoder";
|
|
8
|
+
export const CTRL_C = String.fromCharCode(3);
|
|
9
|
+
export function terminalInput(source = process.stdin, sink = process.stderr) {
|
|
10
|
+
const decoder = new StringDecoder("utf8");
|
|
11
|
+
/** 行で読むときの、読み残し */
|
|
12
|
+
let buffered = "";
|
|
13
|
+
let ended = false;
|
|
14
|
+
const takeLine = () => {
|
|
15
|
+
const end = buffered.indexOf("\n");
|
|
16
|
+
if (end < 0)
|
|
17
|
+
return null;
|
|
18
|
+
const line = buffered.slice(0, end).replace(/\r$/, "");
|
|
19
|
+
buffered = buffered.slice(end + 1);
|
|
20
|
+
return line.trim();
|
|
21
|
+
};
|
|
22
|
+
/** 次の 1 回分。入力が終わっていれば null */
|
|
23
|
+
const next = (raw) => new Promise((done) => {
|
|
24
|
+
if (!raw) {
|
|
25
|
+
const line = takeLine();
|
|
26
|
+
if (line !== null)
|
|
27
|
+
return done(line);
|
|
28
|
+
}
|
|
29
|
+
if (ended || source.readableEnded)
|
|
30
|
+
return done(null);
|
|
31
|
+
const finish = (value) => {
|
|
32
|
+
source.off("data", onData);
|
|
33
|
+
source.off("end", onEnd);
|
|
34
|
+
source.off("error", onError);
|
|
35
|
+
if (raw)
|
|
36
|
+
source.setRawMode?.(false);
|
|
37
|
+
source.pause();
|
|
38
|
+
source.unref?.();
|
|
39
|
+
done(value);
|
|
40
|
+
};
|
|
41
|
+
const onData = (chunk) => {
|
|
42
|
+
const received = typeof chunk === "string" ? chunk : decoder.write(chunk);
|
|
43
|
+
if (raw)
|
|
44
|
+
return finish(received);
|
|
45
|
+
buffered += received;
|
|
46
|
+
const line = takeLine();
|
|
47
|
+
if (line !== null)
|
|
48
|
+
finish(line);
|
|
49
|
+
};
|
|
50
|
+
const onEnd = () => {
|
|
51
|
+
ended = true;
|
|
52
|
+
// 改行のない最後の 1 行も、1 回分にする
|
|
53
|
+
const rest = buffered.trim();
|
|
54
|
+
buffered = "";
|
|
55
|
+
finish(rest === "" ? null : rest);
|
|
56
|
+
};
|
|
57
|
+
// 入力の流れの失敗は、読み残しを答えにしない
|
|
58
|
+
const onError = () => {
|
|
59
|
+
ended = true;
|
|
60
|
+
buffered = "";
|
|
61
|
+
finish(null);
|
|
62
|
+
};
|
|
63
|
+
source.on("data", onData);
|
|
64
|
+
source.on("end", onEnd);
|
|
65
|
+
source.on("error", onError);
|
|
66
|
+
if (raw)
|
|
67
|
+
source.setRawMode?.(true);
|
|
68
|
+
source.ref?.();
|
|
69
|
+
source.resume();
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* 秘密の入力。Enter まで raw モードを外さない(途中で外すと、速く打った文字が、端末にそのまま表示されることがあるため)。
|
|
73
|
+
* BS・DEL で 1 文字消す。Ctrl+C は CTRL_C、Ctrl+D(空のとき)・入力の終わり・失敗は null
|
|
74
|
+
*/
|
|
75
|
+
const secret = () => new Promise((done) => {
|
|
76
|
+
if (ended || source.readableEnded)
|
|
77
|
+
return done(null);
|
|
78
|
+
let value = "";
|
|
79
|
+
const finish = (result) => {
|
|
80
|
+
source.off("data", onData);
|
|
81
|
+
source.off("end", onEnd);
|
|
82
|
+
source.off("error", onEnd);
|
|
83
|
+
source.setRawMode?.(false);
|
|
84
|
+
source.pause();
|
|
85
|
+
source.unref?.();
|
|
86
|
+
sink.write("\n");
|
|
87
|
+
done(result);
|
|
88
|
+
};
|
|
89
|
+
// 矢印キーなどの制御シーケンス(ESC [ … 終わりの文字 / ESC O 1 文字)は、最後まで読み捨てる(値に入れない)
|
|
90
|
+
let esc = "";
|
|
91
|
+
const onData = (chunk) => {
|
|
92
|
+
for (const ch of typeof chunk === "string" ? chunk : decoder.write(chunk)) {
|
|
93
|
+
if (esc === "start") {
|
|
94
|
+
esc = ch === "[" ? "csi" : ch === "O" ? "ss3" : "";
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (esc === "csi") {
|
|
98
|
+
if (ch >= "@" && ch <= "~")
|
|
99
|
+
esc = "";
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (esc === "ss3") {
|
|
103
|
+
esc = "";
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (ch === "\u001b") {
|
|
107
|
+
esc = "start";
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (ch === "\r" || ch === "\n")
|
|
111
|
+
return finish(value);
|
|
112
|
+
if (ch === CTRL_C)
|
|
113
|
+
return finish(CTRL_C);
|
|
114
|
+
if (ch === String.fromCharCode(4) && value === "")
|
|
115
|
+
return finish(null);
|
|
116
|
+
if (ch === String.fromCharCode(127) || ch === "\b") {
|
|
117
|
+
if (value !== "") {
|
|
118
|
+
value = [...value].slice(0, -1).join("");
|
|
119
|
+
sink.write("\b \b");
|
|
120
|
+
}
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (ch < " ")
|
|
124
|
+
continue; // ほかの制御文字(矢印キーの一部など)は、値に入れない
|
|
125
|
+
value += ch;
|
|
126
|
+
sink.write("*");
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const onEnd = () => {
|
|
130
|
+
ended = true;
|
|
131
|
+
finish(null);
|
|
132
|
+
};
|
|
133
|
+
source.on("data", onData);
|
|
134
|
+
source.on("end", onEnd);
|
|
135
|
+
source.on("error", onEnd);
|
|
136
|
+
source.setRawMode?.(true);
|
|
137
|
+
source.ref?.();
|
|
138
|
+
source.resume();
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
readLine: async (prompt) => {
|
|
142
|
+
sink.write(prompt);
|
|
143
|
+
return next(false);
|
|
144
|
+
},
|
|
145
|
+
readSecret: async (prompt) => {
|
|
146
|
+
if (!(source.isTTY === true && typeof source.setRawMode === "function"))
|
|
147
|
+
return "no-tty";
|
|
148
|
+
sink.write(prompt);
|
|
149
|
+
return secret();
|
|
150
|
+
},
|
|
151
|
+
readKey: async (prompt) => {
|
|
152
|
+
const raw = source.isTTY === true && typeof source.setRawMode === "function";
|
|
153
|
+
sink.write(prompt);
|
|
154
|
+
const key = await next(raw);
|
|
155
|
+
// 端末のキー 1 つは画面に出ないので、打った文字を出して改行する
|
|
156
|
+
if (raw)
|
|
157
|
+
sink.write(key !== null && /^[ -~]$/.test(key) ? `${key}\n` : "\n");
|
|
158
|
+
return key;
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// sft log の外の部品の本物(lib/apexlog.js の場所・印のフォルダのロック・一時フォルダ・プロセスの生死)。テストは、偽物を渡す
|
|
2
|
+
import { mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { RUNTIME_SELF } from "../core/log-pull.js";
|
|
7
|
+
import { upgradeRunning } from "./update-cli-deps.js";
|
|
8
|
+
/** 別の環境のロックは、10 分で古いとみなす(シェル版の FOREIGN_LOCK_AGE と同じ) */
|
|
9
|
+
const FOREIGN_LOCK_AGE = 600;
|
|
10
|
+
export function realLogSummarizeDeps(box) {
|
|
11
|
+
return { apexlogJs: box.env("SF_APEXLOG_JS") || fileURLToPath(new URL("../../lib/apexlog.js", import.meta.url)) };
|
|
12
|
+
}
|
|
13
|
+
export function pidAlive(pid) {
|
|
14
|
+
try {
|
|
15
|
+
process.kill(pid, 0);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return e.code === "EPERM";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** ロックの印(「PID 環境 時刻」)から、古いロックか。同じ環境(と環境のない古い形)は持ち主の生死、別の環境は時刻で決める */
|
|
23
|
+
export function lockIsStale(line, nowSec, alive = pidAlive) {
|
|
24
|
+
const [pid = "", rt = "", at = ""] = line.trim().split(/\s+/);
|
|
25
|
+
if (!/^[0-9]+$/.test(pid))
|
|
26
|
+
return false;
|
|
27
|
+
if (rt === "" || rt === RUNTIME_SELF)
|
|
28
|
+
return !alive(Number(pid));
|
|
29
|
+
if (!/^[0-9]+$/.test(at))
|
|
30
|
+
return false;
|
|
31
|
+
return Number(at) <= nowSec && nowSec - Number(at) > FOREIGN_LOCK_AGE;
|
|
32
|
+
}
|
|
33
|
+
/** 印のフォルダのロック(シェル版の lock_take・lock_drop と同じ形) */
|
|
34
|
+
export function folderLock() {
|
|
35
|
+
return {
|
|
36
|
+
take: async (dir, tries) => {
|
|
37
|
+
for (let i = 0;;) {
|
|
38
|
+
try {
|
|
39
|
+
mkdirSync(dirname(dir), { recursive: true }); // 新しいプロジェクトでは、logs/apex がまだない
|
|
40
|
+
mkdirSync(dir); // 作れたら自分のもの(すでにあれば失敗する)
|
|
41
|
+
try {
|
|
42
|
+
const tmp = join(dir, `pid.tmp.${process.pid}`);
|
|
43
|
+
writeFileSync(tmp, `${process.pid} ${RUNTIME_SELF} ${Math.floor(Date.now() / 1000)}\n`);
|
|
44
|
+
renameSync(tmp, join(dir, "pid"));
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
rmSync(dir, { recursive: true, force: true }); // pid のないロックを残さない(残ると、以後ずっと取れない)
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
if (e.code !== "EEXIST")
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
let line = "";
|
|
57
|
+
try {
|
|
58
|
+
line = readFileSync(join(dir, "pid"), "utf8");
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
line = "";
|
|
62
|
+
}
|
|
63
|
+
if (lockIsStale(line, Math.floor(Date.now() / 1000))) {
|
|
64
|
+
rmSync(dir, { recursive: true, force: true });
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
i++;
|
|
68
|
+
if (i >= tries)
|
|
69
|
+
return false;
|
|
70
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
drop: (dir) => rmSync(dir, { recursive: true, force: true }),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function realLogPullDeps(box) {
|
|
77
|
+
return {
|
|
78
|
+
...realLogSummarizeDeps(box),
|
|
79
|
+
lock: folderLock(),
|
|
80
|
+
makeTempDir: () => mkdtempSync(join(tmpdir(), "sft-log-")),
|
|
81
|
+
removeDir: (path) => rmSync(path, { recursive: true, force: true }),
|
|
82
|
+
pidAlive,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** 取得役(同じプロセスの中で動く)の部品 */
|
|
86
|
+
export function realCollectorDeps(box) {
|
|
87
|
+
return { ...realLogPullDeps(box), pid: process.pid, pidAlive, upgradeRunning: () => upgradeRunning(box) };
|
|
88
|
+
}
|
|
89
|
+
/** 自動の取り始め(sft の起動)の部品。待つあいだに中断されたら、すぐ戻る */
|
|
90
|
+
export function realSessionDeps(box) {
|
|
91
|
+
return {
|
|
92
|
+
...realCollectorDeps(box),
|
|
93
|
+
interactive: process.stdin.isTTY === true,
|
|
94
|
+
wait: (ms, signal) => new Promise((resolve) => {
|
|
95
|
+
if (signal.aborted)
|
|
96
|
+
return resolve();
|
|
97
|
+
const t = setTimeout(done, ms);
|
|
98
|
+
function done() {
|
|
99
|
+
clearTimeout(t);
|
|
100
|
+
signal.removeEventListener("abort", done);
|
|
101
|
+
resolve();
|
|
102
|
+
}
|
|
103
|
+
signal.addEventListener("abort", done);
|
|
104
|
+
}),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// sft metasync の外の部品の本物(一時フォルダ・lib/metasync.js と config/ の場所・今日の日付)。テストは、偽物を渡す(src/core/metasync.ts)
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { askYesNo } from "../ui/confirm.js";
|
|
7
|
+
export function realMetasyncDeps(box) {
|
|
8
|
+
const here = (rel) => fileURLToPath(new URL(rel, import.meta.url));
|
|
9
|
+
return {
|
|
10
|
+
makeTempDir: () => mkdtempSync(join(process.env.TMPDIR || tmpdir(), "sf-metasync-")),
|
|
11
|
+
removeDir: (path) => rmSync(path, { recursive: true, force: true }),
|
|
12
|
+
confirm: (q) => askYesNo(box.input, q),
|
|
13
|
+
metasyncJs: box.env("SF_METASYNC_JS") || here("../../lib/metasync.js"),
|
|
14
|
+
configDir: box.env("SF_METASYNC_CONFIG_DIR") || here("../../config"),
|
|
15
|
+
today: () => {
|
|
16
|
+
const d = box.clock.now();
|
|
17
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
18
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// ブラウザで URL を開く(シェル版の open_browser の移し先)。開けなくても、呼び出し側が URL を表示するため、失敗は黙って返す。
|
|
2
|
+
// URL は、sf-tools が作った https:// の形だけを渡す(利用者の入力を、そのまま渡さない)
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
export async function openBrowser(url) {
|
|
5
|
+
// 子プロセスは shell を通さず、引数の配列で起動する(Windows の rundll32 も)ので、& を含む URL(PAT の作成画面など)も渡せる。空白・制御文字・引用符は断る
|
|
6
|
+
if (!/^https:\/\/[^\s"<>]+$/.test(url) || [...url].some((c) => c.charCodeAt(0) < 32))
|
|
7
|
+
return;
|
|
8
|
+
const [command, args] = process.platform === "darwin"
|
|
9
|
+
? ["open", [url]]
|
|
10
|
+
: process.platform === "win32"
|
|
11
|
+
? ["rundll32", ["url.dll,FileProtocolHandler", url]]
|
|
12
|
+
: ["xdg-open", [url]];
|
|
13
|
+
await new Promise((resolve) => {
|
|
14
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
15
|
+
child.on("error", () => resolve());
|
|
16
|
+
child.on("spawn", () => {
|
|
17
|
+
child.unref();
|
|
18
|
+
resolve();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// sft の版を、package.json から読む。
|
|
2
|
+
// src/io/ と dist/io/ は、どちらも package.json の 2 つ下にあるので、開発中も公開物も同じ指定で読める
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
export function readVersion() {
|
|
5
|
+
const url = new URL("../../package.json", import.meta.url);
|
|
6
|
+
const pkg = JSON.parse(readFileSync(url, "utf8"));
|
|
7
|
+
if (typeof pkg.version !== "string")
|
|
8
|
+
throw new Error("package.json に版がありません");
|
|
9
|
+
return pkg.version;
|
|
10
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// 今いる場所の Git の作業ツリーの中で、sf-tools の版を固定したプロジェクトを探して、事実を集める(doc/rebuild-design.md 4.2)。
|
|
2
|
+
// 版を固定したプロジェクト = sf-tools/package.json の依存の欄に @tama-create/sf-tools がある。
|
|
3
|
+
// 探すのは、今いる場所から、作業ツリーの一番上まで。それより上は見ない(上の階層に置かれたファイルを、確認なしで実行しないため)。
|
|
4
|
+
// 一番上は git に聞く(.git という名前があるだけでは信じない。ほかの利用者が持つリポジトリは、git の safe.directory の確認で
|
|
5
|
+
// 失敗する)。git の中でなければ固定なし。git に聞けない・ほかの理由で失敗するときは、例外にして止める。
|
|
6
|
+
// 上のどこにも固定の指定がなければ、git は呼ばない(上の階層のファイルは、指定があるかを見るだけで、使わない)
|
|
7
|
+
// 今のシェル版の force-* は sf-tools/ に package.json がないので、固定なしになる
|
|
8
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
9
|
+
import { dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
10
|
+
import { PACKAGE_NAME } from "../core/delegate.js";
|
|
11
|
+
import { run } from "./run.js";
|
|
12
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
/** ファイルがなければ null。あるのに JSON のオブジェクトでないときは例外(黙って自分の版で動かさない) */
|
|
14
|
+
function readJson(path) {
|
|
15
|
+
if (!existsSync(path))
|
|
16
|
+
return null;
|
|
17
|
+
let value;
|
|
18
|
+
try {
|
|
19
|
+
value = JSON.parse(readFileSync(path, "utf8"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Error(`JSON として読めません: ${path}`);
|
|
23
|
+
}
|
|
24
|
+
if (!isObject(value))
|
|
25
|
+
throw new Error(`JSON の形が違います: ${path}`);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
const sections = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
|
|
29
|
+
function declaredIn(manifest) {
|
|
30
|
+
return sections.flatMap((section) => {
|
|
31
|
+
const list = manifest?.[section];
|
|
32
|
+
return isObject(list) && Object.hasOwn(list, PACKAGE_NAME) ? [{ section, value: list[PACKAGE_NAME] }] : [];
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function lockEntry(lock) {
|
|
36
|
+
const packages = lock?.packages;
|
|
37
|
+
const found = isObject(packages) ? packages[`node_modules/${PACKAGE_NAME}`] : undefined;
|
|
38
|
+
return isObject(found) ? found : null;
|
|
39
|
+
}
|
|
40
|
+
function factsIn(folder, declared) {
|
|
41
|
+
const locked = lockEntry(readJson(join(folder, "package-lock.json")));
|
|
42
|
+
const installedFolder = join(folder, "node_modules", ...PACKAGE_NAME.split("/"));
|
|
43
|
+
const installed = readJson(join(installedFolder, "package.json"));
|
|
44
|
+
const completed = lockEntry(readJson(join(folder, "node_modules", ".package-lock.json")))?.version;
|
|
45
|
+
const entry = join(installedFolder, "bin", "sft.js");
|
|
46
|
+
return {
|
|
47
|
+
folder,
|
|
48
|
+
declared,
|
|
49
|
+
lock: locked && { version: locked.version, resolved: locked.resolved, integrity: locked.integrity },
|
|
50
|
+
installed: installed && { name: installed.name, version: installed.version },
|
|
51
|
+
completed: typeof completed === "string" ? completed : null,
|
|
52
|
+
entry: existsSync(entry) ? realpathSync.native(entry) : null,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** 上のどこかに、固定の指定がありそうな sf-tools/package.json があるか(読めないものも、あるとみなす) */
|
|
56
|
+
function mayBePinnedAbove(start) {
|
|
57
|
+
for (let current = start;; current = dirname(current)) {
|
|
58
|
+
try {
|
|
59
|
+
if (declaredIn(readJson(join(current, "sf-tools", "package.json"))).length > 0)
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
if (dirname(current) === current)
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** git が考える、作業ツリーの一番上。git の作業ツリーの中でなければ null */
|
|
70
|
+
async function workTreeTop(start, runGit) {
|
|
71
|
+
const result = await runGit("git", ["rev-parse", "--show-toplevel"], {
|
|
72
|
+
cwd: start,
|
|
73
|
+
timeoutMs: 30_000,
|
|
74
|
+
// 呼び出し元(git のフックなど)の指定に引きずられないように消す。失敗の理由を読めるように、英語の文にする
|
|
75
|
+
// (GIT_CEILING_DIRECTORIES が残ると、固定のあるプロジェクトが「Git の外」に見えて、固定が黙って無視される)
|
|
76
|
+
env: { GIT_DIR: undefined, GIT_WORK_TREE: undefined, GIT_COMMON_DIR: undefined, GIT_CEILING_DIRECTORIES: undefined, LC_ALL: "C" },
|
|
77
|
+
});
|
|
78
|
+
if (result.code === 0) {
|
|
79
|
+
const top = result.stdout.replace(/\r?\n$/, "");
|
|
80
|
+
return top === "" ? null : realpathSync.native(top);
|
|
81
|
+
}
|
|
82
|
+
if (/not a git repository \(or any/i.test(result.stderr))
|
|
83
|
+
return null;
|
|
84
|
+
throw new Error(`git で作業ツリーの一番上を確かめられません: ${result.stderr.trim() || `終了コード ${result.code}`}`);
|
|
85
|
+
}
|
|
86
|
+
const sameFolder = (a, b) => (process.platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b);
|
|
87
|
+
/** folder が、place と同じか、その上のフォルダか */
|
|
88
|
+
function isSameOrAbove(folder, place) {
|
|
89
|
+
const way = relative(folder, place);
|
|
90
|
+
return way !== ".." && !way.startsWith(`..${sep}`) && !isAbsolute(way);
|
|
91
|
+
}
|
|
92
|
+
export async function findPinnedProject(start, runGit = run) {
|
|
93
|
+
// ジャンクションやシンボリックリンクを通った場所は、実際の場所にしてから比べる
|
|
94
|
+
const real = realpathSync.native(start);
|
|
95
|
+
if (!mayBePinnedAbove(real))
|
|
96
|
+
return null;
|
|
97
|
+
const top = await workTreeTop(real, runGit);
|
|
98
|
+
if (top === null)
|
|
99
|
+
return null;
|
|
100
|
+
// core.worktree が別の場所を指すと、一番上に行き当たらず、ドライブの一番上まで探してしまう
|
|
101
|
+
if (!isSameOrAbove(top, real))
|
|
102
|
+
throw new Error(`git が教える作業ツリーの一番上が、今の場所の上にありません: ${top}`);
|
|
103
|
+
for (let current = real;; current = dirname(current)) {
|
|
104
|
+
const folder = join(current, "sf-tools");
|
|
105
|
+
const declared = declaredIn(readJson(join(folder, "package.json")));
|
|
106
|
+
if (declared.length > 0)
|
|
107
|
+
return factsIn(folder, declared);
|
|
108
|
+
if (sameFolder(current, top) || dirname(current) === current)
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// promote-check の外の部品の本物(一時フォルダ・マニフェストの作成)。テストは、偽物を渡す(src/core/promote-check.ts)
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { buildManifest } from "../core/manifest-build.js";
|
|
6
|
+
import { pairsAll } from "../core/promote.js";
|
|
7
|
+
export function realPromoteDeps(box) {
|
|
8
|
+
let count = 0;
|
|
9
|
+
let base = null;
|
|
10
|
+
return {
|
|
11
|
+
makeTempDir: () => mkdtempSync(join(tmpdir(), "sf-promote-check-")),
|
|
12
|
+
removeDir: (path) => {
|
|
13
|
+
rmSync(path, { recursive: true, force: true });
|
|
14
|
+
if (base)
|
|
15
|
+
rmSync(base, { recursive: true, force: true });
|
|
16
|
+
},
|
|
17
|
+
buildPairs: async (dir, branch) => {
|
|
18
|
+
base ??= mkdtempSync(join(tmpdir(), "sf-promote-manifest-"));
|
|
19
|
+
const out = join(base, `m-${count++}`);
|
|
20
|
+
const built = await buildManifest(box, dir, `sf-tools/release/${branch}`, out);
|
|
21
|
+
if (built.kind !== "ok")
|
|
22
|
+
return null;
|
|
23
|
+
const pairs = pairsAll(built.packageXml + (built.destructiveXml ?? ""));
|
|
24
|
+
return pairs.length > 0 ? pairs : null;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// pre-push フックの標準入力(git が渡す、push する参照の一覧)を読む。端末から手で動かしたときは、読まない(待たない)。
|
|
2
|
+
// 1 行ごとに最大 2 秒待つ(フックの先で止まらないため)
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
export async function readPushRefs() {
|
|
5
|
+
if (process.stdin.isTTY)
|
|
6
|
+
return "";
|
|
7
|
+
const rl = createInterface({ input: process.stdin, crlfDelay: Number.POSITIVE_INFINITY });
|
|
8
|
+
const lines = [];
|
|
9
|
+
const iterator = rl[Symbol.asyncIterator]();
|
|
10
|
+
for (;;) {
|
|
11
|
+
let timer;
|
|
12
|
+
const next = await Promise.race([iterator.next(), new Promise((resolve) => (timer = setTimeout(() => resolve(null), 2000)))]);
|
|
13
|
+
clearTimeout(timer); // 残すと、終わりが最大 2 秒遅れる
|
|
14
|
+
if (next === null || next.done)
|
|
15
|
+
break;
|
|
16
|
+
lines.push(next.value);
|
|
17
|
+
}
|
|
18
|
+
rl.close();
|
|
19
|
+
return lines.length === 0 ? "" : `${lines.join("\n")}\n`;
|
|
20
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// コマンド名から、実際に起動するものを決める(doc/rebuild-design.md 7.1)。
|
|
2
|
+
// - Windows 以外: 名前のまま起動する(PATH は OS が探す)
|
|
3
|
+
// - Windows: PATH の絶対パスのフォルダだけを自分で探し、完全なパスで起動する
|
|
4
|
+
// (名前のまま起動すると、今いるフォルダの同じ名前の実行ファイルが先に動くおそれがある)
|
|
5
|
+
// .exe・.com → そのまま起動する
|
|
6
|
+
// npm の起動ファイル → 中の JavaScript を、今の Node で直接起動する(cmd.exe を通さない)
|
|
7
|
+
// npm.cmd → npm.cmd と同じ順で npm-cli.js を探す
|
|
8
|
+
// そのほかの .cmd → 許されたときだけ cmd.exe を通す
|
|
9
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
10
|
+
import { basename, delimiter, dirname, extname, isAbsolute, join, resolve } from "node:path";
|
|
11
|
+
import { parseNodeShim } from "../core/windows-shim.js";
|
|
12
|
+
/** Windows の環境変数は、名前の大文字・小文字を区別しない */
|
|
13
|
+
export function envValue(env, name) {
|
|
14
|
+
const key = Object.keys(env).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
15
|
+
return key === undefined ? undefined : env[key];
|
|
16
|
+
}
|
|
17
|
+
const isFile = (path) => existsSync(path) && statSync(path).isFile();
|
|
18
|
+
function findOnWindows(command, context) {
|
|
19
|
+
const known = [".exe", ".com", ".cmd", ".bat"];
|
|
20
|
+
const extensions = known.includes(extname(command).toLowerCase()) ? [""] : known;
|
|
21
|
+
const hasFolder = /[\\/]/.test(command);
|
|
22
|
+
const folders = hasFolder
|
|
23
|
+
? [""]
|
|
24
|
+
: (envValue(context.env, "PATH") ?? "")
|
|
25
|
+
.split(delimiter)
|
|
26
|
+
.map((f) => f.replace(/^"(.*)"$/, "$1"))
|
|
27
|
+
.filter((f) => isAbsolute(f));
|
|
28
|
+
for (const folder of folders) {
|
|
29
|
+
for (const extension of extensions) {
|
|
30
|
+
const base = hasFolder ? resolve(context.cwd, command) : join(folder, command);
|
|
31
|
+
if (isFile(base + extension))
|
|
32
|
+
return base + extension;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
async function npmCli(folder, context) {
|
|
38
|
+
const own = join(folder, "node_modules", "npm", "bin");
|
|
39
|
+
const prefixScript = join(own, "npm-prefix.js");
|
|
40
|
+
if (isFile(prefixScript)) {
|
|
41
|
+
const prefix = (await context.nodeOutput(prefixScript)).trim();
|
|
42
|
+
const preferred = join(prefix, "node_modules", "npm", "bin", "npm-cli.js");
|
|
43
|
+
if (prefix !== "" && isFile(preferred))
|
|
44
|
+
return preferred;
|
|
45
|
+
}
|
|
46
|
+
const fallback = join(own, "npm-cli.js");
|
|
47
|
+
return isFile(fallback) ? fallback : null;
|
|
48
|
+
}
|
|
49
|
+
export async function resolveCommand(command, context) {
|
|
50
|
+
if (context.platform !== "win32")
|
|
51
|
+
return { file: command, prefixArgs: [], viaCmd: false };
|
|
52
|
+
const found = findOnWindows(command, context);
|
|
53
|
+
if (!found)
|
|
54
|
+
throw new Error(`コマンドが見つかりません: ${command}`);
|
|
55
|
+
const extension = extname(found).toLowerCase();
|
|
56
|
+
if (extension === ".exe" || extension === ".com")
|
|
57
|
+
return { file: found, prefixArgs: [], viaCmd: false };
|
|
58
|
+
const folder = dirname(found);
|
|
59
|
+
if (basename(found).toLowerCase() === "npm.cmd") {
|
|
60
|
+
const cli = await npmCli(folder, context);
|
|
61
|
+
if (cli)
|
|
62
|
+
return { file: process.execPath, prefixArgs: [cli], viaCmd: false };
|
|
63
|
+
}
|
|
64
|
+
const shim = parseNodeShim(readFileSync(found, "utf8"));
|
|
65
|
+
if (shim) {
|
|
66
|
+
const script = join(folder, ...shim.script.split("\\"));
|
|
67
|
+
if (!isFile(script))
|
|
68
|
+
throw new Error(`起動ファイルの中の JavaScript が見つかりません: ${script}`);
|
|
69
|
+
return { file: process.execPath, prefixArgs: [...shim.nodeFlags, script], viaCmd: false };
|
|
70
|
+
}
|
|
71
|
+
if (!context.allowCmdShell)
|
|
72
|
+
throw new Error(`cmd.exe を通さないと起動できないファイルです: ${found}`);
|
|
73
|
+
return { file: found, prefixArgs: [], viaCmd: true };
|
|
74
|
+
}
|