dev-xcg 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +183 -2
- package/dist/arguments.js +19 -0
- package/dist/generator.js +164 -0
- package/dist/index.js +104 -0
- package/dist/naming.js +47 -0
- package/dist/templates.js +121 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shahadat Himel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,184 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dev-xcg
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**A lightweight CLI for scaffolding Express.js CRUD modules in TypeScript.**
|
|
4
|
+
|
|
5
|
+
`dev-xcg` creates a complete, convention-based module under `src/module/<resource>/` so you can start implementing business logic instead of repeatedly creating the same files.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/dev-xcg)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Generates five TypeScript files with matching names and imports
|
|
13
|
+
- Generates typed Express controller, service, and repository objects
|
|
14
|
+
- Uses relative Express router paths
|
|
15
|
+
- Produces consistent CRUD naming and service/repository contracts
|
|
16
|
+
- Validates resource names before writing files
|
|
17
|
+
- Refuses to overwrite existing modules
|
|
18
|
+
- Cleans up partial output when generation fails
|
|
19
|
+
- Has no runtime dependencies
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- Node.js 20 or newer
|
|
24
|
+
- An Express + TypeScript project
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Run `dev-xcg` once with `npx`:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx dev-xcg <resource>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or install it globally:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install --global dev-xcg
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
You can also add it as a development dependency:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install --save-dev dev-xcg
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
Run the CLI from the root of the project where CRUD modules should be generated:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
dev-xcg blog-post
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
This creates:
|
|
55
|
+
|
|
56
|
+
```text
|
|
57
|
+
src/module/blog-post/
|
|
58
|
+
├── blog-post.interface.ts
|
|
59
|
+
├── blog-post.controller.ts
|
|
60
|
+
├── blog-post.service.ts
|
|
61
|
+
├── blog-post.repository.ts
|
|
62
|
+
└── blog-post.route.ts
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Display CLI help with:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
dev-xcg --help
|
|
69
|
+
# or
|
|
70
|
+
dev-xcg -h
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Generated Module
|
|
74
|
+
|
|
75
|
+
For `blog-post`, `dev-xcg` creates the following interface, functions, and routes:
|
|
76
|
+
|
|
77
|
+
| Layer | Exports |
|
|
78
|
+
|-------|---------|
|
|
79
|
+
| Interface | `IBlogPost` |
|
|
80
|
+
| Controller | `blogPostController` with `createBlogPost`, `getBlogPosts`, `getBlogPostById`, `updateBlogPost`, `deleteBlogPost` |
|
|
81
|
+
| Service | `blogPostService` with `createBlogPost`, `getBlogPosts`, `getBlogPostById`, `updateBlogPost`, `deleteBlogPost` |
|
|
82
|
+
| Repository | `blogPostRepository` with `create`, `findAll`, `findById`, `update`, `deleteById` |
|
|
83
|
+
| Route | Typed `Router` instance exported as `blogPostRouter` |
|
|
84
|
+
|
|
85
|
+
The generated router declares these relative paths:
|
|
86
|
+
|
|
87
|
+
| Method | Path | Controller |
|
|
88
|
+
|--------|------|------------|
|
|
89
|
+
| `POST` | `/` | `createBlogPost` |
|
|
90
|
+
| `GET` | `/` | `getBlogPosts` |
|
|
91
|
+
| `GET` | `/:id` | `getBlogPostById` |
|
|
92
|
+
| `PATCH` | `/:id` | `updateBlogPost` |
|
|
93
|
+
| `DELETE` | `/:id` | `deleteBlogPost` |
|
|
94
|
+
|
|
95
|
+
The generated service, repository, and controller methods are empty implementation stubs. Add the appropriate application logic as needed.
|
|
96
|
+
|
|
97
|
+
## Resource Name Rules
|
|
98
|
+
|
|
99
|
+
Resource names must use lowercase kebab-case:
|
|
100
|
+
|
|
101
|
+
- Valid: `user`, `blog-post`, `product2`
|
|
102
|
+
- Invalid: `User`, `blog_post`, `blog post`, `-blog-post`, `blog--post`
|
|
103
|
+
|
|
104
|
+
Names must also satisfy these constraints:
|
|
105
|
+
|
|
106
|
+
- Exactly one resource argument
|
|
107
|
+
- Lowercase letters and digits only, with single hyphens between segments
|
|
108
|
+
- No spaces, underscores, path separators, or traversal sequences
|
|
109
|
+
|
|
110
|
+
## Safe Generation
|
|
111
|
+
|
|
112
|
+
`dev-xcg` is intentionally conservative:
|
|
113
|
+
|
|
114
|
+
- A resource directory is created only after validation and preflight checks
|
|
115
|
+
- Existing modules and files are never overwritten
|
|
116
|
+
- Files are written exclusively with the `wx` flag
|
|
117
|
+
- Directories and files created during a failed generation are removed
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
Clone the repository and install dependencies:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
git clone https://github.com/dev-himel-0o0/dev-xcg.git
|
|
125
|
+
cd dev-xcg
|
|
126
|
+
npm install
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Build the CLI:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
npm run build
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Run the strict typecheck:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npm run typecheck
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Build and run the test suite:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
npm test
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The tests cover argument parsing, resource naming, template rendering, filesystem generation, CLI integration, and generated TypeScript code.
|
|
148
|
+
|
|
149
|
+
Run one compiled test suite directly when useful:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
node --test dist-test/test/naming.test.js
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Publishing
|
|
156
|
+
|
|
157
|
+
Publishing is manual and should be performed from a clean `main` branch.
|
|
158
|
+
|
|
159
|
+
1. Update the package version in `package.json`.
|
|
160
|
+
2. Build and validate the package:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
npm run typecheck
|
|
164
|
+
npm test
|
|
165
|
+
npm run build
|
|
166
|
+
npm pack
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
3. Inspect the generated `.tgz` contents and test the packed CLI if necessary.
|
|
170
|
+
4. Publish with:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm publish
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The npm package contains the compiled `dist/` directory. The package has no runtime dependencies and requires Node.js 20 or newer.
|
|
177
|
+
|
|
178
|
+
## Contributing
|
|
179
|
+
|
|
180
|
+
Contributions are welcome. Keep changes focused, follow the existing TypeScript conventions, add or update tests for behavior changes, and run `npm run typecheck` and `npm test` before opening a pull request.
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
MIT © [Shahadat Himel](https://github.com/dev-himel-0o0)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function parseArguments(argv) {
|
|
2
|
+
const [resource, ...additional] = argv;
|
|
3
|
+
if (resource === undefined) {
|
|
4
|
+
return {
|
|
5
|
+
kind: "error",
|
|
6
|
+
message: "Missing resource name. Usage: dev-xcg <resource>",
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
if (additional.length > 0) {
|
|
10
|
+
return {
|
|
11
|
+
kind: "error",
|
|
12
|
+
message: `Expected exactly one resource name, but received ${argv.length}: ${argv.join(" ")}`,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (resource === "--help" || resource === "-h") {
|
|
16
|
+
return { kind: "help" };
|
|
17
|
+
}
|
|
18
|
+
return { kind: "resource", resource };
|
|
19
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { lstat, mkdir, open, rm, rmdir, stat } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { parseResourceName } from "./naming.js";
|
|
4
|
+
import { renderResourceTemplates } from "./templates.js";
|
|
5
|
+
function hasErrorCode(error, expectedCode) {
|
|
6
|
+
return (typeof error === "object" &&
|
|
7
|
+
error !== null &&
|
|
8
|
+
"code" in error &&
|
|
9
|
+
error.code === expectedCode);
|
|
10
|
+
}
|
|
11
|
+
function errorMessage(error) {
|
|
12
|
+
if (error instanceof Error) {
|
|
13
|
+
return error.message;
|
|
14
|
+
}
|
|
15
|
+
return String(error);
|
|
16
|
+
}
|
|
17
|
+
async function statIfExists(path) {
|
|
18
|
+
try {
|
|
19
|
+
return await stat(path);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (hasErrorCode(error, "ENOENT")) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function lstatIfExists(path) {
|
|
29
|
+
try {
|
|
30
|
+
return await lstat(path);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (hasErrorCode(error, "ENOENT")) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function writeExclusiveFile(filePath, content) {
|
|
40
|
+
const handle = await open(filePath, "wx");
|
|
41
|
+
try {
|
|
42
|
+
await handle.writeFile(content, "utf8");
|
|
43
|
+
await handle.close();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
await handle.close().catch(() => undefined);
|
|
47
|
+
await rm(filePath, { force: true }).catch(() => undefined);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function createDirectory(directory) {
|
|
52
|
+
try {
|
|
53
|
+
await mkdir(directory);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (hasErrorCode(error, "EEXIST")) {
|
|
58
|
+
const directoryStats = await stat(directory);
|
|
59
|
+
if (directoryStats.isDirectory()) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`${directory} exists and is not a directory.`);
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function preflight(paths) {
|
|
68
|
+
const srcStats = await statIfExists(paths.srcDirectory);
|
|
69
|
+
if (srcStats !== undefined && !srcStats.isDirectory()) {
|
|
70
|
+
return `${paths.srcDirectory} exists and is not a directory.`;
|
|
71
|
+
}
|
|
72
|
+
const moduleStats = await statIfExists(paths.moduleDirectory);
|
|
73
|
+
if (moduleStats !== undefined && !moduleStats.isDirectory()) {
|
|
74
|
+
return `${paths.moduleDirectory} exists and is not a directory.`;
|
|
75
|
+
}
|
|
76
|
+
if ((await lstatIfExists(paths.resourceDirectory)) !== undefined) {
|
|
77
|
+
return `Resource directory already exists: ${paths.resourceDirectory}. Refusing to overwrite.`;
|
|
78
|
+
}
|
|
79
|
+
for (const filePath of paths.files) {
|
|
80
|
+
if ((await lstatIfExists(filePath)) !== undefined) {
|
|
81
|
+
return `Refusing to overwrite existing file: ${filePath}`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
async function cleanup(files, directories) {
|
|
87
|
+
for (const filePath of [...files].reverse()) {
|
|
88
|
+
try {
|
|
89
|
+
await rm(filePath, { force: true });
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
for (const directory of [...directories].reverse()) {
|
|
96
|
+
try {
|
|
97
|
+
await rmdir(directory);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function generateResource(options) {
|
|
105
|
+
const parsedResource = parseResourceName(options.resource);
|
|
106
|
+
if (!parsedResource.ok) {
|
|
107
|
+
return { ok: false, message: parsedResource.message };
|
|
108
|
+
}
|
|
109
|
+
const resource = parsedResource.value;
|
|
110
|
+
const projectRoot = resolve(options.projectRoot);
|
|
111
|
+
const templates = renderResourceTemplates(resource);
|
|
112
|
+
const resourceDirectory = join(projectRoot, "src", "module", resource.directory);
|
|
113
|
+
const paths = {
|
|
114
|
+
srcDirectory: join(projectRoot, "src"),
|
|
115
|
+
moduleDirectory: join(projectRoot, "src", "module"),
|
|
116
|
+
resourceDirectory,
|
|
117
|
+
files: templates.map((template) => join(resourceDirectory, template.filename)),
|
|
118
|
+
};
|
|
119
|
+
const createdDirectories = [];
|
|
120
|
+
const createdFiles = [];
|
|
121
|
+
const writeFileExclusive = options.fileWriter ?? writeExclusiveFile;
|
|
122
|
+
try {
|
|
123
|
+
const conflict = await preflight(paths);
|
|
124
|
+
if (conflict !== undefined) {
|
|
125
|
+
return { ok: false, message: conflict };
|
|
126
|
+
}
|
|
127
|
+
if (await createDirectory(paths.srcDirectory)) {
|
|
128
|
+
createdDirectories.push(paths.srcDirectory);
|
|
129
|
+
}
|
|
130
|
+
if (await createDirectory(paths.moduleDirectory)) {
|
|
131
|
+
createdDirectories.push(paths.moduleDirectory);
|
|
132
|
+
}
|
|
133
|
+
if (!(await createDirectory(paths.resourceDirectory))) {
|
|
134
|
+
throw new Error(`Resource directory already exists: ${paths.resourceDirectory}. Refusing to overwrite.`);
|
|
135
|
+
}
|
|
136
|
+
createdDirectories.push(paths.resourceDirectory);
|
|
137
|
+
for (const template of templates) {
|
|
138
|
+
const filePath = join(paths.resourceDirectory, template.filename);
|
|
139
|
+
options.onFileStart?.(template.filename);
|
|
140
|
+
try {
|
|
141
|
+
await writeFileExclusive(filePath, template.content);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
if (hasErrorCode(error, "EEXIST")) {
|
|
145
|
+
throw new Error(`Refusing to overwrite existing file: ${filePath}`);
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
createdFiles.push(filePath);
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
ok: true,
|
|
153
|
+
outputDirectory: paths.resourceDirectory,
|
|
154
|
+
files: templates.map((template) => template.filename),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
await cleanup(createdFiles, createdDirectories);
|
|
159
|
+
return {
|
|
160
|
+
ok: false,
|
|
161
|
+
message: `Failed to generate module: ${errorMessage(error)}`,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArguments } from "./arguments.js";
|
|
3
|
+
import { generateResource } from "./generator.js";
|
|
4
|
+
import { relative, sep } from "node:path";
|
|
5
|
+
const usage = `dev-xcg - Express CRUD Generator
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
dev-xcg <resource>
|
|
9
|
+
|
|
10
|
+
Resource names:
|
|
11
|
+
Lowercase letters, numbers, and single hyphens
|
|
12
|
+
Examples: user, blog-post`;
|
|
13
|
+
function fail(message, includeUsage) {
|
|
14
|
+
console.error(message);
|
|
15
|
+
if (includeUsage) {
|
|
16
|
+
console.error();
|
|
17
|
+
console.error(usage);
|
|
18
|
+
}
|
|
19
|
+
process.exitCode = 1;
|
|
20
|
+
}
|
|
21
|
+
async function main(argv) {
|
|
22
|
+
const parsed = parseArguments(argv);
|
|
23
|
+
if (parsed.kind === "help") {
|
|
24
|
+
console.log(usage);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (parsed.kind === "error") {
|
|
28
|
+
fail(parsed.message, true);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const projectRoot = process.cwd();
|
|
32
|
+
const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
33
|
+
let spinnerFrame = 0;
|
|
34
|
+
let spinnerTimer;
|
|
35
|
+
let spinnerLabel = `Creating ${parsed.resource} module`;
|
|
36
|
+
const isInteractive = process.stdout.isTTY === true;
|
|
37
|
+
const renderSpinner = () => {
|
|
38
|
+
if (isInteractive) {
|
|
39
|
+
process.stdout.write(`\r\x1b[2K${spinnerFrames[spinnerFrame]} ${spinnerLabel}`);
|
|
40
|
+
spinnerFrame = (spinnerFrame + 1) % spinnerFrames.length;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const stopSpinner = () => {
|
|
44
|
+
if (spinnerTimer !== undefined) {
|
|
45
|
+
clearInterval(spinnerTimer);
|
|
46
|
+
spinnerTimer = undefined;
|
|
47
|
+
}
|
|
48
|
+
if (isInteractive) {
|
|
49
|
+
process.stdout.write("\r\x1b[2K\x1b[?25h");
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
if (isInteractive) {
|
|
53
|
+
process.stdout.write("\x1b[?25l");
|
|
54
|
+
renderSpinner();
|
|
55
|
+
spinnerTimer = setInterval(renderSpinner, 80);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
console.log(`◌ ${spinnerLabel}`);
|
|
59
|
+
}
|
|
60
|
+
const result = await generateResource({
|
|
61
|
+
projectRoot,
|
|
62
|
+
resource: parsed.resource,
|
|
63
|
+
onFileStart: (filename) => {
|
|
64
|
+
spinnerLabel = `Creating ${filename}`;
|
|
65
|
+
if (!isInteractive) {
|
|
66
|
+
console.log(`◌ ${spinnerLabel}`);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
renderSpinner();
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
stopSpinner();
|
|
74
|
+
if (!result.ok) {
|
|
75
|
+
fail(result.message, false);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const outputDirectory = relative(projectRoot, result.outputDirectory)
|
|
79
|
+
.split(sep)
|
|
80
|
+
.join("/");
|
|
81
|
+
const filenamePrefix = parsed.resource;
|
|
82
|
+
const orderedFiles = [
|
|
83
|
+
`${filenamePrefix}.route.ts`,
|
|
84
|
+
`${filenamePrefix}.interface.ts`,
|
|
85
|
+
`${filenamePrefix}.controller.ts`,
|
|
86
|
+
`${filenamePrefix}.service.ts`,
|
|
87
|
+
`${filenamePrefix}.repository.ts`,
|
|
88
|
+
];
|
|
89
|
+
console.log(`✔ Resource name: ${parsed.resource}`);
|
|
90
|
+
console.log(`✔ Created ${outputDirectory}/`);
|
|
91
|
+
for (const filename of orderedFiles) {
|
|
92
|
+
console.log(`✔ Created ${filename}`);
|
|
93
|
+
}
|
|
94
|
+
console.log();
|
|
95
|
+
console.log(`✨ ${parsed.resource} module generated successfully!`);
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
await main(process.argv.slice(2));
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
102
|
+
console.error(`Failed to generate module: ${message}`);
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
}
|
package/dist/naming.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const RESOURCE_NAME_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z][a-z0-9]*)*$/;
|
|
2
|
+
function invalidResourceNameMessage(input) {
|
|
3
|
+
if (input.length === 0) {
|
|
4
|
+
return "Resource name must not be empty.";
|
|
5
|
+
}
|
|
6
|
+
if (input.includes("/") || input.includes("\\") || input.includes("..")) {
|
|
7
|
+
return `Resource name "${input}" must not contain path separators or traversal sequences.`;
|
|
8
|
+
}
|
|
9
|
+
if (input !== input.toLowerCase()) {
|
|
10
|
+
return `Resource name "${input}" must be lowercase.`;
|
|
11
|
+
}
|
|
12
|
+
if (/\s/.test(input)) {
|
|
13
|
+
return `Resource name "${input}" must not contain spaces.`;
|
|
14
|
+
}
|
|
15
|
+
return `Invalid resource name "${input}". Use lowercase letters, numbers, and single hyphens (for example "user" or "blog-post").`;
|
|
16
|
+
}
|
|
17
|
+
function capitalize(value) {
|
|
18
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
19
|
+
}
|
|
20
|
+
export function toCamelCase(value) {
|
|
21
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
22
|
+
}
|
|
23
|
+
export function toPluralPascalCase(value) {
|
|
24
|
+
if (/[^aeiou]y$/i.test(value)) {
|
|
25
|
+
return `${value.slice(0, -1)}ies`;
|
|
26
|
+
}
|
|
27
|
+
if (/(?:s|x|z|ch|sh)$/i.test(value)) {
|
|
28
|
+
return `${value}es`;
|
|
29
|
+
}
|
|
30
|
+
return `${value}s`;
|
|
31
|
+
}
|
|
32
|
+
export function parseResourceName(input) {
|
|
33
|
+
if (!RESOURCE_NAME_PATTERN.test(input)) {
|
|
34
|
+
return { ok: false, message: invalidResourceNameMessage(input) };
|
|
35
|
+
}
|
|
36
|
+
const pascalCase = input.split("-").map(capitalize).join("");
|
|
37
|
+
return {
|
|
38
|
+
ok: true,
|
|
39
|
+
value: {
|
|
40
|
+
slug: input,
|
|
41
|
+
directory: input,
|
|
42
|
+
filenamePrefix: input,
|
|
43
|
+
pascalCase,
|
|
44
|
+
functionPrefix: pascalCase,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { toCamelCase, toPluralPascalCase } from "./naming.js";
|
|
2
|
+
function renderInterface(resource) {
|
|
3
|
+
return `export interface I${resource.pascalCase} {}\n`;
|
|
4
|
+
}
|
|
5
|
+
function renderController(resource) {
|
|
6
|
+
const entity = resource.functionPrefix;
|
|
7
|
+
const collection = toPluralPascalCase(entity);
|
|
8
|
+
const objectName = `${toCamelCase(entity)}Controller`;
|
|
9
|
+
return `import type { Request, Response } from "express";
|
|
10
|
+
|
|
11
|
+
const create${entity} = async (req: Request, res: Response) => {};
|
|
12
|
+
|
|
13
|
+
const get${collection} = async (req: Request, res: Response) => {};
|
|
14
|
+
|
|
15
|
+
const get${entity}ById = async (req: Request, res: Response) => {};
|
|
16
|
+
|
|
17
|
+
const update${entity} = async (req: Request, res: Response) => {};
|
|
18
|
+
|
|
19
|
+
const delete${entity} = async (req: Request, res: Response) => {};
|
|
20
|
+
|
|
21
|
+
export const ${objectName} = {
|
|
22
|
+
create${entity},
|
|
23
|
+
get${collection},
|
|
24
|
+
get${entity}ById,
|
|
25
|
+
update${entity},
|
|
26
|
+
delete${entity},
|
|
27
|
+
};
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
function renderService(resource) {
|
|
31
|
+
const entity = resource.functionPrefix;
|
|
32
|
+
const collection = toPluralPascalCase(entity);
|
|
33
|
+
const interfaceName = `I${resource.pascalCase}`;
|
|
34
|
+
const objectName = `${toCamelCase(entity)}Service`;
|
|
35
|
+
return `import type { ${interfaceName} } from "./${resource.filenamePrefix}.interface";
|
|
36
|
+
|
|
37
|
+
const create${entity} = async (payload: ${interfaceName}) => {};
|
|
38
|
+
|
|
39
|
+
const get${collection} = async () => {};
|
|
40
|
+
|
|
41
|
+
const get${entity}ById = async (id: string) => {};
|
|
42
|
+
|
|
43
|
+
const update${entity} = async (id: string, payload: ${interfaceName}) => {};
|
|
44
|
+
|
|
45
|
+
const delete${entity} = async (id: string) => {};
|
|
46
|
+
|
|
47
|
+
export const ${objectName} = {
|
|
48
|
+
create${entity},
|
|
49
|
+
get${collection},
|
|
50
|
+
get${entity}ById,
|
|
51
|
+
update${entity},
|
|
52
|
+
delete${entity},
|
|
53
|
+
};
|
|
54
|
+
`;
|
|
55
|
+
}
|
|
56
|
+
function renderRepository(resource) {
|
|
57
|
+
const interfaceName = `I${resource.pascalCase}`;
|
|
58
|
+
const objectName = `${toCamelCase(resource.functionPrefix)}Repository`;
|
|
59
|
+
return `import type { ${interfaceName} } from "./${resource.filenamePrefix}.interface";
|
|
60
|
+
|
|
61
|
+
const create = async (payload: ${interfaceName}) => {};
|
|
62
|
+
|
|
63
|
+
const findAll = async () => {};
|
|
64
|
+
|
|
65
|
+
const findById = async (id: string) => {};
|
|
66
|
+
|
|
67
|
+
const update = async (id: string, payload: ${interfaceName}) => {};
|
|
68
|
+
|
|
69
|
+
const deleteById = async (id: string) => {};
|
|
70
|
+
|
|
71
|
+
export const ${objectName} = {
|
|
72
|
+
create,
|
|
73
|
+
findAll,
|
|
74
|
+
findById,
|
|
75
|
+
update,
|
|
76
|
+
deleteById,
|
|
77
|
+
};
|
|
78
|
+
`;
|
|
79
|
+
}
|
|
80
|
+
function renderRoute(resource) {
|
|
81
|
+
const entity = resource.functionPrefix;
|
|
82
|
+
const collection = toPluralPascalCase(entity);
|
|
83
|
+
const routerName = `${toCamelCase(entity)}Router`;
|
|
84
|
+
const controllerName = `${toCamelCase(entity)}Controller`;
|
|
85
|
+
return `import type { Router } from "express";
|
|
86
|
+
import express from "express";
|
|
87
|
+
import { ${controllerName} } from "./${resource.filenamePrefix}.controller";
|
|
88
|
+
|
|
89
|
+
export const ${routerName}: Router = express.Router();
|
|
90
|
+
|
|
91
|
+
${routerName}.post("/", ${controllerName}.create${entity});
|
|
92
|
+
${routerName}.get("/", ${controllerName}.get${collection});
|
|
93
|
+
${routerName}.get("/:id", ${controllerName}.get${entity}ById);
|
|
94
|
+
${routerName}.patch("/:id", ${controllerName}.update${entity});
|
|
95
|
+
${routerName}.delete("/:id", ${controllerName}.delete${entity});
|
|
96
|
+
`;
|
|
97
|
+
}
|
|
98
|
+
export function renderResourceTemplates(resource) {
|
|
99
|
+
return [
|
|
100
|
+
{
|
|
101
|
+
filename: `${resource.filenamePrefix}.interface.ts`,
|
|
102
|
+
content: renderInterface(resource),
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
filename: `${resource.filenamePrefix}.controller.ts`,
|
|
106
|
+
content: renderController(resource),
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
filename: `${resource.filenamePrefix}.service.ts`,
|
|
110
|
+
content: renderService(resource),
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
filename: `${resource.filenamePrefix}.repository.ts`,
|
|
114
|
+
content: renderRepository(resource),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
filename: `${resource.filenamePrefix}.route.ts`,
|
|
118
|
+
content: renderRoute(resource),
|
|
119
|
+
},
|
|
120
|
+
];
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dev-xcg",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Express CRUD Generator - Generate TypeScript CRUD modules for Express.js applications with ease.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"dev-xcg": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"build:test": "tsc -p tsconfig.test.json",
|
|
18
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.test.json --noEmit",
|
|
19
|
+
"test": "npm run build:test && node --test dist-test/test",
|
|
20
|
+
"start": "node dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"express",
|
|
27
|
+
"crud",
|
|
28
|
+
"generator",
|
|
29
|
+
"cli",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/dev-himel-0o0/dev-xcg.git"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/express": "^5.0.6",
|
|
39
|
+
"@types/node": "^26.6.6",
|
|
40
|
+
"express": "^5.3.0",
|
|
41
|
+
"typescript": "^7.0.2"
|
|
42
|
+
}
|
|
43
|
+
}
|