dsh-aiyayi 1.5.30 → 1.5.31
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/CHANGELOG.md +24 -1
- package/lib/pwsh-check.js +252 -46
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> **维护约定**:新版本发布时在顶部加条目。本文件由 README 正文拆出(2026-11-09 重构):版本条目与踩坑记录原文照录,数字、时间、日期一律不改(量化保真是本插件的规矩)。版本事实以 npm registry 为准。
|
|
4
4
|
>
|
|
5
|
-
> **npm 发布一览(2026-11-11 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.28**(2026-11-11 发布;**初版记为 1.5.27,1.5.29 发版时据 registry 更正**)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29
|
|
5
|
+
> **npm 发布一览(2026-11-11 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.28**(2026-11-11 发布;**初版记为 1.5.27,1.5.29 发版时据 registry 更正**)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 为本次发版版**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目;其 npm 发布状态以 registry 为准)。
|
|
6
6
|
>
|
|
7
7
|
> **环境标识约定(2026-11-10 新增)**:写版本条目/坑条目时,**顺手记录当时的 智能体工具-智能体模式-模型供应商-模型**(如 **DSH-创造模式-loomy讯飞-GLM5.3FLASH**),写在条目内(紧跟 git 记录行、或坑条目首行),方便回溯该条目是在哪套智能体/模型环境下产出与验证的。
|
|
8
8
|
|
|
@@ -10,6 +10,29 @@
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
+
## 1.5.31(2026-11-11 修复:PowerShell 体检拿到 `major: null`——探测链补上 powershell.exe 一级)
|
|
14
|
+
|
|
15
|
+
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
|
16
|
+
- 症状:1.5.28 的首次对话 PowerShell 体检,落盘的 `pwsh.json` 里 `major` 恒为 `null`。提醒虽然提过一次,但底层判断是空的——`isPwshTooOld(null)` 恒为 true,碰巧说出了对的话,却拿不出"当前到底是几"。
|
|
17
|
+
- **根因(实测确认,不是 BOM、不是编码、不是字段名对不上)**:探测链只到 `pwsh` 为止。本机(`C:\Users\LH`,2026-11-11 实测)根本没装 PowerShell 7——`where pwsh` 零命中、`Get-AppxPackage '*PowerShell*'` 空、`winget list --name PowerShell` 无匹配、`C:\Program Files\PowerShell` 不存在、`%LOCALAPPDATA%\Microsoft\WindowsApps\pwsh.exe` 也不存在(全盘常见位置递归搜 `pwsh.exe` 零命中),机器上只有系统自带的 `powershell.exe` 5.1(`$PSVersionTable.PSVersion = 5.1.19041.6456`,PSEdition=Desktop)。探测链在 pwsh 全落空后就直接收工给了 `null`,**从来没退回去问 5.1 是几**。于是 `null` 这个值把"没装 pwsh"和"装了但解析失败"糊成一团,谁也分不出来。
|
|
18
|
+
- 顺带记一笔:1.5.28 头注释里写的"探 `powershell.exe` 永远不会失败,所以不探"是**错的设计前提**——正因为 5.1 几乎必然存在,才应该探它拿真实版本号。
|
|
19
|
+
- 修法(改动全部落在 `lib/pwsh-check.js`,不涉其他模块):
|
|
20
|
+
1. **探测链加一级(核心修复)**:`pwsh`(走 PATH)→ 已知绝对路径候选 → **`powershell.exe`**。拿到 5 就正常报"5 是旧版、该提示升级",**不再算探测失败**;只有三级全落空才是真的没探到东西。
|
|
21
|
+
2. **绝对路径候选用 `lstat` 预筛**(派生自 dsh-ops 的 `isSpawnableFile`,MIT,文件头已注明出处):`existsSync`/`statSync` 在 Windows Store 的 0 字节 app execution alias 上会因 EACCES 吞异常返回 false,把"已装好"误判成"没装"。`lstat` 认符号链接;且**只筛确认的 ENOENT/ENOTDIR**,EACCES 等一律放行交给 `spawn` 判——真正作数的永远是 spawn 的结果。
|
|
22
|
+
3. **候选清单补全**:`ProgramFiles\PowerShell\7` → `7-preview` → `ProgramFiles(x86)` 同路径 → `%LOCALAPPDATA%\Microsoft\WindowsApps` → scoop(`~\scoop\shims`、`~\scoop\apps\powershell\current`)→ chocolatey(`%ProgramData%\chocolatey\bin`)。
|
|
23
|
+
4. **失败不静默**:状态文件升到 `version: 2`,除 `major` 外新增 `status` / `source`(`pwsh` / `pwsh-path` / `windows-powershell` / `none`)/ `detail` 三个字段;解析不出来时 `detail` 带上输出原文前 120 字,不再只给一个 `unparsable`。
|
|
24
|
+
5. **编码兜底**:探针命令前置 `try{[Console]::OutputEncoding=[Text.Encoding]::UTF8}catch{}`;stdout 改成**保留原始字节**(原先 `String(chunk)` 先按 UTF-8 猜一遍就丢了信息),由新增的 `decodeOutput()` 按 BOM 判 UTF-8/UTF-16LE/UTF-16BE、无 BOM 走 UTF-8、解出替换字符再退 GBK。
|
|
25
|
+
6. **解析放宽**:整行匹配不上时,整串是个版本号(`7.4.1`)也认主版本;但噪音里的数字不认(`WARNING: 12 things` → `null`),宁可降级也不瞎猜出一个 ≥7 把提醒吞掉。
|
|
26
|
+
7. **向后兼容**:读到 1.5.30 及以前那种没有 `version` 字段的旧 `pwsh.json`(正是本 bug 留下的 `major: null` 旧账)时判为"没查过"重测,但把 `notifiedAt` 保住——升级后不会把已经说过的话再唠叨一遍。
|
|
27
|
+
- 验证(2026-11-11 本机实跑,不是推演):
|
|
28
|
+
- 全量单测 `node --test "test/*.test.mjs"`:**156 项全通过 / 0 失败**(`pwsh-check.test.mjs` 由 38 项扩到 44 项)。
|
|
29
|
+
- 真机端到端(import 真模块、起真子进程):本机只有 5.1 的环境下 `detectPwshVersion()` 返回 `{status:'ok', major:5, via:'powershell.exe', source:'windows-powershell'}`,落盘 `major: 5`——**`major` 从 `null` 变成真实的 5**;`maybePwshNotice()` 680ms 内给出升级提示。
|
|
30
|
+
- 失败降级:pwsh 与 powershell.exe 都不存在时 `{status:'missing', major:null, source:'none', detail:'pwsh: ENOENT; …; powershell.exe: ENOENT'}`,不抛错、不崩;非零退出时 `detail` 带退出码。
|
|
31
|
+
- 编码怪异:UTF-8 BOM + 中文噪音行 → 7;UTF-16LE(带 BOM)→ 5;`7.4.1` → 7;纯噪音 → `null`(老实降级)。
|
|
32
|
+
- 向后兼容:读 1.5.30 旧格式缓存 → 判为需要重测、`notifiedAt` 保留;重测后拿到 `major: 5`。
|
|
33
|
+
- 版本号三处对齐:`package.json` version、本条目、profile `cordis.patch.yml` 里 `aiyayi-feishu` 的 `config.reloadTag` 均为 `1.5.31`(reloadTag 语义同 1.5.29 条目:只要求值变了以触发 HMR,不参与部署值比对)。
|
|
34
|
+
- 不含功能性改动的部分:微信通道(1.5.27 并入、1.5.28 收尾,默认关闭)本版**一行未动**。
|
|
35
|
+
|
|
13
36
|
## 1.5.30(2026-11-11 修复:create-bot 脚本注释里的实测出处改用 <记忆根> 占位)
|
|
14
37
|
|
|
15
38
|
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
package/lib/pwsh-check.js
CHANGED
|
@@ -11,24 +11,39 @@
|
|
|
11
11
|
*
|
|
12
12
|
* 设计约束(照需求逐条落实):
|
|
13
13
|
* 1. **只在 win32 检查**:其他平台 `status:'skip'`,一次子进程都不起。
|
|
14
|
-
* 2.
|
|
14
|
+
* 2. **优先探 `pwsh`,探不到再退回 `powershell.exe`**(1.5.31 修):只探 pwsh 的话,
|
|
15
|
+
* 机器上只有系统自带 5.1 时结果就是 `major:null`——数字空着,"没装 pwsh"和
|
|
16
|
+
* "装了但解析失败"分不出来,用户看到的提醒底层判断是空的。退回 5.1 拿真实
|
|
17
|
+
* 的 5,"低于 7"才是拿数据说话。(1.5.28 的注释写的"探它永远不会失败"是错的。)
|
|
15
18
|
* 3. **绝不拖住启动或对话**:子进程 2.5s 超时;调用方还叠了一道更短的 await 预算
|
|
16
19
|
* (见 maybePwshNotice 的 budgetMs),预算内没结果就这一轮先不说,下一轮/下个
|
|
17
20
|
* 会话再说。探针在 apply 期就预热(warmPwshProbe),等真有人开口时多半早已有值。
|
|
18
21
|
* 4. **只提一次**:结果落 `pwsh.json`(与 updates.json 同目录同风格),提过就记
|
|
19
22
|
* notifiedAt,之后不再提;装上新版(≥7)连记录一起清掉,降级了才会重新提。
|
|
20
|
-
* 5.
|
|
21
|
-
* "需要提示"
|
|
23
|
+
* 5. **失败不静默**:超时、非零退出、输出解析不出来,一律按 5.1 处理(这就是
|
|
24
|
+
* "需要提示"),但状态里必须留下 `status` / `source` / `detail`——「为什么没拿到
|
|
25
|
+
* 数字」要能查,不能再是一个谁也不认识的 `major:null`(1.5.31 修)。
|
|
22
26
|
*
|
|
23
27
|
* 判定口径:`major >= 7` 才算够用;`major === null`(没装/超时/解析失败)按旧版算。
|
|
28
|
+
*
|
|
29
|
+
* 绝对路径候选的 lstat 预筛思路(isSpawnableFile)派生自 dsh-ops
|
|
30
|
+
* (MIT, Copyright (c) 2026 T-Auto and the dsh-ops contributors)。
|
|
24
31
|
*/
|
|
25
32
|
import { spawn } from 'node:child_process'
|
|
26
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
|
|
33
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
|
|
27
34
|
import { dirname, join } from 'node:path'
|
|
28
35
|
import { pwshFile, resolveRoot } from './home.js'
|
|
29
36
|
|
|
30
|
-
/**
|
|
31
|
-
|
|
37
|
+
/**
|
|
38
|
+
* 探针命令:只问主版本号,输出一个数字,别把 $PSVersionTable 整份吐回来。
|
|
39
|
+
*
|
|
40
|
+
* 前置 `[Console]::OutputEncoding`:powershell.exe 5.1 默认按控制台代码页(本机是
|
|
41
|
+
* ANSI/GBK)往管道里写,中文环境下一遇到非 ASCII 就是乱码;显式设成 UTF-8 后
|
|
42
|
+
* 输出稳定是单字节 ASCII,解析不再看运气。设不上(极少数无控制台场景)就 try 吞掉,
|
|
43
|
+
* 后面还有 BOM/UTF-16 解码兜底。
|
|
44
|
+
*/
|
|
45
|
+
export const PWSH_COMMAND = 'try{[Console]::OutputEncoding=[Text.Encoding]::UTF8}catch{};$PSVersionTable.PSVersion.Major'
|
|
46
|
+
export const PWSH_ARGS = ['-NoProfile', '-Command', PWSH_COMMAND]
|
|
32
47
|
|
|
33
48
|
/** 够用的最低主版本。5.1 是系统自带那个,不是这个。 */
|
|
34
49
|
export const MIN_MAJOR = 7
|
|
@@ -57,14 +72,75 @@ export const PWSH_TIP = [
|
|
|
57
72
|
'这条只提这一次,说过了就别再念叨。',
|
|
58
73
|
].join('')
|
|
59
74
|
|
|
75
|
+
/**
|
|
76
|
+
* 按探测到的真实主版本号给提示文案(1.5.31)。
|
|
77
|
+
* 5.x / null 沿用原文案;其他旧版本(6.x 之类)把括号里的版本号换掉,别报错版本。
|
|
78
|
+
* @param {number|null} major
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function pwshTipFor(major) {
|
|
82
|
+
if (!Number.isFinite(major) || major === 5) return PWSH_TIP
|
|
83
|
+
return PWSH_TIP.replace('(5.1)', `(${major}.x)`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 把子进程吐出来的原始字节解成文本。**不抛错**。
|
|
88
|
+
*
|
|
89
|
+
* 为什么要自己解而不是 `String(chunk)`:`String(Buffer)` 走的是 UTF-8 解码,
|
|
90
|
+
* 遇到 UTF-16LE 输出(某些被策略改过控制台的机器)会得到一堆夹 NUL 的乱码;
|
|
91
|
+
* powershell.exe 5.1 在中文控制台下还可能吐 UTF-8 BOM。这里按 BOM 判编码,
|
|
92
|
+
* 没有 BOM 就当 UTF-8、失败再退 GBK(Windows 中文环境的 ANSI 代码页)。
|
|
93
|
+
*
|
|
94
|
+
* @param {Buffer|string} raw
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
export function decodeOutput(raw) {
|
|
98
|
+
if (typeof raw === 'string') return raw.replace(/^\uFEFF/, '')
|
|
99
|
+
if (!raw || typeof raw.length !== 'number') return ''
|
|
100
|
+
const buf = Buffer.isBuffer(raw) ? raw : Buffer.from(raw)
|
|
101
|
+
if (buf.length === 0) return ''
|
|
102
|
+
// UTF-16LE BOM:FF FE
|
|
103
|
+
if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) {
|
|
104
|
+
return buf.subarray(2).toString('utf16le')
|
|
105
|
+
}
|
|
106
|
+
// UTF-16BE BOM:FE FF(Node 不原生支持 utf16be,手工翻字节)
|
|
107
|
+
if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) {
|
|
108
|
+
const swapped = Buffer.from(buf.subarray(2))
|
|
109
|
+
for (let i = 0; i + 1 < swapped.length; i += 2) {
|
|
110
|
+
const t = swapped[i]
|
|
111
|
+
swapped[i] = swapped[i + 1]
|
|
112
|
+
swapped[i + 1] = t
|
|
113
|
+
}
|
|
114
|
+
return swapped.toString('utf16le')
|
|
115
|
+
}
|
|
116
|
+
// UTF-8 BOM:EF BB BF
|
|
117
|
+
if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
|
|
118
|
+
return buf.subarray(3).toString('utf8')
|
|
119
|
+
}
|
|
120
|
+
const utf8 = buf.toString('utf8')
|
|
121
|
+
// 解出替换字符 = 不是合法 UTF-8,退回 GBK 再试(中文 Windows 的 ANSI 页)。
|
|
122
|
+
if (utf8.includes('\uFFFD')) {
|
|
123
|
+
try {
|
|
124
|
+
const gbk = new TextDecoder('gbk', { fatal: false }).decode(buf)
|
|
125
|
+
if (gbk && !gbk.includes('\uFFFD')) return gbk
|
|
126
|
+
} catch {
|
|
127
|
+
/* 该 Node 不带 gbk 解码器(full-icu 缺失)就用 UTF-8 的结果 */
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return utf8
|
|
131
|
+
}
|
|
132
|
+
|
|
60
133
|
/**
|
|
61
134
|
* 解析 `$PSVersionTable.PSVersion.Major` 的输出。
|
|
62
|
-
*
|
|
63
|
-
* @param {unknown} raw 子进程 stdout
|
|
135
|
+
* 容忍 BOM、UTF-16、CRLF、以及启动噪音(取第一处独立的 1~2 位数字)。
|
|
136
|
+
* @param {unknown} raw 子进程 stdout(Buffer 或字符串都行)
|
|
64
137
|
* @returns {number|null} 主版本号;解析不出来给 null(= 按旧版处理)
|
|
65
138
|
*/
|
|
66
139
|
export function parsePwshMajor(raw) {
|
|
67
|
-
|
|
140
|
+
let text = decodeOutput(raw)
|
|
141
|
+
if (text.length === 0) return null
|
|
142
|
+
// 先把 BOM/零宽字符清干净,免得数字被不可见字符包住匹配不上。
|
|
143
|
+
text = text.replace(/[\uFEFF\u200B]/g, '')
|
|
68
144
|
// 逐行找第一处 1~2 位纯数字;7.x 的 Major 是 7,5.1 是 5。
|
|
69
145
|
for (const line of text.split(/\r?\n/)) {
|
|
70
146
|
const match = /^\s*(\d{1,2})\s*$/.exec(line)
|
|
@@ -73,6 +149,14 @@ export function parsePwshMajor(raw) {
|
|
|
73
149
|
if (Number.isFinite(major)) return major
|
|
74
150
|
}
|
|
75
151
|
}
|
|
152
|
+
// 整行匹配不上再兜一次(1.5.31):只认"看起来就是个版本号"的整串,比如
|
|
153
|
+
// `7.4.1`——封顶防误判,纯噪音里的数字("WARNING: 12 things")不算数,
|
|
154
|
+
// 宁可 return null 让它走降级,也不要瞎猜出一个 ≥7 把提醒吞掉。
|
|
155
|
+
const loose = /^\s*(\d{1,2})(?:\.\d+)+/.exec(text)
|
|
156
|
+
if (loose) {
|
|
157
|
+
const major = Number.parseInt(loose[1], 10)
|
|
158
|
+
if (Number.isFinite(major)) return major
|
|
159
|
+
}
|
|
76
160
|
return null
|
|
77
161
|
}
|
|
78
162
|
|
|
@@ -81,6 +165,33 @@ export function isPwshTooOld(major) {
|
|
|
81
165
|
return !(Number.isFinite(major) && major >= MIN_MAJOR)
|
|
82
166
|
}
|
|
83
167
|
|
|
168
|
+
/**
|
|
169
|
+
* 这是不是一个"能 spawn 的条目"(1.5.31,思路派生自 dsh-ops 的 isSpawnableFile)。
|
|
170
|
+
*
|
|
171
|
+
* 为什么不用 existsSync/statSync:Windows Store 版 pwsh 的入口是一个 0 字节的
|
|
172
|
+
* app execution alias(符号链接形态),statSync 在普通用户上下文里可能直接抛
|
|
173
|
+
* EACCES → existsSync 吞异常返回 false,把"已装好"误判成"没装"。lstat 认符号链接本身。
|
|
174
|
+
*
|
|
175
|
+
* 注意:这里**只做快速筛除明显不存在的候选**(省掉 N 次子进程超时),命中与否
|
|
176
|
+
* 都不代表一定能跑起来——真正作数的仍是 spawn 的结果。lstat 也失败时(alias 场景)
|
|
177
|
+
* 一律返回 true,宁可多起一个进程,也不要把真 alias 筛出去。
|
|
178
|
+
*
|
|
179
|
+
* @param {string} path
|
|
180
|
+
* @returns {boolean}
|
|
181
|
+
*/
|
|
182
|
+
export function isSpawnableFile(path) {
|
|
183
|
+
if (typeof path !== 'string' || path.length === 0) return false
|
|
184
|
+
try {
|
|
185
|
+
const stat = lstatSync(path)
|
|
186
|
+
return stat.isFile() || stat.isSymbolicLink()
|
|
187
|
+
} catch (error) {
|
|
188
|
+
const code = String(error?.code ?? '')
|
|
189
|
+
// ENOENT/ENOTDIR = 这条路径确实不存在,可以筛掉;
|
|
190
|
+
// EACCES/EPERM 等 = 看不到不等于没有(WindowsApps 的 alias),放行让 spawn 去判。
|
|
191
|
+
return code !== 'ENOENT' && code !== 'ENOTDIR'
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
84
195
|
/**
|
|
85
196
|
* pwsh 已知安装位置(**绝对路径兜底**,坑 #75)。
|
|
86
197
|
*
|
|
@@ -92,7 +203,8 @@ export function isPwshTooOld(major) {
|
|
|
92
203
|
* 只看 PATH 就会把"已装好"误判成"没装",对用户弹一条**错误的**提示。
|
|
93
204
|
*
|
|
94
205
|
* 所以 ENOENT 之后再按下面这些已知位置挨个试绝对路径;命中就视为已装。
|
|
95
|
-
* 顺序按"
|
|
206
|
+
* 顺序按"传统 MSI 装法"→"预览版"→"msstore alias"→"scoop / chocolatey"(1.5.31
|
|
207
|
+
* 补了后两种包管理器的落点,它们的 shim 也经常不在宿主进程的 PATH 里)。
|
|
96
208
|
*
|
|
97
209
|
* @param {object} [env] 默认 process.env(单测可注入)
|
|
98
210
|
* @returns {string[]} 绝对路径候选(已去重、去掉解析不出盘符的)
|
|
@@ -105,16 +217,21 @@ export function pwshPathCandidates(env = process.env) {
|
|
|
105
217
|
if (!candidates.includes(full)) candidates.push(full)
|
|
106
218
|
}
|
|
107
219
|
|
|
108
|
-
// ①
|
|
109
|
-
push(env?.LOCALAPPDATA, 'Microsoft', 'WindowsApps', 'pwsh.exe')
|
|
110
|
-
// ② 传统 MSI 安装位置(PowerShell 7 正式版)。
|
|
220
|
+
// ① 传统 MSI 安装位置(PowerShell 7 正式版)。
|
|
111
221
|
push(env?.ProgramFiles, 'PowerShell', '7', 'pwsh.exe')
|
|
112
|
-
//
|
|
222
|
+
// ② 预览版。
|
|
113
223
|
push(env?.ProgramFiles, 'PowerShell', '7-preview', 'pwsh.exe')
|
|
114
|
-
//
|
|
224
|
+
// ③ 32 位宿主可能看到的重定向目录,顺手覆盖。
|
|
115
225
|
push(env?.['ProgramFiles(x86)'], 'PowerShell', '7', 'pwsh.exe')
|
|
226
|
+
// ④ msstore / winget 的 app execution alias(本机实测踩过这条,坑 #75)。
|
|
227
|
+
push(env?.LOCALAPPDATA, 'Microsoft', 'WindowsApps', 'pwsh.exe')
|
|
228
|
+
// ⑤ scoop:shim 目录 + 常见持久安装位置(1.5.31)。
|
|
229
|
+
push(env?.USERPROFILE, 'scoop', 'shims', 'pwsh.exe')
|
|
230
|
+
push(env?.USERPROFILE, 'scoop', 'apps', 'powershell', 'current', 'pwsh.exe')
|
|
231
|
+
// ⑥ chocolatey(1.5.31)。bin 下是 shim,lib 下是真实安装目录。
|
|
232
|
+
push(env?.ProgramData, 'chocolatey', 'bin', 'pwsh.exe')
|
|
116
233
|
|
|
117
|
-
//
|
|
234
|
+
// ⑦ msstore 包目录(父级点名要试的 `%ProgramFiles%\WindowsApps\Microsoft.PowerShell_*`)。
|
|
118
235
|
// 这是 winget/商店装的 PowerShell 落盘位置,目录名形如 Microsoft.PowerShell_8wekyb3d8bbwe_x_x_x_<hash>。
|
|
119
236
|
// 注意:Program Files\WindowsApps 通常只有 TrustedInstaller 能读,普通用户大概率
|
|
120
237
|
// readdir 直接 EACCES——静默跳过(当不存在处理),绝不因为这个把探测拖垮。
|
|
@@ -169,6 +286,7 @@ function probeCommand(command, timeoutMs, spawnImpl) {
|
|
|
169
286
|
}
|
|
170
287
|
|
|
171
288
|
let stdout = ''
|
|
289
|
+
let rawChunks = []
|
|
172
290
|
|
|
173
291
|
timer = setTimeout(() => {
|
|
174
292
|
// 超时:先把子进程掐掉再结算,别留个孤儿进程在后台。
|
|
@@ -193,9 +311,16 @@ function probeCommand(command, timeoutMs, spawnImpl) {
|
|
|
193
311
|
})
|
|
194
312
|
|
|
195
313
|
child?.stdout?.on?.('data', (chunk) => {
|
|
196
|
-
|
|
314
|
+
// 先留原始字节(1.5.31):编码判定(BOM / UTF-16 / GBK)必须在字节上做,
|
|
315
|
+
// 提前 String(chunk) 就等于先按 UTF-8 猜一遍,猜错的信息就没了。
|
|
316
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
|
|
317
|
+
rawChunks.push(buf)
|
|
197
318
|
// 输出够短,提前收工也无所谓;这里只是别让它无限涨。
|
|
198
|
-
|
|
319
|
+
const total = rawChunks.reduce((sum, b) => sum + b.length, 0)
|
|
320
|
+
if (total > 8192) {
|
|
321
|
+
rawChunks = [Buffer.concat(rawChunks).subarray(0, 8192)]
|
|
322
|
+
}
|
|
323
|
+
stdout = ''
|
|
199
324
|
})
|
|
200
325
|
|
|
201
326
|
child?.on?.('close', (code) => {
|
|
@@ -203,9 +328,12 @@ function probeCommand(command, timeoutMs, spawnImpl) {
|
|
|
203
328
|
finish({ status: 'error', major: null, detail: `exit ${code}` })
|
|
204
329
|
return
|
|
205
330
|
}
|
|
206
|
-
const
|
|
331
|
+
const raw = Buffer.concat(rawChunks)
|
|
332
|
+
const major = parsePwshMajor(raw)
|
|
207
333
|
if (major === null) {
|
|
208
|
-
|
|
334
|
+
// 1.5.31:解析不出来必须留下**原文**(截断、转义),不再只给一个 'unparsable'。
|
|
335
|
+
const preview = decodeOutput(raw).replace(/\s+/g, ' ').trim().slice(0, 120)
|
|
336
|
+
finish({ status: 'error', major: null, detail: `unparsable: ${preview || '<empty>'}` })
|
|
209
337
|
return
|
|
210
338
|
}
|
|
211
339
|
finish({ status: 'ok', major })
|
|
@@ -214,10 +342,13 @@ function probeCommand(command, timeoutMs, spawnImpl) {
|
|
|
214
342
|
}
|
|
215
343
|
|
|
216
344
|
/**
|
|
217
|
-
* 跑一次探针。**不抛错**:所有失败都折叠成 status
|
|
345
|
+
* 跑一次探针。**不抛错**:所有失败都折叠成 status,并带上 source 说明从哪来的。
|
|
218
346
|
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
347
|
+
* 顺序(1.5.31):`pwsh`(走 PATH)→ 已知绝对路径候选 → `powershell.exe`(系统自带 5.1)。
|
|
348
|
+
* 前三步全落空才是真的没探到东西。最后这一级是修 `major:null` 的关键:机器上只有
|
|
349
|
+
* 5.1 时也能拿到**真实数字 5**,"低于 7"才是有依据的判断,而不是 null 碰巧当旧版。
|
|
350
|
+
*
|
|
351
|
+
* 绝对路径兜底只在 ENOENT 上触发——ENOENT 是毫秒级返回;如果 pwsh 存在但卡住,那是
|
|
221
352
|
* `timeout`,不触发兜底(否则每条候选都要等满 2.5s,把对话拖死)。
|
|
222
353
|
*
|
|
223
354
|
* @param {object} [opts]
|
|
@@ -226,42 +357,66 @@ function probeCommand(command, timeoutMs, spawnImpl) {
|
|
|
226
357
|
* @param {Function} [opts.spawnImpl] 默认 node:child_process.spawn(单测注入假进程)
|
|
227
358
|
* @param {object} [opts.env] 默认 process.env(单测注入)
|
|
228
359
|
* @param {boolean} [opts.noFallback] 关掉绝对路径兜底(单测对照用)
|
|
360
|
+
* @param {boolean} [opts.noLegacy] 关掉 powershell.exe 那一级(单测对照用)
|
|
361
|
+
* @param {string} [opts.legacyCommand] 覆盖 5.1 的命令名(单测注入)
|
|
229
362
|
* @param {string[]} [opts.candidates] 覆盖绝对路径候选(单测注入)
|
|
230
|
-
* @returns {Promise<{status:'skip'|'ok'|'missing'|'timeout'|'error', major:number|null, detail?:string, via?:string}>}
|
|
363
|
+
* @returns {Promise<{status:'skip'|'ok'|'missing'|'timeout'|'error', major:number|null, detail?:string, via?:string, source?:string}>}
|
|
231
364
|
*/
|
|
232
365
|
export function detectPwshVersion(opts = {}) {
|
|
233
366
|
const platform = opts.platform ?? process.platform
|
|
234
367
|
const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : PROBE_TIMEOUT_MS
|
|
235
368
|
const spawnImpl = typeof opts.spawnImpl === 'function' ? opts.spawnImpl : spawn
|
|
369
|
+
const legacyCommand = typeof opts.legacyCommand === 'string' && opts.legacyCommand.length > 0
|
|
370
|
+
? opts.legacyCommand
|
|
371
|
+
: 'powershell.exe'
|
|
236
372
|
|
|
237
373
|
// 硬要求 1:非 Windows 直接跳过,不起子进程。
|
|
238
374
|
if (platform !== 'win32') {
|
|
239
|
-
return Promise.resolve({ status: 'skip', major: null, detail: 'non-win32' })
|
|
375
|
+
return Promise.resolve({ status: 'skip', major: null, detail: 'non-win32', source: 'skip' })
|
|
240
376
|
}
|
|
241
377
|
|
|
242
378
|
return (async () => {
|
|
243
379
|
const first = await probeCommand('pwsh', timeoutMs, spawnImpl)
|
|
244
|
-
if (first.status === 'ok') return { ...first, via: 'path' }
|
|
245
|
-
|
|
246
|
-
if (first.status !== 'missing' || opts.noFallback === true) return first
|
|
380
|
+
if (first.status === 'ok') return { ...first, via: 'path', source: 'pwsh' }
|
|
381
|
+
if (first.status === 'timeout') return { ...first, source: 'pwsh' }
|
|
247
382
|
|
|
248
|
-
const candidates = Array.isArray(opts.candidates) ? opts.candidates : pwshPathCandidates(opts.env ?? process.env)
|
|
249
383
|
const tried = []
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
384
|
+
if (first.status === 'missing' && opts.noFallback !== true) {
|
|
385
|
+
const candidates = Array.isArray(opts.candidates) ? opts.candidates : pwshPathCandidates(opts.env ?? process.env)
|
|
386
|
+
for (const candidate of candidates) {
|
|
387
|
+
if (typeof candidate !== 'string' || candidate.length === 0) continue
|
|
388
|
+
tried.push(candidate)
|
|
389
|
+
// 预筛只是省时间:明显不存在的路径(ENOENT/ENOTDIR)直接跳过,不必起进程等它
|
|
390
|
+
// 报错。**但 lstat 也看不透的(EACCES 等)一律放行**,让 spawn 自己去判——
|
|
391
|
+
// WindowsApps 的 0 字节 app execution alias 就是这种(坑 #75),筛掉它就等于
|
|
392
|
+
// 把"已装好"判成"没装"。真正作数的永远是 spawn 的结果,不是预筛。
|
|
393
|
+
if (!isSpawnableFile(candidate)) continue
|
|
394
|
+
const result = await probeCommand(candidate, timeoutMs, spawnImpl)
|
|
395
|
+
if (result.status === 'ok') return { ...result, via: candidate, source: 'pwsh-path' }
|
|
396
|
+
// 候选存在但跑不通(超时/非零退出):别再耗下去了,交给下一级。
|
|
397
|
+
if (result.status === 'timeout') break
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// 二级兜底(1.5.31):系统自带 Windows PowerShell 5.1。它几乎必然存在,
|
|
402
|
+
// 问到就是真实主版本号 5 —— 比 null 有用得多。
|
|
403
|
+
if (opts.noLegacy !== true) {
|
|
404
|
+
const legacy = await probeCommand(legacyCommand, timeoutMs, spawnImpl)
|
|
405
|
+
if (legacy.status === 'ok') return { ...legacy, via: legacyCommand, source: 'windows-powershell' }
|
|
406
|
+
// 5.1 也探不出来:把两个失败原因都带上,别丢信息。
|
|
407
|
+
return {
|
|
408
|
+
status: legacy.status === 'missing' ? 'missing' : legacy.status,
|
|
409
|
+
major: null,
|
|
410
|
+
detail: `pwsh: ${first.detail ?? first.status}; fallback tried: ${tried.length > 0 ? tried.join(' | ') : '(none)'}; ${legacyCommand}: ${legacy.detail ?? legacy.status}`,
|
|
411
|
+
source: 'none',
|
|
412
|
+
}
|
|
259
413
|
}
|
|
260
414
|
|
|
261
415
|
// 兜底也没命中:保持原来的 missing,并把试过哪些绝对路径记进 detail,便于排障。
|
|
262
416
|
return {
|
|
263
417
|
...first,
|
|
264
418
|
detail: tried.length > 0 ? `ENOENT (fallback tried: ${tried.join(' | ')})` : 'ENOENT',
|
|
419
|
+
source: 'none',
|
|
265
420
|
}
|
|
266
421
|
})()
|
|
267
422
|
}
|
|
@@ -270,9 +425,26 @@ export function detectPwshVersion(opts = {}) {
|
|
|
270
425
|
* 状态落盘(pwsh.json,与 updates.json 同目录同风格)
|
|
271
426
|
* ------------------------------------------------------------------ */
|
|
272
427
|
|
|
273
|
-
/**
|
|
428
|
+
/**
|
|
429
|
+
* 状态文件形状(version 2,1.5.31):
|
|
430
|
+
* `{ version:2, checkedAt, major, status, source, detail, notifiedAt, notifiedMajor }`
|
|
431
|
+
*
|
|
432
|
+
* version 2 新增 `status` / `source` / `detail`:老版本只存 `major:null`,"为什么是 null"
|
|
433
|
+
* 一点线索都不留。现在至少能看出是「没装 pwsh 但 5.1 探到了(5)」还是「压根没探到」。
|
|
434
|
+
*/
|
|
435
|
+
const STATE_VERSION = 2
|
|
436
|
+
|
|
274
437
|
function emptyState() {
|
|
275
|
-
return {
|
|
438
|
+
return {
|
|
439
|
+
version: STATE_VERSION,
|
|
440
|
+
checkedAt: null,
|
|
441
|
+
major: null,
|
|
442
|
+
status: null,
|
|
443
|
+
source: null,
|
|
444
|
+
detail: null,
|
|
445
|
+
notifiedAt: null,
|
|
446
|
+
notifiedMajor: null,
|
|
447
|
+
}
|
|
276
448
|
}
|
|
277
449
|
|
|
278
450
|
function toIso(now) {
|
|
@@ -280,18 +452,35 @@ function toIso(now) {
|
|
|
280
452
|
}
|
|
281
453
|
|
|
282
454
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
455
|
+
* 读状态。文件不在/坏了/根不可达/是旧格式 → 空状态(等于"还没查过",会重测)。
|
|
456
|
+
*
|
|
457
|
+
* **向后兼容**:1.5.30 及以前写的 `pwsh.json` 没有 `version` 字段,那是 `major:null`
|
|
458
|
+
* 的旧账(正是本版要修的 bug 留下的)。读到就当作"没查过"重测一遍,不崩、也不
|
|
459
|
+
* 拿一份不可信的旧结果继续判断。
|
|
460
|
+
*
|
|
461
|
+
* @returns {{version:number, checkedAt:string|null, major:number|null, status:string|null, source:string|null, detail:string|null, notifiedAt:string|null, notifiedMajor:number|null}}
|
|
285
462
|
*/
|
|
286
463
|
export function readPwshState() {
|
|
287
464
|
try {
|
|
288
465
|
const file = pwshFile()
|
|
289
466
|
if (!file || !existsSync(file)) return emptyState()
|
|
290
467
|
const parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
468
|
+
// 旧格式(无 version):值不可信,当没查过。
|
|
469
|
+
if (!Number.isFinite(parsed?.version) || Number(parsed.version) < STATE_VERSION) {
|
|
470
|
+
// 「提过没」这条要保住:旧账里 notifiedAt 是准的,别因为升级了就再唠叨一遍。
|
|
471
|
+
const kept = emptyState()
|
|
472
|
+
kept.notifiedAt = typeof parsed?.notifiedAt === 'string' ? parsed.notifiedAt : null
|
|
473
|
+
kept.notifiedMajor = Number.isFinite(parsed?.notifiedMajor) ? Number(parsed.notifiedMajor) : null
|
|
474
|
+
return kept
|
|
475
|
+
}
|
|
291
476
|
const major = Number.isFinite(parsed?.major) ? Number(parsed.major) : null
|
|
292
477
|
return {
|
|
478
|
+
version: STATE_VERSION,
|
|
293
479
|
checkedAt: typeof parsed?.checkedAt === 'string' ? parsed.checkedAt : null,
|
|
294
480
|
major,
|
|
481
|
+
status: typeof parsed?.status === 'string' ? parsed.status : null,
|
|
482
|
+
source: typeof parsed?.source === 'string' ? parsed.source : null,
|
|
483
|
+
detail: typeof parsed?.detail === 'string' ? parsed.detail : null,
|
|
295
484
|
notifiedAt: typeof parsed?.notifiedAt === 'string' ? parsed.notifiedAt : null,
|
|
296
485
|
notifiedMajor: Number.isFinite(parsed?.notifiedMajor) ? Number(parsed.notifiedMajor) : null,
|
|
297
486
|
}
|
|
@@ -312,7 +501,9 @@ export function writePwshState(state) {
|
|
|
312
501
|
const rootState = resolveRoot()
|
|
313
502
|
if (!rootState.reachable) return false
|
|
314
503
|
mkdirSync(dirname(file), { recursive: true })
|
|
315
|
-
|
|
504
|
+
// 版本戳由这里统一盖(1.5.31):调用方不必每次记得写 version,
|
|
505
|
+
// 免得写出一个"看起来像旧格式"的文件、下次又被判成没查过。
|
|
506
|
+
writeFileSync(file, `${JSON.stringify({ ...state, version: STATE_VERSION }, null, 2)}\n`, 'utf8')
|
|
316
507
|
return true
|
|
317
508
|
} catch {
|
|
318
509
|
return false
|
|
@@ -402,8 +593,12 @@ export function warmPwshProbe(opts = {}) {
|
|
|
402
593
|
const probeMajor = result.status === 'ok' ? result.major : null
|
|
403
594
|
const next = {
|
|
404
595
|
...current,
|
|
596
|
+
version: STATE_VERSION,
|
|
405
597
|
checkedAt: toIso(Date.now()),
|
|
406
598
|
major: probeMajor,
|
|
599
|
+
status: result.status ?? null,
|
|
600
|
+
source: result.source ?? null,
|
|
601
|
+
detail: result.detail ?? null,
|
|
407
602
|
}
|
|
408
603
|
if (!isPwshTooOld(probeMajor)) {
|
|
409
604
|
// 够用了:把"提过"的记录清掉,将来万一降级还能再提一次。
|
|
@@ -423,8 +618,17 @@ export function warmPwshProbe(opts = {}) {
|
|
|
423
618
|
|
|
424
619
|
/** 把探针结果并进状态。 */
|
|
425
620
|
function mergeProbe(state, detection, now) {
|
|
426
|
-
const major = detection
|
|
427
|
-
const next = {
|
|
621
|
+
const major = detection?.status === 'ok' ? detection.major : null
|
|
622
|
+
const next = {
|
|
623
|
+
...state,
|
|
624
|
+
version: STATE_VERSION,
|
|
625
|
+
checkedAt: toIso(now),
|
|
626
|
+
major,
|
|
627
|
+
// 1.5.31:把"为什么是这个数"一并记下,下次排障不用再猜。
|
|
628
|
+
status: detection?.status ?? null,
|
|
629
|
+
source: detection?.source ?? detection?.via ?? null,
|
|
630
|
+
detail: detection?.detail ?? null,
|
|
631
|
+
}
|
|
428
632
|
if (!isPwshTooOld(major)) {
|
|
429
633
|
next.notifiedAt = null
|
|
430
634
|
next.notifiedMajor = null
|
|
@@ -506,7 +710,9 @@ export async function maybePwshNotice(opts = {}) {
|
|
|
506
710
|
|
|
507
711
|
// 记"说过了"——这一步失败也只影响下次会不会重复提,不影响这轮说话。
|
|
508
712
|
writePwshState({ ...state, notifiedAt: toIso(now), notifiedMajor: state.major ?? null })
|
|
509
|
-
|
|
713
|
+
// 1.5.31:文案按真实主版本号给。探到 5 就说 5.1(原样),探到 6 说 6.x,
|
|
714
|
+
// 探不到(null)仍按旧版处理,用默认文案。
|
|
715
|
+
return pwshTipFor(state.major)
|
|
510
716
|
} catch {
|
|
511
717
|
return ''
|
|
512
718
|
}
|