@elysiumjs/styx 0.4.0 → 0.5.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/src/styx.ts ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env bun
2
+ // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ import 'reflect-metadata';
16
+
17
+ import {
18
+ MakeCommandCommand,
19
+ MakeControllerCommand,
20
+ MakeJobCommand,
21
+ MakeMiddlewareCommand,
22
+ MakeModelCommand,
23
+ MakeRepositoryCommand,
24
+ MakeServiceCommand,
25
+ MakeValidatorCommand,
26
+ MigrationGenerateCommand,
27
+ MigrationRunCommand,
28
+ ModuleNewCommand,
29
+ ModuleRenameCommand
30
+ } from './commands';
31
+ import { getProjectPath } from './utils';
32
+
33
+ const projectPath = getProjectPath();
34
+ const { App } = await import(`${projectPath}/src/app`);
35
+
36
+ @App.register({
37
+ commands: [
38
+ MakeCommandCommand,
39
+ MakeControllerCommand,
40
+ MakeJobCommand,
41
+ MakeMiddlewareCommand,
42
+ MakeModelCommand,
43
+ MakeRepositoryCommand,
44
+ MakeServiceCommand,
45
+ MakeValidatorCommand,
46
+ MigrationGenerateCommand,
47
+ MigrationRunCommand,
48
+ ModuleNewCommand,
49
+ ModuleRenameCommand
50
+ ]
51
+ })
52
+ class StyxApp extends App {}
53
+
54
+ new StyxApp();
package/src/utils.ts CHANGED
@@ -12,7 +12,9 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
- import { dirname } from 'node:path';
15
+ import { existsSync } from 'node:fs';
16
+ import { mkdir, readdir, rename } from 'node:fs/promises';
17
+ import { dirname, extname, join } from 'node:path';
16
18
 
17
19
  /**
18
20
  * Gets the root path of the styx package.
@@ -21,3 +23,126 @@ import { dirname } from 'node:path';
21
23
  export const getRootPath = () => {
22
24
  return dirname(Bun.fileURLToPath(import.meta.url));
23
25
  };
26
+
27
+ /**
28
+ * Gets the root path of the Elysium.js project.
29
+ * @author Axel Nana <axel.nana@workbud.com>
30
+ *
31
+ * @returns The detected project path.
32
+ */
33
+ export const getProjectPath = () => {
34
+ let startPath = process.cwd(),
35
+ dir = startPath;
36
+
37
+ while (true) {
38
+ if (existsSync(join(dir, '.elysiumrc'))) return dir;
39
+
40
+ const parentDir = dirname(dir);
41
+ if (dir === parentDir) break;
42
+
43
+ dir = parentDir;
44
+ }
45
+
46
+ return startPath;
47
+ };
48
+
49
+ /**
50
+ * Creates a new module or rename an existing module.
51
+ * @author Axel Nana <axel.nana@workbud.com>
52
+ *
53
+ * @param name The name of the module to create or rename.
54
+ * @param oldName The old name of the module to rename.
55
+ */
56
+ export const createModule = async (name: string, oldName?: string) => {
57
+ const root = getProjectPath();
58
+
59
+ // Create module directory
60
+ {
61
+ const moduleDir = join(root, 'src', 'modules', name);
62
+ await mkdir(moduleDir, { recursive: true });
63
+
64
+ if (oldName) {
65
+ if (oldName === 'root') {
66
+ const entries = await readdir(join(root, 'src'));
67
+ for (const entry of entries) {
68
+ if (['app.ts', 'env.ts', 'database', 'modules'].includes(entry)) continue;
69
+ const oldPath = join(root, 'src', entry);
70
+ const newPath = join(moduleDir, entry === 'main.module.ts' ? `${name}.module.ts` : entry);
71
+ await rename(oldPath, newPath);
72
+ }
73
+ } else {
74
+ const oldModuleDir = join(root, 'src', 'modules', oldName);
75
+ await rename(oldModuleDir, moduleDir);
76
+ }
77
+ }
78
+ }
79
+
80
+ // Update package.json imports
81
+ {
82
+ const pkg = await Bun.file(join(root, 'package.json')).json();
83
+
84
+ pkg.imports = pkg.imports ?? {};
85
+ pkg.imports[`#${name}/*`] = `./src/modules/${name}/*.ts`;
86
+
87
+ if (oldName && oldName !== 'root') {
88
+ delete pkg.imports[`#${oldName}/*`];
89
+ }
90
+
91
+ await Bun.write(join(root, 'package.json'), JSON.stringify(pkg, null, 2));
92
+ }
93
+
94
+ // Update .elysiumrc
95
+ {
96
+ const rc = await Bun.file(join(root, '.elysiumrc')).json();
97
+
98
+ rc.mono = false;
99
+ rc.modules = rc.modules ?? {};
100
+ rc.modules[name] = `./src/modules/${name}`;
101
+
102
+ if (oldName) {
103
+ delete rc.modules[oldName];
104
+ }
105
+
106
+ await Bun.write(join(root, '.elysiumrc'), JSON.stringify(rc, null, 2));
107
+ }
108
+
109
+ // Update imports in all modules
110
+ if (oldName) {
111
+ const rewriteImports = async (dir: string) => {
112
+ const entries = await readdir(dir, { withFileTypes: true });
113
+
114
+ for (const entry of entries) {
115
+ const fullPath = join(dir, entry.name);
116
+
117
+ if (entry.isDirectory()) {
118
+ await rewriteImports(fullPath);
119
+ } else if (extname(entry.name) === '.ts') {
120
+ let text = await Bun.file(fullPath).text();
121
+
122
+ if (oldName === 'root') {
123
+ text = text.replace(
124
+ new RegExp(`(['"])#${oldName}/((?!(env|app|database(?:/|$))).*?)(['"])`, 'g'),
125
+ `$1#${name}/$2$4`
126
+ );
127
+ } else {
128
+ text = text.replace(new RegExp(`(['"])#${oldName}/`, 'g'), `$1#${name}/`);
129
+ }
130
+
131
+ text = text.replace(
132
+ new RegExp(`(['"])#${oldName}/main\\.module(['"])/g`, 'g'),
133
+ `$1#${name}/${name}.module$2`
134
+ );
135
+
136
+ text = text.replace(
137
+ new RegExp(`(['"])#${oldName}/main\\.module\\.ts(['"])?/g`, 'g'),
138
+ `$1#${name}/${name}.module$2`
139
+ );
140
+
141
+ await Bun.write(fullPath, text);
142
+ }
143
+ }
144
+ };
145
+
146
+ await rewriteImports(root);
147
+ }
148
+ };