-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·207 lines (173 loc) · 5.54 KB
/
Copy pathindex.js
File metadata and controls
executable file
·207 lines (173 loc) · 5.54 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
#!/usr/bin/env node
import program from 'commander'
import path from 'path'
import fs from 'fs'
import http from 'http'
import https from 'https'
import express from 'express'
import serveStatic from 'serve-static'
import fallback from 'express-history-api-fallback'
import mkdirp from 'mkdirp'
import { exec } from 'child-process-promise'
import Bluebird from 'bluebird'
import sitemap from 'sitemap'
import Nightmare from 'nightmare'
import { minify } from 'html-minifier'
import debugFactory from 'debug'
const debug = debugFactory('prep')
const { version } = require('../package.json')
let buildDir, targetDir, tmpDir
async function crawlAndWrite (configuration) {
// prepare configuration
const dimensions = Object.assign({}, {width: 1440, height: 900}, configuration.dimensions)
delete configuration.dimensions
configuration = Object.assign({}, {
routes: ['/'],
timeout: 1000,
dimensions,
https: false,
hostname: 'http://localhost',
useragent: 'Prep',
minify: false,
concurrency: 4,
additionalSitemapUrls: [],
flatStructure: false,
}, configuration)
debug('Config prepared', configuration)
// render sitemap
const sitemapUrs = configuration.routes.map(route => ({url: route}))
.concat(configuration.additionalSitemapUrls.map(route => ({url: route})))
const sm = sitemap.createSitemap({
hostname: configuration.hostname,
urls: sitemapUrs,
})
mkdirp.sync(targetDir)
fs.writeFileSync(`${targetDir}/sitemap.xml`, sm.toString());
debug('Sitemap created')
// start temporary local webserver
const app = express()
.use(serveStatic(buildDir))
.use(fallback('index.html', { root: buildDir }))
let server
if (configuration.https) {
const credentials = {
key: fs.readFileSync(`${__dirname}/../ssl/key.pem`),
cert: fs.readFileSync(`${__dirname}/../ssl/cert.pem`),
}
server = https.createServer(credentials, app)
} else {
server = http.createServer(app)
}
server.listen(program.port)
debug('Server started')
// render routes
const promises = configuration.routes.map((route) => async () => {
let retryCount = 0
while (retryCount < 10) {
try {
await prepRoute(route, configuration)
return
} catch (e) {
retryCount++
console.warn(`Retry ${retryCount} for route: ${route}`)
}
}
})
// clean up files
await Bluebird.map(promises, fn => fn(), {concurrency: configuration.concurrency})
server.close()
await exec(`cp -rf "${tmpDir}"/* "${targetDir}"/`)
await exec(`rm -rf "${tmpDir}"`)
process.exit(0)
}
async function prepRoute (route, configuration) {
// remove leading slash from route
route = route.replace(/^\//, '')
const nightmare = Nightmare({
show: false,
switches: {
'ignore-certificate-errors': true,
},
})
debug('Nightmare started')
const url = `http${configuration.https ? 's' : ''}://localhost:${program.port}/${route}`
const content = await nightmare
.useragent(configuration.useragent)
.viewport(configuration.dimensions.width, configuration.dimensions.height)
.goto(url)
.evaluate(() => false) // wait until page loaded
.wait(configuration.timeout)
.evaluate(() => document.documentElement.outerHTML)
.end()
debug('Crawling completed: %s', url)
const cleanRoute = route.replace('.html', '')
let filePath
if (configuration.flatStructure) {
filePath = tmpDir
mkdirp.sync(filePath)
} else {
filePath = path.join(tmpDir, cleanRoute)
mkdirp.sync(filePath)
debug('Directory created: %s', filePath)
}
if (configuration.minify) {
const minifyConfig = configuration.minify === true ? {} : configuration.minify
const minifiedContent = minify(content, minifyConfig)
if (configuration.flatStructure) {
fs.writeFileSync(path.join(filePath, `${cleanRoute}.html`), minifiedContent)
} else {
fs.writeFileSync(path.join(filePath, 'index.html'), minifiedContent)
}
} else {
if (configuration.flatStructure) {
fs.writeFileSync(path.join(filePath, `${cleanRoute}.html`), content)
} else {
fs.writeFileSync(path.join(filePath, 'index.html'), content)
}
}
let logFileName
if (configuration.flatStructure) {
logFileName = `${cleanRoute}.html`.replace(/^\//, '')
} else {
logFileName = `${route}/index.html`.replace(/^\//, '')
}
console.log(`prep: Rendered ${logFileName}`)
}
async function run () {
try {
program
.version(version)
.description('Server-side rendering tool for your web app.\n Prerenders your app into static HTML files and supports routing.')
.arguments('<build-dir> [target-dir]')
.option('-c, --config [path]', 'Config file (Default: prep.js)', 'prep.js')
.option('-p, --port [port]', 'Temporary webserver port (Default: 45678)', 45678)
.action((bdir, tdir) => {
if (!bdir) {
console.log('No target directory provided.')
process.exit(1)
}
buildDir = path.resolve(bdir)
targetDir = tdir ? path.resolve(tdir) : buildDir
tmpDir = path.resolve('.prep-tmp')
})
program.parse(process.argv)
if (!buildDir) {
program.help()
}
const config = require(path.resolve(program.config)).default
if (typeof config === 'function') {
const fnConfig = config()
if (Promise.resolve(fnConfig) === fnConfig) {
await crawlAndWrite(await fnConfig)
} else {
await crawlAndWrite(fnConfig)
}
} else {
await crawlAndWrite(config)
}
} catch (e) {
console.log(e)
process.exit(1)
}
}
run()