-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
307 lines (228 loc) · 8.51 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
'use strict'
const chalk = require('chalk')
const globby = require('globby')
const glob = require('glob')
const path = require('path')
const fs = require('fs-extra')
const YAML = require('yamljs')
const shell = require('shelljs')
const formatYamlFile = require('format-yaml')
const gitDownload = require('download-git-repo')
const del = require('del')
const ora = require('ora')
const replaceInfo = ({ name, version, c_version: cVersion, author, isESNext, type }, str) => {
return str
// 替换 项目名称
.replace(/\[name\]/g, name)
// 替换 项目版本号
.replace(/\[version\]/g, version)
// 替换 开发者用户
.replace(/\[author\]/g, author)
// 替换 lf 版本
.replace(/\[c_version\]/g, cVersion)
// 替换 项目类型
.replace(/\[type\]/g, type)
// 替换 是否使用 ESNext
.replace(/\[ESNext\]/g, isESNext)
}
// 获取项目模板
const getProjectType = () => {
const { project } = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json'), 'utf8'))
for (let k in project) {
project[ k ] = path.resolve(__dirname, `./project/${project[ k ]}`)
}
return project
}
exports.getProjectType = getProjectType
// 新建默认类型项目
const newDefaultProject = async data => {
let { name, type, path: projectPath, version, isESNext, isSourcePath, author, c_version: cVersion, description = '', isESLint, from } = data
const types = getProjectType()
const projectTypePath = types[ type ]
fs.ensureDirSync(projectPath)
// package.json
let packageJSON = {
name,
version,
author,
description,
license: 'UNLICENSED',
scripts: {
'dev': 'lf dev',
'build': 'lf build'
}
}
// legoflow.json
let legoflowJSON = {
name,
version: cVersion,
type
}
typeof isESNext !== 'undefined' && (legoflowJSON['ES.Next'] = isESNext)
let isNeedNpminstall = false
let isNeedCreateDefalutFolder = true
let isTsConfigJson = false
let isJsEslint = false
let isTsEslint = false
if (type.indexOf('Vue') == 0) {
isNeedCreateDefalutFolder = false
isNeedNpminstall = from !== 'app'
legoflowJSON.mode = 'webpack'
legoflowJSON.webpack = {
'sass.globalResources': [
'./src/style/var.scss'
]
}
legoflowJSON[ 'workflow.dev' ] = {}
legoflowJSON[ 'workflow.build' ] = {}
packageJSON.dependencies = {
'axios': '^0.18.0',
'vue': '^2.5.16',
'vue-router': '^3.0.1',
'vuex': '^3.0.1'
}
data.newProjectSuccessMessage = `➜ You can${!isSourcePath ? `${chalk.blue.bold(` **cd ${name}**`)} and` : ''} run ${chalk.blue.bold('**lf dev**')} to start workflow.dev`
}
switch (type) {
case 'Mobile': {
legoflowJSON.REM = true
legoflowJSON.alias = {}
legoflowJSON.global = {}
break
}
case 'PC': {
legoflowJSON.alias = {}
legoflowJSON.global = {}
break
}
case 'Vue.js': {
isJsEslint = true
legoflowJSON.entry = [ './src/main.js' ]
break
}
case 'Vue.ts': {
isTsConfigJson = true
isTsEslint = true
legoflowJSON.entry = [ './src/main.ts' ]
const vueTsDependencies = {
'vue-class-component': '^6.2.0',
'vue-property-decorator': '^6.1.0'
}
packageJSON.dependencies = Object.assign(packageJSON.dependencies, vueTsDependencies)
break
}
}
if (typeof isESLint !== 'undefined') {
legoflowJSON.ESLint = isESLint
}
// package.json
fs.writeFileSync(path.resolve(projectPath, './package.json'), JSON.stringify(packageJSON, null, 2))
const configFile = path.resolve(projectPath, './legoflow.yml')
fs.writeFileSync(configFile, YAML.stringify(legoflowJSON, 2, 2))
let formatYamlString = await formatYamlFile(configFile)
formatYamlString = `# 参数说明: https://legoflow.com/wiki/config.html\n\n${formatYamlString}`
// format YAML file
fs.writeFileSync(configFile, formatYamlString)
// cope type folder
fs.copySync(projectTypePath, path.resolve(projectPath, './src'))
// README
fs.writeFileSync(path.resolve(projectPath, './README.md'), `# ${name}`, 'utf8')
// create img folder
const imgFolder = path.resolve(projectPath, './src/img')
const imgBase64Folder = path.resolve(projectPath, './src/img/base64')
const imgSliceFolder = path.resolve(projectPath, './src/img/slice')
if (isNeedCreateDefalutFolder) {
fs.mkdirSync(imgFolder)
fs.mkdirSync(imgBase64Folder)
fs.mkdirSync(imgSliceFolder)
}
// copy .gitignore .editorconfig
const gitignoreFile = path.resolve(__dirname, './project/gitignore')
const editorconfigFile = path.resolve(__dirname, './project/editorconfig')
const tsconfigJsonFile = path.resolve(__dirname, './project/tsconfig.json')
const jsEslintFile = path.resolve(__dirname, './project/js_eslint.js')
const tsEslintFile = path.resolve(__dirname, './project/ts_eslint.js')
const eslintIgnoreFile = path.resolve(__dirname, './project/eslintignore')
fs.copySync(gitignoreFile, path.resolve(projectPath, './.gitignore'))
fs.copySync(editorconfigFile, path.resolve(projectPath, './.editorconfig'))
isTsConfigJson && fs.copySync(tsconfigJsonFile, path.resolve(projectPath, 'tsconfig.json'))
isJsEslint && fs.copySync(jsEslintFile, path.resolve(projectPath, '.eslintrc.js'))
isTsEslint && fs.copySync(tsEslintFile, path.resolve(projectPath, '.eslintrc.js'))
;(isJsEslint || isTsEslint) && (fs.copySync(eslintIgnoreFile, path.resolve(projectPath, '.eslintignore')))
if (isNeedNpminstall && shell.cd(projectPath)) {
console.log('installing local node_modules')
shell.exec(`npm i`)
}
return data
}
// 新建自定义路径模板
const newGitProject = async data => {
let { name, path: projectPath, gitSourcePath, isSourcePath } = data
data.type = `git+${gitSourcePath}`
if (path.extname(gitSourcePath) !== '.git') {
console.error(chalk.red('Git Repo Error'))
process.exit(1)
}
const tmp = path.resolve(__dirname, `temp-${new Date().getTime()}`)
fs.mkdirSync(tmp)
const spinner = ora('downloading git repo').start()
await new Promise(resolve => gitDownload(`direct:${gitSourcePath}`, tmp, { clone: true }, function (error) {
spinner.stop()
if (error) {
console.error(chalk.red('下载 Git 模板错误'), error)
del.sync(tmp, { force: true })
process.exit(1)
} else {
resolve()
}
}))
fs.mkdirsSync(projectPath)
shell.cd(projectPath)
const templatePath = path.resolve(tmp, 'template')
const yamlConfig = replaceInfo(data, fs.readFileSync(path.resolve(templatePath, 'legoflow.yml'), 'utf8'))
const packageJson = replaceInfo(data, fs.readFileSync(path.resolve(templatePath, 'package.json'), 'utf8'))
fs.writeFileSync(path.resolve(projectPath, 'legoflow.yml'), yamlConfig)
fs.writeFileSync(path.resolve(projectPath, 'package.json'), packageJson)
console.log(`➜ rewrite ${chalk.yellow('package.json')} & ${chalk.yellow('legoflow.yml')} success`)
const otherFiles = await globby([ `${templatePath}/**/*`, `${templatePath}/**/.*`, `!${templatePath}/package.json`, `!${templatePath}/legoflow.yml` ])
for (let f of otherFiles) {
let distPath = path.resolve(projectPath, path.relative(templatePath, f))
let basename = path.basename(f)
fs.copySync(f, distPath)
}
// 重写 README
const README = path.resolve(projectPath, 'README.md')
if (fs.existsSync(README)) {
fs.writeFileSync(README, replaceInfo(data, fs.readFileSync(README, 'utf8')))
}
console.log(`➜ copy ${chalk.yellow('remaining files')} success`)
if (shell.exec(`npm i`).code !== 0) {
return 'install dependencies error'
}
console.log('➜ install dependencies success')
data.newProjectSuccessMessage = `➜ You can${!isSourcePath ? `${chalk.blue.bold(` **cd ${name}**`)} and` : ''} run ${chalk.blue.bold('**lf dev**')} to start workflow.dev`
del.sync(tmp, { force: true })
return data
}
// 新建项目
exports.new = async (data) => {
let { name, path: projectPath, isSourcePath, type, typeSourcePath, gitSourcePath } = data
if (!isSourcePath) {
data.path = projectPath = path.resolve(projectPath, `./${name}`)
}
if (!isSourcePath && fs.existsSync(projectPath)) {
return '项目已存在'
}
if (fs.existsSync(data.path) && glob.sync(`${data.path}/**/*`).length > 0) {
return '路径存在其他文件'
}
if (gitSourcePath) {
/* eslint-disable no-return-await */
return await newGitProject(data)
} else if (getProjectType()[ type ]) {
/* eslint-disable no-return-await */
return await newDefaultProject(data)
} else {
return '找不到该类型项目'
}
}