Skip to content

Commit fd5e08f

Browse files
author
Alexandre Lotte
committed
Add command line usage
1 parent a291693 commit fd5e08f

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('./dist/renderPages').default()

package-lock.json

Lines changed: 21 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
"path-to-regexp": "^6.2.0",
1818
"puppeteer": "^7.1.0",
1919
"replace-in-file": "^6.2.0",
20-
"serve-handler": "^6.1.3"
20+
"serve-handler": "^6.1.3",
21+
"yargs": "^16.2.0"
2122
},
2223
"devDependencies": {
23-
"@types/node": "^14.14.28"
24+
"@types/node": "^14.14.28",
25+
"@types/yargs": "^16.0.0"
26+
},
27+
"bin": {
28+
"hyperstatic": "./cli.js"
2429
},
2530
"repository": {
2631
"type": "git",

src/static.ts renamed to src/renderPages.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { replaceInFile } from 'replace-in-file'
66
import handler from 'serve-handler'
77
import http from 'http'
88
import events from 'events'
9+
import yargs from 'yargs'
910

1011
const createStaticServer = (port, distFolder) =>
1112
http.createServer((request, response) =>
@@ -17,15 +18,26 @@ const createStaticServer = (port, distFolder) =>
1718
})
1819
).listen(port)
1920

21+
const renderPages = async () => {
22+
23+
const argv = yargs(process.argv.slice(2))
24+
.usage('Usage: $0 [options]')
25+
.describe('port', 'Port to use for the prerendering server')
26+
.alias('p', 'port')
27+
.describe('dist', 'Directory containing built static files from the bundler')
28+
.alias('d', 'dist')
29+
.describe('entry', 'Entry page to start crawling the site from')
30+
.alias('e', 'entry')
31+
.describe('extra', 'Comma separated list of paths to render if they aren\'t automatically crawled')
32+
.alias('x', 'extra')
33+
.argv;
34+
35+
// Get command line arguments with defaults
36+
const port = argv.port ? Number(argv.port) : 54321
37+
const distFolder = argv.dist ? String(argv.dist) : 'dist'
38+
const entryPoint = argv.entry ? String(argv.entry) : '/'
39+
const extraPages = typeof argv.extra === 'string' ? argv.extra.split(',') : []
2040

21-
interface StaticOptions {
22-
port?: number;
23-
distFolder?: string;
24-
entryPoint?: string;
25-
extraPages?: string[]
26-
}
27-
28-
const renderPages = async ({ port = 54322, distFolder = 'dist', entryPoint = '/', extraPages = [] }: StaticOptions) => {
2941
try {
3042

3143
// Spin up a static server to use for prerendering with pupeteer
@@ -157,4 +169,4 @@ const renderPages = async ({ port = 54322, distFolder = 'dist', entryPoint = '/'
157169
process.exit(0)
158170
}
159171

160-
renderPages({})
172+
export default renderPages

0 commit comments

Comments
 (0)