domain-driver 0.0.4 → 0.1.0
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 +173 -28
- package/dist/commands/component.js +62 -0
- package/dist/commands/container.js +73 -0
- package/dist/commands/feature.js +41 -14
- package/dist/commands/hook.js +153 -0
- package/dist/commands/repository.js +121 -0
- package/dist/commands/schema.js +74 -0
- package/dist/commands/service.js +119 -0
- package/dist/commands/types.js +57 -0
- package/dist/index.js +121 -12
- package/dist/utils.js +132 -0
- package/package.json +10 -9
- package/src/__tests__/utils.test.ts +128 -0
- package/src/commands/__tests__/feature.test.ts +85 -0
- package/src/commands/__tests__/imports.test.ts +115 -0
- package/src/commands/__tests__/individual.test.ts +137 -0
- package/src/commands/component.ts +33 -0
- package/src/commands/container.ts +42 -0
- package/src/commands/feature.ts +47 -15
- package/src/commands/hook.ts +122 -0
- package/src/commands/repository.ts +95 -0
- package/src/commands/schema.ts +47 -0
- package/src/commands/service.ts +93 -0
- package/src/commands/types.ts +25 -0
- package/src/index.ts +121 -11
- package/src/utils.ts +115 -0
- package/tsconfig.json +1 -1
- package/vitest.config.ts +7 -0
package/README.md
CHANGED
|
@@ -13,47 +13,182 @@ npm install -g domain-driver
|
|
|
13
13
|
Or use without installing:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npx domain-driver make:feature <
|
|
16
|
+
npx domain-driver make:feature <n>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Commands
|
|
22
|
+
|
|
23
|
+
### `make:feature`
|
|
24
|
+
|
|
25
|
+
Scaffold a full feature folder structure.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
domain-driver make:feature <n>
|
|
29
|
+
domain-driver make:feature <n> -a
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `-a` flag scaffolds all files inside each folder automatically.
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
domain-driver make:feature coffee-type # folders + .gitkeep only
|
|
36
|
+
domain-driver make:feature coffee-type -a # folders + all files
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
### `make:component`
|
|
42
|
+
|
|
43
|
+
Scaffold a component inside an existing feature. Defaults to `client` if no type is specified.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
domain-driver make:component <feature> <n>
|
|
47
|
+
domain-driver make:component <feature> <n> client
|
|
48
|
+
domain-driver make:component <feature> <n> server
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
domain-driver make:component coffee-type CoffeeTypeList
|
|
53
|
+
domain-driver make:component coffee-type CoffeeTypeForm client
|
|
54
|
+
domain-driver make:component coffee-type CoffeeTypeCard server
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
### `make:container`
|
|
60
|
+
|
|
61
|
+
Scaffold a smart container component inside an existing feature.
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
domain-driver make:container <feature> <n>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
domain-driver make:container coffee-type CoffeeTypeContainer
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
### `make:hook`
|
|
74
|
+
|
|
75
|
+
Scaffold a custom hook inside an existing feature.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
domain-driver make:hook <feature> <n>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
domain-driver make:hook coffee-type useCoffeeType
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
### `make:service`
|
|
88
|
+
|
|
89
|
+
Scaffold a set of single-responsibility service files inside an existing feature.
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
domain-driver make:service <feature> <n>
|
|
93
|
+
```
|
|
22
94
|
|
|
23
95
|
```bash
|
|
24
|
-
domain-driver make:
|
|
96
|
+
domain-driver make:service coffee-type CoffeeType
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Generates:
|
|
100
|
+
|
|
25
101
|
```
|
|
102
|
+
app/coffee-type/services/
|
|
103
|
+
├── ListCoffeeType.service.ts
|
|
104
|
+
├── ShowCoffeeType.service.ts
|
|
105
|
+
├── CreateCoffeeType.service.ts
|
|
106
|
+
├── UpdateCoffeeType.service.ts
|
|
107
|
+
└── DeleteCoffeeType.service.ts
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### `make:repository`
|
|
113
|
+
|
|
114
|
+
Scaffold a set of single-responsibility repository files inside an existing feature.
|
|
26
115
|
|
|
27
|
-
|
|
116
|
+
```bash
|
|
117
|
+
domain-driver make:repository <feature> <n>
|
|
118
|
+
```
|
|
28
119
|
|
|
29
120
|
```bash
|
|
30
|
-
domain-driver make:
|
|
31
|
-
|
|
32
|
-
|
|
121
|
+
domain-driver make:repository coffee-type CoffeeType
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Generates:
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
app/coffee-type/repositories/
|
|
128
|
+
├── ListCoffeeType.repository.ts
|
|
129
|
+
├── ShowCoffeeType.repository.ts
|
|
130
|
+
├── CreateCoffeeType.repository.ts
|
|
131
|
+
├── UpdateCoffeeType.repository.ts
|
|
132
|
+
└── DeleteCoffeeType.repository.ts
|
|
33
133
|
```
|
|
34
134
|
|
|
35
135
|
---
|
|
36
136
|
|
|
37
|
-
|
|
137
|
+
### `make:schema`
|
|
138
|
+
|
|
139
|
+
Scaffold Zod schemas for create and update operations inside an existing feature.
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
domain-driver make:schema <feature> <n>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
domain-driver make:schema coffee-type CoffeeType
|
|
147
|
+
```
|
|
38
148
|
|
|
39
|
-
|
|
149
|
+
Generates:
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
app/coffee-type/schemas/
|
|
153
|
+
├── CreateCoffeeType.schema.ts
|
|
154
|
+
└── UpdateCoffeeType.schema.ts
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## What `make:feature -a` generates
|
|
160
|
+
|
|
161
|
+
Running `domain-driver make:feature coffee-type -a` creates the full structure:
|
|
40
162
|
|
|
41
163
|
```
|
|
42
164
|
app/
|
|
43
|
-
└──
|
|
165
|
+
└── coffee-type/
|
|
44
166
|
├── components/
|
|
45
|
-
│ ├── server/
|
|
46
|
-
│ └── client/
|
|
47
|
-
|
|
48
|
-
├──
|
|
49
|
-
|
|
50
|
-
├──
|
|
51
|
-
|
|
52
|
-
|
|
167
|
+
│ ├── server/
|
|
168
|
+
│ └── client/
|
|
169
|
+
│ └── CoffeeType.tsx
|
|
170
|
+
├── containers/
|
|
171
|
+
│ └── CoffeeTypeContainer.tsx
|
|
172
|
+
├── hooks/
|
|
173
|
+
│ └── useCoffeeType.ts
|
|
174
|
+
├── services/
|
|
175
|
+
│ ├── ListCoffeeType.service.ts
|
|
176
|
+
│ ├── ShowCoffeeType.service.ts
|
|
177
|
+
│ ├── CreateCoffeeType.service.ts
|
|
178
|
+
│ ├── UpdateCoffeeType.service.ts
|
|
179
|
+
│ └── DeleteCoffeeType.service.ts
|
|
180
|
+
├── repositories/
|
|
181
|
+
│ ├── ListCoffeeType.repository.ts
|
|
182
|
+
│ ├── ShowCoffeeType.repository.ts
|
|
183
|
+
│ ├── CreateCoffeeType.repository.ts
|
|
184
|
+
│ ├── UpdateCoffeeType.repository.ts
|
|
185
|
+
│ └── DeleteCoffeeType.repository.ts
|
|
186
|
+
├── schemas/
|
|
187
|
+
│ ├── CreateCoffeeType.schema.ts
|
|
188
|
+
│ └── UpdateCoffeeType.schema.ts
|
|
189
|
+
└── page.tsx
|
|
53
190
|
```
|
|
54
191
|
|
|
55
|
-
Each folder includes a `.gitkeep` file so empty directories are tracked in Git.
|
|
56
|
-
|
|
57
192
|
---
|
|
58
193
|
|
|
59
194
|
## Philosophy
|
|
@@ -67,22 +202,27 @@ The layer responsibilities are:
|
|
|
67
202
|
- **hooks** — React state and side effects, calls services
|
|
68
203
|
- **containers** — wire hooks into UI, no direct data fetching
|
|
69
204
|
- **components** — pure presentational UI, no data dependencies
|
|
70
|
-
- **schemas** — Zod validation for
|
|
205
|
+
- **schemas** — Zod validation for create and update operations
|
|
71
206
|
|
|
72
207
|
---
|
|
73
208
|
|
|
74
209
|
## Requirements
|
|
75
210
|
|
|
76
211
|
- Node.js 18+
|
|
77
|
-
- A Next.js project with an `app/` directory (App Router)
|
|
78
212
|
|
|
79
213
|
---
|
|
80
214
|
|
|
215
|
+
## Framework Support
|
|
216
|
+
|
|
217
|
+
This tool is optimised for **Next.js App Router** projects. All files are scaffolded into the `app/` directory following Next.js conventions (`page.tsx`, server/client component separation, etc.).
|
|
218
|
+
|
|
219
|
+
If no `app/` directory exists, it will be created automatically. This means the tool can also be used in any project where an `app/<feature>` folder structure makes sense.
|
|
220
|
+
|
|
81
221
|
## Local Development
|
|
82
222
|
|
|
83
223
|
```bash
|
|
84
224
|
# Clone the repo
|
|
85
|
-
git clone https://github.com/
|
|
225
|
+
git clone https://github.com/IsaacHatilima/domain-driver
|
|
86
226
|
cd domain-driver
|
|
87
227
|
|
|
88
228
|
# Install dependencies
|
|
@@ -96,17 +236,22 @@ npm link
|
|
|
96
236
|
|
|
97
237
|
# Test it
|
|
98
238
|
domain-driver make:feature test-feature
|
|
239
|
+
domain-driver make:feature test-feature -a
|
|
99
240
|
```
|
|
100
241
|
|
|
101
242
|
---
|
|
102
243
|
|
|
103
244
|
## Roadmap
|
|
104
245
|
|
|
105
|
-
- [
|
|
106
|
-
- [
|
|
107
|
-
- [
|
|
108
|
-
- [
|
|
109
|
-
- [
|
|
246
|
+
- [x] `make:feature` — scaffold feature folder structure
|
|
247
|
+
- [x] `make:feature -a` — scaffold feature with all files
|
|
248
|
+
- [x] `make:component` — scaffold a component
|
|
249
|
+
- [x] `make:container` — scaffold a container
|
|
250
|
+
- [x] `make:hook` — scaffold a custom hook
|
|
251
|
+
- [x] `make:service` — scaffold single-responsibility services
|
|
252
|
+
- [x] `make:repository` — scaffold single-responsibility repositories
|
|
253
|
+
- [x] `make:schema` — scaffold Zod schemas
|
|
254
|
+
- [ ] Interactive mode — prompt for name if not provided
|
|
110
255
|
- [ ] Config file — customize folder structure per project
|
|
111
256
|
|
|
112
257
|
---
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.makeComponent = makeComponent;
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
const utils_1 = require("../utils");
|
|
39
|
+
function renderComponent(name, type) {
|
|
40
|
+
const directive = type === 'client' ? "'use client';\n\n" : '';
|
|
41
|
+
return `${directive}interface ${name}Props {
|
|
42
|
+
id: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default function ${name}({ id }: ${name}Props) {
|
|
46
|
+
return (
|
|
47
|
+
<div>
|
|
48
|
+
<h1>${name}</h1>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
function makeComponent(feature, name, type = 'client') {
|
|
55
|
+
const base = (0, utils_1.ensureFeatureExists)(feature, `components/${type}`);
|
|
56
|
+
const filePath = path.join(base, `${name}.tsx`);
|
|
57
|
+
if ((0, utils_1.fileExists)(filePath)) {
|
|
58
|
+
throw new Error(`Component "${name}" already exists at ${filePath}`);
|
|
59
|
+
}
|
|
60
|
+
(0, utils_1.writeFileSafe)(filePath, renderComponent(name, type));
|
|
61
|
+
console.log(`✅ Component "${name}" created at ${filePath}`);
|
|
62
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.makeContainer = makeContainer;
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
const utils_1 = require("../utils");
|
|
39
|
+
function renderContainer(containerName, name, feature) {
|
|
40
|
+
const hookName = `use${name}`;
|
|
41
|
+
const hookPath = (0, utils_1.resolveImport)(feature, `hooks/${hookName}`);
|
|
42
|
+
const componentPath = (0, utils_1.resolveImport)(feature, `components/client/${name}`);
|
|
43
|
+
return `'use client';
|
|
44
|
+
|
|
45
|
+
import { ${hookName} } from '${hookPath}';
|
|
46
|
+
import ${name} from '${componentPath}';
|
|
47
|
+
|
|
48
|
+
export default function ${containerName}() {
|
|
49
|
+
const { items, loading, error } = ${hookName}();
|
|
50
|
+
|
|
51
|
+
if (loading) return <div>Loading...</div>;
|
|
52
|
+
if (error) return <div>Error: {error}</div>;
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div>
|
|
56
|
+
{items.map((item) => (
|
|
57
|
+
<${name} key={item.id} {...item} />
|
|
58
|
+
))}
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
function makeContainer(feature, name, pascalName) {
|
|
65
|
+
const base = (0, utils_1.ensureFeatureExists)(feature, 'containers');
|
|
66
|
+
const filePath = path.join(base, `${name}.tsx`);
|
|
67
|
+
if ((0, utils_1.fileExists)(filePath)) {
|
|
68
|
+
throw new Error(`Container "${name}" already exists at ${filePath}`);
|
|
69
|
+
}
|
|
70
|
+
const entityName = pascalName ?? name.replace(/Container$/, '');
|
|
71
|
+
(0, utils_1.writeFileSafe)(filePath, renderContainer(name, entityName, feature));
|
|
72
|
+
console.log(`✅ Container "${name}" created at ${filePath}`);
|
|
73
|
+
}
|
package/dist/commands/feature.js
CHANGED
|
@@ -34,8 +34,15 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.makeFeature = makeFeature;
|
|
37
|
-
const fs = __importStar(require("fs"));
|
|
38
37
|
const path = __importStar(require("path"));
|
|
38
|
+
const utils_1 = require("../utils");
|
|
39
|
+
const component_1 = require("./component");
|
|
40
|
+
const hook_1 = require("./hook");
|
|
41
|
+
const service_1 = require("./service");
|
|
42
|
+
const repository_1 = require("./repository");
|
|
43
|
+
const schema_1 = require("./schema");
|
|
44
|
+
const container_1 = require("./container");
|
|
45
|
+
const types_1 = require("./types");
|
|
39
46
|
const FEATURE_DIRS = [
|
|
40
47
|
'components/server',
|
|
41
48
|
'components/client',
|
|
@@ -44,28 +51,48 @@ const FEATURE_DIRS = [
|
|
|
44
51
|
'services',
|
|
45
52
|
'repositories',
|
|
46
53
|
'schemas',
|
|
54
|
+
'types',
|
|
47
55
|
];
|
|
48
|
-
function
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return `export default function ${componentName}Page() {
|
|
56
|
+
function renderPage(name, pascalName) {
|
|
57
|
+
const containerPath = (0, utils_1.resolveImport)(name, `containers/${pascalName}Container`, true);
|
|
58
|
+
return `import ${pascalName}Container from '${containerPath}';
|
|
59
|
+
|
|
60
|
+
export default function ${pascalName}Page() {
|
|
54
61
|
return (
|
|
55
62
|
<div>
|
|
56
|
-
|
|
63
|
+
<${pascalName}Container />
|
|
57
64
|
</div>
|
|
58
65
|
);
|
|
59
66
|
}
|
|
60
67
|
`;
|
|
61
68
|
}
|
|
62
|
-
async function makeFeature(name) {
|
|
63
|
-
const base =
|
|
69
|
+
async function makeFeature(name, all = false) {
|
|
70
|
+
const base = (0, utils_1.featureBasePath)(name);
|
|
71
|
+
const pascalName = (0, utils_1.toPascalCase)(name);
|
|
72
|
+
if ((0, utils_1.fileExists)(base)) {
|
|
73
|
+
throw new Error(`Feature "${name}" already exists at ${base}`);
|
|
74
|
+
}
|
|
64
75
|
for (const dir of FEATURE_DIRS) {
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
(0, utils_1.mkdirSafe)(path.join(base, dir));
|
|
77
|
+
if (!all) {
|
|
78
|
+
(0, utils_1.writeFileSafe)(path.join(base, dir, '.gitkeep'), '');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (all) {
|
|
82
|
+
(0, utils_1.writeFileSafe)(path.join(base, 'page.tsx'), renderPage(name, pascalName));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
(0, utils_1.writeFileSafe)(path.join(base, 'page.tsx'), `export default function ${pascalName}Page() {\n return (\n <div>\n <h1>${pascalName}</h1>\n </div>\n );\n}\n`);
|
|
67
86
|
}
|
|
68
|
-
const page = renderTemplate(name);
|
|
69
|
-
fs.writeFileSync(path.join(base, 'page.tsx'), page);
|
|
70
87
|
console.log(`✅ Feature "${name}" scaffolded at ${base}`);
|
|
88
|
+
if (all) {
|
|
89
|
+
(0, types_1.makeTypes)(name, pascalName);
|
|
90
|
+
(0, schema_1.makeSchema)(name, pascalName);
|
|
91
|
+
(0, repository_1.makeRepository)(name, pascalName);
|
|
92
|
+
(0, service_1.makeService)(name, pascalName);
|
|
93
|
+
(0, hook_1.makeHook)(name, `use${pascalName}`, pascalName);
|
|
94
|
+
(0, component_1.makeComponent)(name, pascalName, 'client');
|
|
95
|
+
(0, container_1.makeContainer)(name, `${pascalName}Container`, pascalName);
|
|
96
|
+
console.log(`✅ All files scaffolded for "${name}"`);
|
|
97
|
+
}
|
|
71
98
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.makeHook = makeHook;
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
const utils_1 = require("../utils");
|
|
39
|
+
function renderHook(hookName, name, feature) {
|
|
40
|
+
const typePath = (0, utils_1.resolveImport)(feature, `types/${name}.types`);
|
|
41
|
+
const svcPath = (op) => (0, utils_1.resolveImport)(feature, `services/${op}${name}.service`);
|
|
42
|
+
const schemaPath = (op) => (0, utils_1.resolveImport)(feature, `schemas/${op}${name}.schema`);
|
|
43
|
+
return `'use client';
|
|
44
|
+
|
|
45
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
46
|
+
import { ${name} } from '${typePath}';
|
|
47
|
+
import { List${name}Service } from '${svcPath('List')}';
|
|
48
|
+
import { Show${name}Service } from '${svcPath('Show')}';
|
|
49
|
+
import { Create${name}Service } from '${svcPath('Create')}';
|
|
50
|
+
import { Update${name}Service } from '${svcPath('Update')}';
|
|
51
|
+
import { Delete${name}Service } from '${svcPath('Delete')}';
|
|
52
|
+
import { Create${name} } from '${schemaPath('Create')}';
|
|
53
|
+
import { Update${name} } from '${schemaPath('Update')}';
|
|
54
|
+
|
|
55
|
+
const listService = new List${name}Service();
|
|
56
|
+
const showService = new Show${name}Service();
|
|
57
|
+
const createService = new Create${name}Service();
|
|
58
|
+
const updateService = new Update${name}Service();
|
|
59
|
+
const deleteService = new Delete${name}Service();
|
|
60
|
+
|
|
61
|
+
export function ${hookName}() {
|
|
62
|
+
const [items, setItems] = useState<${name}[]>([]);
|
|
63
|
+
const [selected, setSelected] = useState<${name} | null>(null);
|
|
64
|
+
const [loading, setLoading] = useState(false);
|
|
65
|
+
const [error, setError] = useState<string | null>(null);
|
|
66
|
+
|
|
67
|
+
const fetchAll = useCallback(async () => {
|
|
68
|
+
setLoading(true);
|
|
69
|
+
setError(null);
|
|
70
|
+
try {
|
|
71
|
+
const data = await listService.handle();
|
|
72
|
+
setItems(data);
|
|
73
|
+
} catch (err: unknown) {
|
|
74
|
+
setError(err instanceof Error ? err.message : 'Failed to fetch');
|
|
75
|
+
} finally {
|
|
76
|
+
setLoading(false);
|
|
77
|
+
}
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
const fetchOne = useCallback(async (id: string) => {
|
|
81
|
+
setLoading(true);
|
|
82
|
+
setError(null);
|
|
83
|
+
try {
|
|
84
|
+
const data = await showService.handle(id);
|
|
85
|
+
setSelected(data);
|
|
86
|
+
} catch (err: unknown) {
|
|
87
|
+
setError(err instanceof Error ? err.message : 'Failed to fetch');
|
|
88
|
+
} finally {
|
|
89
|
+
setLoading(false);
|
|
90
|
+
}
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
const create = useCallback(async (data: Create${name}) => {
|
|
94
|
+
setLoading(true);
|
|
95
|
+
setError(null);
|
|
96
|
+
try {
|
|
97
|
+
const created = await createService.handle(data);
|
|
98
|
+
setItems((prev) => [...prev, created]);
|
|
99
|
+
return created;
|
|
100
|
+
} catch (err: unknown) {
|
|
101
|
+
setError(err instanceof Error ? err.message : 'Failed to create');
|
|
102
|
+
return null;
|
|
103
|
+
} finally {
|
|
104
|
+
setLoading(false);
|
|
105
|
+
}
|
|
106
|
+
}, []);
|
|
107
|
+
|
|
108
|
+
const update = useCallback(async (id: string, data: Update${name}) => {
|
|
109
|
+
setLoading(true);
|
|
110
|
+
setError(null);
|
|
111
|
+
try {
|
|
112
|
+
const updated = await updateService.handle(id, data);
|
|
113
|
+
setItems((prev) => prev.map((item) => (item.id === id ? updated : item)));
|
|
114
|
+
return updated;
|
|
115
|
+
} catch (err: unknown) {
|
|
116
|
+
setError(err instanceof Error ? err.message : 'Failed to update');
|
|
117
|
+
return null;
|
|
118
|
+
} finally {
|
|
119
|
+
setLoading(false);
|
|
120
|
+
}
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
const remove = useCallback(async (id: string) => {
|
|
124
|
+
setLoading(true);
|
|
125
|
+
setError(null);
|
|
126
|
+
try {
|
|
127
|
+
await deleteService.handle(id);
|
|
128
|
+
setItems((prev) => prev.filter((item) => item.id !== id));
|
|
129
|
+
} catch (err: unknown) {
|
|
130
|
+
setError(err instanceof Error ? err.message : 'Failed to delete');
|
|
131
|
+
} finally {
|
|
132
|
+
setLoading(false);
|
|
133
|
+
}
|
|
134
|
+
}, []);
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
fetchAll();
|
|
138
|
+
}, [fetchAll]);
|
|
139
|
+
|
|
140
|
+
return { items, selected, loading, error, fetchAll, fetchOne, create, update, remove };
|
|
141
|
+
}
|
|
142
|
+
`;
|
|
143
|
+
}
|
|
144
|
+
function makeHook(feature, name, pascalName) {
|
|
145
|
+
const base = (0, utils_1.ensureFeatureExists)(feature, 'hooks');
|
|
146
|
+
const filePath = path.join(base, `${name}.ts`);
|
|
147
|
+
if ((0, utils_1.fileExists)(filePath)) {
|
|
148
|
+
throw new Error(`Hook "${name}" already exists at ${filePath}`);
|
|
149
|
+
}
|
|
150
|
+
const entityName = pascalName ?? name.replace(/^use/, '');
|
|
151
|
+
(0, utils_1.writeFileSafe)(filePath, renderHook(name, entityName, feature));
|
|
152
|
+
console.log(`✅ Hook "${name}" created at ${filePath}`);
|
|
153
|
+
}
|