lovelope-react-cli
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -5,12 +5,10 @@ #!/usr/bin/env node | ||
const fs = require('fs'); | ||
const program = require('commander'); | ||
const download = require('download-git-repo'); | ||
const inquirer = require('inquirer'); | ||
const fs = require('fs'); | ||
const ora = require('ora'); | ||
const chalk = require('chalk'); | ||
const symbols = require('log-symbols'); | ||
const chalk = require('chalk'); | ||
const validate = require('validate-npm-package-name'); | ||
const pkg = require('../package.json'); | ||
const init = require('./init'); | ||
@@ -25,3 +23,2 @@ require('./checkNodeVersion')(pkg.engines.node, pkg.name); | ||
.action(name => { | ||
console.log(name); | ||
const valid = validate(name); | ||
@@ -34,53 +31,4 @@ const { validForNewPackages } = valid; | ||
} | ||
if (!fs.existsSync(name)) { | ||
inquirer | ||
.prompt([ | ||
{ | ||
name: 'description', | ||
message: '请输入项目描述', | ||
}, | ||
{ | ||
name: 'author', | ||
message: '请输入作者名称', | ||
}, | ||
]) | ||
.then(answers => { | ||
// 开始下载 | ||
const spinner = ora('正在下载模板...'); | ||
spinner.start(); | ||
// 项目类型 | ||
let remoteUrl = pkg.template.default; | ||
if ( | ||
process.argv.includes('-t') || | ||
process.argv.includes('--typescript') | ||
) { | ||
remoteUrl = pkg.template.typescript; | ||
} | ||
download(remoteUrl, name, { clone: true }, err => { | ||
if (err) { | ||
// 下载失败调用 | ||
spinner.fail(); | ||
console.log(symbols.error, chalk.red(err)); | ||
} else { | ||
// 下载成功调用 | ||
spinner.succeed(); | ||
const meta = { | ||
name, | ||
description: answers.description, | ||
author: answers.author, | ||
}; | ||
const fileName = `${name}/package.json`; | ||
if (fs.existsSync(fileName)) { | ||
const content = fs.readFileSync(fileName).toString(); | ||
const json = JSON.parse(content); | ||
const result = { ...json, ...meta }; | ||
fs.writeFileSync(fileName, JSON.stringify(result, null, 2)); | ||
} | ||
console.log(symbols.success, chalk.green('项目初始化完成')); | ||
} | ||
}); | ||
}); | ||
init(name); | ||
} else { | ||
@@ -87,0 +35,0 @@ // 错误提示项目已存在,避免覆盖原有项目 |
{ | ||
"name": "lovelope-react-cli", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A cli for react-starter.", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -1,9 +0,19 @@ | ||
# r-cli | ||
# [R-CLI](https://github.com/lovelope/r-cli) · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/lovelope/r-cli/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/lovelope-react-cli.svg?style=flat)](https://www.npmjs.com/package/lovelope-react-cli) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/lovelope/r-cli/pulls) | ||
A cli for react-starter. | ||
Usage: `r-cli <command> [options]` | ||
## Install | ||
Options: | ||
```bash | ||
npm i -g lovelope-react-cli | ||
``` | ||
## Usage | ||
```bash | ||
r-cli <command> [options] | ||
``` | ||
## Options: | ||
- -v, --version output the version number | ||
@@ -10,0 +20,0 @@ - -h, --help output usage information |
7185
9
116
26
2