diff --git a/package.json b/package.json index 2802d3f..ac6bc98 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,8 @@ "name": "create-horo-app", "version": "1.0.0", "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "bin": { + "create-horo-app": "src/index.js" }, "keywords": [], "author": "", @@ -13,10 +12,13 @@ "chalk": "^4.1.0", "commander": "^6.2.1", "cross-spawn": "^7.0.3", + "download-git-repo": "^3.0.2", "execa": "^5.0.0", "fs-extra": "^9.0.1", "inquirer": "^7.3.3", + "log-symbols": "^4.0.0", "minimist": "^1.2.5", + "ora": "^5.2.0", "semver": "^7.3.4", "validate-npm-package-name": "^3.0.0" } diff --git a/src/create.js b/src/create.js index f6dcb2b..5913f5f 100644 --- a/src/create.js +++ b/src/create.js @@ -5,6 +5,9 @@ const inquirer = require('inquirer') const validatePackageName = require('validate-npm-package-name') const execSync = require('child_process').execSync const { spawn } = require('cross-spawn') +const ora = require('ora') +const logSymbols = require('log-symbols') +const download = require('download-git-repo') async function create(projectName, options) { const cwd = options.cwd || process.cwd() @@ -12,18 +15,25 @@ async function create(projectName, options) { const name = inCurrent ? path.resolve('../', cwd) : projectName const targetDir = path.resolve(cwd, projectName || '.') const result = validatePackageName(name) - const { fource } = options + const { force } = options if (!result.validForNewPackages) { console.error(chalk.red(`invalid project name ${name}`)) - result.errors && result.errors.forEach(err => console.error(chalk.red.dim('❌ ' + err))) - result.warnings && result.warnings.forEach(warn => console.error(chalk.red.dim('⚠ ' + warn))) + result.errors && result.errors.forEach(err => console.error(chalk.red.dim(`${logSymbols.error} ${err}`))) + result.warnings && result.warnings.forEach(warn => console.error(chalk.red.dim(`${logSymbols.warning} ${warn}`))) process.exit(1) } if (fs.existsSync(targetDir)) { - if (fource) { - await fs.remove(targetDir) + const spinner = ora(`Removing ${chalk.cyan(targetDir)}`) + if (force) { + try { + spinner.start() + await fs.remove(targetDir) + spinner.succeed() + } catch (e) { + spinner.fail() + } } else { const { action } = await inquirer.prompt({ name: 'action', @@ -38,8 +48,13 @@ async function create(projectName, options) { if (!action) { return } else if (action === 'overwrite') { - console.log(`\nRemoving ${chalk.cyan(targetDir)}...`) - await fs.remove(targetDir) + try { + spinner.start() + await fs.remove(targetDir) + spinner.succeed() + } catch (e) { + spinner.fail() + } } } } @@ -59,8 +74,29 @@ async function create(projectName, options) { // JSON.stringify(packageJson, null, 2) // ) - fs.copySync(path.join(__dirname, '../template'), targetDir) - fs.copySync(path.join(__dirname, '../build'), path.join(targetDir, './build')) + const spinner = ora('creating template') + try { + spinner.start() + let downloadUrl = 'http://10.124.163.76:8888/group-customer-platform/OrderCenter/frontend/frontend.git' + let downlaodTarget = path.join(__dirname, '../test') + // await downloadTemplate(downloadUrl, downlaodTarget) + + if (fs.existsSync(path.join(__dirname, '../template'))) { + fs.copySync(path.join(__dirname, '../template'), targetDir) + const packageJson = require(path.join(targetDir, './package.json')) + packageJson.name = projectName + fs.writeFileSync( + path.join(targetDir, 'package.json'), + JSON.stringify(packageJson, null, 2) + ) + } + if (fs.existsSync(path.join(__dirname, '../build'))) { + fs.copySync(path.join(__dirname, '../build'), path.join(targetDir, './build')) + } + spinner.succeed() + } catch (e) { + spinner.fail() + } const templatePackageJsonPath = path.join(targetDir, './package.json') const hasPackageJson = fs.existsSync(templatePackageJsonPath) @@ -72,12 +108,16 @@ async function create(projectName, options) { depsKeys.forEach(key => deps.push(`${key}@${dependencies[key]}`)) devDepsKeys.forEach(key => deps.push(`${key}@${devDependencies[key]}`)) installDependencies(targetDir, deps).then(() => { - console.log(chalk.green(`✔ create ${projectName} success, joy codding`)) + console.log(chalk.green(`${logSymbols.success} create ${projectName} success, joy codding`)) - if (shouldUseYarn()) { - execSync('yarn run start', { cwd: targetDir, stdio: 'inherit' }) - } else { - execSync('npm start', { cwd: targetDir }) + try { + if (shouldUseYarn()) { + execSync('yarn run start', { cwd: targetDir, stdio: 'inherit' }) + } else { + execSync('npm start', { cwd: targetDir }) + } + } catch (e) { + console.log(e) } }) } @@ -86,6 +126,23 @@ async function create(projectName, options) { // await generator.create() } +function downloadTemplate(url, target) { + return new Promise((resolve, reject) => { + const spinner = ora('download template') + spinner.start() + download(url, target, { clone: true }, (err) => { + if (err) { + console.log(chalk.red(err)) + spinner.fail() + reject(err) + } else { + spinner.succeed() + resolve() + } + }) + }) +} + function shouldUseYarn() { try { execSync('yarnpkg --version', { stdio: 'ignore' }); diff --git a/src/index.js b/src/index.js index e3c5cdd..8dfec9d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node const { Command } = require('commander') const minimist = require('minimist') const semver = require('semver') @@ -21,7 +22,7 @@ program .version(`${packageJson.version}`, '-v, --version') program - .command('create-horo-app ') + .arguments('') .description('create a new project') .option('-f, --force', 'Overwrite target directory if it exists') .action((name, cmd) => { diff --git a/template/package.json b/template/package.json index 524be5b..c84dd40 100644 --- a/template/package.json +++ b/template/package.json @@ -1,5 +1,5 @@ { - "name": "cli", + "name": "cha-template", "version": "1.0.0", "description": "", "main": "index.js",