-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
287 lines (228 loc) · 8 KB
/
gulpfile.babel.js
File metadata and controls
287 lines (228 loc) · 8 KB
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
const {
parallel,
series,
src,
dest
} = require('gulp')
const del = require('delete')
const through2 = require('through2');
const rename = require('gulp-rename')
const stats = require('gulp-count-stat')
const log = require('fancy-log')
const convert = require('convert-vinyl-to-vfile')
const markdown = require('./markdown')
const writeGood = require('write-good')
const spellchecker = require('spellchecker')
const path = require('path')
const fs = require('fs')
const {
Book,
Page
} = require('book')
const glob = require('fast-glob')
const git = require('simple-git')()
const min = require('minimist')
const Shell = require('node-powershell')
const fetch = require('node-fetch')
const title = 'Sufficient Reason'
const sourceGlob = ['src/**/*.md']
const assetsGlob = ['src/assets/**']
const destination = 'html/'
const destinationGlob = 'html/**'
const publishTarget = "c:/src/BookShelf-SufficientReason/src/pages"
var book = null
function render() {
book = new Book(title, path.resolve(destination))
return src(sourceGlob)
.pipe(through2.obj(function (vinyl, _, callback) {
if (vinyl.isBuffer()) {
var vfile = convert(vinyl)
markdown.process(vfile, function (err, parsed) {
var contents
if (err) {
return callback(new Error(err))
}
logWarnings(parsed)
contents = parsed.contents
/* istanbul ignore else - There aren’t any unified compilers
* that output buffers, but this logic is here to keep allow them
* (and binary files) to pass through untouched. */
if (typeof contents === 'string') {
contents = Buffer.from(contents, 'utf8')
}
vinyl.contents = contents
if (parsed.data.metadata) {
// record the original .md file path
vinyl.pageData = parsed.data.metadata
} else {
vinyl.pageData = {
name: vinyl.stem,
order: book.allPages.length + 1
}
}
vinyl.pageData.sourcePath = parsed.path
callback(null, vinyl)
})
}
}))
.pipe(rename({
extname: ".html"
}))
.pipe(dest(destination))
.pipe(through2.obj(function (vinyl, _, callback) {
if (vinyl.pageData) {
let page = new Page(vinyl.pageData.title, path.relative(book.root, vinyl.path), vinyl.pageData.order)
page.data = vinyl.pageData
book.addPage(page)
}
callback(null, vinyl)
}))
function logWarnings(parsed) {
parsed.messages.forEach(msg => {
console.log(`'${parsed.path}' ${msg.location.start.line},${msg.location.start.column},${msg.location.end.line || msg.location.start.line},${msg.location.end.column || msg.location.start.column} ${msg.reason}`)
})
}
}
function lint() {
var options = min(process.argv.slice(2), {
string: 'file'
})
return src(options.file || sourceGlob)
.pipe(through2.obj(function (vinyl, _, callback) {
if (vinyl.isBuffer()) {
var vfile = convert(vinyl)
markdown.process(vfile, function (err, parsed) {
var contents
if (err) {
return callback(new Error(err))
}
logWarnings(parsed)
contents = parsed.contents
/* istanbul ignore else - There aren’t any unified compilers
* that output buffers, but this logic is here to keep allow them
* (and binary files) to pass through untouched. */
if (typeof contents === 'string') {
contents = Buffer.from(contents, 'utf8')
}
vinyl.contents = contents
callback(null, vinyl)
})
}
}))
function logWarnings(parsed) {
parsed.messages.forEach(msg => {
console.log(`'${parsed.path}' ${msg.location.start.line},${msg.location.start.column},${msg.location.end.line || msg.location.start.line},${msg.location.end.column || msg.location.start.column} ${msg.reason}`)
})
}
}
function writeBook(callback) {
// todo - write out a list of pages in order so that consuming apps can construct a book object?
// could also write an export for each page
fs.writeFile("html/book.js", `module.exports = ${JSON.stringify(book, null, 3)}`, err => {
if (err) throw err
log.info(`wrote book.js`)
})
callback()
}
function assets() {
return src(assetsGlob).pipe(dest(destination + "/assets"))
}
function clean(callback) {
return del(destinationGlob, callback)
}
function publish() {
log.info(`publishing to ${publishTarget}`)
return src(destinationGlob)
.pipe(dest(publishTarget))
}
function spelling() {
var options = min(process.argv.slice(2), {
string: 'file'
})
return src(options.file || sourceGlob)
.pipe(through2.obj(function (file, _, callback) {
if (file.isBuffer()) {
file.contents.toString().split("\n").forEach((line, idx) => {
let misspellings = spellchecker.checkSpelling(line)
misspellings.forEach(err => {
let word = line.substring(err.start, err.end)
let suggestions = spellchecker.getCorrectionsForMisspelling(word)
console.log(`'${file.path}' ${idx + 1}:${err.start + 1} ${word} -> ${suggestions.join(' ')}`)
})
})
callback(null, file)
}
}))
}
function count() {
return src(sourceGlob)
.pipe(stats())
}
function prose() {
var options = min(process.argv.slice(2), {
string: 'file'
})
return src(options.file || sourceGlob)
.pipe(through2.obj(function (file, _, callback) {
if (file.isBuffer()) {
file.contents.toString().split("\n").forEach((line, idx) => {
let suggestions = writeGood(line)
suggestions.forEach(sug => {
console.log(`'${file.path}' ${idx + 1}:${sug.index + 1}:${sug.offset + sug.index + 1} ${sug.reason}`)
})
})
callback(null, file)
}
}))
}
async function save(callback) {
var options = min(process.argv.slice(2), {
string: 'm'
})
if (!options.m) {
options.m = 'page edits'
}
// todo - we'll add interesting stuff to the additional data like location and weather and word count.
// maybe location as json?
// other hand, do I really want my exact location on every commit to a public repo?
var location = await getLocation()
// var additional = `LatLong: ${location.Latitude},${location.Longitude} Altitude: ${location.Altitude} Address: ${location.address}`
var additional = ``
const files = await glob.async(sourceGlob)
git.commit([options.m, additional], files, {}, (err, data) => {
if (err) {
callback(err)
}
console.log(JSON.stringify(data, null, 2))
callback()
})
}
async function getLocation() {
var ret = {}
let ps = new Shell({
executionPolicy: 'Bypass',
noProfile: true
})
await ps.addCommand('./geolocation.ps1')
ret = await ps.invoke()
ret = JSON.parse(ret.replace(/\bNaN\b/g, 'null'))
ps.dispose()
const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${ret.Latitude},${ret.Longitude}&key=${process.env.google_api}`
const loc = await (await fetch(url)).json()
ret.address = (loc.results[0] || {
formatted_address: ''
}).formatted_address
return ret
}
const build = series(clean, render, writeBook, assets)
exports.build = build
exports.publish = series(build, publish)
exports.spelling = spelling
exports.spell = spelling
exports.count = count
exports.prose = prose
exports.lint = lint
exports.render = render
exports.check = series(spelling, prose, lint, count)
exports.save = save
exports.default = build