UNPKG

pathpida

Version:

TypeScript friendly pages and static path generator for Next.js

41 lines 2.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createStaticTemplate = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const isIgnored_1 = require("./isIgnored"); const replaceWithUnderscore_1 = require("./replaceWithUnderscore"); const createStaticTemplate = (input, basepath, ignorePath) => { const ig = (0, isIgnored_1.createIg)(ignorePath); const createPublicString = (targetDir, parentIndent, url, text) => { const indent = ` ${parentIndent}`; const files = fs_1.default.readdirSync(targetDir).sort(); const replacedFiles = files.map(replaceWithUnderscore_1.replaceWithUnderscore); const duplicatedInfo = replacedFiles.reduce((a, b, i) => ({ ...a, [b]: [...(a[b] ?? []), i] }), {}); const props = files .map((file, i) => { const newUrl = `${url}/${file}`; const target = path_1.default.posix.join(targetDir, file); if ((0, isIgnored_1.isIgnored)(ig, ignorePath, targetDir, file)) return ''; const replacedFile = replacedFiles[i]; const valFn = `${indent}${duplicatedInfo[replacedFile].length > 1 ? `${replacedFile}_${duplicatedInfo[replacedFile].indexOf(i)}` : replacedFile}: <% next %>`; return fs_1.default.statSync(target).isFile() ? valFn.replace('<% next %>', `'${newUrl}'`) : fs_1.default.statSync(target).isDirectory() ? createPublicString(target, indent, newUrl, valFn.replace('<% next %>', `{\n<% props %>\n${indent}}`)) : ''; }) .filter(Boolean); return text.replace('<% props %>', props.join(',\n')); }; const text = createPublicString(input, '', typeof basepath === 'string' ? basepath.replace(/\/+$/, '') : '', '{\n<% props %>\n} as const;'); return `\nexport const staticPath = ${text}\n\nexport type StaticPath = typeof staticPath;\n`; }; exports.createStaticTemplate = createStaticTemplate; //# sourceMappingURL=createStaticTemplate.js.map