{"size": 1379, "ext": "js", "max_stars_repo_path": "scripts/utils.js", "max_stars_repo_name": "whatsmk/mk-command", "max_stars_repo_stars_event_min_datetime": "2018-08-19T15:33:56.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-19T15:33:56.000Z", "max_issues_repo_path": "scripts/utils.js", "max_issues_repo_name": "whatsmk/mk-command", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "scripts/utils.js", "max_forks_repo_name": "whatsmk/mk-command", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.775862069, "max_line_length": 67, "alphanum_fraction": 0.5141406817}
const execSync = require('child_process').execSync;
const dns = require('dns');
const chalk = require('chalk');
const spawn = require('react-dev-utils/crossSpawn');

async function yarn(args, root){
    let command;
    let isOnline = await checkIfOnline()

    command = 'yarnpkg';
    if (!isOnline) {
        args.push('--offline');
    }
    args.push('--cwd');
    args.push(root);

    if (!isOnline) {
        console.log(chalk.yellow('请联网.'));
        console.log();
    }
    spawn.sync(command, args, { stdio: 'inherit' });
}

function getProxy() {
    if (process.env.https_proxy) {
        return process.env.https_proxy;
    } else {
        try {
            let httpsProxy = execSync('npm config get https-proxy')
                .toString()
                .trim();
            return httpsProxy !== 'null' ? httpsProxy : undefined;
        } catch (e) {
            return;
        }
    }
}

function checkIfOnline(useYarn) {
    return new Promise(resolve => {
        dns.lookup('registry.yarnpkg.com', err => {
            let proxy;
            if (err != null && (proxy = getProxy())) {
                dns.lookup(url.parse(proxy).hostname, proxyErr => {
                    resolve(proxyErr == null);
                });
            } else {
                resolve(err == null);
            }
        });
    });
}

module.exports = {
    yarn : yarn
}

