@craftycodesmith/node-structure 1.0.0 → 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/dist/index.js +51 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,54 +1,66 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const node_util_1 = require("node:util");
|
|
8
|
+
const { values, positionals } = (0, node_util_1.parseArgs)({
|
|
9
|
+
allowPositionals: true,
|
|
10
|
+
options: {
|
|
11
|
+
help: { type: 'boolean', short: 'h' }
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
const command = positionals[0];
|
|
15
|
+
const root = process.cwd();
|
|
19
16
|
const getTemplate = (type, name) => {
|
|
20
17
|
const templates = {
|
|
21
18
|
controller: `export class ${name}Controller {\n // Logic for ${name}\n}`,
|
|
22
19
|
service: `export class ${name}Service {\n // Business logic for ${name}\n}`,
|
|
23
20
|
model: `export interface ${name} {\n id: string;\n}`,
|
|
24
|
-
tsconfig: `{\n "compilerOptions": {\n "target": "ESNext",\n "module": "
|
|
21
|
+
tsconfig: `{\n "compilerOptions": {\n "target": "ESNext",\n "module": "NodeNext",\n "outDir": "./dist",\n "strict": true\n }\n}`
|
|
25
22
|
};
|
|
26
23
|
return templates[type] || '';
|
|
27
24
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
.
|
|
31
|
-
.action(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
// --- Command Handlers ---
|
|
26
|
+
const init = () => {
|
|
27
|
+
console.log('Initializing feature-based structure...');
|
|
32
28
|
const folders = ['src/features', 'src/common', 'src/config'];
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
folders.forEach(folder => {
|
|
30
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(root, folder), { recursive: true });
|
|
31
|
+
});
|
|
32
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(root, 'tsconfig.json'), getTemplate('tsconfig', ''));
|
|
33
|
+
if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'package.json'))) {
|
|
34
|
+
(0, node_child_process_1.execSync)('npm init -y', { stdio: 'inherit' });
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
console.log('
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
(0, node_child_process_1.execSync)('npm install -D typescript @types/node ts-node', { stdio: 'inherit' });
|
|
37
|
+
console.log('Project initialized successfully.');
|
|
38
|
+
};
|
|
39
|
+
const generateResource = (name) => {
|
|
40
|
+
if (!name) {
|
|
41
|
+
console.error('Error: Please provide a resource name.');
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
const featureDir = (0, node_path_1.join)(root, 'src/features', name.toLowerCase());
|
|
45
|
+
(0, node_fs_1.mkdirSync)(featureDir, { recursive: true });
|
|
44
46
|
const files = [
|
|
45
|
-
{
|
|
46
|
-
{
|
|
47
|
-
{
|
|
47
|
+
{ fileName: `${name.toLowerCase()}.controller.ts`, type: 'controller' },
|
|
48
|
+
{ fileName: `${name.toLowerCase()}.service.ts`, type: 'service' },
|
|
49
|
+
{ fileName: `${name.toLowerCase()}.model.ts`, type: 'model' }
|
|
48
50
|
];
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
console.log(`Module "${name}" generated in
|
|
53
|
-
}
|
|
54
|
-
|
|
51
|
+
files.forEach(file => {
|
|
52
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(featureDir, file.fileName), getTemplate(file.type, name));
|
|
53
|
+
});
|
|
54
|
+
console.log(`Module "${name}" generated in ${featureDir}`);
|
|
55
|
+
};
|
|
56
|
+
// --- Simple Router ---
|
|
57
|
+
switch (command) {
|
|
58
|
+
case 'init':
|
|
59
|
+
init();
|
|
60
|
+
break;
|
|
61
|
+
case 'res':
|
|
62
|
+
generateResource(positionals[1]);
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
console.log('Usage: npx your-tool <command> [args]\nCommands: init, res [name]');
|
|
66
|
+
}
|