Skip to content

Commit

Permalink
feat: 新增 git init ,package.json 写入 name/author
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Dec 16, 2020
1 parent 82436b4 commit 6d0de77
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/plopfile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import path from 'path'
import { NodePlopAPI, ActionType } from 'plop'
import { __DEV__ } from './env'
import { downloadGitRepo, install } from './utils'
import { downloadGitRepo, init } from './utils'

module.exports = function(plop: NodePlopAPI) {
plop.setActionType('initProject', async (answers: any, config) => {
const name = answers.name as string
const author = answers.author as string
const template = answers.template as string
const projectPath = path.join(process.cwd(), name)
await downloadGitRepo(`github:CaoMeiYouRen/${template}`, projectPath)
await install(projectPath)
await init(projectPath, {
name,
author,
})
return '- 下载项目模板成功!'
})
plop.setGenerator('create', {
Expand All @@ -25,6 +29,16 @@ module.exports = function(plop: NodePlopAPI) {
default: __DEV__ ? 'temp' : '',
filter: (e: string) => e.trim(),
},
{
type: 'input',
name: 'author',
message: '请输入作者名称',
validate(input: string, answers){
return input.trim().length !== 0
},
default: __DEV__ ? 'CaoMeiYouRen' : '',
filter: (e: string) => e.trim(),
},
{
type: 'list',
name: 'template',
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs-extra'
import path from 'path'
import ora from 'ora'
import download from 'download-git-repo'
import { exec, ExecOptions, execSync } from 'child_process'
Expand Down Expand Up @@ -70,10 +71,17 @@ export async function asyncExec(cmd: string, options?: ExecOptions) {
})
}

export async function install(projectPath: string) {
export async function init(projectPath: string, pkgData: IPackage) {
const loading = ora('正在安装依赖……')
loading.start()
try {
await asyncExec('git init', {
cwd: projectPath,
})
const pkgPath = path.join(projectPath, 'package.json')
const pkg: IPackage = await fs.readJSON(pkgPath)
const newPkg = Object.assign({}, pkg, pkgData)
await fs.writeFile(pkgPath, JSON.stringify(newPkg, null, 2))
await asyncExec('npm i', {
cwd: projectPath,
})
Expand Down

0 comments on commit 6d0de77

Please sign in to comment.