UNPKG

cryptocom-react-cli

Version:

It's a quick tool to help you build a react project with one command

83 lines (74 loc) 2.56 kB
#!/usr/bin/env node import chalk from 'chalk' import fs from 'fs' import { fileURLToPath } from 'url'; import path from 'path' import commander from 'commander' import inquirer from 'inquirer' import shell from 'shelljs' import ora from 'ora' import { spawnSync } from 'child_process' import { getGitDemoPackages } from './utils/downloadGitPackage.js' const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename) const packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json'), 'utf-8')); commander.version(packageJson.version, '-v --version'); commander.command('upgrade [version]').action(function (name) { shell.exec('npm install cryptocom-react-cli -g'); }); commander.command('init <name>').action(async name => { const outPath = path.resolve(process.cwd(), name); console.log(outPath) const ifExistOutPath = fs.existsSync(outPath); if (ifExistOutPath) { let answer = await inquirer.prompt({ type: 'list', name: 'outDirStratgy', message: `Target directory ${outPath} already exists \n pick an action:`, choices: [ { name: 'Overwrite', value: 'overwrite' }, { name: 'Cancel', value: 'cancel' } ] }); if (answer.outDirStratgy === 'cancel') { return false; } else if (answer.outDirStratgy === 'overwrite') { const spinner = ora('Clearing files...').start(); shell.rm('-Rf', outPath); spinner.succeed(`Cleared files under ${outPath}`) fs.mkdirSync(outPath); await getGitDemoPackages(outPath); } else { await getGitDemoPackages(outPath); } } else { fs.mkdirSync(outPath); await getGitDemoPackages(outPath); } shell.cd(outPath); if(shell.which('yarn')) { const spinner = ora('Installing dependencies...\n').start(); spawnSync('yarn', ['install'],{stdio: 'inherit','cwd': process.cwd()}); spinner.succeed('Done.') process.exit(1); } else { console.log(chalk.red('please install yarn in global.')) } }); if (!process.argv.slice(2).length) { commander.outputHelp(function(text){ return chalk.cyan(text); }); } // error on unknown commands commander.on('command:*', function () { console.error('Invalid command: %s\nSee --help for a list of available commands.', commander.args.join(' ')); process.exit(1); }); commander.parse(process.argv);