forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent-init.js
206 lines (190 loc) · 5.49 KB
/
component-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
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
const prompt = require('inquirer').createPromptModule()
const shellJs = require('shelljs')
const path = require('path')
const bluebird = require('bluebird')
const ora = require('ora')
const moment = require('moment')
const userName = require('git-user-name')
const userEmail = require('git-user-email')
const fs = bluebird.promisifyAll(require('fs'))
// path
const CWD = process.cwd()
const COMPONENTS_PATH = path.resolve(CWD, './components')
const EXPECT_SHELL = path.resolve(CWD, './build/template.exp')
const DEMO_INDEX_PATH = path.resolve(CWD, './examples/demo-index.js')
const TYPES = path.resolve(CWD, './types/index.d.ts')
const COMPONENT_INDEX = path.resolve(CWD, './components/index.js')
const COMPONENT_JSON = path.resolve(CWD, './examples/components.json')
function init (answers) {
return Promise.resolve(answers)
.then(checkNoRepeat)
.then(create)
.then(sync)
.catch(err => {
console.warn(err)
})
}
function checkNoRepeat(answers) {
return fs.readdirAsync(COMPONENTS_PATH)
.then(files => {
return Promise.all(files.map(file => checkFile(COMPONENTS_PATH, file, answers.componentName))).then(() => answers)
})
}
function upperFirst(str) {
return String.prototype.replace.call(str, /^\w/, function (match) {
return String.prototype.toUpperCase.call(match)
})
}
function changeKebabToCamel(str) {
return String.prototype.replace.call(str, /\-(\w)/g, function(match, p1) {
return String.prototype.toUpperCase.call(p1)
})
}
function sync(answers) {
return Promise.all([syncToComponentJson(answers), syncToExample(answers), syncToIndex(answers), syncToTypes(answers)]).then(() =>answers)
}
function syncToExample(answers) {
fs.readFileAsync(DEMO_INDEX_PATH, 'utf8')
.then(str => {
return compile(answers, str)
})
.then(str => fs.writeFileAsync(DEMO_INDEX_PATH, str, 'utf8'))
.then(() => answers)
}
function syncToTypes(answers) {
fs.readFileAsync(TYPES, 'utf8')
.then(str => {
return compile(answers, str)
})
.then(str => fs.writeFileAsync(TYPES, str, 'utf8'))
.then(() => answers)
}
function syncToIndex(answers) {
/* 同步components/index文件 */
fs.readFileAsync(COMPONENT_INDEX, 'utf8')
.then(str => {
return compile(answers, str)
})
.then(str => fs.writeFileAsync(COMPONENT_INDEX, str, 'utf8'))
.then(() => answers)
}
function syncToComponentJson(answers) {
const json = require(COMPONENT_JSON)
let index = json.findIndex(item => item.category === answers.componentType)
if (index === -1) {
index = json.length
json.push({
category: answers.componentType,
list: [],
})
}
json[index].list.push({
name: answers.componentNameUpper,
path: `/${answers.componentName}`,
icon: answers.componentName,
text: answers.componentCnName,
})
Array.prototype.forEach.call(json, element => {
Array.prototype.sort.call(element.list, function(a, b) {
return a.name > b.name ? 1 : -1
})
})
Array.prototype.sort.call(json, (a, b) => {
return a.category > b.category ? 1: -1
})
return fs.writeFileAsync(COMPONENT_JSON, JSON.stringify(json), 'utf8')
.then(answers => answers)
}
function compile(metaData, fileStr) {
return String.prototype.replace.call(fileStr, /\/\*.*@init<%(.*)%>.*\*\//g, function (match, p1) {
return (String.prototype.replace.call(p1, /\${(\w*)}/g, function (innMatch, innP1) {
return metaData[innP1]
})) + '\r' +match
})
}
function checkFile(dir, file, name) {
const filePath = path.resolve(dir, `./${file}`)
return fs.statAsync(filePath)
.then(stat => {
if (stat.isDirectory()) {
if (name === file) {
return Promise.reject(`组件库中已经存在名为${name}的组件!请仔细核对后重新创建`)
}
}
return
})
}
function exec (command, argvs) {
const spinner = ora('Loading...').start()
const result = shellJs.exec(`${command} ${argvs.map(item => `\'${item}\'`).join(' ')} >> /dev/null`)
spinner.succeed(['执行完毕'])
return result
}
function create(answers) {
exec('expect', [EXPECT_SHELL, answers.componentCnName, answers.componentName, answers.componentType, answers.componentDesc, answers.author, answers.time, COMPONENTS_PATH])
return answers
}
function getUserInfo() {
let user = userName()
if (!user) {
user = 'anonymous'
}
const email = userEmail()
if (email) {
user += ` <${email}>`
}
return user
}
function launch() {
return prompt([
{
type: 'input',
name: 'componentName',
message: '请输入要创建的组件名称(kebab-case):',
validate: function (str) {
return /^[a-z][a-z|-]*[a-z]$/.test(str)
}
},
{
type: 'input',
name: 'componentCnName',
message: '请输入要创建的组件中文名称(中文):',
},
{
type: 'list',
choices: [
"basic",
"feedback",
"form",
"business",
"gesture"
],
name: 'componentType',
message: '组件类型',
},
{
type: 'input',
name: 'componentDesc',
message: '组件描述',
},
{
type: 'input',
name: 'author',
message: '作者',
default: getUserInfo(),
},
{
type: 'input',
name: 'time',
message: 'time',
default: moment().format('YYYY年MM月DD日')
}
])
.then(answers => {
answers = Object.assign(answers, {
componentNameUpper: upperFirst(changeKebabToCamel(answers.componentName))
})
return init(answers)
})
}
launch()