Files
lehuo/apps/miniprogram/scripts/check-runtime-imports.mjs
T

30 lines
1018 B
JavaScript
Raw Normal View History

import { readFileSync, readdirSync, statSync } from 'node:fs';
import { join, relative } from 'node:path';
const root = new URL('../miniprogram/', import.meta.url);
const sourceRoot = decodeURIComponent(root.pathname).replace(/^\/(?:([A-Za-z]:))/, '$1');
const failures = [];
function walk(directory) {
for (const name of readdirSync(directory)) {
const path = join(directory, name);
if (statSync(path).isDirectory()) walk(path);
else if (path.endsWith('.ts') && !path.endsWith('.spec.ts')) inspect(path);
}
}
function inspect(path) {
const source = readFileSync(path, 'utf8');
const imports = source.matchAll(
/import\s+(?!type\b)[\s\S]*?from\s+['"](@lehuo\/contracts)['"];?/g,
);
for (const match of imports) failures.push(relative(sourceRoot, path));
}
walk(sourceRoot);
if (failures.length) {
console.error(`小程序存在未打包的运行时 workspace 依赖:\n${[...new Set(failures)].join('\n')}`);
process.exit(1);
}
console.log('小程序运行时依赖检查通过');