mainPackageImageCdnMigration.test.mjs
5.02 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const rootDir = path.resolve(fileURLToPath(new URL('.', import.meta.url)), '..')
const imageExtensions = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.ico'])
const sourceExtensions = new Set(['.js', '.json', '.ts', '.wxml', '.wxs', '.wxss'])
const ignoredRoots = new Set([
'.cloudbase',
'.codex-tmp',
'.cursor',
'.git',
'cdn-upload',
'docs',
'miniprogram_npm',
'node_modules',
'outputs',
'scripts',
'tests'
])
const expectedLocalImages = [
'images/tab-date-active.png',
'images/tab-date.png',
'images/tab-home-active.png',
'images/tab-home.png',
'images/tab-mine-active.png',
'images/tab-mine.png',
'images/tab-order-active.png',
'images/tab-order.png',
'images/tab-repair-active.png',
'images/tab-repair.png'
]
function walkFiles(directory) {
return fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
const fullPath = path.join(directory, entry.name)
return entry.isDirectory() ? walkFiles(fullPath) : [fullPath]
})
}
function listMainPackageImages() {
const appConfig = JSON.parse(fs.readFileSync(path.join(rootDir, 'app.json'), 'utf8'))
const subpackageRoots = new Set(appConfig.subpackages.map(item => item.root))
return walkFiles(rootDir)
.map(file => path.relative(rootDir, file).split(path.sep).join('/'))
.filter(relativePath => imageExtensions.has(path.extname(relativePath).toLowerCase()))
.filter(relativePath => {
const topLevel = relativePath.split('/')[0]
return !ignoredRoots.has(topLevel) && !subpackageRoots.has(topLevel)
})
.sort()
}
function listDeletedMainPackageImageReferences() {
const appConfig = JSON.parse(fs.readFileSync(path.join(rootDir, 'app.json'), 'utf8'))
const subpackageRoots = new Set(appConfig.subpackages.map(item => item.root))
const uploadedMainPackageImages = new Set(
walkFiles(path.join(rootDir, 'cdn-upload'))
.map(file => path.relative(path.join(rootDir, 'cdn-upload'), file).split(path.sep).join('/'))
.filter(relativePath => imageExtensions.has(path.extname(relativePath).toLowerCase()))
.filter(relativePath => !subpackageRoots.has(relativePath.split('/')[0]))
)
const imageReference = /(?:["'(=]\s*|url\(\s*)(?<asset>(?:\/|\.\.?\/)[^"'()\s{}<>]+?\.(?:png|jpe?g|gif|webp|svg|ico))/gi
const references = []
for (const file of walkFiles(rootDir)) {
const sourceRelativePath = path.relative(rootDir, file).split(path.sep).join('/')
const sourceTopLevel = sourceRelativePath.split('/')[0]
if (ignoredRoots.has(sourceTopLevel) || !sourceExtensions.has(path.extname(file).toLowerCase())) {
continue
}
const source = fs.readFileSync(file, 'utf8')
for (const match of source.matchAll(imageReference)) {
const asset = match.groups.asset.split(/[?#]/, 1)[0]
const resolved = asset.startsWith('/')
? path.resolve(rootDir, `.${asset}`)
: path.resolve(path.dirname(file), asset)
const assetRelativePath = path.relative(rootDir, resolved).split(path.sep).join('/')
const assetTopLevel = assetRelativePath.split('/')[0]
if (
assetRelativePath.startsWith('../') ||
ignoredRoots.has(assetTopLevel) ||
subpackageRoots.has(assetTopLevel) ||
expectedLocalImages.includes(assetRelativePath) ||
!uploadedMainPackageImages.has(assetRelativePath)
) {
continue
}
references.push(`${sourceRelativePath}: ${asset}`)
}
}
return references.sort()
}
function listMissingWxsDependencies(sourceRelativePath) {
const sourcePath = path.join(rootDir, sourceRelativePath)
const source = fs.readFileSync(sourcePath, 'utf8')
const wxsReference = /<wxs\b[^>]*\bsrc=["'](?<dependency>[^"']+)["'][^>]*>/gi
return [...source.matchAll(wxsReference)]
.map(match => match.groups.dependency)
.filter(dependency => {
const resolved = dependency.startsWith('/')
? path.resolve(rootDir, `.${dependency}`)
: path.resolve(path.dirname(sourcePath), dependency)
return !fs.existsSync(resolved)
})
}
describe('main package image CDN migration', () => {
it('keeps only native TabBar images in the main package', () => {
assert.deepEqual(listMainPackageImages(), expectedLocalImages)
})
it('does not force deleted local images into the upload package', () => {
const projectConfig = JSON.parse(
fs.readFileSync(path.join(rootDir, 'project.config.json'), 'utf8')
)
const includedPaths = projectConfig.packOptions.include.map(item => item.value)
assert.ok(!includedPaths.includes('components/images/close.png'))
})
it('does not reference deleted main-package images through local paths', () => {
assert.deepEqual(listDeletedMainPackageImageReferences(), [])
})
it('keeps work-order index WXS dependencies resolvable', () => {
assert.deepEqual(
listMissingWxsDependencies('pages/tabWorkOrder/pages/index/index.wxml'),
[]
)
})
})