/
helpers.ts
208 lines (175 loc) · 6.62 KB
/
helpers.ts
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
import * as M from './models'
export
function createTemplateVariableFromTemplateConfigVariable(config: M.TemplateConfigVariable): M.TemplateVariable {
// Crate types for the variables based on their matched suffix
if (/dir$/i.test(config.name)) return { config, vartype: M.VariableType.directory }
else if (/arr$/i.test(config.name)) return { config, vartype: M.VariableType.array }
else if (/num$|count$/i.test(config.name)) return { config, vartype: M.VariableType.number }
else return { config, vartype: M.VariableType.string }
}
import path = require('path')
import fs = require('fs')
import chalk = require('chalk')
export
function readUndergenConfig(cwd): M.UndergenConfig {
const conffile: string = path.resolve(cwd, './.undergen.js')
// Require undergen conf file
let config: M.UndergenConfig
try {
config = require(conffile)
} catch (err) {
console.warn("No './.undergen.js' file found. Using defaults; looking for templates in './templates'.")
config = {}
}
// Ensure templates directory
if (!config.templatesDir) {
config.templatesDir = './templates'
}
// Assert templates directory exists
const testTemplatesPath = path.resolve(cwd, config.templatesDir)
if (!fs.existsSync(testTemplatesPath)) {
throw `Templates directory not found: ${testTemplatesPath}`
}
return config
}
export
function readTemplateConfig(cwd, templateDir: string, config: M.UndergenConfig): M.TemplateConfig {
let template_conffile: string = path.resolve(templateDir, './template.js')
// Require template conf file
const template_config: M.TemplateConfig = require(template_conffile)
// Ensure files directory
if (!template_config.filesDir) {
template_config.filesDir = './files'
}
// Ensure template config are all advanced variables directory
template_config.variables =
template_config.variables
.map(variable =>
typeof variable === 'string'
? <M.TemplateConfigVariable> { name: variable }
: variable
)
// Assert template's files directory exists
const fullTemplateFilesDir = path.resolve(templateDir, template_config.filesDir)
if (!fs.existsSync(fullTemplateFilesDir)) {
throw `Template filesDir directory not found: ${fullTemplateFilesDir}`
}
template_config.filesDir = fullTemplateFilesDir
// Assert template's out directory exists
const fullTemplateOutDir = path.resolve(cwd, template_config.outDir || './')
if (!fs.existsSync(fullTemplateOutDir)) {
throw `Template outDir directory not found: ${fullTemplateOutDir}`
}
template_config.outDir = fullTemplateOutDir
if (template_config.variables == null) {
template_config.variables = []
}
if (template_config.onComplete == null) {
template_config.onComplete = (noop => noop)
}
return template_config
}
export
function parseValueForTemplateVar(opts: { cwd: string }) {
return (templateVar: M.TemplateVariable, value: string) => {
let res = null
switch(templateVar.vartype) {
case M.VariableType.array:
// We're just a bunch of goofs up here.
res = value
.replace(/([^\\]),/g, '$1😂').split('😂')
// Ensure that there is at least one string
.filter(str => str && str.length)
break;
case M.VariableType.directory:
res = path.resolve(opts.cwd, value)
if (!fs.existsSync(res)) {
throw `Directory does not exist: ${templateVar.config.name} = "${value}" when resolved to "${res}"`
}
break
case M.VariableType.number:
res = parseFloat(value); break
case M.VariableType.string:
res = String(value); break
}
return res
}
}
import inquirer = require('inquirer')
export
function createQuestionFromTemplateVar(template, opts: {cwd: string, vars, prefix: string}) {
return <(t: M.TemplateVariable) => inquirer.Question>
function (tv) {
let res: inquirer.Question = {
name: opts.prefix + tv.config.name
}
let description = ''
// add description for the variable to the message
if (tv.config.description) {
description = `\n${ chalk.reset(tv.config.description) }\n`
}
// add default if defined
if (typeof tv.config.default === 'function') {
res.default = (answers) => {
let vars = Object.assign({}, opts.vars)
// populate with expected keys
Object.keys(answers)
.forEach(k => {
// remove prefix from keys when passing down to default function
const plainKey = k.replace(opts.prefix, '')
vars[plainKey] = answers[k]
})
return tv.config.default(vars)
}
}
let displayName = chalk.yellow(tv.config.name)
switch(tv.vartype) {
case M.VariableType.array:
res.message = `Define ${displayName}. ${description}Enter strings separated by commas.\nEx: "a1,b#, m" => ["a1", "b#", " m"]\n`
res.type = 'string'
break
case M.VariableType.directory:
res.message = `Define ${displayName}. ${description}`
// We need to add basePath for inquirer-directory module
;(<any> res).basePath = opts.cwd
;(<any> res).startPath = template.outDir || opts.cwd
;(<any> res).cwd = opts.cwd
res.type = 'directory'
break
case M.VariableType.number:
res.message = `Define ${displayName}. ${description}Enter number:`
res.validate = (input) => isNaN(parseFloat(input)) ? `Unable to parse not a number "${input}".` : true
break
case M.VariableType.string:
res.message = `Define ${displayName}. ${description}Enter string:`
break
}
return res
}
}
export
/**
* This function generates a function for parsing an argument formatted string ("identifier:value")
* So, example:
* const fn = createKeyValuePairsFromArgument(template, {cwd})
* fn(`f:1`) //=> {key: 'f', value: '1'}
* fn(`fCount:1`) //=> {key: 'fCount', value: 1}
* fn(`aArr:1`) //=> {key: 'aArr', value: ["1"]}
* fn(`aArr:a,b, c`) //=> {key: 'aArr', value: ["a", "b", " c"]}
* fn(`aDir:src`) //=> {key: 'aDir', value: path.resolve(cwd, "src")} // pretend the function is evaluated using the passed in cwd
*
* */
function createKeyValuePairsFromArgument(template: M.Template, {cwd}) {
const parseTemplateVarValue = parseValueForTemplateVar({cwd})
return (arg) => {
const [name, value] = arg.split(/:/)
const templateVariable = template.vars.find(v => v.config.name === name)
if (templateVariable == null) {
throw new Error(`Unknown template variable argument name: ${name}`)
}
return {
key: templateVariable.config.name,
value: parseTemplateVarValue(templateVariable, value)
}
}
}