// 执行脚本 taro build --type h5 ,并且把环境变量 TARO_API_URL 传递过去 const { execSync } = require('child_process'); const path = require('path'); const fs = require('fs-extra'); const TARO_API_URL = ''; // const TARO_MQTT_URL = 'ws://localhost:8083/mqtt'; const rootDir = path.join(__dirname, '..'); // 项目根目录 execSync(`taro build --type h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL, TARO_MQTT_URL } }, (error, stdout, stderr) => { if (error) { console.error(`Error executing command: ${error.message}`); return; } if (stderr) { console.error(`Command stderr: ${stderr}`); return; } console.log(`Command stdout: ${stdout}`); }); try { // Step 1: Copy static H5 resources from dist/h5 to .build/pkg/static const sourceDir = path.join(__dirname, '..', 'dist', 'h5'); const destDir = path.join(__dirname, 'pkg', 'static'); if (!fs.existsSync(destDir)) { fs.mkdirSync(destDir, { recursive: true }); } fs.copySync(sourceDir, destDir); console.log(`Copied static H5 resources from ${sourceDir} to ${destDir}`); // Step 2: Execute the pkg packaging command const pkgJsonPath = path.join(__dirname, 'pkg', 'pkg.json'); const entryPoint = path.join(__dirname, 'pkg', 'server.js'); // Specify the entry point const outputPath = path.join(__dirname, 'pkg'); // Specify the output path const pkgCommand = `npx pkg ${entryPoint} --config ${pkgJsonPath} --output ${outputPath}/dros_server`; try { execSync(pkgCommand, { stdio: 'inherit' }); console.log('Packaging completed successfully.'); } catch (error) { console.error('Error during packaging:', error); } } catch (err) { console.error('Failed to execute npm run pkg:', err.message); process.exit(1); }