84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const PROJECT_ROOT = process.cwd()
|
|
const IGNORED_SEGMENTS = ['node_modules', 'miniprogram_npm', 'coverage', '.worktrees']
|
|
|
|
function shouldIgnore(filePath) {
|
|
return IGNORED_SEGMENTS.some(segment => filePath.includes(`${path.sep}${segment}${path.sep}`))
|
|
}
|
|
|
|
function getProjectFiles(extension) {
|
|
const pendingDirs = [PROJECT_ROOT]
|
|
const collectedFiles = []
|
|
|
|
while (pendingDirs.length) {
|
|
const currentDir = pendingDirs.pop()
|
|
const entries = fs.readdirSync(currentDir, { withFileTypes: true })
|
|
|
|
entries.forEach(entry => {
|
|
const absolutePath = path.join(currentDir, entry.name)
|
|
|
|
if (shouldIgnore(absolutePath)) {
|
|
return
|
|
}
|
|
|
|
if (entry.isDirectory()) {
|
|
pendingDirs.push(absolutePath)
|
|
return
|
|
}
|
|
|
|
if (entry.isFile() && absolutePath.endsWith(extension)) {
|
|
collectedFiles.push(absolutePath)
|
|
}
|
|
})
|
|
}
|
|
|
|
return collectedFiles
|
|
}
|
|
|
|
function getDirectoryImportViolations() {
|
|
const requirePattern = /require\((['"])(\.[^'"]+)\1\)/g
|
|
|
|
return getProjectFiles('.js').flatMap(filePath => {
|
|
const source = fs.readFileSync(filePath, 'utf8')
|
|
const violations = []
|
|
let match = requirePattern.exec(source)
|
|
|
|
while (match) {
|
|
const specifier = match[2]
|
|
const targetPath = path.resolve(path.dirname(filePath), specifier)
|
|
|
|
if (fs.existsSync(targetPath) && fs.statSync(targetPath).isDirectory()) {
|
|
violations.push({
|
|
filePath: path.relative(PROJECT_ROOT, filePath),
|
|
specifier
|
|
})
|
|
}
|
|
|
|
match = requirePattern.exec(source)
|
|
}
|
|
|
|
return violations
|
|
})
|
|
}
|
|
|
|
function getImportTarget(wxssPath, importPath) {
|
|
return path.resolve(path.dirname(wxssPath), importPath)
|
|
}
|
|
|
|
describe('miniprogram compatibility', () => {
|
|
test('avoids directory-level require paths that the miniprogram runtime cannot resolve reliably', () => {
|
|
expect(getDirectoryImportViolations()).toEqual([])
|
|
})
|
|
|
|
test('app.wxss imports the generated TDesign style entry that exists in the project', () => {
|
|
const appWxssPath = path.join(PROJECT_ROOT, 'app.wxss')
|
|
const appWxss = fs.readFileSync(appWxssPath, 'utf8')
|
|
const match = appWxss.match(/@import ['"]([^'"]+)['"];/)
|
|
|
|
expect(match).not.toBeNull()
|
|
expect(fs.existsSync(getImportTarget(appWxssPath, match[1]))).toBe(true)
|
|
})
|
|
})
|