@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.
- package/README.md +25 -14
- package/package.json +11 -12
- package/src/commands/make_command.ts +172 -0
- package/src/commands/make_controller.ts +277 -0
- package/src/commands/make_job.ts +183 -0
- package/src/commands/make_middleware.ts +143 -0
- package/src/commands/make_model.ts +205 -0
- package/src/commands/make_repository.ts +225 -0
- package/src/commands/make_service.ts +201 -0
- package/src/commands/make_validator.ts +147 -0
- package/src/commands/migration_generate.ts +80 -0
- package/src/commands/migration_run.ts +153 -0
- package/src/commands/module_new.ts +112 -0
- package/src/commands/module_rename.ts +78 -0
- package/src/config.ts +101 -0
- package/src/styx.ts +54 -0
- package/src/utils.ts +148 -0
- package/stubs/validator.stub +30 -0
- package/styx.js +60 -60
|
@@ -0,0 +1,153 @@
|
|
|
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 { basename } from 'node:path';
|
|
16
|
+
|
|
17
|
+
import { Command, CommandArgumentType, Database } from '@elysiumjs/core';
|
|
18
|
+
import prompts from 'prompts';
|
|
19
|
+
import { alphabetical } from 'radash';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Execute migration files.
|
|
23
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
24
|
+
*/
|
|
25
|
+
export class MigrationRunCommand extends Command {
|
|
26
|
+
public static readonly command: string = 'migration:run';
|
|
27
|
+
public static readonly description: string = 'Generates migration files.';
|
|
28
|
+
|
|
29
|
+
@Command.arg({
|
|
30
|
+
description: 'The name of the tenant in which the migrations will be executed.',
|
|
31
|
+
type: CommandArgumentType.STRING,
|
|
32
|
+
required: true
|
|
33
|
+
})
|
|
34
|
+
private tenant: string = '';
|
|
35
|
+
|
|
36
|
+
@Command.arg({
|
|
37
|
+
description: 'The name of the connection to use.',
|
|
38
|
+
type: CommandArgumentType.STRING,
|
|
39
|
+
default: 'default'
|
|
40
|
+
})
|
|
41
|
+
private connection: string = 'default';
|
|
42
|
+
|
|
43
|
+
@Command.arg({
|
|
44
|
+
description: 'Drop the entire schema before running the migrations.',
|
|
45
|
+
type: CommandArgumentType.BOOLEAN,
|
|
46
|
+
default: false
|
|
47
|
+
})
|
|
48
|
+
private fresh: boolean = false;
|
|
49
|
+
|
|
50
|
+
public async run(): Promise<void> {
|
|
51
|
+
if (!Database.connectionExists(this.connection)) {
|
|
52
|
+
this.error(`Connection ${this.bold(this.connection)} does not exist.`);
|
|
53
|
+
return process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const sql = Database.getConnection(this.connection).$client;
|
|
57
|
+
await sql.connect();
|
|
58
|
+
|
|
59
|
+
await sql.begin(async (sql) => {
|
|
60
|
+
if (this.fresh) {
|
|
61
|
+
await sql`DROP SCHEMA IF EXISTS ${sql(this.tenant)} CASCADE;`;
|
|
62
|
+
await sql`CREATE SCHEMA ${sql(this.tenant)};`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const [{ schema_exists }] = await sql`SELECT EXISTS (
|
|
66
|
+
SELECT schema_name
|
|
67
|
+
FROM information_schema.schemata
|
|
68
|
+
WHERE schema_name = ${this.tenant}
|
|
69
|
+
) AS schema_exists;`;
|
|
70
|
+
|
|
71
|
+
if (!schema_exists) {
|
|
72
|
+
const answers = await prompts(
|
|
73
|
+
[
|
|
74
|
+
{
|
|
75
|
+
type: 'confirm',
|
|
76
|
+
name: 'create',
|
|
77
|
+
message: `Schema ${this.tenant} does not exist. Do you want to create it?`,
|
|
78
|
+
initial: true
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
{
|
|
82
|
+
onCancel: () => {
|
|
83
|
+
this.error('Operation cancelled.');
|
|
84
|
+
process.exit(0);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (answers.create) {
|
|
90
|
+
await sql`CREATE SCHEMA ${sql(this.tenant)};`;
|
|
91
|
+
} else {
|
|
92
|
+
this.error(`Schema ${this.bold(this.tenant)} creation cancelled.`);
|
|
93
|
+
return process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
await sql`SET search_path TO ${sql(this.tenant)};`;
|
|
98
|
+
|
|
99
|
+
const [{ table_exists }] = await sql`SELECT EXISTS (
|
|
100
|
+
SELECT *
|
|
101
|
+
FROM information_schema.tables
|
|
102
|
+
WHERE table_name = 'elysium_migrations'
|
|
103
|
+
AND table_schema = ${this.tenant}
|
|
104
|
+
) AS table_exists;`;
|
|
105
|
+
|
|
106
|
+
if (!table_exists) {
|
|
107
|
+
await sql`CREATE TABLE elysium_migrations (
|
|
108
|
+
id SERIAL PRIMARY KEY,
|
|
109
|
+
name TEXT NOT NULL,
|
|
110
|
+
batch INTEGER NOT NULL,
|
|
111
|
+
applied_at TIMESTAMP NOT NULL
|
|
112
|
+
);`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const migrations: { name: string }[] = await sql`SELECT name FROM elysium_migrations;`;
|
|
116
|
+
const completedMigrations = migrations.map((row) => row.name);
|
|
117
|
+
|
|
118
|
+
const filesIt = new Bun.Glob(`./src/database/migrations/**/*.sql`).scan({
|
|
119
|
+
absolute: true,
|
|
120
|
+
onlyFiles: true
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const files = await Array.fromAsync(filesIt);
|
|
124
|
+
const sqlFiles = alphabetical(
|
|
125
|
+
files.filter((file) => !completedMigrations.includes(basename(file))),
|
|
126
|
+
(file) => file
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if (sqlFiles.length === 0) {
|
|
130
|
+
this.success('No new migrations to apply.');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const s = this.spinner(`Running migrations for tenant ${this.bold(this.tenant)}...`);
|
|
135
|
+
|
|
136
|
+
const [{ max }]: [{ max?: number }] =
|
|
137
|
+
await sql`SELECT MAX(batch) + 1 AS max FROM elysium_migrations;`;
|
|
138
|
+
|
|
139
|
+
const batch = max ?? 0;
|
|
140
|
+
|
|
141
|
+
for (const path of sqlFiles) {
|
|
142
|
+
await sql.file(path);
|
|
143
|
+
const [{ name }] =
|
|
144
|
+
await sql`INSERT INTO elysium_migrations (name, batch, applied_at) VALUES (${basename(path)}, ${batch}, ${new Date()}) RETURNING name;`;
|
|
145
|
+
s.pause(`Migration ${this.bold(name)} applied successfully.`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
s.complete(`Migrations for tenant ${this.bold(this.tenant)} applied successfully.`);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await sql.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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, rmdir } from 'node:fs/promises';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
|
|
18
|
+
import { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
19
|
+
import prompts from 'prompts';
|
|
20
|
+
import { snake } from 'radash';
|
|
21
|
+
|
|
22
|
+
import { parseProjectConfig } from '../config';
|
|
23
|
+
import { createModule, getProjectPath } from '../utils';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Command for creating Elysium.js modules.
|
|
27
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
28
|
+
*/
|
|
29
|
+
export class ModuleNewCommand extends Command {
|
|
30
|
+
public static readonly command: string = 'module:new';
|
|
31
|
+
public static readonly description: string = 'Creates a new module.';
|
|
32
|
+
|
|
33
|
+
@Command.arg({
|
|
34
|
+
description: 'The name of the module to create',
|
|
35
|
+
type: CommandArgumentType.STRING
|
|
36
|
+
})
|
|
37
|
+
private name?: string;
|
|
38
|
+
|
|
39
|
+
private module?: string;
|
|
40
|
+
private convert: boolean = false;
|
|
41
|
+
|
|
42
|
+
public override async run(): Promise<void> {
|
|
43
|
+
const config = await parseProjectConfig();
|
|
44
|
+
|
|
45
|
+
// 1. Prompt for new module name if not provided
|
|
46
|
+
if (!this.name) {
|
|
47
|
+
const name = await prompts(
|
|
48
|
+
{
|
|
49
|
+
type: 'text',
|
|
50
|
+
name: 'name',
|
|
51
|
+
message: 'New module name:',
|
|
52
|
+
initial: 'module',
|
|
53
|
+
validate(value) {
|
|
54
|
+
return value.length > 0 ? true : 'Module name cannot be empty';
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
onCancel: () => process.exit(0)
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
this.name = snake(name.name);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 2. If mono-module, prompt for conversion and current module name
|
|
66
|
+
if (config.mono) {
|
|
67
|
+
const { module, convert } = await prompts(
|
|
68
|
+
[
|
|
69
|
+
{
|
|
70
|
+
type: 'confirm',
|
|
71
|
+
name: 'convert',
|
|
72
|
+
message:
|
|
73
|
+
'This project is a mono-module project. Do you want to convert it to a multi-module project?',
|
|
74
|
+
initial: false
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
type: (convert) => (convert ? 'text' : null),
|
|
78
|
+
name: 'module',
|
|
79
|
+
message: 'Current module name:',
|
|
80
|
+
initial: 'main',
|
|
81
|
+
validate(value) {
|
|
82
|
+
return value.length > 0 ? true : 'Module name cannot be empty';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
{
|
|
87
|
+
onCancel: () => process.exit(0)
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (!convert) return this.error('Operation cancelled. The project is left unchanged.');
|
|
92
|
+
|
|
93
|
+
this.module = snake(module as string);
|
|
94
|
+
this.convert = convert;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 3. If multi-module conversion is enabled, convert the project to a multi-module project
|
|
98
|
+
if (this.convert) {
|
|
99
|
+
const modulesDir = join(getProjectPath(), 'src', 'modules');
|
|
100
|
+
if (await exists(modulesDir)) await rmdir(modulesDir, { recursive: true });
|
|
101
|
+
|
|
102
|
+
await createModule(this.module!, 'root');
|
|
103
|
+
|
|
104
|
+
this.success(`Project converted to multi-module.`);
|
|
105
|
+
this.success(`Module '${this.module}' created.`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 4. Create the new module
|
|
109
|
+
await createModule(this.name);
|
|
110
|
+
this.success(`Module '${this.name}' created.`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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 { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
16
|
+
import prompts from 'prompts';
|
|
17
|
+
|
|
18
|
+
import { parseProjectConfig } from '../config';
|
|
19
|
+
import { createModule } from '../utils';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Command for renaming Elysium.js modules.
|
|
23
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
24
|
+
*/
|
|
25
|
+
export class ModuleRenameCommand extends Command {
|
|
26
|
+
public static readonly command: string = 'module:rename';
|
|
27
|
+
public static readonly description: string = 'Renames a module.';
|
|
28
|
+
|
|
29
|
+
@Command.arg({
|
|
30
|
+
description: 'The module to rename',
|
|
31
|
+
type: CommandArgumentType.STRING
|
|
32
|
+
})
|
|
33
|
+
private module?: string;
|
|
34
|
+
|
|
35
|
+
@Command.arg({
|
|
36
|
+
description: 'The new name of the module',
|
|
37
|
+
type: CommandArgumentType.STRING
|
|
38
|
+
})
|
|
39
|
+
private name?: string;
|
|
40
|
+
|
|
41
|
+
public override async run(): Promise<void> {
|
|
42
|
+
const config = await parseProjectConfig();
|
|
43
|
+
|
|
44
|
+
if (config.mono) {
|
|
45
|
+
return this.error('This operation is not supported for mono-module projects.');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!this.module) {
|
|
49
|
+
const { module } = await prompts({
|
|
50
|
+
type: 'text',
|
|
51
|
+
name: 'module',
|
|
52
|
+
message: 'Module to rename:',
|
|
53
|
+
validate: (value) => value.length > 0
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.module = module as string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!this.name) {
|
|
60
|
+
const { name } = await prompts({
|
|
61
|
+
type: 'text',
|
|
62
|
+
name: 'name',
|
|
63
|
+
message: 'New module name:',
|
|
64
|
+
initial: this.module,
|
|
65
|
+
validate: (value) => value.length > 0
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
this.name = name as string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.module === this.name) {
|
|
72
|
+
return this.info('Module name is the same as the current name.');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
await createModule(this.name!, this.module!);
|
|
76
|
+
this.success(`Module '${this.module}' renamed to '${this.name}'.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
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 { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
import { getProjectPath } from './utils';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Describes the Elysium project's configuration.
|
|
21
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
22
|
+
*/
|
|
23
|
+
export interface ProjectConfig {
|
|
24
|
+
/**
|
|
25
|
+
* The name of the Elysium project.
|
|
26
|
+
*/
|
|
27
|
+
projectName: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether the Elysium project is a mono module.
|
|
31
|
+
*/
|
|
32
|
+
mono: boolean;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A map of module names to their paths.
|
|
36
|
+
*
|
|
37
|
+
* Only used if the Elysium project is not a mono module.
|
|
38
|
+
*/
|
|
39
|
+
modules?: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parses the Elysium project's configuration file.
|
|
44
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
45
|
+
* @throws If the Elysium config file is not found.
|
|
46
|
+
* @returns The Elysium project's configuration.
|
|
47
|
+
*/
|
|
48
|
+
export const parseProjectConfig = async (): Promise<ProjectConfig> => {
|
|
49
|
+
const file = Bun.file(join(getProjectPath(), '.elysiumrc'));
|
|
50
|
+
if (!file.exists()) {
|
|
51
|
+
throw new Error('Elysium config file not found.');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return file.json();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Writes the Elysium project's configuration file.
|
|
59
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
60
|
+
* @param config The Elysium project's configuration.
|
|
61
|
+
*/
|
|
62
|
+
export const writeProjectConfig = async (config: ProjectConfig) => {
|
|
63
|
+
const file = Bun.file(join(getProjectPath(), '.elysiumrc'));
|
|
64
|
+
await file.write(JSON.stringify(config, null, 2));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks if the Elysium project is a mono-module project.
|
|
69
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
70
|
+
* @returns Whether the Elysium project is a mono module.
|
|
71
|
+
*/
|
|
72
|
+
export const isMonoModule = async () => {
|
|
73
|
+
const config = await parseProjectConfig();
|
|
74
|
+
return config.mono;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Gets the path of a module.
|
|
79
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
80
|
+
* @param moduleName The name of the module.
|
|
81
|
+
* @returns The path of the module.
|
|
82
|
+
*/
|
|
83
|
+
export const getModulePath = async (moduleName: string) => {
|
|
84
|
+
const config = await parseProjectConfig();
|
|
85
|
+
if (config.mono) {
|
|
86
|
+
return './src';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return config.modules![moduleName];
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Checks if a module exists.
|
|
94
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
95
|
+
* @param moduleName The name of the module.
|
|
96
|
+
* @returns Whether the module exists.
|
|
97
|
+
*/
|
|
98
|
+
export const moduleExists = async (moduleName: string) => {
|
|
99
|
+
const path = await getModulePath(moduleName);
|
|
100
|
+
return Bun.file(path).exists();
|
|
101
|
+
};
|
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
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
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 { existsSync } from 'node:fs';
|
|
16
|
+
import { mkdir, readdir, rename } from 'node:fs/promises';
|
|
17
|
+
import { dirname, extname, join } from 'node:path';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets the root path of the styx package.
|
|
21
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
22
|
+
*/
|
|
23
|
+
export const getRootPath = () => {
|
|
24
|
+
return dirname(Bun.fileURLToPath(import.meta.url));
|
|
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
|
+
};
|
package/stubs/validator.stub
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Context } from '@elysiumjs/core';
|
|
2
|
+
|
|
1
3
|
import { t } from 'elysia';
|
|
2
4
|
|
|
3
5
|
export class {name} {
|
|
@@ -34,6 +36,34 @@ export class {name} {
|
|
|
34
36
|
* })
|
|
35
37
|
*/
|
|
36
38
|
public static readonly schema = t.Object({});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether the request should be authorized before validation.
|
|
42
|
+
*
|
|
43
|
+
* If the request is not authorized, the validation will be skipped.
|
|
44
|
+
*
|
|
45
|
+
* @param ctx The request context.
|
|
46
|
+
* @returns Whether the request is authorized.
|
|
47
|
+
*/
|
|
48
|
+
public static async authorize(ctx: Context<{request_name}>): Promise<boolean> {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Processes custom validation logic.
|
|
54
|
+
*
|
|
55
|
+
* This method is called after the validation schema is checked against the input data.
|
|
56
|
+
* So it's safe to assume that the schema is valid.
|
|
57
|
+
*
|
|
58
|
+
* You can use this method to perform any custom validation logic on the request body or
|
|
59
|
+
* query parameters.
|
|
60
|
+
*
|
|
61
|
+
* @param ctx The request context.
|
|
62
|
+
* @returns Whether the input data is valid.
|
|
63
|
+
*/
|
|
64
|
+
public static async validate(ctx: Context<{request_name}>): Promise<boolean> {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
37
67
|
}
|
|
38
68
|
|
|
39
69
|
export type {request_name} = typeof {name}.schema.static;
|