forked from oracle/netsuite-suitecloud-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI.js
100 lines (87 loc) · 3.17 KB
/
CLI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
** Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
'use strict';
const assert = require('assert');
var path = require('path');
const program = require('commander');
const NodeUtils = require('./utils/NodeUtils');
const TranslationService = require('./services/TranslationService');
const {
CLI: { INTERACTIVE_OPTION_DESCRIPTION, TITLE, USAGE },
ERRORS,
} = require('./services/TranslationKeys');
const unwrapExceptionMessage = require('./utils/ExceptionUtils').unwrapExceptionMessage;
const INTERACTIVE_ALIAS = '-i';
const INTERACTIVE_OPTION = '--interactive';
const CLI_VERSION = '19.2.1';
module.exports = class CLI {
constructor(dependencies) {
assert(dependencies);
assert(dependencies.commandsMetadataService);
assert(dependencies.commandActionExecutor);
assert(dependencies.commandRegistrationService);
this._commandsMetadataService = dependencies.commandsMetadataService;
this._commandActionExecutor = dependencies.commandActionExecutor;
this._commandRegistrationService = dependencies.commandRegistrationService;
}
start(process) {
try {
const rootCLIPath = this._getCLIRootPath();
this._commandsMetadataService.initializeCommandsMetadata(rootCLIPath);
const runInInteractiveMode = this._isRunningInInteractiveMode();
const commandMetadataList = this._commandsMetadataService.getCommandsMetadata();
this._initializeCommands(commandMetadataList, runInInteractiveMode);
program
.version(CLI_VERSION, '--version')
.option(
`${INTERACTIVE_ALIAS}, ${INTERACTIVE_OPTION}`,
TranslationService.getMessage(INTERACTIVE_OPTION_DESCRIPTION)
)
.on('command:*', args => {
NodeUtils.println(
TranslationService.getMessage(ERRORS.COMMAND_DOES_NOT_EXIST, args[0]),
NodeUtils.COLORS.ERROR
);
})
.usage(TranslationService.getMessage(USAGE))
.parse(process.argv);
if (!program.args.length) {
this._printHelp();
}
} catch (exception) {
NodeUtils.println(unwrapExceptionMessage(exception), NodeUtils.COLORS.ERROR);
}
}
_getCLIRootPath() {
return path.dirname(require.main.filename);
}
_initializeCommands(commandMetadataList, runInInteractiveMode) {
const commandsMetadataArraySortedByCommandName = Object.values(commandMetadataList).sort(
(command1, command2) => command1.name.localeCompare(command2.name)
);
commandsMetadataArraySortedByCommandName.forEach(commandMetadata => {
this._commandRegistrationService.register({
commandMetadata: commandMetadata,
program: program,
runInInteractiveMode: runInInteractiveMode,
executeCommandFunction: async options => {
await this._commandActionExecutor.executeAction({
executionPath: process.cwd(),
commandName: commandMetadata.name,
runInInteractiveMode: runInInteractiveMode,
arguments: options,
});
},
});
});
}
_isRunningInInteractiveMode() {
return process.argv[3] == INTERACTIVE_ALIAS || process.argv[3] === INTERACTIVE_OPTION;
}
_printHelp() {
NodeUtils.println(TranslationService.getMessage(TITLE, CLI_VERSION), NodeUtils.COLORS.INFO);
program.help();
}
};