Skip to content

Commit

Permalink
Add -o option to set output file path
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa committed Dec 12, 2024
1 parent d2d6f55 commit 7547978
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
Expand All @@ -15,14 +18,17 @@ npm install --save-dev next-routes-list@latest
### 1. Run script

add script to `package.json`:

```json
{
"script": {
"generate-next-routes-list": "generate-next-routes-list"
}
}
```

If you use `src` directory:

```json
{
"script": {
Expand All @@ -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);
/**
[
'/',
Expand All @@ -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.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-routes-list",
"version": "1.1.5",
"version": "1.2.0",
"description": "generate next all routes list",
"keywords": [
"next",
Expand All @@ -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"
Expand Down
28 changes: 20 additions & 8 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}

0 comments on commit 7547978

Please sign in to comment.