Commit 569cde5b by liimin

fix(scripts): 修复生成器语法并完善构建 npm 体检

sync-mp-apps.mjs 被一次性迁移脚本打断的多行字符串语法错误已修复,并同步 5 个 lane 的 src/index.ts 注释;eslint.config.js 补充忽略 **/miniprogram_dist/**(构建产物,避免本地构建后 lint 必挂);mp-npm-core.mjs 新增内容新鲜度体检:仅看文件存在会漏报旧 bundle(缺 createClient 导致运行时 undefined),现按内容比对并中性提示待构建。
1 parent 1ad2227f
Pipeline #386 for 569cde5b failed in 0 seconds
......@@ -14,6 +14,9 @@ export default [
// 工具链只查各 lane 的 src/,lane 自己的 node --test 门禁在 lane 内跑。
ignores: [
'**/dist/**',
// api-core 的 tsup 还会产出 miniprogram_dist/index.js(小程序「构建 npm」用的
// 单文件 bundle),同样是生成物且已被 .gitignore 忽略,不能进 lint 口径。
'**/miniprogram_dist/**',
'**/node_modules/**',
'**/.turbo/**',
'**/coverage/**',
......
......@@ -297,6 +297,19 @@ function collectRelations(laneDir, projectConfig) {
return [{ pkgJsonPath: join(laneDir, 'package.json'), distDir: laneDir }];
}
/**
* 「构建 npm」会把 miniprogram_dist/index.js 原样拷进 miniprogram_npm/@uj/api-core/index.js。
* 只看「文件存在」会把旧 bundle 也判成就位 —— 主入口导出面一变(如新增 createClient),
* 旧包在运行时就是 undefined。按内容比对(换行归一)识别过期,给中性提示而非报错。
*/
function apiCoreBundleFresh(target) {
const src = join(API_CORE_DIR, 'miniprogram_dist', 'index.js');
const dst = join(target, '@uj/api-core', 'index.js');
if (!existsSync(src) || !existsSync(dst)) return undefined;
const norm = (p) => readFileSync(p, 'utf8').replace(/\r\n/g, '\n');
return norm(src) === norm(dst);
}
function checkRelation(rel) {
const pkg = readJson(rel.pkgJsonPath);
if (!pkg) return { error: `读不到 ${rel.pkgJsonPath}`, deps: [], missing: [], broken: [] };
......@@ -309,7 +322,11 @@ function checkRelation(rel) {
if (deps.includes('@uj/api-core') && !existsSync(join(target, '@uj/api-core', 'index.js'))) {
if (!missing.includes('@uj/api-core')) broken.push('@uj/api-core 缺 index.js');
}
return { deps, missing, broken, target, pkgPath: rel.pkgJsonPath };
const stale = [];
if (deps.includes('@uj/api-core') && apiCoreBundleFresh(target) === false) {
stale.push('@uj/api-core 产物不是最新(主入口已更新)');
}
return { deps, missing, broken, stale, target, pkgPath: rel.pkgJsonPath };
}
export function inspectLane(lane) {
......@@ -323,14 +340,16 @@ export function inspectLane(lane) {
return { label, ...checkRelation(rel) };
});
const gap = rows.flatMap((r) => [...(r.missing || []), ...(r.broken || [])]);
const stale = rows.flatMap((r) => r.stale || []);
const hasTarget = rows.some((r) => r.target && existsSync(r.target));
return {
lane,
laneDir,
rows,
gap,
stale,
hasTarget,
built: hasTarget && gap.length === 0,
built: hasTarget && gap.length === 0 && stale.length === 0,
autoMode: config?.setting?.packNpmManually !== true,
nodeModulesOff: config?.setting?.nodeModules === false,
};
......@@ -358,6 +377,7 @@ function printLaneReport(r) {
} else {
bad(`${row.label}: ${total - row.missing.length}/${total} 包就位,缺 ${gap.join(', ')}`);
}
if (row.stale?.length) warn(`${row.label}: ${row.stale.join('、')}`);
}
if (r.nodeModulesOff) {
warn('project.config.json 里 nodeModules: false(旧开关),工具可能不启用 npm 模块');
......@@ -416,6 +436,7 @@ export async function runLanes(lanes, opts = parseArgs()) {
const pending = report.filter((r) => !r.built);
const ready = report.filter((r) => r.built);
const staleLanes = report.filter((r) => r.stale?.length);
const build = res.build || { mode: 'skipped' };
console.log(`\n${paint('── 结果 ──', C.bold)}`);
......@@ -426,7 +447,17 @@ export async function runLanes(lanes, opts = parseArgs()) {
// 「工具 → 构建 npm」是微信小程序的默认手工动作,不是异常,不必报警为白屏风险
console.log(` ${paint('待手动构建 npm', C.yellow)}: ${pending.map((r) => r.lane).join(', ')}`);
} else {
console.log(` ${paint('待构建', C.yellow)}: ${pending.map((r) => r.lane).join(', ')}`);
// 缺包 vs 产物过期分开列:前者要装依赖,后者只需工具重跑一次「构建 npm」。
const needBuild = pending.filter((r) => r.gap.length);
const needManual = pending.filter((r) => !r.gap.length);
if (needBuild.length) {
console.log(` ${paint('待构建', C.yellow)}: ${needBuild.map((r) => r.lane).join(', ')}`);
}
if (needManual.length) {
console.log(
` ${paint('待手动构建 npm', C.yellow)}: ${needManual.map((r) => r.lane).join(', ')} (api-core 产物已更新)`,
);
}
}
}
......@@ -443,10 +474,12 @@ export async function runLanes(lanes, opts = parseArgs()) {
if (!sub || res[`sub:${p.lane}`] !== false) continue;
console.log(` ${p.lane} 子包依赖缺失:${paint(sub.fixHint, C.cyan)}`);
}
if (build.mode === 'manual') {
console.log(`\n ${paint('构建 npm 需要开发者工具:', C.yellow)}${build.reason}`);
if (build.mode === 'manual' || staleLanes.length) {
const reason = build.reason ?? 'api-core 产物已更新,本地 miniprogram_npm 仍是旧 bundle';
console.log(`\n ${paint('构建 npm 需要开发者工具:', C.yellow)}${reason}`);
console.log(' 打开对应项目后执行「工具 → 构建 npm」,再重跑本命令校验:');
for (const p of pending) {
const needBuild = build.mode === 'manual' ? pending : staleLanes;
for (const p of needBuild) {
const label = targets.length === 1 ? p.laneDir : `${p.lane} (${p.laneDir})`;
console.log(` - ${label}`);
}
......
......@@ -87,7 +87,7 @@ function templateIndexTs(dir, displayName) {
L.push("import {");
L.push(" createRpcDispatcher,");
L.push(" createWxSender,");
L.push(" createSha1Signer,");
L.push(" createSigner,");
L.push(" registerGateway,");
L.push(" type GatewayStrategy,");
L.push(" type GatewayCredential,");
......@@ -104,7 +104,8 @@ function templateIndexTs(dir, displayName) {
L.push(' *');
L.push(' * 签名用 api-core 内置 SHA1 预设:原生小程序 RPC 网关走 SHA1(secret 前后包裹、');
L.push(' * appKey/format/method/v + params 全签、不带 timestamp);web points 子应用同属 glsx');
L.push(' * 家族、同样用 SHA1,但 timestamp 进原文,故走 createGlsxSha1Signer,两个预设别混用。');
L.push(' * 家族、同样用 SHA1,但 timestamp 进原文,故走 createSigner() 不传参(默认 glsx);');
L.push(" * 小程序侧必须显式 createSigner({ kind: 'rop' }),两种签名原文别混用。");
L.push(' *');
L.push(' * 注意:本文件在微信运行时被 import 时才会执行 createWxSender();');
L.push(' * 类型检查 / lint 不会执行这段代码,因此无需担心 Node 环境下 wx 缺失。');
......@@ -115,7 +116,7 @@ function templateIndexTs(dir, displayName) {
L.push(` router: 'rop-scrm-${dir}/router',`);
L.push(" tokenHeader: 'token',");
L.push(` // 原生小程序签名:SHA1 预设;换算法(HMAC / MD5)只改这一行。`);
L.push(` signer: createSha1Signer({ id: '${signerId}' }),`);
L.push(` signer: createSigner({ kind: 'rop', id: '${signerId}' }),`);
L.push(" // 网关级常量参数:并进签名与请求体(会话态 sessionId 等业务参数在调用时再传)。");
L.push(" fixedParams: { locale: 'zh_CN', source: 'dj-wechat' },");
L.push(' // 把对方信封归一成主壳拦截器认得的 { data } / { code, message }。');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!