const fs = require('fs'); const glob = require('glob'); const chalk = require('chalk'); const chokidar = require('chokidar'); const framer = require('code-frame'); const { locate } = require('locate-character'); const { dev } = require('./config.js'); let templates; function error(e) { if (e.title) console.error(chalk.bold.red(e.title)); if (e.body) console.error(chalk.red(e.body)); if (e.url) console.error(chalk.cyan(e.url)); if (e.frame) console.error(chalk.grey(e.frame)); process.exit(1); } function create_templates() { templates = glob.sync('*.html', { cwd: 'templates' }) .map(file => { const template = fs.readFileSync(`templates/${file}`, 'utf-8'); const status = file.replace('.html', '').toLowerCase(); if (!/^[0-9x]{3}$/.test(status)) { error({ title: `templates/${file}`, body: `Bad template — should be a valid status code like 404.html, or a wildcard like 2xx.html` }); } const index = template.indexOf('%sapper.main%'); if (index !== -1) { // TODO remove this in a future version const { line, column } = locate(template, index, { offsetLine: 1 }); const frame = framer(template, line, column); error({ title: `templates/${file}`, body: `