navilo 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +17 -5
  2. package/bin/navilo.js +179 -0
  3. package/package.json +9 -4
package/README.md CHANGED
@@ -30,17 +30,29 @@ Navilo automatically generates a route tree from your `src/app` (or custom) dire
30
30
 
31
31
  ## Quick Start
32
32
 
33
- ### Vite Config
33
+ You can use the CLI to setup your project automatically.
34
34
 
35
- 1. Install react router dom since its our peer dependency
35
+ ```bash
36
+ npx navilo init
37
+ ```
38
+
39
+ You can also pass your package manager directly:
36
40
 
37
41
  ```bash
38
- npm install react-router-dom@6.16.0
42
+ npx navilo init --pm pnpm
43
+ ```
39
44
 
45
+ Supported values: `pnpm`, `yarn`, `npm`, `bun`.
46
+
47
+ Or you can do the following steps manually:
48
+
49
+ ### Vite Config
50
+ 1. Install react router dom since its our peer dependency
51
+ ```bash
52
+ npm install react-router-dom@6.16.0
40
53
  ```
41
54
 
42
55
  2.Add the navilo to plugin in vite config
43
-
44
56
  ```ts
45
57
  // vite.config.ts
46
58
  import {defineConfig} from 'vite';
@@ -70,7 +82,7 @@ declare module 'virtual:navilo-routes' {
70
82
 
71
83
  ```tsx
72
84
  import {RouterProvider} from "react-router-dom";
73
- import {router} from 'virtual:preluder-routes';
85
+ import {router} from 'virtual:navilo-routes';
74
86
 
75
87
  export function App() {
76
88
  return (
package/bin/navilo.js ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { execSync } = require('child_process');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const readline = require('readline');
7
+
8
+ const SUPPORTED_PACKAGE_MANAGERS = ['pnpm', 'yarn', 'npm', 'bun'];
9
+
10
+ function parsePackageManagerArg(argv) {
11
+ const pmEqArg = argv.find((arg) => arg.startsWith('--pm='));
12
+ if (pmEqArg) return pmEqArg.split('=')[1]?.trim();
13
+
14
+ const packageManagerEqArg = argv.find((arg) => arg.startsWith('--package-manager='));
15
+ if (packageManagerEqArg) return packageManagerEqArg.split('=')[1]?.trim();
16
+
17
+ const pmIndex = argv.findIndex((arg) => arg === '--pm');
18
+ if (pmIndex !== -1) return argv[pmIndex + 1]?.trim();
19
+
20
+ const packageManagerIndex = argv.findIndex((arg) => arg === '--package-manager');
21
+ if (packageManagerIndex !== -1) return argv[packageManagerIndex + 1]?.trim();
22
+
23
+ return null;
24
+ }
25
+
26
+ function detectPackageManagerFromLockFiles(cwd) {
27
+ if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) return 'pnpm';
28
+ if (fs.existsSync(path.join(cwd, 'yarn.lock'))) return 'yarn';
29
+ if (fs.existsSync(path.join(cwd, 'bun.lockb')) || fs.existsSync(path.join(cwd, 'bun.lock'))) return 'bun';
30
+ if (fs.existsSync(path.join(cwd, 'package-lock.json'))) return 'npm';
31
+ return 'npm';
32
+ }
33
+
34
+ function askQuestion(query) {
35
+ const rl = readline.createInterface({
36
+ input: process.stdin,
37
+ output: process.stdout,
38
+ });
39
+
40
+ return new Promise((resolve) => {
41
+ rl.question(query, (answer) => {
42
+ rl.close();
43
+ resolve(answer.trim());
44
+ });
45
+ });
46
+ }
47
+
48
+ async function resolvePackageManager() {
49
+ const pmFromArgs = parsePackageManagerArg(process.argv.slice(3));
50
+
51
+ if (pmFromArgs) {
52
+ if (!SUPPORTED_PACKAGE_MANAGERS.includes(pmFromArgs)) {
53
+ throw new Error(
54
+ `Invalid package manager "${pmFromArgs}". Use one of: ${SUPPORTED_PACKAGE_MANAGERS.join(', ')}`
55
+ );
56
+ }
57
+ return pmFromArgs;
58
+ }
59
+
60
+ const detected = detectPackageManagerFromLockFiles(process.cwd());
61
+ const answer = await askQuestion(
62
+ `Choose package manager [pnpm/yarn/npm/bun] (default: ${detected}): `
63
+ );
64
+
65
+ if (!answer) return detected;
66
+
67
+ if (!SUPPORTED_PACKAGE_MANAGERS.includes(answer)) {
68
+ console.log(`Invalid selection "${answer}". Falling back to ${detected}.`);
69
+ return detected;
70
+ }
71
+
72
+ return answer;
73
+ }
74
+
75
+ function installDependency(packageManager, pkg) {
76
+ const installCommands = {
77
+ npm: `npm install ${pkg}`,
78
+ pnpm: `pnpm add ${pkg}`,
79
+ yarn: `yarn add ${pkg}`,
80
+ bun: `bun add ${pkg}`,
81
+ };
82
+
83
+ execSync(installCommands[packageManager], { stdio: 'inherit' });
84
+ }
85
+
86
+ const commands = {
87
+ init: async () => {
88
+ console.log('Initializing Navilo...');
89
+
90
+ const packageManager = await resolvePackageManager();
91
+ console.log(`Using package manager: ${packageManager}`);
92
+
93
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
94
+ if (fs.existsSync(packageJsonPath)) {
95
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
96
+ const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
97
+
98
+ if (!dependencies['navilo']) {
99
+ console.log('Installing navilo...');
100
+ installDependency(packageManager, 'navilo');
101
+ }
102
+
103
+ if (!dependencies['react-router-dom'] || dependencies['react-router-dom'] !== '^6.16.0') {
104
+ console.log('Installing react-router-dom@^6.16.0...');
105
+ installDependency(packageManager, 'react-router-dom@^6.16.0');
106
+ }
107
+ } else {
108
+ console.log('package.json not found. Please run in a valid project directory.');
109
+ return;
110
+ }
111
+
112
+ const viteConfigPath = path.join(process.cwd(), 'vite.config.ts');
113
+ if (fs.existsSync(viteConfigPath)) {
114
+ let viteConfig = fs.readFileSync(viteConfigPath, 'utf-8');
115
+ if (!viteConfig.includes('navilo(')) {
116
+ viteConfig = viteConfig.replace(/plugins: \[(.*)\]/s, (match, plugins) => {
117
+ return `plugins: [${plugins.trim()}, navilo({ pagesDir: 'src/app' })]`;
118
+ });
119
+ viteConfig = "import navilo from 'navilo';\n" + viteConfig;
120
+ fs.writeFileSync(viteConfigPath, viteConfig);
121
+ console.log('Updated vite.config.ts');
122
+ }
123
+ } else {
124
+ console.log('vite.config.ts not found. Please ensure you are in a Vite project.');
125
+ }
126
+
127
+ const viteEnvDtsPath = path.join(process.cwd(), 'src/vite-env.d.ts');
128
+ const declaration = `/// <reference types="vite/client" />
129
+ declare module 'virtual:navilo-routes' {
130
+ export const router;
131
+ }
132
+ `;
133
+ if (fs.existsSync(viteEnvDtsPath)) {
134
+ let content = fs.readFileSync(viteEnvDtsPath, 'utf-8');
135
+ if (!content.includes('virtual:navilo-routes')) {
136
+ content += '\n' + declaration;
137
+ fs.writeFileSync(viteEnvDtsPath, content);
138
+ }
139
+ } else {
140
+ fs.writeFileSync(viteEnvDtsPath, declaration);
141
+ }
142
+ console.log('Updated/created vite-env.d.ts');
143
+
144
+
145
+ const appFilePath = path.join(process.cwd(), 'src/App.tsx');
146
+ if (fs.existsSync(appFilePath)) {
147
+ let appFileContent = fs.readFileSync(appFilePath, 'utf-8');
148
+ if (!appFileContent.includes('virtual:navilo-routes')) {
149
+ appFileContent = `import { RouterProvider } from "react-router-dom";
150
+ import { router } from 'virtual:navilo-routes';
151
+
152
+ export function App() {
153
+ return (
154
+ <RouterProvider router={router} />
155
+ );
156
+ }
157
+ `;
158
+ fs.writeFileSync(appFilePath, appFileContent);
159
+ console.log('Updated App.tsx');
160
+ }
161
+ } else {
162
+ console.log('src/App.tsx not found. You will need to manually set up the router provider.');
163
+ }
164
+
165
+ console.log('Navilo initialization complete! 🎉');
166
+ },
167
+ };
168
+
169
+ const command = process.argv[2];
170
+
171
+ if (commands[command]) {
172
+ Promise.resolve(commands[command]())
173
+ .catch((error) => {
174
+ console.error(error.message);
175
+ process.exit(1);
176
+ });
177
+ } else {
178
+ console.log('Unknown command. Available commands: init');
179
+ }
package/package.json CHANGED
@@ -1,17 +1,22 @@
1
1
  {
2
2
  "name": "navilo",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "File-based routing plugin for Vite + React applications",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "bin"
10
11
  ],
12
+ "bin": {
13
+ "navilo": "bin/navilo.js"
14
+ },
11
15
  "scripts": {
12
16
  "build": "tsup",
13
17
  "dev": "tsup --watch",
14
- "typecheck": "tsc --noEmit"
18
+ "typecheck": "tsc --noEmit",
19
+ "prepack": "npm run build"
15
20
  },
16
21
  "keywords": [
17
22
  "vite",
@@ -47,4 +52,4 @@
47
52
  "vitest": "^3.2.4"
48
53
  },
49
54
  "packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748"
50
- }
55
+ }