-
Notifications
You must be signed in to change notification settings - Fork 14
/
make.js
289 lines (220 loc) · 6.38 KB
/
make.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Dependencies {{{
// ================
const CombinedStream = require('combined-stream')
const es = require('event-stream')
const exec = require('mz/child_process').exec
const filter = require('through2-filter')
const fs = require('mz/fs')
const fse = require('fs-extra-promise')
const map = require('through2-map')
const path = require('path')
const promisePipe = require('promisepipe')
const readline = require('readline')
const request = require('request')
const sassdoc = require('sassdoc')
const screenshot = require('atom-screenshot')
const split = require('split')
const yaml = require('js-yaml')
const zip = require('array-zip')
// }}}
// Helpers {{{
// ===========
const repoUrl = 'https://raw.githubusercontent.com/SassDoc/sassdoc/master'
// Say what I'm doing.
function im(...args) {
console.log()
console.log(...args)
console.log()
}
// Get a file as URL.
const furl = file => `file://${path.resolve(file)}`
// Return a callback to write given file.
const writeFile = (file, options) =>
buffer => fs.writeFile(file, buffer, options)
// Filter stream chunks with a count.
const filterCount = (f, i=0) =>
filter(line => f(line, ++i))
// Read a YAML file.
const yamlf = async file =>
yaml.safeLoad(await fs.readFile(file))
const shot = options =>
screenshot({
...options,
css: '::-webkit-scrollbar { display: none; }',
})
// Tasks container.
const t = {}
// }}}
// Pages {{{
// =========
// Turn GitHub code blocks in Liquide code blocks.
const convertCodeBlocks = line =>
line
.replace(/^( *)```([a-z0-9]+)$/, '$1{% highlight $2 %}')
.replace(/^( *)```$/, '$1{% endhighlight %}')
async function page(local, remote, header) {
im(`Retrieving \`${remote}\` from main repository to \`${local}\`.`)
const url = `${repoUrl}/${remote}`
const output = fs.createWriteStream(local)
output.write(header)
await promisePipe(
request(url),
split(/(\n)/),
filterCount((line, i) => i > 4),
map({ wantStrings: true }, convertCodeBlocks),
output
)
}
t.changelog = () =>
page(
'changelog/index.md',
'CHANGELOG.md',
'---\nlayout: default\ntitle: "Changelog"\n---\n\n'
)
t.upgrading = () =>
page(
'upgrading/index.md',
'UPGRADE-2.0.md',
'---\nlayout: default\ntitle: "Upgrade from 1.0 to 2.0"\n---\n\n'
)
// }}}
// Themes {{{
// ==========
const themes = ['default', 'vulcan', 'neat', 'flippant' /*, 'rest' */]
const themeDirs = themes.map(x => `node_modules/sassdoc-theme-${x}`)
const themePackages = themeDirs.map(x => `${x}/package.json`)
const themeGallery = 'theme-gallery'
const sampleDir = `${themeGallery}/sample`
const defaultTheme = 'https://github.com/SassDoc/sassdoc-theme-default'
// Sample {{{
// ----------
t.sample = async () => {
if (!await fs.exists(sampleDir)) {
im('Cloning default theme to get sample SCSS files.')
await exec(`git clone '${defaultTheme}' '${sampleDir}-git'`)
await fs.rename(`${sampleDir}-git/scss/utils`, sampleDir)
await fse.remove(`${sampleDir}-git`)
}
}
// }}}
// Gallery {{{
// -----------
t['theme-gallery'] = async () => {
// Ensure sample directory exists.
await t.sample()
const previewDir = `${themeGallery}/preview`
const thumbDir = `${themeGallery}/thumbs`
await Promise.all([previewDir, thumbDir].map(x => fse.mkdirs(x)))
for (let theme of process.env.THEME ? [process.env.THEME] : themes) {
const themePackage = themePackages[themes.indexOf(theme)]
const preview = `${previewDir}/${theme}`
const thumb = `${thumbDir}/${theme}.png`
im(`Compiling \`${theme}\` theme in \`${preview}\`.`)
await sassdoc(sampleDir, {
dest: preview,
package: themePackage,
theme,
verbose: true,
})
im(`Taking a screenshot of \`${theme}\` theme in \`${thumb}\`.`)
await shot({
url: furl(`${preview}/index.html`),
width: 1024,
height: 768,
})
.then(writeFile(thumb))
await exec(`mogrify -resize 256x192 '${thumb}'`)
}
}
// }}}
t.themes = async () => {
const file = '_data/themes.yml'
const output = fs.createWriteStream(file)
const combined = CombinedStream.create()
im(`Generating \`${file}\`.`)
for (let [theme, themePackage] of zip(themes, themePackages)) {
combined.append(`${theme}: `)
combined.append(fs.createReadStream(themePackage))
}
await promisePipe(combined, output)
}
// }}}
// Preview {{{
// ===========
t.preview = async () => {
// Ensure sample directory exists.
await t.sample()
const preview = 'assets/images/preview-image.png'
im('Compiling `default` theme in `.preview`.')
await sassdoc(sampleDir, {
dest: '.preview',
package: `node_modules/sassdoc-theme-default/package.json`,
verbose: true,
})
im(`Taking a screenshot in \`${preview}\`.`)
await shot({
url: furl('.preview/index.html'),
width: 1200,
height: 675,
})
.then(writeFile(preview))
await fse.remove('.preview')
}
// }}}
// Gallery {{{
// ===========
t.gallery = async () => {
const gallery = await yamlf('_data/gallery.yml')
const galleryDir = 'assets/images/gallery'
const images = gallery
.map(x => x.image)
.map(x => `${galleryDir}/${x}`)
// Ensure directory exists.
await fse.mkdirs(galleryDir)
const filter = !process.env.SITE
? () => true
: x => x.image === `${process.env.SITE}.png`
await Promise.all(
gallery
.filter(filter)
.map(async x => {
const file = `${galleryDir}/${x.image}`
await shot({
url: x.url,
width: 1440,
height: 900,
})
.then(writeFile(file))
await exec(`mogrify -resize 900x '${file}'`)
})
)
}
// }}}
// Execution {{{
// =============
async () => {
const targets = process.argv.slice(2)
// Show existing targets.
if (!targets.length) {
console.error('Please specify a target. Available targets:')
Object.keys(t).map(x => ` ${x}`).forEach(console.error)
process.exit(1)
}
// Show unknown targets and exit if any.
targets
.filter(target => !(target in t))
.map(target => console.error(`Unknown target \`${target}\`.`))
.forEach(() => process.exit(1))
const throwNextTick = err =>
setImmediate(() => { throw err })
// Call all targets, wait promises.
await Promise.all(
targets
.map(target => t[target]())
.filter(p => p instanceof Promise)
.map(p => p.catch(throwNextTick))
)
// Close screenshot service if needed.
screenshot.close()
}()
// }}}