import { cpSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs' import path from 'node:path' import { spawnSync } from 'node:child_process' import { fileURLToPath } from 'node:url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const rootDir = path.resolve(__dirname, '..') const distDir = path.join(rootDir, 'dist', 'ios') const tempArtifactsDir = path.join(distDir, '.artifacts') const packageDir = path.join(distDir, 'DuooomiBleWrapper') const frameworksDir = path.join(packageDir, 'Frameworks') const wrapperTemplateDir = path.join(rootDir, 'wrappers', 'ios') function run(command, args) { const result = spawnSync(command, args, { cwd: rootDir, shell: process.platform === 'win32', stdio: 'inherit', }) if (result.error) { throw result.error } if (result.status !== 0) { process.exit(result.status ?? 1) } } function findDirectoryByName(dir, expectedName) { for (const entry of readdirSync(dir, { withFileTypes: true })) { const fullPath = path.join(dir, entry.name) if (entry.isDirectory() && entry.name === expectedName) { return fullPath } } return null } rmSync(distDir, { recursive: true, force: true }) mkdirSync(frameworksDir, { recursive: true }) run('pnpx', ['expo-brownfield', 'build:ios', '--release', '--artifacts', tempArtifactsDir]) const sdkFramework = findDirectoryByName(tempArtifactsDir, 'DuooomiBleSDK.xcframework') const hermesFramework = findDirectoryByName(tempArtifactsDir, 'hermesvm.xcframework') if (!sdkFramework) { throw new Error(`Missing iOS artifact: ${path.join(tempArtifactsDir, 'DuooomiBleSDK.xcframework')}`) } if (!hermesFramework) { throw new Error(`Missing iOS artifact: ${path.join(tempArtifactsDir, 'hermesvm.xcframework')}`) } cpSync(wrapperTemplateDir, packageDir, { recursive: true }) cpSync(sdkFramework, path.join(frameworksDir, 'DuooomiBleSDK.xcframework'), { recursive: true }) cpSync(hermesFramework, path.join(frameworksDir, 'hermesvm.xcframework'), { recursive: true }) rmSync(tempArtifactsDir, { recursive: true, force: true }) if (!existsSync(path.join(packageDir, 'Package.swift'))) { throw new Error('Swift wrapper package is incomplete: missing Package.swift') } console.log(`iOS wrapper package ready: ${packageDir}`)