ituoke/scripts/stage-mobile-runtime.mjs

27 lines
2.0 KiB
JavaScript

// Build machine only; the installed app uses its private Node and locked packages.
import { cp, mkdir, readFile, writeFile, stat } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { spawnSync } from 'node:child_process'
const root = fileURLToPath(new URL('../', import.meta.url))
const source = path.join(root, 'runtime/mobile')
const dest = path.join(root, 'third_party/mcp-mobile')
if (!['darwin','win32'].includes(process.platform)) throw Error('Prepare on the target Mac or Windows build machine')
if (!process.argv.includes('--check')) {
if (!process.env.npm_execpath) throw Error('Run npm --prefix runtime/mobile run stage')
const result = spawnSync(process.execPath, [process.env.npm_execpath,'ci','--omit=dev','--ignore-scripts','--no-audit','--no-fund'], {cwd:source,stdio:'inherit'})
if (result.status !== 0) throw Error('Cannot install the locked Mobile Next runtime')
await mkdir(dest,{recursive:true})
// Match npm's platform-specific optional packages; Node is shared with Filesystem.
await cp(path.join(source,'node_modules'),path.join(dest,'node_modules'),{recursive:true,dereference:true})
await cp(path.join(source,'package-lock.json'),path.join(dest,'package-lock.json'))
await cp(path.join(source,'server.cjs'),path.join(dest,'server.cjs'))
const upstream=JSON.parse(await readFile(path.join(source,'node_modules/@mobilenext/mobile-mcp/package.json'),'utf8'))
if(upstream.version!=='1.0.16')throw Error('Unexpected Mobile Next version')
await writeFile(path.join(dest,'runtime.json'),JSON.stringify({platform:process.platform,arch:process.arch,mobile:'1.0.16',androidMode:'adb'},null,2))
}
const manifest=JSON.parse(await readFile(path.join(dest,'runtime.json'),'utf8'))
if(manifest.platform!==process.platform || manifest.arch!==process.arch || manifest.mobile!=='1.0.16')throw Error('Mobile runtime platform/version mismatch')
await stat(path.join(dest,'node_modules/@mobilenext/mobile-mcp/lib/index.js'))
console.log(`Mobile Next MCP 1.0.16 staged: ${manifest.platform}/${manifest.arch}`)