@craftycodesmith/node-structure 1.0.1 → 1.0.3
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.
- package/README.md +107 -0
- package/dist/index.js +24 -16
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Feature-Based CLI Generator
|
|
2
|
+
|
|
3
|
+
A zero-dependency CLI tool to scaffold Node.js projects with a **Feature-Based (Modular) Architecture** using TypeScript.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🚀 Quick Start
|
|
8
|
+
|
|
9
|
+
You can run this tool directly using `npx` without installation:
|
|
10
|
+
```bash
|
|
11
|
+
# Initialize a new project
|
|
12
|
+
npx your-package-name init [folder-name]
|
|
13
|
+
|
|
14
|
+
# Generate a feature module
|
|
15
|
+
npx your-package-name res [resource-name]
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 📦 Commands
|
|
21
|
+
|
|
22
|
+
### `init [folder-name]`
|
|
23
|
+
|
|
24
|
+
Creates a new project with the feature-based architecture structure.
|
|
25
|
+
|
|
26
|
+
**Usage:**
|
|
27
|
+
```bash
|
|
28
|
+
npx your-package-name init my-project
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
If no folder name is provided, it initializes in the current directory.
|
|
32
|
+
|
|
33
|
+
**What it does:**
|
|
34
|
+
- Creates the directory structure
|
|
35
|
+
- Runs `npm init -y`
|
|
36
|
+
- Installs TypeScript and `@types/node`
|
|
37
|
+
- Generates a `tsconfig.json`
|
|
38
|
+
- Sets up base folders (`features`, `common`, `config`, `middleware`)
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### `res [resource-name]`
|
|
43
|
+
|
|
44
|
+
Generates a new feature module with Controller, Service, and Model files.
|
|
45
|
+
|
|
46
|
+
**Usage:**
|
|
47
|
+
```bash
|
|
48
|
+
npx your-package-name res User
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This creates:
|
|
52
|
+
```
|
|
53
|
+
src/features/user/
|
|
54
|
+
├── user.controller.ts
|
|
55
|
+
├── user.service.ts
|
|
56
|
+
└── user.model.ts
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Each file includes boilerplate code to get you started quickly.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## 🏗 Generated Architecture
|
|
64
|
+
|
|
65
|
+
The tool enforces a modular structure where each functional area of your application lives in its own "feature" folder.
|
|
66
|
+
```
|
|
67
|
+
project-root/
|
|
68
|
+
├── src/
|
|
69
|
+
│ ├── features/
|
|
70
|
+
│ │ └── [feature-name]/
|
|
71
|
+
│ │ ├── [name].controller.ts
|
|
72
|
+
│ │ ├── [name].service.ts
|
|
73
|
+
│ │ └── [name].model.ts
|
|
74
|
+
│ ├── common/ # Shared utilities and helpers
|
|
75
|
+
│ ├── config/ # Environment and DB configurations
|
|
76
|
+
│ └── middleware/ # Global Express/Koa middlewares
|
|
77
|
+
├── tsconfig.json
|
|
78
|
+
└── package.json
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## 🛠 Features
|
|
84
|
+
|
|
85
|
+
- **Zero Dependencies**: Lightweight and fast
|
|
86
|
+
- **TypeScript Ready**: Automatically generates `tsconfig.json` and installs `@types/node` and `typescript`
|
|
87
|
+
- **Automated Setup**: Runs `npm init -y` and dependency installation automatically
|
|
88
|
+
- **Clean Code**: Generates boilerplate classes and interfaces for rapid development
|
|
89
|
+
- **Modular Architecture**: Each feature is self-contained for better maintainability
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 📝 Example Workflow
|
|
94
|
+
```bash
|
|
95
|
+
# 1. Create a new project
|
|
96
|
+
npx your-package-name init my-app
|
|
97
|
+
|
|
98
|
+
# 2. Navigate to the project
|
|
99
|
+
cd my-app
|
|
100
|
+
|
|
101
|
+
# 3. Generate features
|
|
102
|
+
npx your-package-name res User
|
|
103
|
+
npx your-package-name res Product
|
|
104
|
+
npx your-package-name res Order
|
|
105
|
+
|
|
106
|
+
# 4. Start building!
|
|
107
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -5,43 +5,51 @@ const node_child_process_1 = require("node:child_process");
|
|
|
5
5
|
const node_fs_1 = require("node:fs");
|
|
6
6
|
const node_path_1 = require("node:path");
|
|
7
7
|
const node_util_1 = require("node:util");
|
|
8
|
-
const {
|
|
9
|
-
allowPositionals: true
|
|
10
|
-
options: {
|
|
11
|
-
help: { type: 'boolean', short: 'h' }
|
|
12
|
-
}
|
|
8
|
+
const { positionals } = (0, node_util_1.parseArgs)({
|
|
9
|
+
allowPositionals: true
|
|
13
10
|
});
|
|
14
11
|
const command = positionals[0];
|
|
15
|
-
const root = process.cwd();
|
|
16
12
|
const getTemplate = (type, name) => {
|
|
17
13
|
const templates = {
|
|
18
14
|
controller: `export class ${name}Controller {\n // Logic for ${name}\n}`,
|
|
19
15
|
service: `export class ${name}Service {\n // Business logic for ${name}\n}`,
|
|
20
16
|
model: `export interface ${name} {\n id: string;\n}`,
|
|
21
|
-
tsconfig: `{\n "compilerOptions": {\n "target": "ESNext",\n "module": "NodeNext",\n "outDir": "./dist",\n "strict": true\n }\n}`
|
|
17
|
+
tsconfig: `{\n "compilerOptions": {\n "target": "ESNext",\n "module": "NodeNext",\n "moduleResolution": "NodeNext",\n "outDir": "./dist",\n "rootDir": "./src",\n "strict": true,\n "esModuleInterop": true\n },\n "include": ["src/**/*"]\n}`
|
|
22
18
|
};
|
|
23
19
|
return templates[type] || '';
|
|
24
20
|
};
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
console.log(
|
|
21
|
+
const init = (targetDir = '.') => {
|
|
22
|
+
const root = (0, node_path_1.resolve)(process.cwd(), targetDir);
|
|
23
|
+
console.log(`Initializing structure in: ${root}`);
|
|
24
|
+
// Create target directory if it doesn't exist
|
|
25
|
+
if (!(0, node_fs_1.existsSync)(root)) {
|
|
26
|
+
(0, node_fs_1.mkdirSync)(root, { recursive: true });
|
|
27
|
+
}
|
|
28
28
|
const folders = ['src/features', 'src/common', 'src/config'];
|
|
29
29
|
folders.forEach(folder => {
|
|
30
30
|
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, folder), { recursive: true });
|
|
31
31
|
});
|
|
32
32
|
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'tsconfig.json'), getTemplate('tsconfig', ''));
|
|
33
|
+
// Execute commands inside the target directory
|
|
34
|
+
const execOptions = { cwd: root, stdio: 'inherit' };
|
|
33
35
|
if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'package.json'))) {
|
|
34
|
-
(0, node_child_process_1.execSync)('npm init -y',
|
|
36
|
+
(0, node_child_process_1.execSync)('npm init -y', execOptions);
|
|
35
37
|
}
|
|
36
|
-
(0, node_child_process_1.execSync)('npm install -D typescript @types/node ts-node',
|
|
38
|
+
(0, node_child_process_1.execSync)('npm install -D typescript @types/node ts-node', execOptions);
|
|
37
39
|
console.log('Project initialized successfully.');
|
|
38
40
|
};
|
|
39
41
|
const generateResource = (name) => {
|
|
40
42
|
if (!name) {
|
|
41
|
-
console.error('Error: Please provide a resource name.');
|
|
43
|
+
console.error('Error: Please provide a resource name (e.g., npx my-cli res user)');
|
|
42
44
|
process.exit(1);
|
|
43
45
|
}
|
|
46
|
+
// Assumes execution from project root
|
|
47
|
+
const root = process.cwd();
|
|
44
48
|
const featureDir = (0, node_path_1.join)(root, 'src/features', name.toLowerCase());
|
|
49
|
+
if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'src'))) {
|
|
50
|
+
console.error('Error: src directory not found. Run "init" first.');
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
45
53
|
(0, node_fs_1.mkdirSync)(featureDir, { recursive: true });
|
|
46
54
|
const files = [
|
|
47
55
|
{ fileName: `${name.toLowerCase()}.controller.ts`, type: 'controller' },
|
|
@@ -53,14 +61,14 @@ const generateResource = (name) => {
|
|
|
53
61
|
});
|
|
54
62
|
console.log(`Module "${name}" generated in ${featureDir}`);
|
|
55
63
|
};
|
|
56
|
-
// ---
|
|
64
|
+
// --- Command Dispatcher ---
|
|
57
65
|
switch (command) {
|
|
58
66
|
case 'init':
|
|
59
|
-
init();
|
|
67
|
+
init(positionals[1]); // positionals[1] is the folder name if provided
|
|
60
68
|
break;
|
|
61
69
|
case 'res':
|
|
62
70
|
generateResource(positionals[1]);
|
|
63
71
|
break;
|
|
64
72
|
default:
|
|
65
|
-
console.log('Usage
|
|
73
|
+
console.log('Usage:\n npx my-cli init [folder-name]\n npx my-cli res [resource-name]');
|
|
66
74
|
}
|