31 lines
1.9 KiB
JavaScript
31 lines
1.9 KiB
JavaScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { listFiles, sha256 } from './tang-detective-native-plugin.mjs'
|
|
|
|
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
|
const sourceDirectory = process.argv[2]
|
|
if (!sourceDirectory) throw new Error('Usage: node build/import-tang-detective-native.mjs <source-miniprogram-directory>')
|
|
const targetDirectory = path.join(projectRoot, 'native/tang-detective')
|
|
const allowedRootFiles = new Set(['app.json', 'app.wxss'])
|
|
const allowedDirectories = /^(?:assets|data|utils|pages|package-[a-z0-9-]+)\//
|
|
const selectedFiles = listFiles(sourceDirectory).filter(relative => allowedRootFiles.has(relative) || allowedDirectories.test(relative))
|
|
if (!selectedFiles.includes('app.json') || !selectedFiles.includes('pages/home/home.js')) throw new Error('Source is not the expected native Tang Detective program')
|
|
const files = selectedFiles.map(relative => {
|
|
const bytes = fs.readFileSync(path.join(sourceDirectory, relative))
|
|
const destination = path.join(targetDirectory, relative)
|
|
const hash = sha256(bytes)
|
|
if (fs.existsSync(destination) && sha256(fs.readFileSync(destination)) !== hash) {
|
|
throw new Error(`Existing snapshot differs; refusing to overwrite: ${relative}`)
|
|
}
|
|
return { path: relative, bytes: bytes.length, sha256: hash }
|
|
})
|
|
for (const file of files) {
|
|
const destination = path.join(targetDirectory, file.path)
|
|
fs.mkdirSync(path.dirname(destination), { recursive: true })
|
|
fs.copyFileSync(path.join(sourceDirectory, file.path), destination)
|
|
}
|
|
const manifest = { version: 1, excludedRootFiles: ['app.js', 'sitemap.json'], files }
|
|
fs.writeFileSync(path.join(projectRoot, 'build/tang-detective-source-manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`)
|
|
console.log(`Imported ${files.length} byte-preserved native files; no source App or project configuration was imported.`)
|