'use strict'; const standalone = require('@adonisjs/core/build/standalone'); const fs = require('fs'); const path = require('path'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const path__default = /*#__PURE__*/_interopDefaultCompat(path); class Prerender extends standalone.BaseCommand { ensureDirectoryExistence(filePath) { let dirname = path__default.dirname(filePath); if (fs.existsSync(dirname)) { return true; } this.ensureDirectoryExistence(dirname); fs.mkdirSync(dirname); } async writeRoute(path2, ctx, handler) { await handler(ctx); const html = ctx.response.getBody(); this.ensureDirectoryExistence(path2); fs.writeFileSync(path2, html); } async run() { if (!this.output) { this.output = this.application.directoriesMap.get("public"); } const { getStaticPaths } = await import(this.application.makePath( this.application.directoriesMap.get("ssg") )); const staticPaths = getStaticPaths(); const Router = this.application.container.use("Adonis/Core/Route"); Router.commit(); const routes = Router.toJSON().root.filter( ({ pattern, methods }) => !pattern.includes("*") && methods.includes("GET") && Object.keys(staticPaths).includes(pattern) ); const HttpContext2 = this.application.container.use( "Adonis/Core/HttpContext" ); await Promise.all( routes.map(async (route) => { const { pattern, meta } = route; const { params } = route; if (params.length > 0) { const paths = await staticPaths[pattern](); for (let i = 0; i < paths.length; i++) { const ctx2 = HttpContext2.create(pattern, paths[i].params); ctx2.route = route; let path2 = pattern.replace( `:${params[0]}`, paths[i].params[params[0]] ); for (let j = 1; j < params.length; j++) { path2 = pattern.replace( `:${params[j]}`, paths[i].params[params[j]] ); } this.logger.info(`Prerendering ${path2}`); await this.writeRoute( this.application.makePath(this.output, path2, "index.html"), ctx2, meta.finalHandler ); return Promise.resolve(() => { }); } } const ctx = HttpContext2.create(pattern, /* @__PURE__ */ new Map()); ctx.route = route; await this.writeRoute( this.application.makePath(this.output, pattern, "index.html"), ctx, meta.finalHandler ); return Promise.resolve(() => { }); }) ); } } Prerender.commandName = "prerender:routes"; Prerender.description = "Prerender routes"; Prerender.settings = { loadApp: true, stayAlive: false }; module.exports = Prerender;