pathpida
Version:
TypeScript friendly pages and static path generator for Next.js
89 lines • 5.12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAppDir = 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: (url${importName?.startsWith('Query') ? '' : '?'}: { ${importName ? `query${importName.startsWith('Optional') ? '?' : ''}: ${importName}, ` : ''}hash?: string }) => ({ 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, path: \`${pathname
.replace(/\[\[?\.\.\.(.*?)\]\]?/g, `\${$1?.join('/')}`)
.replace(/\[(.*?)\]/g, `\${$1}`)}\${buildSuffix(url)}\` })`;
exports.createMethods = createMethods;
const parseAppDir = (input, output, ignorePath) => {
const ig = (0, isIgnored_1.createIg)(ignorePath);
const pageFileNames = ['page.tsx', 'page.jsx', 'page.js'];
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) => [
!(0, isIgnored_1.isIgnored)(ig, ignorePath, targetDir, file),
fs_1.default.statSync(path_1.default.posix.join(targetDir, file)).isDirectory(),
].every(Boolean))
.sort()
.map((file) => {
const newSlugs = [...slugs];
const target = path_1.default.posix.join(targetDir, file);
if (file.startsWith('(') && file.endsWith(')')) {
const indexFile = fs_1.default.readdirSync(target).find((name) => pageFileNames.includes(name));
return createPathObjString(target, indent.slice(2), url, newSlugs, '<% props %>', indexFile &&
(0, exports.createMethods)(indent.slice(2), getImportName(path_1.default.posix.join(target, indexFile)), newSlugs, url));
}
const isParallelRoute = file.startsWith('@');
const newUrl = isParallelRoute ? url : `${url}/${file}`;
let valFn = `${indent}'${(0, replaceWithUnderscore_1.replaceWithUnderscore)(file)}': {\n<% next %>\n${indent}}`;
if (file.startsWith('[') && file.endsWith(']')) {
const slug = file.replace(/[.[\]]/g, '');
valFn = `${indent}${`_${slug}`}: (${slug}${file.startsWith('[[') ? '?' : ''}: ${/\[\./.test(file) ? 'string[]' : 'string | number'}) => ({\n<% next %>\n${indent}})`;
newSlugs.push(slug);
}
const indexFile = fs_1.default.readdirSync(target).find((name) => pageFileNames.includes(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));
})
.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) => pageFileNames.includes(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.parseAppDir = parseAppDir;
//# sourceMappingURL=parseAppDir.js.map