dsh-mobile-ios 0.2.14 → 0.2.15
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/package.json +1 -1
- package/src/plugin-update.mjs +112 -1
package/package.json
CHANGED
package/src/plugin-update.mjs
CHANGED
|
@@ -55,6 +55,104 @@ function sha1(buf) {
|
|
|
55
55
|
return createHash('sha1').update(buf).digest('hex');
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** 锁文件里我们这一个包的条目重写:importers / packages / snapshots 三处都要对齐。 */
|
|
59
|
+
function rewriteLock(text, { from, to, specifier, integrity }) {
|
|
60
|
+
const lines = text.split('\n');
|
|
61
|
+
const headPattern = new RegExp(`^(\\s{2})${PLUGIN_NAME}@([^:\\s]+):(.*)$`);
|
|
62
|
+
const depPattern = new RegExp(`^(\\s+)${PLUGIN_NAME}:\\s*$`);
|
|
63
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
64
|
+
// ① importers 下的 specifier / version
|
|
65
|
+
const dep = lines[i].match(depPattern);
|
|
66
|
+
if (dep) {
|
|
67
|
+
const baseIndent = dep[1].length;
|
|
68
|
+
for (let j = i + 1; j < lines.length; j += 1) {
|
|
69
|
+
if (!lines[j].trim()) continue;
|
|
70
|
+
const indent = lines[j].length - lines[j].trimStart().length;
|
|
71
|
+
if (indent <= baseIndent) break;
|
|
72
|
+
const kv = lines[j].match(/^(\s*)(specifier|version):\s*(.+)$/);
|
|
73
|
+
if (!kv) continue;
|
|
74
|
+
if (kv[2] === 'specifier' && specifier) lines[j] = `${kv[1]}specifier: ${specifier}`;
|
|
75
|
+
// version 可能带 peer 后缀,只替换版本前缀
|
|
76
|
+
if (kv[2] === 'version' && kv[3].trim().split('(')[0] === from) {
|
|
77
|
+
lines[j] = `${kv[1]}version: ${to}${kv[3].trim().slice(from.length)}`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// ② packages / snapshots 的条目头,紧随其后的 resolution.integrity 一并改
|
|
82
|
+
const head = lines[i].match(headPattern);
|
|
83
|
+
if (head && head[2] === from) {
|
|
84
|
+
lines[i] = `${head[1]}${PLUGIN_NAME}@${to}:${head[3]}`;
|
|
85
|
+
for (let j = i + 1; j < lines.length; j += 1) {
|
|
86
|
+
if (!lines[j].trim()) continue;
|
|
87
|
+
if (!/^\s{4}/.test(lines[j])) break;
|
|
88
|
+
if (integrity && /^\s*resolution:/.test(lines[j])) {
|
|
89
|
+
lines[j] = lines[j].replace(/integrity:\s*[^,}]+/, `integrity: ${integrity}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return lines.join('\n');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 自更新后同步宿主配置档里的「版本钉」。
|
|
99
|
+
*
|
|
100
|
+
* 为什么必须做:安装目录是宿主包管理器(pnpm)托管的,它的 package.json
|
|
101
|
+
* dependencies 与 pnpm-lock.yaml 都钉着具体版本。只替换 node_modules 里的文件,
|
|
102
|
+
* 下一次 `pnpm install`(插件开关、配置档修复都会触发)就按旧钉重装,自更新被静默
|
|
103
|
+
* 回滚——在 macOS 上表现为「刚修好的自动安装又坏了」。所以这里把 specifier 与
|
|
104
|
+
* 锁文件三处一起改到新版本,并用下载到的 tarball 现算 integrity,避免锁文件校验不过。
|
|
105
|
+
*
|
|
106
|
+
* 只在「确实装在 node_modules 里」时动手:开发态直接跑仓库目录,上层不是配置档。
|
|
107
|
+
*/
|
|
108
|
+
export function syncProfilePin({ pluginRoot, version, tarball, log = () => {} }) {
|
|
109
|
+
if (path.basename(pluginRoot) !== PLUGIN_NAME) return { synced: false, reason: 'not-plugin-root', path: null };
|
|
110
|
+
const modulesDir = path.dirname(pluginRoot);
|
|
111
|
+
if (path.basename(modulesDir) !== 'node_modules') return { synced: false, reason: 'not-in-node-modules', path: null };
|
|
112
|
+
const profileRoot = path.dirname(modulesDir);
|
|
113
|
+
const manifestPath = path.join(profileRoot, 'package.json');
|
|
114
|
+
if (!fs.existsSync(manifestPath)) return { synced: false, reason: 'no-profile-manifest', path: null };
|
|
115
|
+
|
|
116
|
+
let manifest = null;
|
|
117
|
+
try {
|
|
118
|
+
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
119
|
+
} catch {
|
|
120
|
+
return { synced: false, reason: 'bad-profile-manifest', path: null };
|
|
121
|
+
}
|
|
122
|
+
const deps = manifest?.dependencies;
|
|
123
|
+
if (!deps || !Object.prototype.hasOwnProperty.call(deps, PLUGIN_NAME)) {
|
|
124
|
+
return { synced: false, reason: 'not-a-dependency', path: null };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const old = String(deps[PLUGIN_NAME]);
|
|
128
|
+
// 保留用户/宿主原本的区间写法(^ 或 ~),只换版本号
|
|
129
|
+
const next = /^[\^~]/.test(old) ? `${old[0]}${version}` : version;
|
|
130
|
+
let changed = false;
|
|
131
|
+
if (old !== next) {
|
|
132
|
+
deps[PLUGIN_NAME] = next;
|
|
133
|
+
fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
134
|
+
changed = true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const lockPath = path.join(profileRoot, 'pnpm-lock.yaml');
|
|
138
|
+
if (fs.existsSync(lockPath)) {
|
|
139
|
+
const integrity = `sha512-${createHash('sha512').update(tarball).digest('base64')}`;
|
|
140
|
+
const before = fs.readFileSync(lockPath, 'utf8');
|
|
141
|
+
const after = rewriteLock(before, {
|
|
142
|
+
from: old.replace(/^[\^~]/, ''),
|
|
143
|
+
to: version,
|
|
144
|
+
specifier: next,
|
|
145
|
+
integrity,
|
|
146
|
+
});
|
|
147
|
+
if (after !== before) {
|
|
148
|
+
fs.writeFileSync(lockPath, after);
|
|
149
|
+
log(`配置档锁文件已同步 ${PLUGIN_NAME}@${version}`);
|
|
150
|
+
changed = true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { synced: changed, reason: changed ? 'ok' : 'already-current', path: manifestPath };
|
|
154
|
+
}
|
|
155
|
+
|
|
58
156
|
/** 取 registry 元数据:目标版本(缺省 latest)的 tarball 地址与 shasum。 */
|
|
59
157
|
async function loadMetadata(registry, { name = PLUGIN_NAME, target = null, fetchImpl }) {
|
|
60
158
|
const res = await fetchImpl(`${registry}/${name}`, {
|
|
@@ -153,8 +251,21 @@ export async function selfUpdatePlugin({
|
|
|
153
251
|
const now = readVersion(pluginRoot);
|
|
154
252
|
if (now !== meta.version) throw new Error(`覆盖后版本仍为 ${now},更新未生效`);
|
|
155
253
|
|
|
254
|
+
// 同步配置档版本钉:不同步的话,下次宿主 install 会按旧钉把自更新悄悄回滚。
|
|
255
|
+
// 这一步失败不影响本次更新(磁盘上已是新版),只记录原因。
|
|
256
|
+
let pin = { synced: false, reason: 'skipped' };
|
|
257
|
+
try {
|
|
258
|
+
pin = syncProfilePin({ pluginRoot, version: meta.version, tarball: buf, log });
|
|
259
|
+
if (pin.synced) log(`配置档版本钉同步到 ${meta.version}`);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
log(`配置档版本钉同步失败(${err?.message || err}):下次宿主安装可能装回旧版`);
|
|
262
|
+
}
|
|
263
|
+
|
|
156
264
|
log(`plugin 已更新 ${from || '?'} → ${meta.version}`);
|
|
157
|
-
return {
|
|
265
|
+
return {
|
|
266
|
+
ok: true, from, to: meta.version, changed: true, root: pluginRoot, backup,
|
|
267
|
+
restartRequired: true, pinSynced: pin.synced,
|
|
268
|
+
};
|
|
158
269
|
} finally {
|
|
159
270
|
try { fs.rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* 临时目录清理失败不影响结果 */ }
|
|
160
271
|
}
|