@craftycodesmith/node-structure 1.0.0 → 1.0.2
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 +59 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,54 +1,74 @@
|
|
|
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 { positionals } = (0, node_util_1.parseArgs)({
|
|
9
|
+
allowPositionals: true
|
|
10
|
+
});
|
|
11
|
+
const command = positionals[0];
|
|
19
12
|
const getTemplate = (type, name) => {
|
|
20
13
|
const templates = {
|
|
21
14
|
controller: `export class ${name}Controller {\n // Logic for ${name}\n}`,
|
|
22
15
|
service: `export class ${name}Service {\n // Business logic for ${name}\n}`,
|
|
23
16
|
model: `export interface ${name} {\n id: string;\n}`,
|
|
24
|
-
tsconfig: `{\n "compilerOptions": {\n "target": "ESNext",\n "module": "
|
|
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}`
|
|
25
18
|
};
|
|
26
19
|
return templates[type] || '';
|
|
27
20
|
};
|
|
28
|
-
|
|
29
|
-
.
|
|
30
|
-
.
|
|
31
|
-
|
|
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
|
+
}
|
|
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
|
+
// Execute commands inside the target directory
|
|
34
|
+
const execOptions = { cwd: root, stdio: 'inherit' };
|
|
35
|
+
if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'package.json'))) {
|
|
36
|
+
(0, node_child_process_1.execSync)('npm init -y', execOptions);
|
|
35
37
|
}
|
|
36
|
-
|
|
37
|
-
console.log('
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
(0, node_child_process_1.execSync)('npm install -D typescript @types/node ts-node', execOptions);
|
|
39
|
+
console.log('Project initialized successfully.');
|
|
40
|
+
};
|
|
41
|
+
const generateResource = (name) => {
|
|
42
|
+
if (!name) {
|
|
43
|
+
console.error('Error: Please provide a resource name (e.g., npx my-cli res user)');
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
// Assumes execution from project root
|
|
47
|
+
const root = process.cwd();
|
|
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
|
+
}
|
|
53
|
+
(0, node_fs_1.mkdirSync)(featureDir, { recursive: true });
|
|
44
54
|
const files = [
|
|
45
|
-
{
|
|
46
|
-
{
|
|
47
|
-
{
|
|
55
|
+
{ fileName: `${name.toLowerCase()}.controller.ts`, type: 'controller' },
|
|
56
|
+
{ fileName: `${name.toLowerCase()}.service.ts`, type: 'service' },
|
|
57
|
+
{ fileName: `${name.toLowerCase()}.model.ts`, type: 'model' }
|
|
48
58
|
];
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
console.log(`Module "${name}" generated in
|
|
53
|
-
}
|
|
54
|
-
|
|
59
|
+
files.forEach(file => {
|
|
60
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.join)(featureDir, file.fileName), getTemplate(file.type, name));
|
|
61
|
+
});
|
|
62
|
+
console.log(`Module "${name}" generated in ${featureDir}`);
|
|
63
|
+
};
|
|
64
|
+
// --- Command Dispatcher ---
|
|
65
|
+
switch (command) {
|
|
66
|
+
case 'init':
|
|
67
|
+
init(positionals[1]); // positionals[1] is the folder name if provided
|
|
68
|
+
break;
|
|
69
|
+
case 'res':
|
|
70
|
+
generateResource(positionals[1]);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
console.log('Usage:\n npx my-cli init [folder-name]\n npx my-cli res [resource-name]');
|
|
74
|
+
}
|