//@ts-nocheck

import {copyFolder, pathUtils} from "../../utils";
import spawn from 'cross-spawn';
import chalk from 'chalk';
const command = `lint init`;
const aliases = ['lint','-lint'];
const desc = '生成代码风格检查';
const builder = (yargs:any) => {
    return yargs;
};

const handler = async function (argv:any) {
    install(process.cwd(),true,true,['pabala-lint'],false,true).then(()=>{

        copyFolder(pathUtils.relative('../../template/lint/'),pathUtils.cwd(`./`))
    }).catch((e)=>{
        console.log(e);
        process.exit(0);
    })

};


function install(root, useYarn, usePnp, dependencies, verbose, isOnline) {
    return new Promise((resolve, reject) => {
        let command;
        let args;
        if (useYarn) {
            command = 'yarnpkg';
            args = ['add', '--exact'];
            if (!isOnline) {
                args.push('--offline');
            }
            if (usePnp) {
                args.push('--enable-pnp');
            }
            [].push.apply(args, dependencies);

            // Explicitly set cwd() to work around issues like
            // https://github.com/facebook/create-react-app/issues/3326.
            // Unfortunately we can only do this for Yarn because npm support for
            // equivalent --prefix flag doesn't help with this issue.
            // This is why for npm, we run checkThatNpmCanReadCwd() early instead.
            args.push('--cwd');
            args.push(root);

            if (!isOnline) {
                console.log(chalk.yellow('You appear to be offline.'));
                console.log(chalk.yellow('Falling back to the local Yarn cache.'));
                console.log();
            }
        } else {
            command = 'npm';
            args = [
                'install',
                '--save',
                '--save-exact',
                '--loglevel',
                'error',
            ].concat(dependencies);

            if (usePnp) {
                console.log(chalk.yellow("NPM doesn't support PnP."));
                console.log(chalk.yellow('Falling back to the regular installs.'));
                console.log();
            }
        }

        if (verbose) {
            args.push('--verbose');
        }

        const child = spawn(command, args, { stdio: 'inherit' });
        child.on('close', code => {
            if (code !== 0) {
                reject({
                    command: `${command} ${args.join(' ')}`,
                });
                return;
            }
            resolve();
        });
    });
}

export default {
    command,
    desc,
    builder,
    handler,
    aliases
}
