Skip to content

Commit

Permalink
fix: output error when when run with the wrong node version
Browse files Browse the repository at this point in the history
  • Loading branch information
ifeanyiisitor committed Aug 18, 2023
1 parent c119d2f commit 825b78d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 60 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
"license": "MIT",
"dependencies": {
"esm": "^3.2.25",
"lru-cache": "^10.0.1",
"meow": "^12.0.1",
"reflect-metadata": "^0.1.13",
"unbuild": "^1.2.1"
"semver": "^7.5.4",
"unbuild": "^1.2.1",
"yallist": "^4.0.0"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",
Expand Down
67 changes: 9 additions & 58 deletions src/index.ts
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')
71 changes: 71 additions & 0 deletions src/run.ts
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)

Check failure on line 17 in src/run.ts

View workflow job for this annotation

GitHub Actions / ci

Only use `process.exit()` in CLI apps. Throw an error instead
}

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()
5 changes: 5 additions & 0 deletions unbuild.config.ts
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'],
})
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3247,6 +3247,11 @@ loupe@^2.3.1, loupe@^2.3.6:
dependencies:
get-func-name "^2.0.0"

lru-cache@^10.0.1:
version "10.0.1"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-10.0.1.tgz#0a3be479df549cca0e5d693ac402ff19537a6b7a"
integrity sha512-IJ4uwUTi2qCccrioU6g9g/5rvvVl13bsdczUUcqbciD9iLr095yj8DQKdObriEvuNSx325N1rV1O0sJFszx75g==

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
Expand Down Expand Up @@ -4112,7 +4117,7 @@ scule@^1.0.0:
resolved "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8"
integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==

semver@7.5.4, semver@^7.0.0, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8, semver@^7.5.3:
semver@7.5.4, semver@^7.0.0, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8, semver@^7.5.3, semver@^7.5.4:
version "7.5.4"
resolved "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
Expand Down

0 comments on commit 825b78d

Please sign in to comment.