diff --git a/index.ts b/index.ts index 5f23f5a..a23a53f 100644 --- a/index.ts +++ b/index.ts @@ -14,6 +14,11 @@ interface Account { link: string; } +interface Redirect { + src: string; + dest: string; +} + const BUILD_DIR = join(__dirname, 'build'); const SRC_DIR = join(__dirname, 'src'); const PROGRESS_BAR_FORMAT = '[:bar] :rate/ps :percent :current/:total'; @@ -59,6 +64,13 @@ function writeTemplate( writeFileSync(outputFile, template(context)); } +function writeRedirects(redirects: ReadonlyArray) { + const template = Handlebars.compile( + readFileSync(join(SRC_DIR, 'redirects.txt')).toString() + ); + writeFileSync(join(BUILD_DIR, '_redirects'), template({ redirects })); +} + function humanize(value: number) { if (isPrecision(value, 0)) { return value.toString(); @@ -95,6 +107,8 @@ function humanize(value: number) { accounts, }; + const redirects: Redirect[] = []; + statusOutput('Generating pages'); possibleValues.forEach(i => { if (i) { @@ -106,10 +120,22 @@ function humanize(value: number) { }; writeTemplate(template, value.toString(), context); if (isPrecision(value, 1)) { - writeTemplate(template, value.toString() + '0', context); + redirects.push({ + src: value.toString() + '0', + dest: value.toString(), + }); + // writeTemplate(template, value.toString() + '0', context); } if (isPrecision(value, 0)) { - writeTemplate(template, value.toString() + '.00', context); + redirects.push({ + src: value.toString() + '.00', + dest: value.toString(), + }); + redirects.push({ + src: value.toString() + '.0', + dest: value.toString(), + }); + // writeTemplate(template, value.toString() + '.00', context); } } bar.tick(); @@ -117,4 +143,5 @@ function humanize(value: number) { writeTemplate(template, '', { ...baseContext, displayValue: 'money' }); const filesOutput = glob.sync(join(BUILD_DIR, '**/index.html')).length; console.log(`Generated ${filesOutput} files.`); + writeRedirects(redirects); })(); diff --git a/src/redirects.txt b/src/redirects.txt new file mode 100644 index 0000000..d567080 --- /dev/null +++ b/src/redirects.txt @@ -0,0 +1,3 @@ +{{#each redirects }} +/{{src}} /{{dest}} 200 +{{/each}}