From f9f4ae3460e5f7ecd34c60ad53dd89a6244c97c6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 29 Jun 2019 17:21:43 +0100 Subject: [PATCH] Add progress bar when outputting pages --- index.ts | 20 +++++++++++++++++--- package-lock.json | 13 +++++++++++++ package.json | 2 ++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 9c941a2..ecd52db 100644 --- a/index.ts +++ b/index.ts @@ -5,9 +5,11 @@ import { readFileSync, writeFileSync } from 'fs'; import Handlebars from 'handlebars'; import mkdirp from 'mkdirp'; import { range } from 'underscore'; +import ProgressBar from 'progress'; const BUILD_DIR = join(__dirname, 'build'); const SRC_DIR = join(__dirname, 'src'); +const PROGRESS_BAR_FORMAT = "[:bar] :rate/ps :percent"; const BUNDLER_OPTIONS = { outDir: BUILD_DIR, @@ -15,6 +17,10 @@ const BUNDLER_OPTIONS = { minify: true, }; +function statusOutput(message: string) { + console.log("> " + message + "..."); +} + function writeTemplate(content: string, value: string) { const outputFile = join(BUILD_DIR, value, 'index.html'); mkdirp.sync(dirname(outputFile)); @@ -29,14 +35,22 @@ function renderTemplate(template: HandlebarsTemplateDelegate, value: number) { (async function() { rimraf.sync(BUILD_DIR); + statusOutput("Creating template"); const bundler = new Bundler(join(SRC_DIR, 'template.html'), BUNDLER_OPTIONS); - await bundler.bundle(); + statusOutput("Compiling HTML template"); const template = Handlebars.compile(readFileSync(join(BUILD_DIR, 'template.html')).toString()); - range(0, 10, 0.5).forEach((i) => { - console.log(i); + const possibleValues = range(0, 10, 0.5); + const bar = new ProgressBar(PROGRESS_BAR_FORMAT, { + total: possibleValues.length, + width: 40 + }); + + statusOutput("Generating pages"); + possibleValues.forEach((i) => { renderTemplate(template, i); + bar.tick(); }); })(); diff --git a/package-lock.json b/package-lock.json index 9624519..4c59432 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1074,6 +1074,14 @@ "@types/express-serve-static-core": "*" } }, + "@types/progress": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@types/progress/-/progress-2.0.3.tgz", + "integrity": "sha512-bPOsfCZ4tsTlKiBjBhKnM8jpY5nmIll166IPD58D92hR7G7kZDfx5iB9wGF4NfZrdKolebjeAr3GouYkSGoJ/A==", + "requires": { + "@types/node": "*" + } + }, "@types/q": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.2.tgz", @@ -5295,6 +5303,11 @@ "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" }, + "progress": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", + "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==" + }, "proto-list": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", diff --git a/package.json b/package.json index 7752e94..7efd724 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@types/mkdirp": "^0.5.2", "@types/node": "^12.0.10", "@types/parcel-bundler": "^1.12.0", + "@types/progress": "^2.0.3", "@types/rimraf": "^2.0.2", "@types/underscore": "^1.9.1", "animate.css": "3.7.0", @@ -27,6 +28,7 @@ "handlebars": "^4.1.2", "mkdirp": "^0.5.1", "parcel-bundler": "1.12.3", + "progress": "^2.0.3", "rimraf": "^2.6.3", "sass": "^1.22.1", "ts-node": "8.3.0",