@elysiumjs/styx 0.3.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.
@@ -0,0 +1,225 @@
1
+ // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import type { PromptObject } from 'prompts';
16
+
17
+ import { join } from 'node:path';
18
+
19
+ import { Command, CommandArgumentType } from '@elysiumjs/core';
20
+ import prompts from 'prompts';
21
+ import { snake } from 'radash';
22
+ import formatter from 'string-template';
23
+
24
+ import { getModulePath, parseProjectConfig } from '../config';
25
+ import { getRootPath } from '../utils';
26
+
27
+ /**
28
+ * Maker command for creating Elysium repositories.
29
+ * @author Axel Nana <axel.nana@workbud.com>
30
+ */
31
+ export class MakeRepositoryCommand extends Command {
32
+ public static readonly command: string = 'make:repository';
33
+ public static readonly description: string = 'Creates a new repository.';
34
+
35
+ @Command.arg({
36
+ description: 'The name of the repository to create',
37
+ type: CommandArgumentType.STRING
38
+ })
39
+ private name?: string;
40
+
41
+ @Command.arg({
42
+ description: 'The module where the repository will be created',
43
+ type: CommandArgumentType.STRING
44
+ })
45
+ private module?: string;
46
+
47
+ @Command.arg({
48
+ description: 'The alias of the repository to create',
49
+ type: CommandArgumentType.STRING
50
+ })
51
+ private alias?: string;
52
+
53
+ @Command.arg({
54
+ description: 'The model used by the repository',
55
+ type: CommandArgumentType.STRING
56
+ })
57
+ private model?: string;
58
+
59
+ @Command.arg({
60
+ description: 'Create a factory repository',
61
+ type: CommandArgumentType.BOOLEAN,
62
+ default: false
63
+ })
64
+ private factory: boolean = false;
65
+
66
+ @Command.arg({
67
+ description: 'Create a singleton repository',
68
+ type: CommandArgumentType.BOOLEAN,
69
+ default: false
70
+ })
71
+ private singleton: boolean = false;
72
+
73
+ public async run(): Promise<void> {
74
+ if (!this.name) {
75
+ return this.setup();
76
+ }
77
+
78
+ const config = await parseProjectConfig();
79
+
80
+ const answers: Record<string, any> = {
81
+ module: this.module,
82
+ name: this.name,
83
+ alias: this.alias,
84
+ scope: this.factory ? 'FACTORY' : this.singleton ? 'SINGLETON' : 'SINGLETON',
85
+ model: this.model
86
+ };
87
+
88
+ if (!answers.module && !config.mono) {
89
+ const module = await prompts({
90
+ type: 'select',
91
+ name: 'module',
92
+ message: 'Module:',
93
+ choices: Object.keys(config.modules ?? {}).map((moduleName) => ({
94
+ title: moduleName,
95
+ value: moduleName
96
+ }))
97
+ });
98
+
99
+ answers.module = module.module;
100
+ }
101
+
102
+ return this.build(answers);
103
+ }
104
+
105
+ private async setup(): Promise<void> {
106
+ const config = await parseProjectConfig();
107
+
108
+ const items: PromptObject[] = [
109
+ {
110
+ type() {
111
+ return config.mono ? null : 'select';
112
+ },
113
+ name: 'module',
114
+ message: 'Module:',
115
+ choices() {
116
+ return Object.keys(config.modules ?? {}).map((moduleName) => ({
117
+ title: moduleName,
118
+ value: moduleName
119
+ }));
120
+ }
121
+ },
122
+ {
123
+ type: 'text',
124
+ name: 'name',
125
+ message: 'Repository Name:',
126
+ initial: 'UserRepository',
127
+ validate(value: string) {
128
+ if (value.length < 1) {
129
+ return 'Repository name cannot be empty';
130
+ }
131
+
132
+ return true;
133
+ }
134
+ },
135
+ {
136
+ type: 'text',
137
+ name: 'model',
138
+ message: 'Model Name:',
139
+ initial: 'UserModel',
140
+ validate(value: string) {
141
+ if (value.length < 1) {
142
+ return 'Model name cannot be empty';
143
+ }
144
+
145
+ return true;
146
+ }
147
+ },
148
+ {
149
+ type: 'text',
150
+ name: 'alias',
151
+ message: 'Repository Alias:',
152
+ initial(_, values) {
153
+ return values.name;
154
+ },
155
+ validate(value: string) {
156
+ if (value.length < 1) {
157
+ return 'Repository alias cannot be empty';
158
+ }
159
+
160
+ return true;
161
+ }
162
+ },
163
+ {
164
+ type: 'select',
165
+ name: 'scope',
166
+ message: 'Repository Scope:',
167
+ choices: [
168
+ {
169
+ title: 'SINGLETON',
170
+ value: 'SINGLETON',
171
+ description: 'A single instance of the repository is created.'
172
+ },
173
+ {
174
+ title: 'FACTORY',
175
+ value: 'FACTORY',
176
+ description: 'A new instance of the repository is created each time it is injected.'
177
+ }
178
+ ]
179
+ }
180
+ ];
181
+
182
+ const answers = await prompts(items, {
183
+ onCancel: () => {
184
+ this.error('Operation cancelled.');
185
+ process.exit(0);
186
+ }
187
+ });
188
+
189
+ return this.build(answers);
190
+ }
191
+
192
+ private async build(answers: Record<string, any>): Promise<void> {
193
+ if (!answers.name) {
194
+ this.error('Operation cancelled.');
195
+ return;
196
+ }
197
+
198
+ if (!answers.name.endsWith('Repository')) {
199
+ answers.name += 'Repository';
200
+ }
201
+
202
+ if (!answers.alias) {
203
+ answers.alias = answers.name;
204
+ }
205
+
206
+ // Get stub file
207
+ const stubFile = Bun.file(join(getRootPath(), 'stubs/repository.stub'));
208
+
209
+ // Format the stub content
210
+ const stub = formatter(await stubFile.text(), {
211
+ ...answers,
212
+ module: answers.module ?? 'root',
213
+ model_name: snake(answers.model.replace('Model', ''))
214
+ });
215
+
216
+ const path = answers.module ? await getModulePath(answers.module) : './src';
217
+
218
+ // Write to file
219
+ const name = snake(answers.name.replace('Repository', ''));
220
+ const file = Bun.file(`${path}/repositories/${name}.repository.ts`);
221
+ await file.write(stub);
222
+
223
+ this.success(`Repository ${this.bold(file.name!)} created successfully.`);
224
+ }
225
+ }
@@ -0,0 +1,201 @@
1
+ // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import type { PromptObject } from 'prompts';
16
+
17
+ import { join } from 'node:path';
18
+
19
+ import { Command, CommandArgumentType } from '@elysiumjs/core';
20
+ import prompts from 'prompts';
21
+ import { snake } from 'radash';
22
+ import formatter from 'string-template';
23
+
24
+ import { getModulePath, parseProjectConfig } from '../config';
25
+ import { getRootPath } from '../utils';
26
+
27
+ /**
28
+ * Maker command for creating Elysium services.
29
+ * @author Axel Nana <axel.nana@workbud.com>
30
+ */
31
+ export class MakeServiceCommand extends Command {
32
+ public static readonly command: string = 'make:service';
33
+ public static readonly description: string = 'Creates a new service.';
34
+
35
+ @Command.arg({
36
+ description: 'The name of the service to create',
37
+ type: CommandArgumentType.STRING
38
+ })
39
+ private name?: string;
40
+
41
+ @Command.arg({
42
+ description: 'The module where the service will be created',
43
+ type: CommandArgumentType.STRING
44
+ })
45
+ private module?: string;
46
+
47
+ @Command.arg({
48
+ description: 'The alias of the service to create',
49
+ type: CommandArgumentType.STRING
50
+ })
51
+ private alias?: string;
52
+
53
+ @Command.arg({
54
+ description: 'Create a factory service',
55
+ type: CommandArgumentType.BOOLEAN,
56
+ default: false
57
+ })
58
+ private factory: boolean = false;
59
+
60
+ @Command.arg({
61
+ description: 'Create a singleton service',
62
+ type: CommandArgumentType.BOOLEAN,
63
+ default: false
64
+ })
65
+ private singleton: boolean = false;
66
+
67
+ public async run(): Promise<void> {
68
+ if (!this.name) {
69
+ return this.setup();
70
+ }
71
+
72
+ const config = await parseProjectConfig();
73
+
74
+ const answers: Record<string, any> = {
75
+ module: this.module,
76
+ name: this.name,
77
+ alias: this.alias,
78
+ scope: this.factory ? 'FACTORY' : this.singleton ? 'SINGLETON' : 'SINGLETON'
79
+ };
80
+
81
+ if (!answers.module && !config.mono) {
82
+ const module = await prompts({
83
+ type: 'select',
84
+ name: 'module',
85
+ message: 'Module:',
86
+ choices: Object.keys(config.modules ?? {}).map((moduleName) => ({
87
+ title: moduleName,
88
+ value: moduleName
89
+ }))
90
+ });
91
+
92
+ answers.module = module.module;
93
+ }
94
+
95
+ return this.build(answers);
96
+ }
97
+
98
+ private async setup(): Promise<void> {
99
+ const config = await parseProjectConfig();
100
+
101
+ const items: PromptObject[] = [
102
+ {
103
+ type() {
104
+ return config.mono ? null : 'select';
105
+ },
106
+ name: 'module',
107
+ message: 'Module:',
108
+ choices() {
109
+ return Object.keys(config.modules ?? {}).map((moduleName) => ({
110
+ title: moduleName,
111
+ value: moduleName
112
+ }));
113
+ }
114
+ },
115
+ {
116
+ type: 'text',
117
+ name: 'name',
118
+ message: 'Service Name:',
119
+ initial: 'UserService',
120
+ validate(value: string) {
121
+ if (value.length < 1) {
122
+ return 'Service name cannot be empty';
123
+ }
124
+
125
+ return true;
126
+ }
127
+ },
128
+ {
129
+ type: 'text',
130
+ name: 'alias',
131
+ message: 'Service Alias:',
132
+ initial(_, values) {
133
+ return values.name;
134
+ },
135
+ validate(value: string) {
136
+ if (value.length < 1) {
137
+ return 'Service alias cannot be empty';
138
+ }
139
+
140
+ return true;
141
+ }
142
+ },
143
+ {
144
+ type: 'select',
145
+ name: 'scope',
146
+ message: 'Service Scope:',
147
+ choices: [
148
+ {
149
+ title: 'SINGLETON',
150
+ value: 'SINGLETON',
151
+ description: 'A single instance of the service is created.'
152
+ },
153
+ {
154
+ title: 'FACTORY',
155
+ value: 'FACTORY',
156
+ description: 'A new instance of the service is created each time it is injected.'
157
+ }
158
+ ]
159
+ }
160
+ ];
161
+
162
+ const answers = await prompts(items, {
163
+ onCancel: () => {
164
+ this.error('Operation cancelled.');
165
+ process.exit(0);
166
+ }
167
+ });
168
+
169
+ return this.build(answers);
170
+ }
171
+
172
+ private async build(answers: Record<string, any>): Promise<void> {
173
+ if (!answers.name) {
174
+ this.error('Operation cancelled.');
175
+ return;
176
+ }
177
+
178
+ if (!answers.name.endsWith('Service')) {
179
+ answers.name += 'Service';
180
+ }
181
+
182
+ if (!answers.alias) {
183
+ answers.alias = answers.name;
184
+ }
185
+
186
+ // Get stub file
187
+ const stubFile = Bun.file(join(getRootPath(), 'stubs/service.stub'));
188
+
189
+ // Format the stub content
190
+ const stub = formatter(await stubFile.text(), answers);
191
+
192
+ const path = answers.module ? await getModulePath(answers.module) : './src';
193
+
194
+ // Write to file
195
+ const name = snake(answers.name.replace('Service', ''));
196
+ const file = Bun.file(`${path}/services/${name}.service.ts`);
197
+ await file.write(stub);
198
+
199
+ this.success(`Service ${this.bold(file.name!)} created successfully.`);
200
+ }
201
+ }
@@ -0,0 +1,147 @@
1
+ // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import type { PromptObject } from 'prompts';
16
+
17
+ import { join } from 'node:path';
18
+
19
+ import { Command, CommandArgumentType } from '@elysiumjs/core';
20
+ import prompts from 'prompts';
21
+ import { snake } from 'radash';
22
+ import formatter from 'string-template';
23
+
24
+ import { getModulePath, parseProjectConfig } from '../config';
25
+ import { getRootPath } from '../utils';
26
+
27
+ /**
28
+ * Maker command for creating Elysium validators.
29
+ * @author Axel Nana <axel.nana@workbud.com>
30
+ */
31
+ export class MakeValidatorCommand extends Command {
32
+ public static readonly command: string = 'make:validator';
33
+ public static readonly description: string = 'Creates a new validator.';
34
+
35
+ @Command.arg({
36
+ description: 'The name of the validator to create',
37
+ type: CommandArgumentType.STRING
38
+ })
39
+ private name?: string;
40
+
41
+ @Command.arg({
42
+ description: 'The module where the validator will be created',
43
+ type: CommandArgumentType.STRING
44
+ })
45
+ private module?: string;
46
+
47
+ public async run(): Promise<void> {
48
+ if (!this.name) {
49
+ return this.setup();
50
+ }
51
+
52
+ const config = await parseProjectConfig();
53
+
54
+ const answers: Record<string, any> = {
55
+ module: this.module,
56
+ name: this.name
57
+ };
58
+
59
+ if (!answers.module && !config.mono) {
60
+ const module = await prompts({
61
+ type: 'select',
62
+ name: 'module',
63
+ message: 'Module:',
64
+ choices: Object.keys(config.modules ?? {}).map((moduleName) => ({
65
+ title: moduleName,
66
+ value: moduleName
67
+ }))
68
+ });
69
+
70
+ answers.module = module.module;
71
+ }
72
+
73
+ return this.build(answers);
74
+ }
75
+
76
+ private async setup(): Promise<void> {
77
+ const config = await parseProjectConfig();
78
+
79
+ const items: PromptObject[] = [
80
+ {
81
+ type() {
82
+ return config.mono ? null : 'select';
83
+ },
84
+ name: 'module',
85
+ message: 'Module:',
86
+ choices() {
87
+ return Object.keys(config.modules ?? {}).map((moduleName) => ({
88
+ title: moduleName,
89
+ value: moduleName
90
+ }));
91
+ }
92
+ },
93
+ {
94
+ type: 'text',
95
+ name: 'name',
96
+ message: 'Validator Name:',
97
+ initial: 'LoginValidator',
98
+ validate(value: string) {
99
+ if (value.length < 1) {
100
+ return 'Validator name cannot be empty';
101
+ }
102
+
103
+ return true;
104
+ }
105
+ }
106
+ ];
107
+
108
+ const answers = await prompts(items, {
109
+ onCancel: () => {
110
+ this.error('Operation cancelled.');
111
+ process.exit(0);
112
+ }
113
+ });
114
+
115
+ return this.build(answers);
116
+ }
117
+
118
+ private async build(answers: Record<string, any>): Promise<void> {
119
+ if (!answers.name) {
120
+ this.error('Operation cancelled.');
121
+ return;
122
+ }
123
+
124
+ if (!answers.name.endsWith('Validator')) {
125
+ answers.name += 'Validator';
126
+ }
127
+
128
+ if (!answers.request_name) {
129
+ answers.request_name = answers.name.replace('Validator', 'Request');
130
+ }
131
+
132
+ // Get stub file
133
+ const stubFile = Bun.file(join(getRootPath(), 'stubs/validator.stub'));
134
+
135
+ // Format the stub content
136
+ const stub = formatter(await stubFile.text(), answers);
137
+
138
+ const path = answers.module ? await getModulePath(answers.module) : './src';
139
+
140
+ // Write to file
141
+ const name = snake(answers.name.replace('Validator', ''));
142
+ const file = Bun.file(`${path}/validators/${name}.validator.ts`);
143
+ await file.write(stub);
144
+
145
+ this.success(`Validator ${this.bold(file.name!)} created successfully.`);
146
+ }
147
+ }
@@ -0,0 +1,80 @@
1
+ // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import { exists } from 'node:fs/promises';
16
+
17
+ import { Command, CommandArgumentType } from '@elysiumjs/core';
18
+ import prompts from 'prompts';
19
+
20
+ import { getProjectPath } from '../utils';
21
+
22
+ /**
23
+ * Generates migration files.
24
+ * @author Axel Nana <axel.nana@workbud.com>
25
+ */
26
+ export class MigrationGenerateCommand extends Command {
27
+ public static readonly command: string = 'migration:generate';
28
+ public static readonly description: string = 'Generates migration files.';
29
+
30
+ @Command.arg({
31
+ description: 'The name of the migration to create',
32
+ type: CommandArgumentType.STRING
33
+ })
34
+ private name?: string;
35
+
36
+ public async run(): Promise<void> {
37
+ if (!this.name) {
38
+ const answers = await prompts(
39
+ [
40
+ {
41
+ type: 'text',
42
+ name: 'name',
43
+ message: 'Migration Name:',
44
+ initial: 'create_users_table',
45
+ validate(value: string) {
46
+ if (value.length < 1) {
47
+ return 'Migration name cannot be empty';
48
+ }
49
+
50
+ return true;
51
+ }
52
+ }
53
+ ],
54
+ {
55
+ onCancel: () => {
56
+ this.error('Operation cancelled.');
57
+ process.exit(0);
58
+ }
59
+ }
60
+ );
61
+
62
+ this.name = answers.name;
63
+ }
64
+
65
+ const projectRoot = getProjectPath();
66
+ let schemaPath = `${projectRoot}/src/database/schemas`;
67
+ if (await exists(`${projectRoot}/src/database/schema.ts`)) {
68
+ schemaPath = `${projectRoot}/src/database/schema.ts`;
69
+ }
70
+
71
+ await Bun.$`bunx --bun drizzle-kit generate --dialect postgresql --schema ${schemaPath} --out ${projectRoot}/src/database/migrations --prefix timestamp --name ${this.name}`;
72
+
73
+ const sqlFiles = new Bun.Glob(`${projectRoot}/src/database/migrations/**/*.sql`).scan();
74
+ for await (const path of sqlFiles) {
75
+ const file = Bun.file(path);
76
+ const content = await file.text();
77
+ await file.write(content.replace(/"public"\./gm, ''));
78
+ }
79
+ }
80
+ }