scan-component-paths.mjs
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const skipDirs = new Set(['node_modules', 'miniprogram_npm', '.git', 'dist'])
const exts = ['', '.js', '.json', '.wxml', '.wxss', '.sass', '/index.js']
function resolveComponent(fromJson, spec) {
if (!spec.startsWith('.')) return { ok: true }
let target = path.resolve(path.dirname(fromJson), spec)
if (path.extname(target)) return { ok: fs.existsSync(target), target }
for (const ext of exts) {
const p = target + (ext.startsWith('/') ? ext : ext)
if (fs.existsSync(p)) return { ok: true, target: p }
}
return { ok: false, target }
}
function walk(dir, files = []) {
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
if (skipDirs.has(e.name)) continue
const full = path.join(dir, e.name)
if (e.isDirectory()) walk(full, files)
else if (e.name.endsWith('.json') && !e.name.includes('project')) files.push(full)
}
return files
}
const broken = []
for (const file of walk(root)) {
let json
try {
json = JSON.parse(fs.readFileSync(file, 'utf8'))
} catch {
continue
}
const comps = json.usingComponents
if (!comps || typeof comps !== 'object') continue
for (const [name, spec] of Object.entries(comps)) {
if (typeof spec !== 'string' || !spec.startsWith('.')) continue
const { ok } = resolveComponent(file, spec)
if (!ok) {
broken.push({
page: path.relative(root, file).replace(/\\/g, '/'),
name,
spec
})
}
}
}
broken.sort((a, b) => a.page.localeCompare(b.page))
console.log(`BROKEN_COMPONENT_PATHS=${broken.length}`)
for (const b of broken) {
console.log(`${b.page} [${b.name}] -> ${b.spec}`)
}