UNPKG

pathpida

Version:

TypeScript friendly pages and static path generator for Next.js

96 lines 5.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parsePagesDir = exports.createMethods = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const isIgnored_1 = require("./isIgnored"); const parseQueryFromTS_1 = require("./parseQueryFromTS"); const replaceWithUnderscore_1 = require("./replaceWithUnderscore"); const createMethods = (indent, importName, slugs, pathname) => `${indent} $url: ${((opt) => `(url${opt ? '?' : ''}: { ${importName ? `query${opt ? '?' : ''}: ${importName}${opt ? ' | undefined' : ''}, ` : ''}hash?: string | undefined }${opt ? ' | undefined' : ''})`)(!importName?.startsWith('Query'))} => ({ pathname: '${pathname}' as const${slugs.length ? `, query: { ${slugs.join(', ')}${importName ? `, ...url${importName.startsWith('Query') ? '' : '?'}.query` : ''} }` : importName ? `, query: url${importName.startsWith('Query') ? '' : '?'}.query` : ''}, hash: url${importName?.startsWith('Query') ? '' : '?'}.hash })`; exports.createMethods = createMethods; const parsePagesDir = (input, output, ignorePath, pageExtensions = ['tsx', 'ts', 'jsx', 'js']) => { const ig = (0, isIgnored_1.createIg)(ignorePath); const regExpChunk = `\\.(${pageExtensions.join('|').replace(/\./g, '\\.')})$`; const indexPageRegExp = new RegExp(`^index${regExpChunk}`); const pageExtRegExp = new RegExp(regExpChunk); const imports = []; const getImportName = (file) => { const result = (0, parseQueryFromTS_1.parseQueryFromTS)(output, file); if (result) { imports.push(result.importString); return result.importName; } }; const createPathObjString = (targetDir, parentIndent, url, slugs, text, methodsOfIndexTsFile) => { const indent = ` ${parentIndent}`; const props = fs_1.default .readdirSync(targetDir) .filter((file) => [ !file.startsWith('_'), !file.endsWith('.d.ts'), `${url}/${file}` !== '/api', !(0, isIgnored_1.isIgnored)(ig, ignorePath, targetDir, file), fs_1.default.statSync(path_1.default.posix.join(targetDir, file)).isDirectory() || pageExtRegExp.test(file), ].every(Boolean)) .sort() .map((file) => { const newSlugs = [...slugs]; const basename = file.replace(pageExtRegExp, ''); const newUrl = `${url}/${basename}`; let valFn = `${indent}'${(0, replaceWithUnderscore_1.replaceWithUnderscore)(basename)}': {\n<% next %>\n${indent}}`; if (basename.startsWith('[') && basename.endsWith(']')) { const slug = basename.replace(/[.[\]]/g, ''); const opt = basename.startsWith('[['); valFn = `${indent}${`_${slug}`}: (${slug}${opt ? '?' : ''}: ${/\[\./.test(basename) ? `string[]${opt ? ' | undefined' : ''}` : `string | number${opt ? ' | undefined' : ''}`}) => ({\n<% next %>\n${indent}})`; newSlugs.push(slug); } const target = path_1.default.posix.join(targetDir, file); if (fs_1.default.statSync(target).isFile() && basename !== 'index') { return valFn.replace('<% next %>', (0, exports.createMethods)(indent, getImportName(target), newSlugs, newUrl)); } else if (fs_1.default.statSync(target).isDirectory()) { const indexFile = fs_1.default.readdirSync(target).find((name) => indexPageRegExp.test(name)); return createPathObjString(target, indent, newUrl, newSlugs, valFn.replace('<% next %>', '<% props %>'), indexFile && (0, exports.createMethods)(indent, getImportName(path_1.default.posix.join(target, indexFile)), newSlugs, newUrl)); } return ''; }) .filter(Boolean); const joinedProps = props .reduce((accumulator, current) => { const last = accumulator[accumulator.length - 1]; if (last !== undefined) { const [a, ...b] = last.split('\n'); const [x, ...y] = current.split('\n'); if (a === x) { y.pop(); const z = y.pop(); const merged = [a, ...y, `${z},`, ...b].join('\n'); return [...accumulator.slice(0, -1), merged]; } } return [...accumulator, current]; }, []) .join(',\n'); return text.replace('<% props %>', `${joinedProps}${methodsOfIndexTsFile ? `${props.length ? ',\n' : ''}${methodsOfIndexTsFile}` : ''}`); }; const rootIndexFile = fs_1.default.readdirSync(input).find((name) => indexPageRegExp.test(name)); const rootIndent = ''; let rootMethods; if (rootIndexFile) { rootMethods = (0, exports.createMethods)(rootIndent, getImportName(path_1.default.posix.join(input, rootIndexFile)), [], '/'); } const text = createPathObjString(input, rootIndent, '', [], '<% props %>', rootMethods); return { imports, text }; }; exports.parsePagesDir = parsePagesDir; //# sourceMappingURL=parsePagesDir.js.map