Skip to content

Commit

Permalink
chore: log out changes to route files
Browse files Browse the repository at this point in the history
  • Loading branch information
ifeanyiisitor committed Aug 18, 2023
1 parent 7e7b373 commit d8d94a6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"author": "Ifeanyi Isitor",
"license": "MIT",
"dependencies": {
"chalk": "^5.3.0",
"esm": "^3.2.25",
"lru-cache": "^10.0.1",
"meow": "^12.0.1",
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/bin/env node

import * as semver from 'semver'
import semver from 'semver'
import chalk from 'chalk'

const requiredNodeVersion = '16.0.0'

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.`
chalk.red(
`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
}
Expand Down
12 changes: 3 additions & 9 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import 'reflect-metadata'

import meow from 'meow'
import chalk from 'chalk'
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
Expand Down Expand Up @@ -50,6 +42,8 @@ function run() {
watchDirectory(currentDirectory, () => {
unflatten(currentDirectory)
})

console.log(chalk.blue('\n➜ Listening for changes to routes...\n'))
}

function unflatten(dir: string) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/createPageFilesIfNotExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export function createPageFilesIfNotExist(routeFiles: RouteFile[]): void {

// Write the content to the file
fs.writeFileSync(routeFile.pagePath, content)

// Notify the user
console.log(
`✔ Created page: ${path.relative(process.cwd(), routeFile.pagePath)}`
)
}
}
}
4 changes: 4 additions & 0 deletions src/utils/deletePageFilesIfRouteMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export function deletePageFilesIfRouteMissing(routeFiles: RouteFile[]): void {
if (!fs.existsSync(routeFile.routePath)) {
fs.unlinkSync(routeFile.pagePath)

console.log(
`✔ Deleted page: ${path.relative(process.cwd(), routeFile.pagePath)}`
)

// Delete all directories leading to to this one if it is now empty
let currentDirectory = path.dirname(routeFile.pagePath)
while (!currentDirectory.endsWith('(.routes)')) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ chalk@^4.0.0, chalk@^4.1.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^5.2.0:
chalk@^5.2.0, chalk@^5.3.0:
version "5.3.0"
resolved "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz#67c20a7ebef70e7f3970a01f90fa210cb6860385"
integrity sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==
Expand Down

0 comments on commit d8d94a6

Please sign in to comment.