const cmd = process.argv;
const path = require('path');
const ROOT = process.cwd();
const { uid, gid } = require('os').userInfo();
const SMALL_APP = path.join(ROOT, 'node_modules/bili-small-app');
// 判断平台
let isWin = /^win/.test(process.platform);

const spawnSync = require('child_process').spawnSync;

// console.log('[biliSapp - info] 启动编译（若编译速度缓慢，请将「npm」源切换到国内的源或者公司源）');

const _path = process.cwd();
if (/ /.test(_path)) {
    // console.log(`[biliSapp - error] 检测到当前项目目录名称（${_path}）存在异常字符，请将项目调整后再启动'`);
    process.exit(1);
}

// 检查用户是否忽略工具判断
const _ignore_tool_check = process.argv.indexOf('ignore_tool_check') > -1;

// 命令基础参数
let _baseCom = {
        stdio: [process.stdin, process.stdout, process.stderr],
        cwd: process.cwd(),
        shell: process.env.SHELL,
    }
    // mac 追加
if (!isWin) {
    _baseCom.uid = uid;
    _baseCom.gid = gid;
}

// 子进程处理工具检查
if (!_ignore_tool_check) {
    let _updateNpm = ['node', [path.join(SMALL_APP, 'build/build.update.js'), ...cmd.slice(2)]];
    spawnSync(..._updateNpm, _baseCom);
} else {
    // console.log('[biliSapp - info] 已跳过编译工具版本校验');
}

// 子进程处理小程序编译
let _buildApp = ['node', ['--no-warnings', path.join(SMALL_APP, 'build/build.core.js'), ...cmd.slice(2)]];
spawnSync(..._buildApp, _baseCom);
