@craftycodesmith/node-structure 1.0.1 → 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.
Files changed (2) hide show
  1. package/dist/index.js +24 -16
  2. package/package.json +1 -1
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 { values, positionals } = (0, node_util_1.parseArgs)({
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
- // --- Command Handlers ---
26
- const init = () => {
27
- console.log('Initializing feature-based structure...');
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', { stdio: 'inherit' });
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', { stdio: 'inherit' });
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
- // --- Simple Router ---
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: npx your-tool <command> [args]\nCommands: init, res [name]');
73
+ console.log('Usage:\n npx my-cli init [folder-name]\n npx my-cli res [resource-name]');
66
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@craftycodesmith/node-structure",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Feature-based architecture generator",
5
5
  "main": "dist/index.js",
6
6
  "bin": {