diff --git a/package.json b/package.json index d7d071a..9be67d2 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "mkdirp": "^0.5.1", "node-fetch": "^1.7.3", "polka": "^0.3.4", - "port-authority": "^1.0.0", + "port-authority": "^1.0.2", + "pretty-bytes": "^4.0.2", "pretty-ms": "^3.1.0", "require-relative": "^0.8.7", "rimraf": "^2.6.2", diff --git a/src/cli.ts b/src/cli.ts index eeb7814..17a1a69 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -58,7 +58,7 @@ prog.command('export [dest]') try { const { build } = await import('./cli/build'); await build(); - console.error(`\n> Built in ${elapsed(start)}. Exporting...`); + console.error(`\n> Built in ${elapsed(start)}. Crawling site...`); const { exporter } = await import('./cli/export'); await exporter(dest); diff --git a/src/cli/export.ts b/src/cli/export.ts index de3c348..8843de1 100644 --- a/src/cli/export.ts +++ b/src/cli/export.ts @@ -1,10 +1,12 @@ import * as child_process from 'child_process'; import * as path from 'path'; import * as sander from 'sander'; +import * as clorox from 'clorox'; import cheerio from 'cheerio'; import URL from 'url-parse'; import fetch from 'node-fetch'; import * as ports from 'port-authority'; +import prettyBytes from 'pretty-bytes'; import { minify_html } from './utils/minify_html'; import { locations } from '../config'; @@ -41,18 +43,22 @@ export async function exporter(export_dir: string) { proc.on('message', message => { if (!message.__sapper__) return; - const url = new URL(message.url, origin); + let file = new URL(message.url, origin).pathname.slice(1); + let { body } = message; - if (saved.has(url.pathname)) return; - saved.add(url.pathname); + if (saved.has(file)) return; + saved.add(file); - if (message.type === 'text/html') { - const file = `${export_dir}/${url.pathname}/index.html`; - sander.writeFileSync(file, minify_html(message.body)); - } else { - const file = `${export_dir}/${url.pathname}`; - sander.writeFileSync(file, message.body); + const is_html = message.type === 'text/html'; + + if (is_html) { + file = file === '' ? 'index.html' : `${file}/index.html`; + body = minify_html(body); } + + console.log(`${clorox.bold.cyan(file)} ${clorox.gray(`(${prettyBytes(body.length)})`)}`); + + sander.writeFileSync(`${export_dir}/${file}`, body); }); function handle(url: URL) { @@ -79,7 +85,7 @@ export async function exporter(export_dir: string) { } }) .catch((err: Error) => { - console.error(`Error rendering ${url.pathname}: ${err.message}`); + console.log(clorox.red(`> Error rendering ${url.pathname}: ${err.message}`)); }); }