1
1
#!/usr/bin/env node
2
2
3
- var metadata = require ( 'read-metadata' )
4
- var async = require ( 'async' )
5
- var Metalsmith = require ( 'metalsmith' )
6
- var render = require ( 'consolidate' ) . handlebars . render
7
3
var download = require ( 'download-git-repo' )
8
4
var program = require ( 'commander' )
9
5
var exists = require ( 'fs' ) . existsSync
@@ -13,7 +9,6 @@ var uid = require('uid')
13
9
var chalk = require ( 'chalk' )
14
10
var inquirer = require ( 'inquirer' )
15
11
var logger = require ( '../lib/logger' )
16
- var getGitUser = require ( '../lib/git-user' )
17
12
var Spinner = require ( '../lib/spinner' )
18
13
19
14
/**
@@ -43,8 +38,11 @@ program.on('--help', function () {
43
38
* Help.
44
39
*/
45
40
46
- program . parse ( process . argv )
47
- if ( program . args . length < 1 ) return program . help ( )
41
+ function help ( ) {
42
+ program . parse ( process . argv )
43
+ if ( program . args . length < 1 ) return program . help ( )
44
+ }
45
+ help ( )
48
46
49
47
/**
50
48
* Padding.
@@ -88,6 +86,11 @@ if (exists(to)) {
88
86
*/
89
87
90
88
function run ( ) {
89
+ var options = require ( '../lib/options' ) ( name )
90
+ var prompt = require ( '../lib/prompt' )
91
+ var ask = require ( '../lib/ask' ) ( options , prompt )
92
+ var generate = require ( '../lib/generate' ) ( ask )
93
+
91
94
// check if template is local
92
95
if ( hasSlash && exists ( template ) ) {
93
96
generate ( template , to , function ( err ) {
@@ -116,142 +119,3 @@ function run () {
116
119
} )
117
120
}
118
121
}
119
-
120
- var promptInquirerTypeMapping = {
121
- string : 'input' ,
122
- boolean : 'confirm'
123
- }
124
-
125
- /**
126
- * Prompt plugin.
127
- *
128
- * @param {Object } files
129
- * @param {Metalsmith } metalsmith
130
- * @param {Function } done
131
- */
132
-
133
- function ask ( files , metalsmith , done ) {
134
- var opts = options ( metalsmith . _directory + '/..' )
135
-
136
- var prompts = Object . keys ( opts . schema )
137
- var metalsmithMetadata = metalsmith . metadata ( )
138
-
139
- async . eachSeries ( prompts , run , done )
140
-
141
- function run ( key , done ) {
142
- var prompt = opts . schema [ key ]
143
-
144
- inquirer . prompt ( [ {
145
- type : promptInquirerTypeMapping [ prompt . type ] || prompt . type ,
146
- name : key ,
147
- message : prompt . label || key ,
148
- default : prompt . default ,
149
- choices : prompt . choices || [ ]
150
- } ] , function ( answers ) {
151
- if ( Array . isArray ( answers [ key ] ) ) {
152
- metalsmithMetadata [ key ] = { }
153
- answers [ key ] . forEach ( function ( multiChoiceAnswer ) {
154
- metalsmithMetadata [ key ] [ multiChoiceAnswer ] = true
155
- } )
156
- } else {
157
- metalsmithMetadata [ key ] = answers [ key ]
158
- }
159
-
160
- done ( )
161
- } )
162
- }
163
- }
164
-
165
- /**
166
- * Template in place plugin.
167
- *
168
- * @param {Object } files
169
- * @param {Metalsmith } metalsmith
170
- * @param {Function } done
171
- */
172
-
173
- function renderTemplateFiles ( files , metalsmith , done ) {
174
- var keys = Object . keys ( files )
175
- var metalsmithMetadata = metalsmith . metadata ( )
176
-
177
- async . each ( keys , run , done )
178
-
179
- function run ( file , done ) {
180
- var str = files [ file ] . contents . toString ( )
181
- // do not attempt to render files that do not have mustaches
182
- if ( ! / \{ \{ [ # ^ ] * ( \w + ) * \} \} / . test ( str ) ) {
183
- return done ( )
184
- }
185
- render ( str , metalsmithMetadata , function ( err , res ) {
186
- if ( err ) return done ( err )
187
- files [ file ] . contents = new Buffer ( res )
188
- done ( )
189
- } )
190
- }
191
- }
192
-
193
- /**
194
- * Generate a template given a `src` and `dest`.
195
- *
196
- * @param {String } src
197
- * @param {String } dest
198
- * @param {Function } fn
199
- */
200
-
201
- function generate ( src , dest , fn ) {
202
- var template = path . join ( src , 'template' )
203
-
204
- Metalsmith ( template )
205
- . use ( ask )
206
- . use ( renderTemplateFiles )
207
- . clean ( false )
208
- . source ( '.' ) // start from template root instead of `./src` which is Metalsmith's default for `source`
209
- . destination ( dest )
210
- . build ( function ( err ) {
211
- if ( err ) throw err
212
- fn ( )
213
- } )
214
- }
215
-
216
- /**
217
- * Read prompts metadata.
218
- *
219
- * @param {String } dir
220
- * @return {Object }
221
- */
222
-
223
- function options ( dir ) {
224
- var file = path . join ( dir , 'meta.json' )
225
- var opts = exists ( file )
226
- ? metadata . sync ( file )
227
- : { }
228
-
229
- setDefault ( opts , 'name' , name )
230
-
231
- var author = getGitUser ( )
232
- if ( author ) {
233
- setDefault ( opts , 'author' , author )
234
- }
235
-
236
- return opts
237
- }
238
-
239
- /**
240
- * Set the default value for a schema key
241
- *
242
- * @param {Object } opts
243
- * @param {String } key
244
- * @param {String } val
245
- */
246
-
247
- function setDefault ( opts , key , val ) {
248
- var schema = opts . schema || ( opts . schema = { } )
249
- if ( ! schema [ key ] || typeof schema [ key ] !== 'object' ) {
250
- schema [ key ] = {
251
- 'type' : 'string' ,
252
- 'default' : val
253
- }
254
- } else {
255
- schema [ key ] [ 'default' ] = val
256
- }
257
- }
0 commit comments