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