From 7547978eff27d5fd77cd38906ed79a4192e314cd Mon Sep 17 00:00:00 2001 From: lihaozheng001 Date: Thu, 12 Dec 2024 17:39:11 +0800 Subject: [PATCH] Add -o option to set output file path --- README.md | 20 ++++++++++++++++++-- package.json | 4 ++-- src/generate.ts | 28 ++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a105ab6..2d3e163 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ # next-routes-list + Easy generate all available routes in Next.js . ## Support + - [x] App routes - [x] Page routes ## Install + ```bash npm install --save-dev next-routes-list@latest ``` @@ -15,6 +18,7 @@ npm install --save-dev next-routes-list@latest ### 1. Run script add script to `package.json`: + ```json { "script": { @@ -22,7 +26,9 @@ add script to `package.json`: } } ``` + If you use `src` directory: + ```json { "script": { @@ -32,15 +38,17 @@ If you use `src` directory: ``` then run npm script: + ```shell npm run generate-next-routes-list ``` ### 2. Import routes + ```ts -import { routes } from 'next-routes-list' +import { routes } from "next-routes-list"; -console.log(routes) +console.log(routes); /** [ '/', @@ -51,8 +59,16 @@ console.log(routes) */ ``` +## Options + +| Option | Type | Description | Example | +| ------ | ------ | ------------------------------- | -------------------------------------------- | +| `-o` | string | set the output routes file path | `npx generate-next-routes-list -o routes.ts` | + ## Example + next projct is [here](./test/next-project/), generate result is: [here](./test/routes.js). ## Contribute + feel free to contribute anything or report any issues. diff --git a/package.json b/package.json index e0b9631..7cea6bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-routes-list", - "version": "1.1.5", + "version": "1.2.0", "description": "generate next all routes list", "keywords": [ "next", @@ -25,7 +25,7 @@ "scripts": { "build": "rm -rf dist && tsc", "prepublishOnly": "npm run build", - "test": "npm run build && cd test/next-project && node ../../dist/generate.js && node -e 'JSON.stringify(require(\"../routes.js\").routes)===JSON.stringify(require(\"../../dist/routes.js\").routes) ? console.log(\"app route test succeeded\") : throw new Error(\"app route test failed\")'" + "test": "npm run build && cd test/next-project && node ../../dist/generate.js && node -e \"if (JSON.stringify(require('../routes.js').routes) === JSON.stringify(require('../../dist/routes.js').routes)) { console.log('Test succeeded'); } else { throw new Error('Test failed'); }\"" }, "dependencies": { "list-paths": "1.0.2" diff --git a/src/generate.ts b/src/generate.ts index 72bc374..427660d 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -6,19 +6,31 @@ const routes = getNextRoutes(); // write to file const templatePath = `${process.argv[1]!.replace("/.bin", "") - .replace(/\/next-routes-list\/.*/g, "/next-routes-list") - .replace( - /\/generate-next-routes-list\/*.*/g, - "/next-routes-list" - )}/dist/routes.js`; + .replace(/\/next-routes-list\/.*/g, "/next-routes-list") + .replace( + /\/generate-next-routes-list\/*.*/g, + "/next-routes-list" + )}/dist/routes.js`; const templateString = fs.readFileSync(templatePath, "utf-8"); const newTemplateString = templateString.replace( - "const routes = [];", - `const routes = ${JSON.stringify(routes)};` + "const routes = [];", + `const routes = ${JSON.stringify(routes)};` ); fs.writeFileSync(templatePath, newTemplateString); -console.log("Routes list generated successfully!"); +console.log("✅ Routes list generated successfully!"); + +// -o options +if (process.argv[2] === "-o") { + const outputPath = process.argv[3]; + if (outputPath) { + fs.writeFileSync( + outputPath, + `export const routes = ${JSON.stringify(routes)};` + ); + console.log(`✅ Routes list saved to ${outputPath}`); + } +}