next-dev
Version:
Tentu, berikut adalah markdown yang telah diperbaiki:
57 lines (56 loc) • 2.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const readdirp_1 = __importDefault(require("readdirp"));
const fs_1 = __importDefault(require("fs"));
const lodash_1 = __importDefault(require("lodash"));
const dir = 'src/lib/';
async function genPage(argv) {
const name = "pagePath";
const list = [];
for await (const entry of (0, readdirp_1.default)(path_1.default.join(process.cwd(), '/src/app'), { fileFilter: ['*.tsx', '!layout.tsx'] })) {
list.push(generateFunctionCode(entry.fullPath, entry.path));
}
fs_1.default.writeFileSync(path_1.default.join(process.cwd(), dir + `/${name}.tsx`), `export const ${name} = {\n${list.join(',\n')} \n};`, { encoding: 'utf8' });
console.log(`✨✨✨ file://${dir}/${name}.ts DONE ✨✨✨`);
}
function generateFunctionName(filePath) {
let baseFunctionName = filePath
.replace('/page.tsx', '')
.replace('.tsx', '')
.replace(/[^\w\s]/g, ' ')
.replace(/\s+/g, ' ')
.trim()
.split(' ')
.map((word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1))
.join('');
// Tambahkan "By" untuk setiap parameter
const params = extractParams(filePath);
if (params.length > 0) {
const paramPart = params.map((param) => 'By' + param.charAt(0).toUpperCase() + param.slice(1)).join('');
baseFunctionName += paramPart;
}
return baseFunctionName;
}
function extractParams(filePath) {
const matches = filePath.match(/\[([^\]]+)\]/g);
return matches ? matches.map(param => param.replace(/[\[\]]/g, '')) : [];
}
function generateFunctionCode(fullPath, filePath) {
const functionName = generateFunctionName(filePath).replace("page", "home");
const params = extractParams(filePath);
const paramString = params.map(param => `${param}: string`).join(', ');
const paramObjString = params.join(', ');
const apiPath = filePath.replace(/\[([^\]]+)\]/g, '${$1}').replace(/\\/g, '/').replace("/page.tsx", "").replace("page.tsx", "");
return `
/**
* [${fullPath}](file://${fullPath})
*/
${functionName} : (${lodash_1.default.isEmpty(params) ? "" : "{" + paramObjString + "}"}${lodash_1.default.isEmpty(params) ? "" : ":{" + paramString + "}"}) => {
return \`/${apiPath}\`;
}`;
}
exports.default = genPage;