openprototype 1.6.0 → 1.7.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/bin/cli.js +138 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -111,6 +111,75 @@ const ONBOARDING_BODY = [
|
|
|
111
111
|
'<script src="/_kit/shell/onboarding.js"></script>',
|
|
112
112
|
'<!-- onboarding:end -->'
|
|
113
113
|
].join('\n');
|
|
114
|
+
const LAN_ADDRESS_HELPER_MARK = 'async function loadLanAddress()';
|
|
115
|
+
const LAN_ADDRESS_HELPER = `async function loadLanAddress() {
|
|
116
|
+
const parts = window.location.pathname.split('/').filter(Boolean);
|
|
117
|
+
const productAt = parts.indexOf('product');
|
|
118
|
+
const productId = productAt >= 0 ? decodeURIComponent(parts[productAt + 1] || '') : '';
|
|
119
|
+
const surface = productAt >= 0 ? decodeURIComponent(parts[productAt + 2] || '') : '';
|
|
120
|
+
const params = new URLSearchParams({ productId, surface });
|
|
121
|
+
const res = await fetch('/api/lan-address?' + params.toString(), { cache: 'no-store' });
|
|
122
|
+
const data = await res.json().catch(() => ({}));
|
|
123
|
+
if (!res.ok || !data.ok) throw new Error(data.error || '局域网地址读取失败');
|
|
124
|
+
return String(data.lanAddress || '').trim();
|
|
125
|
+
}`;
|
|
126
|
+
const LAN_ADDRESS_CLICK = ` document.getElementById('copyAddressBtn').addEventListener('click', async () => {
|
|
127
|
+
try {
|
|
128
|
+
const lanAddress = await loadLanAddress();
|
|
129
|
+
if (!lanAddress) {
|
|
130
|
+
showToast('请先在 proto-kit.config.json 配置该产品当前端的 lanAddress');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
copyText(lanAddress, '局域网地址已复制到剪贴板');
|
|
134
|
+
} catch (err) {
|
|
135
|
+
showToast(err.message || '局域网地址读取失败');
|
|
136
|
+
}
|
|
137
|
+
});`;
|
|
138
|
+
const LEGACY_LAN_ADDRESS_MARK = 'openprototype:lan-address:start';
|
|
139
|
+
const LEGACY_LAN_ADDRESS_BLOCK = `<!-- openprototype:lan-address:start -->
|
|
140
|
+
<script>
|
|
141
|
+
(() => {
|
|
142
|
+
const button = document.getElementById('prototypeCopyAddress');
|
|
143
|
+
const toast = document.getElementById('prototypeToast');
|
|
144
|
+
if (!button || !toast) return;
|
|
145
|
+
const showToast = message => {
|
|
146
|
+
toast.textContent = message;
|
|
147
|
+
toast.classList.add('is-visible');
|
|
148
|
+
window.setTimeout(() => toast.classList.remove('is-visible'), 1800);
|
|
149
|
+
};
|
|
150
|
+
button.addEventListener('click', async event => {
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
event.stopImmediatePropagation();
|
|
153
|
+
try {
|
|
154
|
+
const parts = window.location.pathname.split('/').filter(Boolean);
|
|
155
|
+
const productAt = parts.indexOf('product');
|
|
156
|
+
const params = new URLSearchParams({
|
|
157
|
+
productId: productAt >= 0 ? decodeURIComponent(parts[productAt + 1] || '') : '',
|
|
158
|
+
surface: productAt >= 0 ? decodeURIComponent(parts[productAt + 2] || '') : ''
|
|
159
|
+
});
|
|
160
|
+
const res = await fetch('/api/lan-address?' + params.toString(), { cache: 'no-store' });
|
|
161
|
+
const data = await res.json().catch(() => ({}));
|
|
162
|
+
if (!res.ok || !data.ok) throw new Error(data.error || '局域网地址读取失败');
|
|
163
|
+
const lanAddress = String(data.lanAddress || '').trim();
|
|
164
|
+
if (!lanAddress) throw new Error('请先在 proto-kit.config.json 配置该产品当前端的 lanAddress');
|
|
165
|
+
try {
|
|
166
|
+
await navigator.clipboard.writeText(lanAddress);
|
|
167
|
+
} catch {
|
|
168
|
+
const textarea = document.createElement('textarea');
|
|
169
|
+
textarea.value = lanAddress;
|
|
170
|
+
document.body.appendChild(textarea);
|
|
171
|
+
textarea.select();
|
|
172
|
+
document.execCommand('copy');
|
|
173
|
+
textarea.remove();
|
|
174
|
+
}
|
|
175
|
+
showToast('局域网地址已复制到剪贴板');
|
|
176
|
+
} catch (err) {
|
|
177
|
+
showToast(err.message || '局域网地址读取失败');
|
|
178
|
+
}
|
|
179
|
+
}, true);
|
|
180
|
+
})();
|
|
181
|
+
</script>
|
|
182
|
+
<!-- openprototype:lan-address:end -->`;
|
|
114
183
|
|
|
115
184
|
/** 列出 product/<id>/<root>/index.html —— 已生成的产品壳 */
|
|
116
185
|
function listProductShells(projectRoot) {
|
|
@@ -161,6 +230,74 @@ function patchShellOnboarding(projectRoot) {
|
|
|
161
230
|
return patched;
|
|
162
231
|
}
|
|
163
232
|
|
|
233
|
+
/**
|
|
234
|
+
* 把旧产品壳的“复制地址”迁移到运行时 /api/lan-address。
|
|
235
|
+
* 只识别脚手架的 copyAddressBtn 和旧导航的 prototypeCopyAddress;其他定制壳不动。
|
|
236
|
+
*/
|
|
237
|
+
function patchShellLanAddress(projectRoot) {
|
|
238
|
+
let patched = 0;
|
|
239
|
+
const oldClick = / document\.getElementById\('copyAddressBtn'\)\.addEventListener\('click', \(\) => \{\s*copyText\((?:window\.location\.href|NAVIGATOR_ADDRESS),\s*'[^']*'\);\s*\}\);/;
|
|
240
|
+
const initAnchor = "window.addEventListener('DOMContentLoaded', async () => {";
|
|
241
|
+
|
|
242
|
+
for (const shell of listProductShells(projectRoot)) {
|
|
243
|
+
const rel = path.relative(projectRoot, shell);
|
|
244
|
+
let html = fs.readFileSync(shell, 'utf8');
|
|
245
|
+
|
|
246
|
+
if (/id=(['"])prototypeCopyAddress\1/.test(html)) {
|
|
247
|
+
if (html.includes(LEGACY_LAN_ADDRESS_MARK)) {
|
|
248
|
+
info(C.dim(' 跳过(已有局域网复制): ') + rel);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const legacyScript = /<script\b[^>]*\bsrc=(['"])\/scripts\/prototype-agent\/prototype-navigator\.js[^'"]*\1[^>]*><\/script>/i;
|
|
252
|
+
const legacyMatch = html.match(legacyScript);
|
|
253
|
+
if (!legacyMatch) {
|
|
254
|
+
info(C.dim(' 跳过(旧导航结构不认识): ') + rel);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
html = html.replace(
|
|
258
|
+
/(<button\b[^>]*\bid=(['"])prototypeCopyAddress\2[^>]*>)[\s\S]*?(<\/button>)/i,
|
|
259
|
+
'$1复制局域网地址$3'
|
|
260
|
+
);
|
|
261
|
+
html = html.replace(legacyMatch[0], legacyMatch[0] + '\n' + LEGACY_LAN_ADDRESS_BLOCK);
|
|
262
|
+
fs.writeFileSync(shell, html);
|
|
263
|
+
ok('已迁移局域网复制功能 ' + rel);
|
|
264
|
+
patched++;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (!/id=(['"])copyAddressBtn\1/.test(html)) continue;
|
|
269
|
+
|
|
270
|
+
const nextLabel = html.replace(
|
|
271
|
+
/(<button\b[^>]*\bid=(['"])copyAddressBtn\2[^>]*>)[\s\S]*?(<\/button>)/i,
|
|
272
|
+
'$1复制局域网地址$3'
|
|
273
|
+
);
|
|
274
|
+
if (html.includes(LAN_ADDRESS_HELPER_MARK)) {
|
|
275
|
+
if (nextLabel !== html) {
|
|
276
|
+
fs.writeFileSync(shell, nextLabel);
|
|
277
|
+
ok('已更新局域网复制文案 ' + rel);
|
|
278
|
+
patched++;
|
|
279
|
+
} else {
|
|
280
|
+
info(C.dim(' 跳过(已有局域网复制): ') + rel);
|
|
281
|
+
}
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (!oldClick.test(html) || !html.includes(initAnchor)) {
|
|
285
|
+
info(C.dim(' 跳过(复制逻辑不是已知旧版本): ') + rel);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
html = nextLabel.replace(initAnchor, LAN_ADDRESS_HELPER + '\n\n' + initAnchor);
|
|
290
|
+
html = html.replace(oldClick, LAN_ADDRESS_CLICK);
|
|
291
|
+
if (!html.includes('copyText(NAVIGATOR_ADDRESS')) {
|
|
292
|
+
html = html.replace(/^\s*const NAVIGATOR_ADDRESS = [^\n]+;\n/m, '');
|
|
293
|
+
}
|
|
294
|
+
fs.writeFileSync(shell, html);
|
|
295
|
+
ok('已迁移局域网复制功能 ' + rel);
|
|
296
|
+
patched++;
|
|
297
|
+
}
|
|
298
|
+
return patched;
|
|
299
|
+
}
|
|
300
|
+
|
|
164
301
|
/** 新项目 package.json 里本框架的依赖版本(已发布 npm,语义化版本管理) */
|
|
165
302
|
function kitDependencySpec() {
|
|
166
303
|
const pkg = readJson(path.join(PKG_ROOT, 'package.json'), {});
|
|
@@ -243,6 +380,7 @@ async function cmdInit() {
|
|
|
243
380
|
} else info(C.dim(' 跳过(已存在): ') + CONFIG_FILENAME);
|
|
244
381
|
|
|
245
382
|
patchShellOnboarding(root);
|
|
383
|
+
patchShellLanAddress(root);
|
|
246
384
|
|
|
247
385
|
// 合并 package.json 脚本(不覆盖用户已有脚本)
|
|
248
386
|
const pkgPath = path.join(root, 'package.json');
|