-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
206 lines (173 loc) · 4.38 KB
/
generator.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
'use strict';
var path = require('path');
var del = require('delete');
var clone = require('gh-clone');
var hekyll = require('hekyll');
var DataStore = require('data-store');
var writeJson = require('write-json');
var isValid = require('is-valid-app');
var prompts = require('./prompts');
/**
* generate-hekyll
*/
module.exports = function(app) {
if (!isValid(app, 'generate-hekyll')) return;
app.on('error', console.log.bind(console));
app.task('noop', function() {});
/**
* Defaults
*/
var store = new DataStore({path: path.join(__dirname, 'themes.json')});
var themes = path.resolve.bind(path, __dirname, 'themes');
var dest = path.resolve.bind(path, app.cwd, 'src');
var state = { themes: themes, dest: dest };
var enquirer = app.enquirer = prompts(app, store, state);
/**
* Register plugins
*/
app.use(require('generate-defaults'));
app.use(require('generate-package'));
/**
* Scaffold out a new handlebars blog or site from a jekyll theme.
*
* ```sh
* $ gen hekyll
* $ gen hekyll:default
* ```
* @name default
* @api public
*/
app.task('default', ['hekyll']);
app.task('hekyll', function() {
return enquirer.prompt('theme')
.then(task('prompt-clone'))
.then(task('clone'))
.then(task('prompt-color'))
.then(task('convert-to-handlebars'));
});
/**
* Prompt for the color to use. Only runs if an applicable theme
* was chosen by the user.
*
* ```sh
* $ gen hekyll:prompt-color
* ```
* @name prompt-color
*/
app.task('prompt-color', function(cb) {
return enquirer.prompt('color');
});
/**
* Prompt the user to clone a theme repository from GitHub.
*
* ```sh
* $ gen hekyll:prompt-clone
* ```
* @name prompt-clone
*/
app.task('prompt-clone', function() {
return enquirer.prompt('clone');
});
/**
* Clone a theme repository from GitHub, if specified by the user.
*
* ```sh
* $ gen hekyll:clone
* ```
* @name clone
*/
app.task('clone', function() {
if (!state.exists()) {
return clone({dest: themes(state.theme), repo: state.repo});
}
return Promise.resolve(null);
});
/**
* Convert Jekyll theme to handlebars
*/
app.task('convert-to-handlebars', [
'hekyll-build',
'theme-json',
'theme-custom',
'prompt-assemblefile'
]);
/**
* Run [hekyll][]'s `.build` method with the given theme
* and destination directory.
*
* ```sh
* $ gen hekyll:hekyll-build
* ```
* @name default
*/
app.task('hekyll-build', function() {
return hekyll.build({cwd: themes(state.theme), destBase: dest()});
});
/**
* Get `theme.json` config file, if one exists for the chosen theme.
*/
app.task('theme-json', function(cb) {
return writeJson(dest('_data/theme.json'), state);
});
/**
* Get custom `themefile.js`, if one exists for the chosen theme.
*/
app.task('theme-custom', function(cb) {
var patterns = ['templates/themefile.js', `templates/${state.theme}/*.js`];
return app.src(patterns, {cwd: __dirname})
.pipe(app.dest(dest()));
});
/**
* Prompt the user to generate an `assemblefile.js`.
*
* ```sh
* $ gen hekyll:prompt-assemblefile
* ```
* @name prompt-assemblefile
*/
app.task('prompt-assemblefile', function() {
return enquirer.prompt('assemblefile');
});
/**
* Generate an assemblefile.js to the current working directory.
*/
app.task('assemblefile', function(cb) {
return app.src('templates/assemblefile.js', {cwd: __dirname})
.pipe(app.dest(dest()));
});
/**
* Delete themes that have been cloned and cached by `generate-hekyll`
* from the `generate-hekyll/themes` directory. This does not delete
* your copy of the theme, only the cached version used by `generate-hekyll`.
*
* ```sh
* $ gen hekyll:delete
* ```
* @name delete
* @api public
*/
app.task('delete', function(cb) {
store.del('choices');
return del(path.resolve(__dirname, 'themes'));
});
/**
* For unit tests
*/
app.task('hekyll-test', function(cb) {
app.enable('hekyll-test');
cb();
});
function task(name) {
return function() {
return new Promise(function(resolve, reject) {
app.build(name, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
};
}
};