import {promises as fsp} from 'fs';
import {join} from 'path';
import nunjucks from 'nunjucks';
import {renderTemplate, months} from './html';

async function entry(): Promise<void> {
  const redirectDirectory: string = join(__dirname, '../../public/posts/');
  await fsp.mkdir(redirectDirectory, {recursive: true});

  nunjucks.configure(join(__dirname, '../pages/templates/'), {
    lstripBlocks: true,
    throwOnUndefined: true,
    trimBlocks: true
  });

  for (let year = 2018; year < new Date().getFullYear() + 1; year++) {
    for (let monthNumber = 1; monthNumber < 13; monthNumber++) {
      if (year === 2018 && monthNumber < 5) {
        continue;
      }

      const month: string = months[monthNumber];
      await renderTemplate(
        'redirect.html',
        join(redirectDirectory, `${month}-${year}.html`),
        {
          month,
          year
        }
      );
    }
  }
}

if (require.main === module) {
  void entry();
}
