file-based-routing-cli
Advanced tools
+1
-1
| { | ||
| "name": "file-based-routing-cli", | ||
| "version": "1.1.6", | ||
| "version": "1.1.7", | ||
| "description": "CLI tool for file-based routing in React projects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+22
-20
| import { watch } from "chokidar"; | ||
| import chalk from "chalk"; | ||
| import { promises as fs } from "fs"; | ||
| import path from "path"; | ||
| import { | ||
@@ -15,6 +16,4 @@ writeFile, | ||
| // Debug: Check current directory | ||
| console.log(chalk.gray(`Current working directory: ${process.cwd()}`)); | ||
| // Determine which pages directory exists | ||
| const srcPagesExists = await checkDirectoryExists("src/pages"); | ||
@@ -26,4 +25,6 @@ const rootPagesExists = await checkDirectoryExists("pages"); | ||
| const pagesPath = srcPagesExists ? "src/pages" : "pages"; | ||
| const routingPath = pagesPath.startsWith("src/") | ||
| const relativePagesPath = srcPagesExists ? "src/pages" : "pages"; | ||
| const pagesPath = path.resolve(process.cwd(), relativePagesPath); | ||
| const routingPath = relativePagesPath.startsWith("src/") | ||
| ? "src/routing.jsx" | ||
@@ -33,7 +34,9 @@ : "routing.jsx"; | ||
| console.log( | ||
| chalk.blue(`Watching for file changes in ${pagesPath} directory...`) | ||
| chalk.blue(`Watching for file changes in ${relativePagesPath}...`) | ||
| ); | ||
| console.log(chalk.gray(`Resolved absolute pages path: ${pagesPath}`)); | ||
| console.log(chalk.gray(`Using routing file: ${routingPath}`)); | ||
| console.log(chalk.gray(`Watch pattern: ${pagesPath}/**/*.{jsx,tsx}`)); | ||
| const watcher = watch(`.`, { | ||
| const watcher = watch(`${pagesPath}/**/*.{jsx,tsx}`, { | ||
| persistent: true, | ||
@@ -48,9 +51,9 @@ ignoreInitial: true, | ||
| watcher | ||
| .on("add", async (path) => { | ||
| console.log(chalk.yellow(`File added: ${path}`)); | ||
| await handleFileAdd(path, pages, routingPath); | ||
| .on("add", async (filePath) => { | ||
| console.log(chalk.yellow(`File added: ${filePath}`)); | ||
| await handleFileAdd(filePath, pages, routingPath); | ||
| }) | ||
| .on("unlink", async (path) => { | ||
| console.log(chalk.yellow(`File removed: ${path}`)); | ||
| await handleFileRemove(path, pages, routingPath); | ||
| .on("unlink", async (filePath) => { | ||
| console.log(chalk.yellow(`File removed: ${filePath}`)); | ||
| await handleFileRemove(filePath, pages, routingPath); | ||
| }) | ||
@@ -64,3 +67,2 @@ .on("error", (error) => { | ||
| // Keep the process alive | ||
| process.on("SIGINT", () => { | ||
@@ -73,13 +75,13 @@ console.log(chalk.yellow("\nStopping file watcher...")); | ||
| async function handleFileAdd(path, pages, routingPath) { | ||
| const { name, ext, relativePath, route, component } = parsePagePath(path); | ||
| async function handleFileAdd(filePath, pages, routingPath) { | ||
| const { name, ext, relativePath, route, component } = parsePagePath(filePath); | ||
| try { | ||
| console.log(chalk.cyan(`Processing file: ${path}`)); | ||
| console.log(chalk.cyan(`Processing file: ${filePath}`)); | ||
| console.log(chalk.cyan(`Component: ${component}, Route: ${route}`)); | ||
| const stats = await fs.stat(path); | ||
| const stats = await fs.stat(filePath); | ||
| if (stats.size === 0) { | ||
| console.log(chalk.cyan(`File is empty, creating component template...`)); | ||
| await writeFile(path, componentTemplate(component)); | ||
| await writeFile(filePath, componentTemplate(component)); | ||
| } | ||
@@ -96,4 +98,4 @@ | ||
| async function handleFileRemove(path, pages, routingPath) { | ||
| const { name } = parsePagePath(path); | ||
| async function handleFileRemove(filePath, pages, routingPath) { | ||
| const { name } = parsePagePath(filePath); | ||
| const index = pages.findIndex((p) => p.component === name); | ||
@@ -100,0 +102,0 @@ if (index !== -1) { |
| import { promises as fs } from "fs"; | ||
| import { parse, relative } from "path"; | ||
| import { parse, relative, normalize, sep } from "path"; | ||
@@ -35,9 +35,30 @@ export async function checkDirectoryExists(path) { | ||
| export function parsePagePath(path) { | ||
| const { name, ext } = parse(path); | ||
| const baseDir = path.includes("src/pages") ? "src/pages" : "pages"; | ||
| const relativePath = relative(baseDir, path).replace(ext, ""); | ||
| export function parsePagePath(filePath) { | ||
| const { name, ext } = parse(filePath); | ||
| // Normalize the path to handle Windows/Unix differences | ||
| const normalizedPath = normalize(filePath).replace(/\\/g, "/"); | ||
| // Determine the base directory and extract relative path | ||
| let relativePath; | ||
| if (normalizedPath.includes("src/pages/")) { | ||
| const pagesIndex = normalizedPath.indexOf("src/pages/"); | ||
| relativePath = normalizedPath.substring(pagesIndex + "src/pages/".length); | ||
| } else if (normalizedPath.includes("pages/")) { | ||
| const pagesIndex = normalizedPath.indexOf("pages/"); | ||
| relativePath = normalizedPath.substring(pagesIndex + "pages/".length); | ||
| } else { | ||
| relativePath = name; | ||
| } | ||
| // Remove the file extension from the relative path | ||
| relativePath = relativePath.replace(ext, ""); | ||
| // Generate the route path | ||
| const route = "/" + (name === "index" ? "" : relativePath); | ||
| // Generate component name (capitalize first letter) | ||
| const component = name.charAt(0).toUpperCase() + name.slice(1); | ||
| return { name, ext, relativePath, route, component }; | ||
| } |
@@ -11,3 +11,7 @@ import { writeFile } from "./fileSystem.js"; | ||
| ${pages | ||
| .map((page) => `import ${page.component} from '../pages/${page.file}';`) | ||
| .map((page) => { | ||
| // Clean up the file path for import | ||
| const cleanPath = page.file.replace(/\\/g, "/"); | ||
| return `import ${page.component} from '../pages/${cleanPath}';`; | ||
| }) | ||
| .join("\n")} | ||
@@ -14,0 +18,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12541
8.53%270
7.57%