dsh-git-push 3.0.0 → 3.1.0
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/README.md +76 -48
- package/cli.mjs +4 -3
- package/lib/app/apply.js +11 -9
- package/lib/app/command-registry.js +4 -2
- package/lib/app/handlers/http-constants.js +8 -0
- package/lib/app/handlers/preprocess.js +3 -6
- package/lib/app/handlers/repo-actions.js +0 -1
- package/lib/app/handlers/repos.js +137 -10
- package/lib/app/handlers/settings.js +34 -15
- package/lib/app/index.js +1 -1
- package/lib/app/inject-text.js +3 -18
- package/lib/app/schema.js +20 -10
- package/lib/app/settings-bridge.js +60 -8
- package/lib/app/tool-call.js +26 -22
- package/lib/cli/commands-vcs.mjs +35 -1
- package/lib/cli/shared.mjs +1 -1
- package/lib/client/index.js +11 -16
- package/lib/client-parts/git/account.js +21 -12
- package/lib/client-parts/git/actions.js +23 -77
- package/lib/client-parts/git/apply.js +8 -2
- package/lib/client-parts/git/browser.js +1 -1
- package/lib/client-parts/git/const.js +33 -1
- package/lib/client-parts/git/controller.js +175 -176
- package/lib/client-parts/git/icons.js +1 -1
- package/lib/client-parts/git/loader.js +1 -1
- package/lib/client-parts/git/npm.js +56 -37
- package/lib/client-parts/git/page.js +53 -1
- package/lib/client-parts/git/repo.js +8 -1
- package/lib/client-parts/git/settings.js +13 -1
- package/lib/client-parts/git/state.js +237 -0
- package/lib/client-parts/git/utils.js +1 -1
- package/lib/client.js +631 -312
- package/lib/commit-push/pre-commit-engine.js +584 -32
- package/lib/commit-push.js +63 -47
- package/lib/context/index.js +7 -5
- package/lib/core/config.js +1 -27
- package/lib/core/exec.js +2 -11
- package/lib/git/identity-rewrite.js +1 -1
- package/lib/git/index.js +1 -1
- package/lib/git/push.js +2 -17
- package/lib/git/repo-index.js +8 -3
- package/lib/npm/install.js +1 -1
- package/lib/npm/registry.js +2 -2
- package/lib/plugin/auto-push.js +3 -6
- package/lib/preprocess/providers.js +5 -1
- package/lib/self/index.js +1 -1
- package/package.json +4 -7
- package/skills/dev/dsh-git-push.md +9 -9
- package/skills/guide/guide-commit-push.md +11 -13
- package/skills/workflow/README.md +0 -1
- package/skills/workflow/task-completion-report.md +2 -2
- package/.auditignore +0 -30
- package/.gitignore +0 -43
- package/lib/BUGFIX-NOTES-2026-09-14.md +0 -278
- package/lib/app/schema-preprocess.js +0 -42
- package/lib/user-requirements.json +0 -15
- package/scripts/build-client.mjs +0 -133
- package/scripts/bump-version.mjs +0 -178
- package/scripts/check.mjs +0 -205
- package/scripts/doc-func.mjs +0 -212
- package/scripts/doc-tree.mjs +0 -458
- package/scripts/doc-version.mjs +0 -149
- package/scripts/local-registry.mjs +0 -38
- package/scripts/scan-version.mjs +0 -125
- package/scripts/version-source.mjs +0 -173
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// dsh-skip-func-length: NPM 子页为自包含组件(自有 useState/useEffect,不依赖 controller),拆分会改动 hook 顺序与状态归属,风险高于收益
|
|
2
|
-
/* ─── 第
|
|
2
|
+
/* ─── 第 9/14 片 · NPM 面板(账号卡 + 装包表单 + 已装列表 + npm 凭据字段) ───────────────────
|
|
3
3
|
* 设计取舍:完全自包含(自己 useState/useEffect + 直接走 HTTP),不碰 40-controller(884 行)与 41-actions,
|
|
4
4
|
* 把 Step 4 的改动面压到最小;数据源:/npm-account、/npm-packages、/npm-install、/settings-set、/status。
|
|
5
5
|
* 安全默认与后端一致:`ignore-scripts` 默认勾选(不执行 postinstall)、默认非全局、**安装需二次确认**。
|
|
@@ -9,20 +9,27 @@
|
|
|
9
9
|
/** npm 账号卡(与 GitHub 账号卡**同款**:渐变面板 + 图标块 + 状态徽标 + 勾选明细块 + 底部操作)。 */
|
|
10
10
|
function dshgp_NpmAccountCard(props) {
|
|
11
11
|
const a = props.acct || {};
|
|
12
|
-
|
|
12
|
+
// 「还没读 / 读失败」与「读到了、确实没配」必须分开:
|
|
13
|
+
// 旧实现用 configured=false 覆盖了前两种情况,于是从未加载也会断言「未配置 npm token」(假话)。
|
|
14
|
+
const info = props.info || { state: 'idle', error: '' };
|
|
15
|
+
const unread = info.state === 'idle' || info.state === 'loading';
|
|
16
|
+
const errored = info.state === 'error';
|
|
17
|
+
const busy = !!props.busy || unread;
|
|
13
18
|
const okState = !!(a.configured && a.loggedIn);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const statTxt = okState ? '已连接'
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const blockBody =
|
|
19
|
+
const statCls = okState ? 'dshgp_acctstat_ok'
|
|
20
|
+
: (unread || errored || a.configured ? 'dshgp_acctstat_warn' : 'dshgp_acctstat_err');
|
|
21
|
+
const statTxt = okState ? '已连接'
|
|
22
|
+
: (unread ? '读取中' : (errored ? '读取失败' : (a.configured ? '校验未过' : '未配置')));
|
|
23
|
+
const blockCls = okState ? 'dshgp_acctblock2_ok' : (unread || errored ? 'dshgp_acctblock2_loading' : 'dshgp_acctblock2_err');
|
|
24
|
+
const blockBody = unread
|
|
20
25
|
? '正在读取 npm 登录态…'
|
|
21
|
-
: (
|
|
22
|
-
?
|
|
23
|
-
: (
|
|
24
|
-
?
|
|
25
|
-
:
|
|
26
|
+
: (errored
|
|
27
|
+
? 'npm 登录态读取失败(可点下方「刷新」重试;上方提示条有失败原因)'
|
|
28
|
+
: (okState
|
|
29
|
+
? `✅ 凭据可用\n✅ npm token:${a.user}|registry ${a.registry || ''}\n✅ 登录态:npm whoami 通过${a.maskedToken ? `(${a.maskedToken})` : ''}`
|
|
30
|
+
: (a.configured
|
|
31
|
+
? `⚠️ npm token 已配置但校验未通过\n⚠️ registry:${a.registry || ''}\n⚠️ 原因:${a.error || '未知'}`
|
|
32
|
+
: `⚠️ 未配置 npm token\n⚠️ registry:${a.registry || ''}\n⚠️ 到「设置 → 凭据与密钥」填写 npm token 后可显示登录态`)));
|
|
26
33
|
return jsx.jsxs('div', {
|
|
27
34
|
className: 'dshgp_acctpanel',
|
|
28
35
|
children: [
|
|
@@ -43,6 +50,11 @@
|
|
|
43
50
|
jsx.jsxs('span', { className: 'dshgp_acctstat ' + statCls, children: [jsx.jsx('span', { className: 'dshgp_acctdot' }), statTxt] }),
|
|
44
51
|
],
|
|
45
52
|
}),
|
|
53
|
+
jsx.jsx(dshgp_ResourceNotice, {
|
|
54
|
+
info: info,
|
|
55
|
+
what: 'npm 账号状态',
|
|
56
|
+
onRetry: errored && props.onRetry ? props.onRetry : null,
|
|
57
|
+
}),
|
|
46
58
|
jsx.jsx('pre', { className: 'dshgp_acctblock2 ' + blockCls, children: blockBody }),
|
|
47
59
|
jsx.jsxs('div', {
|
|
48
60
|
className: 'dshgp_acctfoot',
|
|
@@ -127,8 +139,10 @@
|
|
|
127
139
|
/** NPM 子页:登录账号名下的包列表(与 GitHub 云端仓库列表**同款行**)+ 手动安装框。 */
|
|
128
140
|
function dshgp_NpmPanel(props) {
|
|
129
141
|
const s = (props && props.state) || {};
|
|
130
|
-
|
|
131
|
-
|
|
142
|
+
// 数据(账号态 / 已装依赖)一律取自状态工厂(R1:组件不留显示副本);
|
|
143
|
+
// 这里只保留**纯表单交互状态**(输入框内容、勾选、忙碌标记)。
|
|
144
|
+
const pkgs = s.npmPackages || null;
|
|
145
|
+
const pkgsStatus = (s.uiStatus && s.uiStatus.npmPackages) || { state: 'idle', error: '' };
|
|
132
146
|
const [dir, setDir] = react.useState('');
|
|
133
147
|
const [names, setNames] = react.useState('');
|
|
134
148
|
const [global, setGlobal] = react.useState(false);
|
|
@@ -138,24 +152,17 @@
|
|
|
138
152
|
const [result, setResult] = react.useState(null);
|
|
139
153
|
const [err, setErr] = react.useState('');
|
|
140
154
|
|
|
141
|
-
/**
|
|
155
|
+
/** 读「已装依赖」:交给状态工厂(单飞、有状态、失败不清空),组件只负责触发。 */
|
|
142
156
|
const load = react.useCallback(async (d) => {
|
|
143
157
|
setBusy(true);
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
} catch (e) {
|
|
152
|
-
setErr(String((e && e.message) || e));
|
|
153
|
-
} finally {
|
|
154
|
-
setBusy(false);
|
|
155
|
-
}
|
|
158
|
+
if (props.reloadNpmPackages) props.reloadNpmPackages(d || '');
|
|
159
|
+
setBusy(false);
|
|
160
|
+
}, [props]);
|
|
161
|
+
// 挂载时按需补读(已读过则不重复请求 —— ensureFresh 的语义就在这里)
|
|
162
|
+
react.useEffect(() => {
|
|
163
|
+
if (props.panelRebuilt) props.panelRebuilt();
|
|
164
|
+
if (props.reloadNpmPackages) props.reloadNpmPackages('');
|
|
156
165
|
}, []);
|
|
157
|
-
// 只在挂载时读一次(与既有 tab 的读取时机约定一致:不在每次切 tab 时重复触发)
|
|
158
|
-
react.useEffect(() => { load(''); }, [load]);
|
|
159
166
|
|
|
160
167
|
const doInstall = async () => {
|
|
161
168
|
setBusy(true);
|
|
@@ -175,6 +182,11 @@
|
|
|
175
182
|
}
|
|
176
183
|
};
|
|
177
184
|
|
|
185
|
+
// 服务端返回的目录回填到输入框(数据来自工厂,避免组件自己记一份)
|
|
186
|
+
react.useEffect(() => {
|
|
187
|
+
if (pkgs && pkgs.dir && !dir) setDir(pkgs.dir);
|
|
188
|
+
}, [pkgs]);
|
|
189
|
+
|
|
178
190
|
const st = {
|
|
179
191
|
names, setNames, dir, setDir, global, setGlobal, ignoreScripts, setIgnoreScripts,
|
|
180
192
|
confirming, setConfirming, busy, result, doInstall,
|
|
@@ -183,10 +195,8 @@
|
|
|
183
195
|
return jsx.jsxs('div', {
|
|
184
196
|
className: 'dshgp_section',
|
|
185
197
|
children: [
|
|
186
|
-
//
|
|
187
|
-
//
|
|
188
|
-
// 改为照 GitHub 的四段交互:行内「安装」→ 选择目录 / 全局安装 → 进度轮询 → 回执。
|
|
189
|
-
// 组件 dshgp_NpmInstallForm 的源码暂留备查,不再渲染(comment-not-delete:只停用不删代码)。
|
|
198
|
+
// 账号卡在顶部账号区(与 GitHub 卡同位置、滑动切换),这里只列已装依赖;
|
|
199
|
+
// 安装交互走 GitHub 同款四段:行内「安装」→ 选目录/全局 → 进度轮询 → 回执。
|
|
190
200
|
jsx.jsxs('div', {
|
|
191
201
|
className: 'dshgp_block',
|
|
192
202
|
children: [
|
|
@@ -194,7 +204,10 @@
|
|
|
194
204
|
pkgs && pkgs.hasPackageJson === false
|
|
195
205
|
? jsx.jsx('p', { className: 'dshgp_hint', children: '该目录没有 package.json —— 安装会在目录下新建依赖树(建议先初始化)。' })
|
|
196
206
|
: null,
|
|
197
|
-
jsx.jsx(
|
|
207
|
+
jsx.jsx(dshgp_ResourceNotice, { info: pkgsStatus, what: '已装依赖' }),
|
|
208
|
+
jsx.jsx('p', { className: 'dshgp_hint', children: pkgs
|
|
209
|
+
? 'node_modules:' + (pkgs.nodeModules ? '已存在' : '不存在') + ' · 依赖 ' + deps.length + ' 个'
|
|
210
|
+
: '依赖清单读取中…' }),
|
|
198
211
|
deps.length
|
|
199
212
|
? jsx.jsx('div', { className: 'dshgp_field', children: deps.map((d) => jsx.jsx('div', { key: d.name, className: 'dshgp_hint', children: d.name + ' ' + d.range })) })
|
|
200
213
|
: jsx.jsx('p', { className: 'dshgp_hint', children: '(无已装依赖)' }),
|
|
@@ -206,7 +219,8 @@
|
|
|
206
219
|
jsx.jsxs('div', {
|
|
207
220
|
style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px', marginBottom: '8px' },
|
|
208
221
|
children: [
|
|
209
|
-
jsx.jsx('p', { className: 'dshgp_h2', style: { margin: 0 }, children: '我的包(' + (s.npmAccountUser
|
|
222
|
+
jsx.jsx('p', { className: 'dshgp_h2', style: { margin: 0 }, children: '我的包(' + (s.npmAccountUser
|
|
223
|
+
|| ((s.uiStatus && s.uiStatus.npm && ['idle', 'loading'].includes(s.uiStatus.npm.state)) ? '读取中…' : '未登录')) + ')' }),
|
|
210
224
|
jsx.jsx('button', { type: 'button', className: 'dshgp_keybtn', disabled: !!s.npmOwnedLoading, onClick: () => props.refreshNpmOwned(), children: s.npmOwnedLoading ? '读取中…' : '刷新列表' }),
|
|
211
225
|
],
|
|
212
226
|
}),
|
|
@@ -305,6 +319,7 @@
|
|
|
305
319
|
block,
|
|
306
320
|
error: '',
|
|
307
321
|
};
|
|
322
|
+
const npmInfo = (s.uiStatus && s.uiStatus.npm) || { state: 'idle', error: '' };
|
|
308
323
|
return jsx.jsxs('div', {
|
|
309
324
|
children: [
|
|
310
325
|
jsx.jsx('div', {
|
|
@@ -323,7 +338,11 @@
|
|
|
323
338
|
children: [
|
|
324
339
|
which === 'github'
|
|
325
340
|
? jsx.jsx(dshgp_AccountCard, props)
|
|
326
|
-
: jsx.jsx(dshgp_NpmAccountCard, {
|
|
341
|
+
: jsx.jsx(dshgp_NpmAccountCard, {
|
|
342
|
+
acct: npmAcct, busy: !!s.npmAccountLoading, info: npmInfo,
|
|
343
|
+
onRefresh: props.recheckNpmAccount,
|
|
344
|
+
onRetry: () => { if (props.retryResource) props.retryResource('npm'); },
|
|
345
|
+
}),
|
|
327
346
|
],
|
|
328
347
|
}),
|
|
329
348
|
],
|
|
@@ -1,4 +1,56 @@
|
|
|
1
|
-
/* ─── 第
|
|
1
|
+
/* ─── 第 11/14 片 · 页面装配(GitPushPage:两选项卡聚合;审计 Tab 已移出构建) ─── */
|
|
2
|
+
/**
|
|
3
|
+
* 资源状态提示条(§四:失败/过期如实说,且**不清空**已有内容)。
|
|
4
|
+
*
|
|
5
|
+
* 关键点:只有"真的读到了确定结论"才允许说否定结论(未配置/未连接)。
|
|
6
|
+
* idle/loading ⇒ 「读取中…」;error ⇒ 「读取失败(可重试)」;
|
|
7
|
+
* stale ⇒ 「显示的是上次结果,正在同步」。
|
|
8
|
+
* @param {{info: object, onRetry?: Function, what?: string}} props info = uiStatus(name)
|
|
9
|
+
* @returns {object|null} 无提示时返回 null(内容照常显示)
|
|
10
|
+
*/
|
|
11
|
+
function dshgp_ResourceNotice(props) {
|
|
12
|
+
const info = props.info || {};
|
|
13
|
+
const what = props.what || '数据';
|
|
14
|
+
if (info.state === 'idle' || info.state === 'loading') {
|
|
15
|
+
return jsx.jsx('p', { className: 'dshgp_resstatus dshgp_resstatus_stale', children: '正在读取' + what + '…' });
|
|
16
|
+
}
|
|
17
|
+
if (info.state === 'error') {
|
|
18
|
+
return jsx.jsxs('div', { className: 'dshgp_resstatus dshgp_resstatus_err', children: [
|
|
19
|
+
jsx.jsx('span', { children: '读取' + what + '失败:' + (info.error || '未知原因') }),
|
|
20
|
+
props.onRetry
|
|
21
|
+
? jsx.jsx('button', { type: 'button', className: 'dshgp_keybtn', onClick: props.onRetry, style: { marginLeft: '8px' }, children: '重试' })
|
|
22
|
+
: null,
|
|
23
|
+
] });
|
|
24
|
+
}
|
|
25
|
+
if (info.state === 'stale') {
|
|
26
|
+
return jsx.jsx('p', { className: 'dshgp_resstatus dshgp_resstatus_stale', children: '显示的是上次读取结果,正在后台同步…' });
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 首屏骨架(R2:读回早于渲染)。
|
|
33
|
+
*
|
|
34
|
+
* 为什么必须有它:读回是异步的,若首帧直接渲染"真内容",那内容里的字段全是默认值 ——
|
|
35
|
+
* 用户看到的就成了「未配置」「未连接」这类**确定性的否定结论**,而事实是"还没读"。
|
|
36
|
+
* 骨架明确表达"正在读",读完由 ready 门禁切到真内容。
|
|
37
|
+
*/
|
|
38
|
+
function dshgp_LoadingSkeleton() {
|
|
39
|
+
const block = (key) => jsx.jsxs('div', { className: 'dshgp_skelblock', children: [
|
|
40
|
+
jsx.jsx('div', { className: 'dshgp_skelline dshgp_skelline_m' }),
|
|
41
|
+
jsx.jsx('div', { className: 'dshgp_skelline' }),
|
|
42
|
+
jsx.jsx('div', { className: 'dshgp_skelline dshgp_skelline_s' }),
|
|
43
|
+
] }, key);
|
|
44
|
+
return jsx.jsxs('div', {
|
|
45
|
+
className: 'dshgp_skeleton',
|
|
46
|
+
children: [
|
|
47
|
+
jsx.jsxs('h2', { style: { margin: '0 0 10px', fontSize: '14px' }, children: ['Git 提交推送'] }),
|
|
48
|
+
jsx.jsx('p', { className: 'dshgp_hint', children: '正在读取插件状态…' }),
|
|
49
|
+
jsx.jsxs('div', { className: 'dshgp_skel', children: [block('a'), block('b')] }),
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
2
54
|
function dshgp_GitPushPage(props) {
|
|
3
55
|
dshgp_ensureCss();
|
|
4
56
|
const [tab, setTab] = react.useState('account');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* ─── 第
|
|
1
|
+
/* ─── 第 8/14 片 · 仓库管理卡(RepoManagerCard/RepoLocalPane/RepoCloudPane) ─────────────────── */
|
|
2
2
|
function dshgp_RepoManagerCard(props) {
|
|
3
3
|
const s = props.state;
|
|
4
4
|
const [view, setView] = react.useState('local');
|
|
@@ -149,6 +149,13 @@
|
|
|
149
149
|
}),
|
|
150
150
|
],
|
|
151
151
|
}),
|
|
152
|
+
// 列表读取的「状态」单独表达(读取中/失败可重试/过期同步中):
|
|
153
|
+
// 旧实现把失败文案塞进 localMsg,还顺手把列表清空 —— 于是"读失败"看起来像"你没有仓库"。
|
|
154
|
+
jsx.jsx(dshgp_ResourceNotice, {
|
|
155
|
+
info: (s.uiStatus && s.uiStatus.repos) || { state: 'idle', error: '' },
|
|
156
|
+
what: '本地仓库列表',
|
|
157
|
+
onRetry: props.retryResource ? () => props.retryResource('repos') : null,
|
|
158
|
+
}),
|
|
152
159
|
jsx.jsx('p', { className: 'dshgp_repomsg', children: s.localMsg || '扫描目录下的 git 仓库;仓库「领先远端(有未推送提交)」时可手动 push(工作树未提交改动不影响 push,推的是已提交内容)' }),
|
|
153
160
|
rows.length ? jsx.jsxs('div', { className: 'dshgp_replist', children: rows }) : null,
|
|
154
161
|
],
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* ─── 第
|
|
1
|
+
/* ─── 第 10/14 片 · 设置 Tab(SettingsTab) ─────────────────── */
|
|
2
2
|
function dshgp_CredentialBlock(props) {
|
|
3
3
|
const s = props.state;
|
|
4
4
|
return jsx.jsxs('div', {
|
|
@@ -75,6 +75,16 @@
|
|
|
75
75
|
className: 'dshgp_block',
|
|
76
76
|
children: [
|
|
77
77
|
jsx.jsx('p', { className: 'dshgp_h2', children: '推送与默认值' }),
|
|
78
|
+
// 提交前预处理总开关:本插件唯一的门禁开关。
|
|
79
|
+
// 具体审什么(扫描范围/权重/规则包)归预处理实现自己的配置,这里不放那些项。
|
|
80
|
+
jsx.jsxs('div', { className: 'dshgp_field', children: [
|
|
81
|
+
jsx.jsx('label', { className: 'dshgp_label', htmlFor: 'dshgp-precommit', children: '提交前预处理' }),
|
|
82
|
+
jsx.jsx('input', {
|
|
83
|
+
id: 'dshgp-precommit', type: 'checkbox', className: 'dshgp_check',
|
|
84
|
+
checked: !!s.preCommitEnabled, onChange: (ev) => props.setAdvanced('preCommitEnabled', ev.target.checked),
|
|
85
|
+
}),
|
|
86
|
+
jsx.jsx('p', { className: 'dshgp_hint', children: '开启后提交前调用预处理实现做检查(默认关)。检查范围、规则包与权重属于预处理实现自己的配置,在本页不做设置。' }),
|
|
87
|
+
] }),
|
|
78
88
|
jsx.jsxs('div', { className: 'dshgp_field', children: [
|
|
79
89
|
jsx.jsx('label', { className: 'dshgp_label', htmlFor: 'dshgp-pushmethod', children: '推送通道' }),
|
|
80
90
|
// 下拉标签带括号注释(选项 value 仍是 ssh/api/auto,不改值——
|
|
@@ -134,6 +144,8 @@
|
|
|
134
144
|
}
|
|
135
145
|
|
|
136
146
|
function dshgp_SettingsTab(props) {
|
|
147
|
+
// §三:与账号页同一约定——面板重建后补读没读过/已过期的资源并重放显示态。
|
|
148
|
+
react.useEffect(() => { if (props.panelRebuilt) props.panelRebuilt(); }, []);
|
|
137
149
|
const s = props.state;
|
|
138
150
|
return jsx.jsxs('div', {
|
|
139
151
|
className: 'dshgp_section',
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/* ─── 第 4/14 片 · UI 状态工厂(单一真相源 + 读回早于首帧 + 重建重放) ─────────────────── */
|
|
2
|
+
// 这一层存在的原因:数据读取此前散在各处各写一份状态(controller 字段 + 组件 useState),
|
|
3
|
+
// 于是「没读到」与「读到了但是空」在界面上无法区分(实测:npm 从未被请求,界面却显示
|
|
4
|
+
// 「未配置 npm token」);读取还都是一次性 fire-and-forget,面板重建后既不重读也不重放。
|
|
5
|
+
//
|
|
6
|
+
// 六条契约(对齐 ui-state 参考实现):
|
|
7
|
+
// R1 单一真相源:一个资源只存这里一份;控制器字段是**派生投影**,组件只读投影,禁止各留副本
|
|
8
|
+
// R2 读回早于首次渲染:loadAll() 完成前 UI 只渲染骨架(由 uiReady 门禁),不渲染"像数据的空值"
|
|
9
|
+
// R3 apply/render 分离:renderer 只把当前值刷到已挂载的显示态,幂等、可重放
|
|
10
|
+
// §三 面板重建后:markRebuilt() → 所有 renderer 按当前值重放一次
|
|
11
|
+
// §四 读失败/不支持 ⇒ **保留上次值**并标 stale/error,绝不回落默认值
|
|
12
|
+
// §五 默认值唯一来源 = SETTINGS_SCHEMA / 后端返回;本模块只派生,不另写一份
|
|
13
|
+
const dshgp_UI_DEFAULT_TIMEOUT_MS = 10_000; // 单资源读超时(列表另有预算,由后端保证)
|
|
14
|
+
const dshgp_UI_RETRY_BACKOFF_MS = 800; // 失败重试退避(一次就够:网络抖动可救,真坏不硬耗)
|
|
15
|
+
const dshgp_UI_DEFAULT_RETRY = 1;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 工厂内部上下文(不对外:调用方只拿 api 对象)。
|
|
19
|
+
* @param {Function} onChange 任一资源状态/值变化时回调(用于 publish)
|
|
20
|
+
* @returns {{onChange: Function, resources: Map, renderers: Map, inflight: Map}}
|
|
21
|
+
*/
|
|
22
|
+
function dshgp_uiCtx(onChange) {
|
|
23
|
+
return {
|
|
24
|
+
onChange: onChange || function () {},
|
|
25
|
+
resources: new Map(), // name → {load, timeoutMs, retry, staleAfterMs, value, at, state, error}
|
|
26
|
+
renderers: new Map(), // name → 幂等渲染函数
|
|
27
|
+
inflight: new Map(), // name → 进行中的 Promise(单飞)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 取资源声明;未注册立即抛(拼错的资源名必须当场可见,不能静默无数据)。 */
|
|
32
|
+
function dshgp_uiEnsure(ctx, name) {
|
|
33
|
+
const res = ctx.resources.get(String(name));
|
|
34
|
+
if (!res) throw new Error('未注册的 UI 资源:' + name);
|
|
35
|
+
return res;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 通知订阅者(订阅者异常不得打断状态机)。 */
|
|
39
|
+
function dshgp_uiEmit(ctx) {
|
|
40
|
+
try { ctx.onChange(); } catch (e) { /* 订阅者异常忽略 */ }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 某资源的状态快照。界面**必须按 state 分支渲染**,不能只看 value 真假:
|
|
45
|
+
* idle/loading ⇒ 还没读到(渲染占位,别说"未配置")
|
|
46
|
+
* ready ⇒ 有值且新鲜
|
|
47
|
+
* stale ⇒ 有值但过期(照常显示 + 标"同步中")
|
|
48
|
+
* error ⇒ 读失败;value 可能是上一次成功值(§四:不清空)
|
|
49
|
+
*/
|
|
50
|
+
function dshgp_uiStatusOf(ctx, name) {
|
|
51
|
+
const r = ctx.resources.get(String(name));
|
|
52
|
+
if (!r) return { state: 'idle', loading: false, error: '', at: 0, hasValue: false };
|
|
53
|
+
let state = r.state;
|
|
54
|
+
if (state === 'ready' && r.staleAfterMs > 0 && r.at && (Date.now() - r.at) > r.staleAfterMs) state = 'stale';
|
|
55
|
+
return { state: state, loading: state === 'loading', error: r.error, at: r.at, hasValue: r.value !== undefined };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 把某个资源的当前值刷到显示态(R3;渲染异常不影响状态位)。 */
|
|
59
|
+
function dshgp_uiRenderOne(ctx, name, payload) {
|
|
60
|
+
const fn = ctx.renderers.get(String(name));
|
|
61
|
+
if (!fn) return false;
|
|
62
|
+
try { fn(payload); return true; } catch (e) { return false; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 按当前值重放渲染(R3/§三),幂等。面板重建后反复调是安全的。
|
|
67
|
+
* @returns {{rendered: string[], skipped: string[]}}
|
|
68
|
+
*/
|
|
69
|
+
function dshgp_uiApplyAll(ctx, names) {
|
|
70
|
+
const only = Array.isArray(names) ? new Set(names.map(String)) : null;
|
|
71
|
+
const rendered = [];
|
|
72
|
+
const skipped = [];
|
|
73
|
+
for (const [name, r] of ctx.resources) {
|
|
74
|
+
if (only && !only.has(name)) continue;
|
|
75
|
+
if (r.value === undefined || !ctx.renderers.has(name)) { skipped.push(name); continue; }
|
|
76
|
+
if (dshgp_uiRenderOne(ctx, name, r.value)) rendered.push(name); else skipped.push(name);
|
|
77
|
+
}
|
|
78
|
+
return { rendered: rendered, skipped: skipped };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 带超时的单次读取(超时用 Promise.race,不依赖 fetch 自身的 signal 语义)。 */
|
|
82
|
+
function dshgp_uiReadOnce(resource, arg) {
|
|
83
|
+
return Promise.race([
|
|
84
|
+
Promise.resolve(resource.load(arg)),
|
|
85
|
+
new Promise((_, reject) => setTimeout(
|
|
86
|
+
() => reject(new Error('超时(>' + resource.timeoutMs + 'ms)')), resource.timeoutMs,
|
|
87
|
+
)),
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 读一个资源(含一次退避重试)。失败**保留旧值**,只改状态位(§四)。
|
|
93
|
+
* @returns {Promise<{ok: boolean, name: string, value?: unknown, error?: string}>}
|
|
94
|
+
*/
|
|
95
|
+
async function dshgp_uiLoadOne(ctx, name, arg, attempt) {
|
|
96
|
+
const r = dshgp_uiEnsure(ctx, name);
|
|
97
|
+
const tries = Number.isFinite(attempt) ? attempt : 0;
|
|
98
|
+
r.state = 'loading';
|
|
99
|
+
r.error = '';
|
|
100
|
+
dshgp_uiEmit(ctx);
|
|
101
|
+
try {
|
|
102
|
+
const payload = await dshgp_uiReadOnce(r, arg);
|
|
103
|
+
r.value = payload;
|
|
104
|
+
r.at = Date.now();
|
|
105
|
+
r.state = 'ready';
|
|
106
|
+
r.error = '';
|
|
107
|
+
dshgp_uiRenderOne(ctx, name, payload); // 读到就立刻刷一次显示(与重放同一条路径)
|
|
108
|
+
dshgp_uiEmit(ctx);
|
|
109
|
+
return { ok: true, name: String(name), value: payload };
|
|
110
|
+
} catch (e) {
|
|
111
|
+
const msg = String((e && e.message) || e);
|
|
112
|
+
if (tries < r.retry) {
|
|
113
|
+
await new Promise((resolve) => setTimeout(resolve, dshgp_UI_RETRY_BACKOFF_MS));
|
|
114
|
+
ctx.inflight.delete(String(name));
|
|
115
|
+
return dshgp_uiLoadOne(ctx, name, arg, tries + 1);
|
|
116
|
+
}
|
|
117
|
+
// §四:不清空 value —— 有上一次成功值就继续显示,仅状态降级
|
|
118
|
+
r.state = 'error';
|
|
119
|
+
r.error = msg;
|
|
120
|
+
dshgp_uiEmit(ctx);
|
|
121
|
+
return { ok: false, name: String(name), error: msg };
|
|
122
|
+
} finally {
|
|
123
|
+
ctx.inflight.delete(String(name));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 单飞重读:进行中则复用同一个 Promise(防切 tab/多组件同时触发造成请求风暴)。 */
|
|
128
|
+
function dshgp_uiReload(ctx, name, arg) {
|
|
129
|
+
const key = String(name);
|
|
130
|
+
if (ctx.inflight.has(key)) return ctx.inflight.get(key);
|
|
131
|
+
const p = dshgp_uiLoadOne(ctx, key, arg).finally(() => { ctx.inflight.delete(key); });
|
|
132
|
+
ctx.inflight.set(key, p);
|
|
133
|
+
return p;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 并发读多个资源(R2 的落点:调用方在首帧前等它读完,再放开渲染门禁)。
|
|
138
|
+
* @returns {Promise<{ok: boolean, failed: string[], loaded: string[]}>}
|
|
139
|
+
*/
|
|
140
|
+
async function dshgp_uiLoadAll(ctx, opts) {
|
|
141
|
+
const o = opts || {};
|
|
142
|
+
const names = Array.isArray(o.names) ? o.names.map(String) : [...ctx.resources.keys()];
|
|
143
|
+
const results = await Promise.all(names.map((name) => {
|
|
144
|
+
const r = ctx.resources.get(name);
|
|
145
|
+
if (!r) return Promise.resolve({ ok: false, name: name, error: '未注册' });
|
|
146
|
+
if (!o.force && r.state === 'ready' && r.value !== undefined) return Promise.resolve({ ok: true, name: name, cached: true });
|
|
147
|
+
return dshgp_uiReload(ctx, name);
|
|
148
|
+
}));
|
|
149
|
+
return {
|
|
150
|
+
ok: results.every((x) => x.ok),
|
|
151
|
+
failed: results.filter((x) => !x.ok).map((x) => x.name),
|
|
152
|
+
loaded: results.filter((x) => x.ok).map((x) => x.name),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 按需补读(面板挂载/切 tab 用):只读「没读过」或「已过期/上次失败」的资源。
|
|
158
|
+
* 幂等且不制造请求风暴 —— 这正是"切 tab 就重挂载"场景需要的语义。
|
|
159
|
+
*/
|
|
160
|
+
function dshgp_uiEnsureFresh(ctx, names) {
|
|
161
|
+
const list = Array.isArray(names) ? names.map(String) : [String(names)];
|
|
162
|
+
const todo = list.filter((name) => {
|
|
163
|
+
const r = ctx.resources.get(name);
|
|
164
|
+
if (!r) return false;
|
|
165
|
+
if (r.state === 'ready' && r.value !== undefined) return false; // 有新鲜值:不读
|
|
166
|
+
return !ctx.inflight.has(name);
|
|
167
|
+
});
|
|
168
|
+
if (!todo.length) return Promise.resolve({ ok: true, failed: [], loaded: [] });
|
|
169
|
+
return dshgp_uiLoadAll(ctx, { names: todo, force: true });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 是否所有指定资源都已"有值可用"(ready/stale/error 但留有旧值都算)。 */
|
|
173
|
+
function dshgp_uiAllReady(ctx, names) {
|
|
174
|
+
const list = Array.isArray(names) ? names.map(String) : [...ctx.resources.keys()];
|
|
175
|
+
return list.every((name) => {
|
|
176
|
+
const r = ctx.resources.get(name);
|
|
177
|
+
return !!r && r.value !== undefined;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 是否还有资源在首次读取中(骨架期判断)。 */
|
|
182
|
+
function dshgp_uiAnyLoading(ctx) {
|
|
183
|
+
for (const r of ctx.resources.values()) if (r.state === 'loading') return true;
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 建一个 UI 状态仓库(对外唯一入口)。
|
|
189
|
+
*
|
|
190
|
+
* 用法(装配期注册资源,运行期读):
|
|
191
|
+
* const ui = dshgp_createUiState({ onChange: () => this.publish() });
|
|
192
|
+
* ui.register('npm', { load: () => dshgp_getJson('/api/git-push/npm-account') });
|
|
193
|
+
* ui.setRenderer('npm', (payload) => this.applyNpmPayload(payload));
|
|
194
|
+
* await ui.loadAll(); // R2:读完再放开首帧
|
|
195
|
+
* ui.markRebuilt(); // §三:面板重建后重放
|
|
196
|
+
*
|
|
197
|
+
* @param {{onChange?: Function}} [options] onChange:任一资源状态/值变化时回调(用于 publish)
|
|
198
|
+
* @returns {object} 仓库实例
|
|
199
|
+
*/
|
|
200
|
+
function dshgp_createUiState(options) {
|
|
201
|
+
const ctx = dshgp_uiCtx(options && options.onChange);
|
|
202
|
+
const api = {
|
|
203
|
+
register: function (name, spec) {
|
|
204
|
+
if (typeof (spec && spec.load) !== 'function') throw new Error('资源 ' + name + ' 缺少 load()');
|
|
205
|
+
ctx.resources.set(String(name), {
|
|
206
|
+
name: String(name),
|
|
207
|
+
load: spec.load,
|
|
208
|
+
timeoutMs: Number(spec.timeoutMs) > 0 ? Number(spec.timeoutMs) : dshgp_UI_DEFAULT_TIMEOUT_MS,
|
|
209
|
+
retry: Number.isFinite(Number(spec.retry)) ? Number(spec.retry) : dshgp_UI_DEFAULT_RETRY,
|
|
210
|
+
staleAfterMs: Number(spec.staleAfterMs) > 0 ? Number(spec.staleAfterMs) : 0,
|
|
211
|
+
value: undefined,
|
|
212
|
+
at: 0,
|
|
213
|
+
state: 'idle',
|
|
214
|
+
error: '',
|
|
215
|
+
});
|
|
216
|
+
return api;
|
|
217
|
+
},
|
|
218
|
+
setRenderer: function (name, render) {
|
|
219
|
+
if (typeof render !== 'function') throw new Error('renderer 必须是函数:' + name);
|
|
220
|
+
ctx.renderers.set(String(name), render);
|
|
221
|
+
return api;
|
|
222
|
+
},
|
|
223
|
+
value: function (name) { return ctx.resources.has(String(name)) ? ctx.resources.get(String(name)).value : undefined; },
|
|
224
|
+
values: function () { const out = {}; for (const [k, r] of ctx.resources) out[k] = r.value; return out; },
|
|
225
|
+
status: function (name) { return dshgp_uiStatusOf(ctx, name); },
|
|
226
|
+
reload: function (name, arg) { return dshgp_uiReload(ctx, name, arg); },
|
|
227
|
+
loadAll: function (opts) { return dshgp_uiLoadAll(ctx, opts); },
|
|
228
|
+
ensureFresh: function (names) { return dshgp_uiEnsureFresh(ctx, names); },
|
|
229
|
+
applyAll: function (opts) { return dshgp_uiApplyAll(ctx, opts && opts.names); },
|
|
230
|
+
// §三:面板重建完成 ⇒ 按当前值重放全部显示态(不触发任何请求)
|
|
231
|
+
markRebuilt: function () { const r = dshgp_uiApplyAll(ctx, null); dshgp_uiEmit(ctx); return r; },
|
|
232
|
+
allReady: function (names) { return dshgp_uiAllReady(ctx, names); },
|
|
233
|
+
anyLoading: function () { return dshgp_uiAnyLoading(ctx); },
|
|
234
|
+
has: function (name) { return ctx.resources.has(String(name)); },
|
|
235
|
+
};
|
|
236
|
+
return api;
|
|
237
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* ─── 第 3/
|
|
1
|
+
/* ─── 第 3/14 片 · 工具区(ensureCss/getJson/postJson/tokenConfigured/copyText) ─────────────────── */
|
|
2
2
|
function dshgp_ensureCss() {
|
|
3
3
|
if (typeof document === 'undefined') return;
|
|
4
4
|
if (document.querySelector('style[data-plugin-css="dsh-git-push"]')) return;
|