-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompts.js
178 lines (158 loc) · 4.25 KB
/
prompts.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
'use strict';
var fs = require('fs');
var path = require('path');
var Enquirer = require('enquirer');
var Confirm = require('prompt-confirm');
module.exports = function(app, store, state) {
var enquirer = new Enquirer();
var clone = promisify(require('gh-clone'));
var writeFile = promisify(fs.writeFile);
var template = path.join.bind(path, __dirname, 'templates');
state.exists = function() {
return fs.existsSync(state.path);
};
/**
* Register prompts
*/
enquirer.register('confirm', require('prompt-confirm'));
enquirer.register('radio', require('prompt-radio'));
/**
* Default choices
*/
store.union('choices', [
'none',
'pages-themes/architect',
'pages-themes/cayman',
'pages-themes/dinky',
'pages-themes/hacker',
'pages-themes/leap-day',
'pages-themes/merlot',
'pages-themes/midnight',
'pages-themes/minimal',
'pages-themes/modernist',
'pages-themes/slate',
'pages-themes/tactile',
'pages-themes/time-machine',
'pietromenna/jekyll-cayman-theme',
'poole/hyde',
'poole/lanyon',
'poole/poole',
'other'
]);
/**
* Get the theme to generate
*/
enquirer.question('theme', {
type: 'radio',
message: 'Which theme? (choose "other" to specify any GitHub repository)',
default: store.get('default'),
choices: store.get('choices'),
when: function() {
return !state.theme;
},
transform: function(repo) {
update(repo);
return repo;
}
});
/**
* Get the color to use for the specified theme
*/
enquirer.question('color', {
type: 'radio',
message: 'Theme color?',
default: store.get('color') || 'default',
choices: [
{value: 'default', name: 'default'},
{value: 'red', name: 'theme-base-08'},
{value: 'orange', name: 'theme-base-09'},
{value: 'yellow', name: 'theme-base-0a'},
{value: 'green', name: 'theme-base-0b'},
{value: 'sea-green', name: 'theme-base-0c'},
{value: 'blue', name: 'theme-base-0d'},
{value: 'purple', name: 'theme-base-0e'},
{value: 'brown', name: 'theme-base-0f'}
],
when: function() {
return /poole/.test(state.repo) && !state.color;
},
transform: function(color) {
state.bodyClass = color;
store.set('color', color);
state.color = color;
return color;
}
});
/**
* Get the theme to generate
*/
enquirer.question('clone', {
type: 'input',
message: 'GitHub repo to clone? (owner/name)',
when: function() {
return state.theme === 'other';
},
transform: function(repo) {
if (repo) update(repo);
if (repo && !state.exists()) {
return clone({dest: state.themes(state.theme), repo: repo});
}
}
});
/**
* Generate an assemblefile.js
*/
enquirer.question('assemblefile', {
type: 'confirm',
message: 'Want to add an assemblefile.js to build the theme?',
default: true,
when: function() {
return !fs.existsSync(path.join(app.cwd, 'assemblefile.js'));
},
transform: function(write) {
if (write) {
var dest = path.join(app.cwd, 'assemblefile.js');
var src = fs.readFileSync(template('assemblefile.js'));
return writeFile(dest, src)
.then(function() {
return new Promise(function(resolve, reject) {
app.generate(['package', 'install'], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
}
}
});
app.task('install', function(cb) {
app.npm.devDependencies(['assemble', 'assemble-hekyll', 'minimist'], cb);
});
function update(repo) {
state.repo = repo;
state.theme = path.basename(repo);
state.path = state.themes(state.theme);
store.union('choices', state.repo);
store.set('default', state.repo);
}
return enquirer;
};
/**
* Convert the given async function to a promise
*/
function promisify(fn) {
return function(...args) {
return new Promise(function(resolve, reject) {
return fn.apply(null, args.concat(function(err, ...rest) {
if (err) {
reject(err);
} else {
resolve(...rest);
}
}));
});
};
}