-
Notifications
You must be signed in to change notification settings - Fork 9
/
init.js
95 lines (83 loc) · 2.43 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
/*
* 初始化项目 (或使用 yarn init 来初始化)
* @Author: ciwyaitd
* @Date: 2017-12-15 18:30:14
* @Last Modified by: hejinming
* @Last Modified time: 2018-01-12 19:22:26
*/
'use strict'
const inquirer = require('inquirer')
const fs = require('fs-extra')
const path = require('path')
const confirm = require('./template/tools/util/confirm')
require('shelljs/global')
async function init() {
let answers = await inquirer.prompt([{
type: 'input',
name: 'path',
message: '请输入要新建的项目的相对路径(包含项目名称)?',
default: () => {
return path.join(__dirname, 'wepy-template')
},
}, {
type: 'input',
name: 'name',
message: 'Project name(项目名称):',
default: () => {
return 'wepy-project'
},
validate: (name) => {
if (!name) return '请输入项目名称'
return true
}
}, {
type: 'input',
name: 'description',
message: '项目描述:',
default: () => {
return 'wepy typescript project'
}
}, {
type: 'input',
name: 'author',
message: '作者:',
default: () => {
return ''
}
}])
let target = path.join(__dirname, answers.path)
if (fs.existsSync(target)) {
confirm(`The "${target}" is exist. Do your want to override it?`, async (flag) => {
if (flag) {
await build(answers)
}
process.exit()
return
})
return
}
await build(answers)
process.exit()
return
}
async function build(answers) {
mkdir('-p', answers.path)
cp('-R', 'template/.', answers.path)
const target = path.join(__dirname, answers.path, 'package.json')
const packagePath = path.join(__dirname, 'template', 'package.json')
let packageFile = JSON.parse( fs.readFileSync(packagePath, 'utf8') )
packageFile.name = answers.name
packageFile.description = answers.description
packageFile.author = answers.author
let packageJson = JSON.stringify(packageFile, null, 4)
await new Promise((resolve) => {
setTimeout(function () {
resolve(true)
}, 500)
})
fs.writeFileSync(target, packageJson, 'utf8')
console.log('初始化 package.json 操作')
console.log('项目初始化完成')
}
init()