UNPKG

1 kBJavaScriptView Raw
1/**
2 * @file get meta.js config data from template
3 * @author tracy(qiushidev@gmail.com)
4 */
5
6const path = require('path')
7const exists = require('fs').existsSync
8const getGitUser = require('./git')
9
10module.exports = dir => {
11 const metajs = path.join(dir, 'meta.js')
12 let opts = {}
13
14 if (exists(metajs)) {
15 const req = require(path.resolve(metajs))
16 if (req !== Object(req)) {
17 throw new Error('meta.js needs to expose an object')
18 }
19 opts = req
20 }
21
22 const author = getGitUser()
23 if (author) {
24 setDefault(opts, 'author', author)
25 }
26
27 return opts
28}
29
30/**
31 * Set the default value for a prompt question
32 *
33 * @param {Object} opts meta 信息
34 * @param {string} key 键名
35 * @param {string} val 键值
36 */
37function setDefault (opts, key, val) {
38 const prompts = opts.prompts || (opts.prompts = {})
39 if (!prompts[key] || typeof prompts[key] !== 'object') {
40 prompts[key] = {
41 'type': 'string',
42 'default': val
43 }
44 } else {
45 prompts[key].default = val
46 }
47}