-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: output error when when run with the wrong node version
- Loading branch information
1 parent
c119d2f
commit 825b78d
Showing
5 changed files
with
95 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,15 @@ | ||
#!/usr/bin/env node | ||
|
||
import 'reflect-metadata' | ||
import * as semver from 'semver' | ||
|
||
import meow from 'meow' | ||
import { findPageFiles } from './utils/findPageFiles' | ||
import { findRouteFiles } from './utils/findRouteFiles' | ||
import { watchDirectory } from './utils/watchDirectory' | ||
import { createPageFilesIfNotExist } from './utils/createPageFilesIfNotExists' | ||
import { deleteEmptyDirectoriesWithinRoutes } from './utils/deleteEmptyDirectoriesWithinRoutes' | ||
import { deletePageFilesIfRouteMissing } from './utils/deletePageFilesIfRouteMissing' | ||
const requiredNodeVersion = '16.0.0' | ||
|
||
meow( | ||
` | ||
Usage | ||
$ npx unflatten-next-routes | ||
Description | ||
Convert flat Next.js 13 route files to the nested structure | ||
How it works | ||
The CLI will watch for any flat route files located within '/routes/' folders located anywhere within the 'app' directory of your Next.js project. | ||
It will then generate the nested equivalent in a parallel '/(.routes)/' folder. | ||
Options | ||
--help Show help | ||
--version Show version | ||
Note | ||
Do not manually modify or delete files in the '/(.routes)/' directory, as they are auto-generated. | ||
`, | ||
{ | ||
importMeta: import.meta, | ||
} | ||
) | ||
|
||
function run() { | ||
const currentDirectory = process.cwd() | ||
|
||
unflatten(currentDirectory) | ||
|
||
watchDirectory(currentDirectory, () => { | ||
unflatten(currentDirectory) | ||
}) | ||
} | ||
|
||
function unflatten(dir: string) { | ||
generatePageFiles(dir) | ||
removeUnlinkedPageFiles(dir) | ||
deleteEmptyDirectoriesWithinRoutes(dir) | ||
} | ||
|
||
function generatePageFiles(dir: string) { | ||
const routeFiles = findRouteFiles(dir) | ||
createPageFilesIfNotExist(routeFiles) | ||
} | ||
|
||
function removeUnlinkedPageFiles(dir: string) { | ||
const pageFiles = findPageFiles(dir) | ||
deletePageFilesIfRouteMissing(pageFiles) | ||
if (!semver.satisfies(process.version, `>=${requiredNodeVersion}`)) { | ||
console.error( | ||
`Error: This tool requires Node.js version ${requiredNodeVersion} or newer. You are using ${process.version}. Please update your Node.js version and try again.` | ||
) | ||
process.exit(1) // eslint-disable-line unicorn/no-process-exit | ||
} | ||
|
||
run() | ||
// If version check passes, dynamically import the main CLI | ||
import('./run') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'reflect-metadata' | ||
|
||
import meow from 'meow' | ||
import { findPageFiles } from './utils/findPageFiles' | ||
import { findRouteFiles } from './utils/findRouteFiles' | ||
import { watchDirectory } from './utils/watchDirectory' | ||
import { createPageFilesIfNotExist } from './utils/createPageFilesIfNotExists' | ||
import { deleteEmptyDirectoriesWithinRoutes } from './utils/deleteEmptyDirectoriesWithinRoutes' | ||
import { deletePageFilesIfRouteMissing } from './utils/deletePageFilesIfRouteMissing' | ||
|
||
const requiredNodeVersion = '16.0.0' | ||
|
||
if (process.version < requiredNodeVersion) { | ||
console.error( | ||
`Error: This tool requires Node.js version ${requiredNodeVersion} or newer. You are using ${process.version}. Please update your Node.js version and try again.` | ||
) | ||
process.exit(1) | ||
} | ||
|
||
meow( | ||
` | ||
Usage | ||
$ npx unflatten-next-routes | ||
Description | ||
Convert flat Next.js 13 route files to the nested structure | ||
How it works | ||
The CLI will watch for any flat route files located within '/routes/' folders located anywhere within the 'app' directory of your Next.js project. | ||
It will then generate the nested equivalent in a parallel '/(.routes)/' folder. | ||
Options | ||
--help Show help | ||
--version Show version | ||
Note | ||
Do not manually modify or delete files in the '/(.routes)/' directory, as they are auto-generated. | ||
`, | ||
{ | ||
importMeta: import.meta, | ||
} | ||
) | ||
|
||
function run() { | ||
const currentDirectory = process.cwd() | ||
|
||
unflatten(currentDirectory) | ||
|
||
watchDirectory(currentDirectory, () => { | ||
unflatten(currentDirectory) | ||
}) | ||
} | ||
|
||
function unflatten(dir: string) { | ||
generatePageFiles(dir) | ||
removeUnlinkedPageFiles(dir) | ||
deleteEmptyDirectoriesWithinRoutes(dir) | ||
} | ||
|
||
function generatePageFiles(dir: string) { | ||
const routeFiles = findRouteFiles(dir) | ||
createPageFilesIfNotExist(routeFiles) | ||
} | ||
|
||
function removeUnlinkedPageFiles(dir: string) { | ||
const pageFiles = findPageFiles(dir) | ||
deletePageFilesIfRouteMissing(pageFiles) | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
externals: ['semver', 'lru-cache', 'yallist'], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters