const project = require('../../project')
const shell = require('shelljs')

/*
* 登录淘宝源npm
*/
async function loginTb() {
    console.log("敬请期待")
}

/*
* 登录官方源npm
*/
async function loginOrgNpm() {
    console.log("敬请期待")
}

/*
* 登录VV npm
*/
async function loginVV() {
    await login("xupd", "124487822", "dailiangjin@vv.cn")
}

/*
* 登录微微npm
*/
async function login(name, pwd, ema) {
    var username = name;
    var password = pwd;
    var email = ema;
    var inputArray = [
        username + "\n",
        password + "\n",
        email + "\n",
    ]

    var child = shell.exec('npm login', { async: true })
    child.stdout.on('data', (chunk) => {
        var cmd = inputArray.shift();
        if (cmd) {
            // shell.echo("input " + cmd);
            child.stdin.write(cmd);
        } else {
            child.stdin.end();
        }
    })
}

async function npmlogin(options) {
    try {
        const type = options.type
        switch (type && type.toLowerCase()) {
            case 'tb':
                await loginTb()
                break
            case 'org':
                await loginOrgNpm()
                break
            case 'vv':
                await loginVV()
                break
            default:
                console.fatal('Invalid type: ' + type)
        }
        console.ok('npm登录成功')
    } catch (e) {
        console.fatal(`npm登录失败:`, e)
    }
}

module.exports = { npmlogin }