Skip to content

Commit

Permalink
feat(完善): 功能完善
Browse files Browse the repository at this point in the history
功能完善
  • Loading branch information
Rabi-Wang committed Jan 4, 2021
1 parent b70b613 commit fedfaf0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand All @@ -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"
}
Expand Down
85 changes: 71 additions & 14 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ 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()
const inCurrent = projectName === '.'
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',
Expand All @@ -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()
}
}
}
}
Expand All @@ -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)
Expand All @@ -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)
}
})
}
Expand All @@ -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' });
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
const { Command } = require('commander')
const minimist = require('minimist')
const semver = require('semver')
Expand All @@ -21,7 +22,7 @@ program
.version(`${packageJson.version}`, '-v, --version')

program
.command('create-horo-app <app-name>')
.arguments('<app-name>')
.description('create a new project')
.option('-f, --force', 'Overwrite target directory if it exists')
.action((name, cmd) => {
Expand Down
2 changes: 1 addition & 1 deletion template/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cli",
"name": "cha-template",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down

0 comments on commit fedfaf0

Please # to comment.