mythix 3.0.0 → 4.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 (128) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -30
  3. package/{src → lib}/application.d.ts +25 -39
  4. package/{src/application.js → lib/application.mjs} +110 -135
  5. package/lib/cli/cli-utils.d.ts +21 -0
  6. package/lib/cli/cli-utils.mjs +319 -0
  7. package/lib/cli/command-base.d.ts +33 -0
  8. package/lib/cli/command-base.mjs +118 -0
  9. package/lib/cli/command-executor.mjs +14 -0
  10. package/lib/cli/commands/deploy-command.mjs +1010 -0
  11. package/lib/cli/commands/generators/generate-command.mjs +117 -0
  12. package/lib/cli/commands/generators/index.mjs +2 -0
  13. package/{src/cli/generators/migration-generator.js → lib/cli/commands/generators/migration-generator.mjs} +11 -13
  14. package/lib/cli/commands/index.mjs +22 -0
  15. package/lib/cli/commands/migrate-command.mjs +184 -0
  16. package/lib/cli/commands/routes-command.mjs +40 -0
  17. package/lib/cli/commands/serve-command.mjs +63 -0
  18. package/lib/cli/commands/shell-command.mjs +95 -0
  19. package/lib/cli/index.mjs +3 -0
  20. package/{src → lib}/controllers/controller-base.d.ts +1 -1
  21. package/{src/controllers/controller-base.js → lib/controllers/controller-base.mjs} +3 -9
  22. package/{src → lib}/controllers/controller-module.d.ts +3 -3
  23. package/lib/controllers/controller-module.mjs +70 -0
  24. package/lib/controllers/generate-client-api-interface-spec.mjs +44 -0
  25. package/{src → lib}/controllers/generate-client-api-interface.d.ts +1 -0
  26. package/{src/controllers/generate-client-api-interface.js → lib/controllers/generate-client-api-interface.mjs} +39 -16
  27. package/{src → lib}/controllers/index.d.ts +2 -3
  28. package/lib/controllers/index.mjs +4 -0
  29. package/lib/controllers/routes/index.mjs +4 -0
  30. package/{src/controllers/routes/route-capture.js → lib/controllers/routes/route-capture.mjs} +2 -8
  31. package/{src/controllers/routes/route-endpoint.js → lib/controllers/routes/route-endpoint.mjs} +8 -8
  32. package/{src/controllers/routes/route-scope-base.js → lib/controllers/routes/route-scope-base.mjs} +19 -15
  33. package/{src/controllers/routes/route-scope.js → lib/controllers/routes/route-scope.mjs} +10 -12
  34. package/{src/http-server/http-errors.js → lib/http/http-errors.mjs} +9 -20
  35. package/{src/utils/http-interface.js → lib/http/http-interface.mjs} +6 -14
  36. package/{src/http-server → lib/http}/http-server-module.d.ts +2 -2
  37. package/{src/http-server/http-server-module.js → lib/http/http-server-module.mjs} +7 -11
  38. package/{src/http-server/http-server.js → lib/http/http-server.mjs} +17 -21
  39. package/lib/http/index.d.ts +4 -0
  40. package/lib/http/index.mjs +5 -0
  41. package/lib/index.d.ts +30 -0
  42. package/lib/index.mjs +31 -0
  43. package/lib/logger-spec.mjs +121 -0
  44. package/{src → lib}/logger.d.ts +1 -3
  45. package/{src/logger.js → lib/logger.mjs} +10 -24
  46. package/lib/models/index.d.ts +2 -0
  47. package/lib/models/index.mjs +2 -0
  48. package/{src/models/migration-model.js → lib/models/migration-model.mjs} +3 -11
  49. package/lib/models/model.d.ts +10 -0
  50. package/lib/models/model.mjs +28 -0
  51. package/{src → lib}/modules/database-module.d.ts +2 -2
  52. package/{src/modules/database-module.js → lib/modules/database-module.mjs} +19 -14
  53. package/lib/modules/index.d.ts +2 -0
  54. package/lib/modules/index.mjs +2 -0
  55. package/{src/modules/base-module.d.ts → lib/modules/module-base.d.ts} +3 -10
  56. package/{src/modules/base-module.js → lib/modules/module-base.mjs} +16 -6
  57. package/lib/tasks/index.d.ts +2 -0
  58. package/lib/tasks/index.mjs +2 -0
  59. package/{src → lib}/tasks/task-base.d.ts +4 -14
  60. package/lib/tasks/task-base.mjs +73 -0
  61. package/{src → lib}/tasks/task-module.d.ts +3 -3
  62. package/{src/tasks/task-module.js → lib/tasks/task-module.mjs} +43 -98
  63. package/{src/utils/config-utils.js → lib/utils/config-utils.mjs} +2 -10
  64. package/lib/utils/crypto-utils-spec.mjs +24 -0
  65. package/{src/utils/crypto-utils.js → lib/utils/crypto-utils.mjs} +16 -38
  66. package/lib/utils/file-utils-spec.mjs +10 -0
  67. package/{src/utils/file-utils.js → lib/utils/file-utils.mjs} +4 -11
  68. package/{src/utils/http-utils.js → lib/utils/http-utils.mjs} +3 -10
  69. package/{src → lib}/utils/index.d.ts +1 -1
  70. package/lib/utils/index.mjs +6 -0
  71. package/lib/utils/mime-utils-spec.mjs +171 -0
  72. package/{src/utils/mime-utils.js → lib/utils/mime-utils.mjs} +5 -14
  73. package/{src/utils/test-utils.js → lib/utils/test-utils.mjs} +14 -42
  74. package/package.json +5 -5
  75. package/src/cli/cli-utils.d.ts +0 -80
  76. package/src/cli/cli-utils.js +0 -547
  77. package/src/cli/command-executor.js +0 -31
  78. package/src/cli/deploy-command.js +0 -1010
  79. package/src/cli/generators/generate-command.js +0 -149
  80. package/src/cli/index.js +0 -5
  81. package/src/cli/migrate-command.js +0 -181
  82. package/src/cli/routes-command.js +0 -40
  83. package/src/cli/serve-command.js +0 -63
  84. package/src/cli/shell-command.js +0 -96
  85. package/src/controllers/controller-module.js +0 -126
  86. package/src/controllers/controller-utils.d.ts +0 -19
  87. package/src/controllers/controller-utils.js +0 -24
  88. package/src/controllers/index.js +0 -19
  89. package/src/controllers/routes/index.js +0 -31
  90. package/src/http-server/index.d.ts +0 -3
  91. package/src/http-server/index.js +0 -16
  92. package/src/index.d.ts +0 -49
  93. package/src/index.js +0 -47
  94. package/src/models/index.d.ts +0 -4
  95. package/src/models/index.js +0 -17
  96. package/src/models/model-module.d.ts +0 -9
  97. package/src/models/model-module.js +0 -130
  98. package/src/models/model-utils.d.ts +0 -20
  99. package/src/models/model-utils.js +0 -46
  100. package/src/models/model.d.ts +0 -20
  101. package/src/models/model.js +0 -65
  102. package/src/modules/file-watcher-module.d.ts +0 -13
  103. package/src/modules/file-watcher-module.js +0 -220
  104. package/src/modules/index.d.ts +0 -3
  105. package/src/modules/index.js +0 -11
  106. package/src/tasks/index.d.ts +0 -3
  107. package/src/tasks/index.js +0 -11
  108. package/src/tasks/task-base.js +0 -117
  109. package/src/tasks/task-utils.d.ts +0 -46
  110. package/src/tasks/task-utils.js +0 -130
  111. package/src/utils/index.js +0 -25
  112. /package/{src → lib}/cli/index.d.ts +0 -0
  113. /package/{src → lib}/controllers/routes/index.d.ts +0 -0
  114. /package/{src → lib}/controllers/routes/route-capture.d.ts +0 -0
  115. /package/{src → lib}/controllers/routes/route-endpoint.d.ts +0 -0
  116. /package/{src → lib}/controllers/routes/route-scope-base.d.ts +0 -0
  117. /package/{src → lib}/controllers/routes/route-scope.d.ts +0 -0
  118. /package/{src/http-server → lib/http}/http-errors.d.ts +0 -0
  119. /package/{src/utils → lib/http}/http-interface.d.ts +0 -0
  120. /package/{src/http-server → lib/http}/http-server.d.ts +0 -0
  121. /package/{src → lib}/interfaces/common.ts +0 -0
  122. /package/{src → lib}/models/migration-model.d.ts +0 -0
  123. /package/{src → lib}/utils/config-utils.d.ts +0 -0
  124. /package/{src → lib}/utils/crypto-utils.d.ts +0 -0
  125. /package/{src → lib}/utils/file-utils.d.ts +0 -0
  126. /package/{src → lib}/utils/http-utils.d.ts +0 -0
  127. /package/{src → lib}/utils/mime-utils.d.ts +0 -0
  128. /package/{src → lib}/utils/test-utils.d.ts +0 -0
@@ -0,0 +1,117 @@
1
+ import Nife from 'nife';
2
+ import { CommandBase } from '../../command-base.mjs';
3
+ import { Logger } from '../../../logger.mjs';
4
+
5
+ import { GenerateMigrationCommand } from './migration-generator.mjs';
6
+
7
+ function prepareGeneratorCommands(commands, application, operationName) {
8
+ return Nife.iterate(commands, ({ index, key, value, context }) => {
9
+ const Klass = value;
10
+
11
+ let commandName = key;
12
+ if (index === key) {
13
+ if (typeof Klass.getGeneratorName === 'function')
14
+ commandName = Klass.getGeneratorName();
15
+ else
16
+ commandName = Klass.name;
17
+ }
18
+
19
+ let commandArgs = {};
20
+ if (typeof Klass.commandArguments === 'function') {
21
+ commandArgs = Klass.commandArguments(application, operationName);
22
+
23
+ if (commandArgs && commandArgs.help)
24
+ commandArgs.help['@see'] = `See: 'mythix-cli generate ${commandName} --help' for more help`;
25
+ }
26
+
27
+ context[commandName.toLocaleLowerCase()] = {
28
+ CommandKlass: Klass,
29
+ ...commandArgs,
30
+ };
31
+ }, {});
32
+ }
33
+
34
+ export class GenerateCommand extends CommandBase {
35
+ static getCommandName() {
36
+ return 'generate';
37
+ }
38
+
39
+ static getGeneratorCommands() {
40
+ return {
41
+ migration: GenerateMigrationCommand,
42
+ };
43
+ }
44
+
45
+ static applicationConfig = () => {
46
+ return {
47
+ httpServer: false,
48
+ autoReload: false,
49
+ runTasks: false,
50
+ logger: {
51
+ level: Logger.LEVEL_ERROR,
52
+ },
53
+ };
54
+ };
55
+
56
+ static commandArguments(application, operationName) {
57
+ let generatorCommands = prepareGeneratorCommands(this.getGeneratorCommands(), application, operationName);
58
+
59
+ let help = {
60
+ '@usage': 'mythix-cli generate {generator}',
61
+ '@title': 'Generate new content using the specified generator',
62
+ };
63
+
64
+ Nife.iterate(generatorCommands, ({ value, key }) => {
65
+ if (value.help)
66
+ help[key] = value.help;
67
+ });
68
+
69
+ return {
70
+ help: help,
71
+ runner: ({ $, fetch, showHelp, scope, store }) => {
72
+ let subCommandResult = $(
73
+ /[\w-]+/,
74
+ ({ store }, parsedResult) => {
75
+ store({ generateSubCommand: parsedResult.value.toLowerCase() });
76
+ return true;
77
+ },
78
+ { formatParsedResult: (result) => ({ value: result[0] }) },
79
+ );
80
+
81
+ if (!subCommandResult)
82
+ return false;
83
+
84
+ let subCommandName = fetch('generateSubCommand');
85
+ let command = generatorCommands[subCommandName];
86
+
87
+ if (!command) {
88
+ console.error(`Error: unknown generator "${subCommandName}"\n`);
89
+ return false;
90
+ }
91
+
92
+ let subCommandRunnerResult = true;
93
+ if (command.runner)
94
+ subCommandRunnerResult = scope(subCommandName, command.runner);
95
+
96
+ if (!subCommandRunnerResult || fetch('help', false)) {
97
+ showHelp(null, help[subCommandName] || help);
98
+ return false;
99
+ }
100
+
101
+ store('generatorCommands', generatorCommands);
102
+
103
+ return true;
104
+ },
105
+ };
106
+ }
107
+
108
+ async execute(args, fullArgs) {
109
+ let application = this.getApplication();
110
+ let subCommandName = args.generateSubCommand;
111
+ let generatorCommands = args.generatorCommands;
112
+ let command = generatorCommands[subCommandName];
113
+
114
+ let commandInstance = new command.CommandKlass(application, this.getOptions());
115
+ return await commandInstance.execute.call(commandInstance, args[subCommandName], fullArgs);
116
+ }
117
+ }
@@ -0,0 +1,2 @@
1
+ export * from './generate-command.mjs';
2
+ export * from './migration-generator.mjs';
@@ -1,23 +1,19 @@
1
1
  /* eslint-disable key-spacing */
2
2
 
3
- 'use strict';
4
-
5
- const Path = require('path');
6
- const FileSystem = require('fs');
7
- const Nife = require('nife');
8
- const { Utils } = require('mythix-orm');
9
- const { CommandBase } = require('../cli-utils');
3
+ import Path from 'node:path';
4
+ import FileSystem from 'node:fs';
5
+ import Nife from 'nife';
6
+ import { Utils } from 'mythix-orm';
7
+ import { CommandBase } from '../../command-base.mjs';
10
8
 
11
9
  class ValidationError extends Error {}
12
10
 
13
11
  function generateMigration(migrationID, upCode, downCode) {
14
12
  let template =
15
13
  `
16
- 'use strict';
17
-
18
14
  const MIGRATION_ID = '${migrationID}';
19
15
 
20
- module.exports = {
16
+ export default {
21
17
  MIGRATION_ID,
22
18
  up: async function(connection, application) {
23
19
  ${upCode || ''}
@@ -31,7 +27,11 @@ ${downCode || ''}
31
27
  return template;
32
28
  }
33
29
 
34
- class GenerateMigrationCommand extends CommandBase {
30
+ export class GenerateMigrationCommand extends CommandBase {
31
+ static getGeneratorName() {
32
+ return 'migration';
33
+ }
34
+
35
35
  static commandArguments() {
36
36
  return {
37
37
  help: {
@@ -306,5 +306,3 @@ class GenerateMigrationCommand extends CommandBase {
306
306
  return this.addOrDropFields(args, true);
307
307
  }
308
308
  }
309
-
310
- module.exports = GenerateMigrationCommand;
@@ -0,0 +1,22 @@
1
+ export * from './generators/index.mjs';
2
+ export * from './deploy-command.mjs';
3
+ export * from './migrate-command.mjs';
4
+ export * from './routes-command.mjs';
5
+ export * from './serve-command.mjs';
6
+ export * from './shell-command.mjs';
7
+
8
+ import { GenerateCommand } from './generators/generate-command.mjs';
9
+ import { DeployCommand } from './deploy-command.mjs';
10
+ import { MigrateCommand } from './migrate-command.mjs';
11
+ import { RoutesCommand } from './routes-command.mjs';
12
+ import { ServeCommand } from './serve-command.mjs';
13
+ import { ShellCommand } from './shell-command.mjs';
14
+
15
+ export const COMMANDS = {
16
+ 'generate': GenerateCommand,
17
+ 'deploy': DeployCommand,
18
+ 'migrate': MigrateCommand,
19
+ 'routes': RoutesCommand,
20
+ 'serve': ServeCommand,
21
+ 'shell': ShellCommand,
22
+ };
@@ -0,0 +1,184 @@
1
+ import Path from 'node:path';
2
+ import Nife from 'nife';
3
+ import { CommandBase } from '../command-base.mjs';
4
+ import { walkDir } from '../../utils/file-utils.mjs';
5
+
6
+ const TIMESTAMP_LENGTH = 14;
7
+ const MILLISECONDS_PER_SECOND = 1000.0;
8
+
9
+ export class MigrateCommand extends CommandBase {
10
+ static getCommandName() {
11
+ return 'migrate';
12
+ }
13
+
14
+ static commandArguments() {
15
+ return {
16
+ help: {
17
+ /* eslint-disable key-spacing */
18
+ '@usage': 'mythix-cli migrate [options]',
19
+ '@title': 'Run or rollback migrations',
20
+ '-r={revision number} | -r {revision number} | --revision={revision number} | --revision {revision number}': 'Start operation at revision specified. For rollbacks, this specifies the revision to stop at (inclusive).',
21
+ '--rollback': 'Rollback migrations to the revision number specified, or rollback the last migration if no revision number is specified.',
22
+ },
23
+ runner: ({ $, Types }) => {
24
+ $('-r', Types.STRING(), { name: 'revision' });
25
+ $('--revision', Types.STRING(), { name: 'revision' });
26
+
27
+ $('--rollback', Types.STRING());
28
+
29
+ return true;
30
+ },
31
+ };
32
+ }
33
+
34
+ getMigrationFiles(migrationsPath) {
35
+ try {
36
+ let files = walkDir(migrationsPath, {
37
+ filter: (fullFileName, fileName, stats) => {
38
+ if (fileName.match(/^_/) && stats.isDirectory())
39
+ return false;
40
+
41
+ if (stats.isFile() && !fileName.match(/^\d{14}.*\.[mc]?js$/))
42
+ return false;
43
+
44
+ return true;
45
+ },
46
+ });
47
+
48
+ return files.sort();
49
+ } catch (error) {
50
+ if (error.code === 'ENOENT') {
51
+ console.log(`No migrations found at ${migrationsPath}. Aborting...`);
52
+ return [];
53
+ }
54
+
55
+ throw error;
56
+ }
57
+ }
58
+
59
+ getMigrationFilesFromRevision(migrationFiles, _revision, isRollback) {
60
+ let revision = BigInt('' + _revision);
61
+
62
+ let index = migrationFiles.findIndex((fullFileName) => {
63
+ let fileName = Path.basename(fullFileName);
64
+ let migrationFileTS = fileName.substring(0, TIMESTAMP_LENGTH);
65
+
66
+ if (BigInt(migrationFileTS) > revision)
67
+ return true;
68
+
69
+ return false;
70
+ });
71
+
72
+ if (index < 0)
73
+ return [];
74
+
75
+ return migrationFiles.slice((isRollback) ? index - 1 : index);
76
+ }
77
+
78
+ async executeMigration(application, dbConnection, migrationFileName, useTransaction, rollback) {
79
+ let migration = await import(migrationFileName);
80
+ if (migration && migration.default)
81
+ migration = migration.default;
82
+
83
+ let startTime = Nife.now();
84
+
85
+ if (rollback) {
86
+ await migration.down(dbConnection, application);
87
+ await this.removeMigrationFromDB(migration.MIGRATION_ID);
88
+
89
+ let seconds = ((Nife.now() - startTime) / MILLISECONDS_PER_SECOND).toFixed(2);
90
+ console.log(`Rolled back migration ${migrationFileName} successfully in ${seconds} seconds`);
91
+ } else {
92
+ await migration.up(dbConnection, application);
93
+ await this.storeSuccessfulMigrationToDB(migration.MIGRATION_ID);
94
+
95
+ let seconds = ((Nife.now() - startTime) / MILLISECONDS_PER_SECOND).toFixed(2);
96
+ console.log(`Migration ${migrationFileName} completed successfully in ${seconds} seconds`);
97
+ }
98
+ }
99
+
100
+ async storeSuccessfulMigrationToDB(migrationID) {
101
+ let MigrationModel = this.getApplication().getModel('Migration');
102
+
103
+ await MigrationModel.create({ id: ('' + migrationID) });
104
+ }
105
+
106
+ async removeMigrationFromDB(migrationID) {
107
+ try {
108
+ let MigrationModel = this.getApplication().getModel('Migration');
109
+
110
+ await MigrationModel.where.id.EQ(('' + migrationID)).LIMIT(1).destroy();
111
+ } catch (error) {
112
+ return;
113
+ }
114
+ }
115
+
116
+ async fetchLastMigrationIDFromDB() {
117
+ try {
118
+ let MigrationModel = this.getApplication().getModel('Migration');
119
+
120
+ let lastMigrationModel = await MigrationModel.where.id.NEQ(null).ORDER.DESC('Migration:id').first();
121
+ if (lastMigrationModel == null)
122
+ return null;
123
+
124
+ return lastMigrationModel.id;
125
+ } catch (error) {
126
+ return null;
127
+ }
128
+ }
129
+
130
+ async execute(args) {
131
+ const nextMigration = (doneCallback, _index) => {
132
+ let index = _index || 0;
133
+ if (index >= migrationFiles.length)
134
+ return doneCallback();
135
+
136
+ let migrationFileName = migrationFiles[index];
137
+
138
+ if (rollback)
139
+ console.log(`Undoing migration ${migrationFileName}...`);
140
+ else
141
+ console.log(`Running migration ${migrationFileName}...`);
142
+
143
+ this.executeMigration(application, dbConnection, migrationFileName, useTransaction, rollback).then(
144
+ () => nextMigration(doneCallback, index + 1),
145
+ (error) => {
146
+ console.error(`Error while running migration ${migrationFileName}: `, error);
147
+ doneCallback(error);
148
+ },
149
+ );
150
+ };
151
+
152
+ let cliConfig = this.getCLIConfig();
153
+ let application = this.getApplication();
154
+ let dbConnection = application.getConnection();
155
+ let migrationsPath = cliConfig.migrationRoot || Path.resolve(cliConfig.appRootPath, 'migrations');
156
+ let migrationFiles = this.getMigrationFiles(migrationsPath);
157
+ let useTransaction = args.transaction;
158
+ let rollback = args.rollback;
159
+ let migrationID = args.revision;
160
+
161
+ if (!migrationID)
162
+ migrationID = await this.fetchLastMigrationIDFromDB(dbConnection);
163
+
164
+ if (migrationID)
165
+ migrationFiles = this.getMigrationFilesFromRevision(migrationFiles, migrationID, rollback);
166
+
167
+ if (args.rollback)
168
+ migrationFiles = migrationFiles.reverse();
169
+
170
+ if (migrationFiles.length === 0) {
171
+ console.log('Nothing to migrate');
172
+ return;
173
+ }
174
+
175
+ return new Promise((resolve, reject) => {
176
+ nextMigration((error) => {
177
+ if (error)
178
+ return reject(error);
179
+
180
+ resolve();
181
+ });
182
+ });
183
+ }
184
+ }
@@ -0,0 +1,40 @@
1
+ import { Logger } from '../../logger.mjs';
2
+ import { CommandBase } from '../command-base.mjs';
3
+
4
+ export class RoutesCommand extends CommandBase {
5
+ static getCommandName() {
6
+ return 'routes';
7
+ }
8
+
9
+ static applicationConfig = { database: false, logger: { level: Logger.LEVEL_ERROR } };
10
+
11
+ static commandArguments() {
12
+ return {
13
+ help: {
14
+ '@usage': 'mythix-cli routes',
15
+ '@title': 'List application routes',
16
+ },
17
+ };
18
+ }
19
+
20
+ execute() {
21
+ let application = this.getApplication();
22
+ let routes = application._getRoutes();
23
+ let whitespace = ' ';
24
+
25
+ console.log(`${application.getApplicationName()} routes:`);
26
+
27
+ routes.walkRoutes(({ endpoint }) => {
28
+ let methods = endpoint.methods || [];
29
+ let flags = [
30
+ (endpoint.isDynamic) ? 'dynamic' : '',
31
+ (endpoint.cors) ? 'COR' : '',
32
+ ].filter(Boolean).join(', ');
33
+
34
+ for (let i = 0, il = methods.length; i < il; i++) {
35
+ let method = methods[i];
36
+ console.log(` ${method}${whitespace.substring(0, whitespace.length - method.length)}/${endpoint.path} -> [${endpoint.controller}]${(flags) ? ` (${flags})` : ''}`);
37
+ }
38
+ });
39
+ }
40
+ }
@@ -0,0 +1,63 @@
1
+ import Nife from 'nife';
2
+ import { CommandBase } from '../command-base.mjs';
3
+
4
+ export class ServeCommand extends CommandBase {
5
+ static getCommandName() {
6
+ return 'serve';
7
+ }
8
+
9
+ static applicationConfig = () => ({ autoStart: false, exitOnShutdown: 0 });
10
+
11
+ static commandArguments() {
12
+ return {
13
+ help: {
14
+ /* eslint-disable key-spacing */
15
+ '@usage': 'mythix-cli serve [options]',
16
+ '@title': 'Start application and HTTP server',
17
+ '--host={hostname or IP} | --host {hostname or IP}': 'Specify hostname or IP to listen on.',
18
+ '--port={port number} | --port {port number}': 'Specify port to listen on.',
19
+ },
20
+ runner: ({ $, Types }) => {
21
+ $('--host', Types.STRING());
22
+ $('--port', Types.INTEGER({
23
+ validate: (value) => {
24
+ // eslint-disable-next-line no-magic-numbers
25
+ if (value < 0 || value > 65535) {
26
+ console.error('Invalid "--port" specified... must be in the range 0-65535');
27
+ return false;
28
+ }
29
+
30
+ return true;
31
+ },
32
+ }));
33
+
34
+ return true;
35
+ },
36
+ };
37
+ }
38
+
39
+ execute(args) {
40
+ // eslint-disable-next-line no-async-promise-executor
41
+ return new Promise(async (resolve, reject) => {
42
+ try {
43
+ let application = this.getApplication();
44
+ let config = {};
45
+
46
+ if (Nife.isNotEmpty(args.host))
47
+ config.host = args.host;
48
+
49
+ if (Nife.isNotEmpty(args.port))
50
+ config.port = args.port;
51
+
52
+ if (Nife.isNotEmpty(config))
53
+ application.setConfig({ httpServer: config });
54
+
55
+ application.on('exit', resolve);
56
+
57
+ await application.start();
58
+ } catch (error) {
59
+ reject(error);
60
+ }
61
+ });
62
+ }
63
+ }
@@ -0,0 +1,95 @@
1
+ import OS from 'node:os';
2
+ import Path from 'node:path';
3
+ import REPL from 'node:repl';
4
+ import Nife from 'nife';
5
+ import * as UUID from 'uuid';
6
+ import { CommandBase } from '../command-base.mjs';
7
+ import { HTTPInterface } from '../../http/http-interface.mjs';
8
+ import * as Utils from '../../utils/index.mjs';
9
+
10
+ export class ShellCommand extends CommandBase {
11
+ static getCommandName() {
12
+ return 'shell';
13
+ }
14
+
15
+ static runtimeArguments = {
16
+ 'node': [ '--experimental-repl-await', '--experimental-top-level-await' ],
17
+ };
18
+
19
+ static commandArguments() {
20
+ return {
21
+ help: {
22
+ '@usage': 'mythix-cli shell',
23
+ '@title': 'Drop into an server shell to execute commands directly',
24
+ },
25
+ };
26
+ }
27
+
28
+ constructor(...args) {
29
+ super(...args);
30
+
31
+ Object.defineProperties(this, {
32
+ 'defaultHeaders': {
33
+ writable: true,
34
+ enumerable: false,
35
+ configurable: true,
36
+ value: {},
37
+ },
38
+ 'defaultURL': {
39
+ writable: true,
40
+ enumerable: false,
41
+ configurable: true,
42
+ value: null,
43
+ },
44
+ });
45
+ }
46
+
47
+ execute() {
48
+ return new Promise((resolve) => {
49
+ let application = this.getApplication();
50
+ let environment = application.getConfigValue('environment', 'development');
51
+ let appName = application.getApplicationName();
52
+ let httpInterface = new HTTPInterface();
53
+
54
+ const interactiveShell = REPL.start({
55
+ prompt: `${appName} (${environment}) > `,
56
+ });
57
+
58
+ interactiveShell.on('exit', () => {
59
+ resolve(0);
60
+ });
61
+
62
+ interactiveShell.setupHistory(Path.join(OS.homedir(), `.${appName}-${environment}-history`), () => {});
63
+
64
+ interactiveShell.context.UUIDV4 = UUID.v4;
65
+ interactiveShell.context.connection = (typeof application.getConnection === 'function') ? application.getConnection() : null;
66
+ interactiveShell.context.application = application;
67
+ interactiveShell.context.Nife = Nife;
68
+ interactiveShell.context.Utils = Utils;
69
+
70
+ interactiveShell.context.HTTP = {
71
+ 'getDefaultURL': httpInterface.getDefaultURL.bind(httpInterface),
72
+ 'setDefaultURL': httpInterface.setDefaultURL.bind(httpInterface),
73
+ 'getDefaultHeader': httpInterface.getDefaultHeader.bind(httpInterface),
74
+ 'getDefaultHeaders': httpInterface.getDefaultHeaders.bind(httpInterface),
75
+ 'setDefaultHeader': httpInterface.setDefaultHeader.bind(httpInterface),
76
+ 'setDefaultHeaders': httpInterface.setDefaultHeaders.bind(httpInterface),
77
+ 'request': httpInterface.request.bind(httpInterface),
78
+ 'get': httpInterface.getRequest.bind(httpInterface),
79
+ 'post': httpInterface.postRequest.bind(httpInterface),
80
+ 'put': httpInterface.putRequest.bind(httpInterface),
81
+ 'delete': httpInterface.deleteRequest.bind(httpInterface),
82
+ 'head': httpInterface.headRequest.bind(httpInterface),
83
+ 'options': httpInterface.optionsRequest.bind(httpInterface),
84
+ 'dataToQueryString': Utils.HTTPUtils.dataToQueryString,
85
+ };
86
+
87
+ if (typeof application.getModels === 'function')
88
+ Object.assign(interactiveShell.context, application.getModels());
89
+
90
+ this.onStart(interactiveShell);
91
+ });
92
+ }
93
+
94
+ onStart() {}
95
+ }
@@ -0,0 +1,3 @@
1
+ export * from './command-base.mjs';
2
+ export * as Commands from './commands/index.mjs';
3
+ export * from './cli-utils.mjs';
@@ -3,7 +3,7 @@ import { Logger } from '../logger';
3
3
  import { Request, Response } from 'express';
4
4
  import { GenericObject } from '../interfaces/common';
5
5
  import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
6
- import { HTTPBaseError } from '../http-server/http-errors';
6
+ import { HTTPBaseError } from '../http/http-errors';
7
7
 
8
8
  export declare type ControllerClass = typeof ControllerBase;
9
9
 
@@ -1,9 +1,7 @@
1
- 'use strict';
1
+ import Nife from 'nife';
2
+ import * as HTTPErrors from '../http/http-errors.mjs';
2
3
 
3
- const Nife = require('nife');
4
- const HTTPErrors = require('../http-server/http-errors');
5
-
6
- class ControllerBase {
4
+ export class ControllerBase {
7
5
  constructor(application, logger, request, response) {
8
6
  Object.defineProperties(this, {
9
7
  'application': {
@@ -220,7 +218,3 @@ class ControllerBase {
220
218
  }
221
219
  }
222
220
  }
223
-
224
- module.exports = {
225
- ControllerBase,
226
- };
@@ -1,9 +1,9 @@
1
- import { HTTPServer } from '../http-server';
1
+ import { HTTPServer } from '../http';
2
2
  import { GenericObject } from '../interfaces/common';
3
- import { BaseModule } from '../modules/base-module';
3
+ import { ModuleBase } from '../modules/module-base';
4
4
  import { ControllerClass, ControllerClasses } from './controller-base';
5
5
 
6
- export class ControllerModule extends BaseModule {
6
+ export class ControllerModule extends ModuleBase {
7
7
  public getControllerFilePaths(controllersPath: string): Array<string>;
8
8
  public loadControllers(controllersPath: string): ControllerClasses;
9
9
  public getController(name: string): { controller: ControllerClass, controllerMethod: string | undefined };