@sap/cli-core 2023.12.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/CHANGELOG.md +40 -0
- package/LICENSE +44 -0
- package/README.md +13 -0
- package/assets/error.html +20 -0
- package/assets/success.html +20 -0
- package/cache/cache.d.ts +6 -0
- package/cache/cache.js +82 -0
- package/cache/index.d.ts +2 -0
- package/cache/index.js +12 -0
- package/cache/utils.d.ts +1 -0
- package/cache/utils.js +16 -0
- package/commands/cache.command/clean.command.d.ts +3 -0
- package/commands/cache.command/clean.command.js +15 -0
- package/commands/cache.command/index.d.ts +3 -0
- package/commands/cache.command/index.js +15 -0
- package/commands/cache.command/init.command.d.ts +3 -0
- package/commands/cache.command/init.command.js +36 -0
- package/commands/cache.command/show.command.d.ts +3 -0
- package/commands/cache.command/show.command.js +30 -0
- package/commands/handler/authentication/index.d.ts +2 -0
- package/commands/handler/authentication/index.js +21 -0
- package/commands/handler/authentication/oauth/index.d.ts +2 -0
- package/commands/handler/authentication/oauth/index.js +18 -0
- package/commands/handler/authentication/oauth/secretsProvider/cache.d.ts +2 -0
- package/commands/handler/authentication/oauth/secretsProvider/cache.js +19 -0
- package/commands/handler/authentication/oauth/secretsProvider/file.d.ts +2 -0
- package/commands/handler/authentication/oauth/secretsProvider/file.js +25 -0
- package/commands/handler/authentication/oauth/secretsProvider/index.d.ts +2 -0
- package/commands/handler/authentication/oauth/secretsProvider/index.js +10 -0
- package/commands/handler/authentication/oauth/secretsProvider/options.d.ts +2 -0
- package/commands/handler/authentication/oauth/secretsProvider/options.js +30 -0
- package/commands/handler/authentication/oauth/tokenProvider/getToken.d.ts +2 -0
- package/commands/handler/authentication/oauth/tokenProvider/getToken.js +88 -0
- package/commands/handler/authentication/oauth/tokenProvider/index.d.ts +2 -0
- package/commands/handler/authentication/oauth/tokenProvider/index.js +11 -0
- package/commands/handler/authentication/oauth/tokenProvider/refreshToken.d.ts +2 -0
- package/commands/handler/authentication/oauth/tokenProvider/refreshToken.js +15 -0
- package/commands/handler/authentication/oauth/tokenProvider/setAuthorization.d.ts +3 -0
- package/commands/handler/authentication/oauth/tokenProvider/setAuthorization.js +35 -0
- package/commands/handler/authentication/oauth/tokenProvider/utils.d.ts +1 -0
- package/commands/handler/authentication/oauth/tokenProvider/utils.js +27 -0
- package/commands/handler/authentication/oauth/utils.d.ts +15 -0
- package/commands/handler/authentication/oauth/utils.js +58 -0
- package/commands/handler/authentication/passcode/function.d.ts +2 -0
- package/commands/handler/authentication/passcode/function.js +22 -0
- package/commands/handler/authentication/passcode/index.d.ts +2 -0
- package/commands/handler/authentication/passcode/index.js +19 -0
- package/commands/handler/authentication/passcode/input.d.ts +2 -0
- package/commands/handler/authentication/passcode/input.js +42 -0
- package/commands/handler/authentication/passcode/setPasscode.d.ts +2 -0
- package/commands/handler/authentication/passcode/setPasscode.js +16 -0
- package/commands/handler/authentication/passcode/types.d.ts +1 -0
- package/commands/handler/authentication/passcode/types.js +2 -0
- package/commands/handler/authentication/technicalJWT/cf.d.ts +4 -0
- package/commands/handler/authentication/technicalJWT/cf.js +133 -0
- package/commands/handler/authentication/technicalJWT/exec.d.ts +9 -0
- package/commands/handler/authentication/technicalJWT/exec.js +196 -0
- package/commands/handler/authentication/technicalJWT/index.d.ts +2 -0
- package/commands/handler/authentication/technicalJWT/index.js +15 -0
- package/commands/handler/authentication/technicalJWT/types.d.ts +2 -0
- package/commands/handler/authentication/technicalJWT/types.js +8 -0
- package/commands/handler/authentication/technicalJWT/utils.d.ts +1 -0
- package/commands/handler/authentication/technicalJWT/utils.js +70 -0
- package/commands/handler/authentication/utils.d.ts +3 -0
- package/commands/handler/authentication/utils.js +8 -0
- package/commands/handler/checkOptionsExistence.d.ts +2 -0
- package/commands/handler/checkOptionsExistence.js +17 -0
- package/commands/handler/error.d.ts +2 -0
- package/commands/handler/error.js +21 -0
- package/commands/handler/fail.d.ts +2 -0
- package/commands/handler/fail.js +10 -0
- package/commands/handler/fetch/fetch.d.ts +2 -0
- package/commands/handler/fetch/fetch.js +83 -0
- package/commands/handler/fetch/index.d.ts +2 -0
- package/commands/handler/fetch/index.js +25 -0
- package/commands/handler/fetch/utils.d.ts +14 -0
- package/commands/handler/fetch/utils.js +152 -0
- package/commands/handler/force.d.ts +2 -0
- package/commands/handler/force.js +35 -0
- package/commands/handler/index.d.ts +18 -0
- package/commands/handler/index.js +39 -0
- package/commands/handler/input/file.d.ts +2 -0
- package/commands/handler/input/file.js +34 -0
- package/commands/handler/input/index.d.ts +2 -0
- package/commands/handler/input/index.js +8 -0
- package/commands/handler/input/input.d.ts +2 -0
- package/commands/handler/input/input.js +44 -0
- package/commands/handler/mandatoryOptions.d.ts +2 -0
- package/commands/handler/mandatoryOptions.js +25 -0
- package/commands/handler/next.d.ts +2 -0
- package/commands/handler/next.js +19 -0
- package/commands/handler/options/env.d.ts +2 -0
- package/commands/handler/options/env.js +18 -0
- package/commands/handler/options/file.d.ts +2 -0
- package/commands/handler/options/file.js +21 -0
- package/commands/handler/options/index.d.ts +7 -0
- package/commands/handler/options/index.js +32 -0
- package/commands/handler/options/option.d.ts +2 -0
- package/commands/handler/options/option.js +20 -0
- package/commands/handler/options/pipe.d.ts +2 -0
- package/commands/handler/options/pipe.js +34 -0
- package/commands/handler/options/utils.d.ts +6 -0
- package/commands/handler/options/utils.js +63 -0
- package/commands/handler/or.d.ts +2 -0
- package/commands/handler/or.js +37 -0
- package/commands/handler/parseArguments.d.ts +4 -0
- package/commands/handler/parseArguments.js +22 -0
- package/commands/handler/resilient.d.ts +2 -0
- package/commands/handler/resilient.js +18 -0
- package/commands/handler/root/index.d.ts +2 -0
- package/commands/handler/root/index.js +18 -0
- package/commands/handler/stackTrace.d.ts +2 -0
- package/commands/handler/stackTrace.js +22 -0
- package/commands/handler/succeed.d.ts +2 -0
- package/commands/handler/succeed.js +9 -0
- package/commands/handler/utils.d.ts +7 -0
- package/commands/handler/utils.js +56 -0
- package/commands/host.command.d.ts +2 -0
- package/commands/host.command.js +68 -0
- package/commands/login.command.d.ts +3 -0
- package/commands/login.command.js +16 -0
- package/commands/logout.command.d.ts +3 -0
- package/commands/logout.command.js +21 -0
- package/commands/openAPI.command/index.d.ts +2 -0
- package/commands/openAPI.command/index.js +102 -0
- package/commands/openAPI.command/utils.d.ts +19 -0
- package/commands/openAPI.command/utils.js +334 -0
- package/commands/passcode.command.d.ts +2 -0
- package/commands/passcode.command.js +26 -0
- package/commands/secrets.command/index.d.ts +3 -0
- package/commands/secrets.command/index.js +13 -0
- package/commands/secrets.command/show.command.d.ts +3 -0
- package/commands/secrets.command/show.command.js +25 -0
- package/config/core.d.ts +10 -0
- package/config/core.js +23 -0
- package/config/index.d.ts +5 -0
- package/config/index.js +90 -0
- package/configureLoggers.d.ts +1 -0
- package/configureLoggers.js +11 -0
- package/constants.d.ts +43 -0
- package/constants.js +175 -0
- package/discovery/index.d.ts +10 -0
- package/discovery/index.js +142 -0
- package/discovery/utils.d.ts +7 -0
- package/discovery/utils.js +27 -0
- package/dwc/dwc.d.ts +10 -0
- package/dwc/dwc.js +180 -0
- package/dwc/run.d.ts +1 -0
- package/dwc/run.js +37 -0
- package/dwc/utils.d.ts +4 -0
- package/dwc/utils.js +37 -0
- package/index.d.ts +16 -0
- package/index.js +59 -0
- package/logger/index.d.ts +28 -0
- package/logger/index.js +63 -0
- package/module.d.ts +4 -0
- package/module.js +45 -0
- package/package.json +32 -0
- package/result/ResultHandlerFactory.d.ts +6 -0
- package/result/ResultHandlerFactory.js +12 -0
- package/result/ResultHandlerImpl.d.ts +6 -0
- package/result/ResultHandlerImpl.js +12 -0
- package/result/types.d.ts +4 -0
- package/result/types.js +2 -0
- package/schemas/discovery.json +347 -0
- package/settings/index.d.ts +4 -0
- package/settings/index.js +34 -0
- package/types.d.ts +182 -0
- package/types.js +67 -0
- package/utils/commands.d.ts +8 -0
- package/utils/commands.js +115 -0
- package/utils/http/index.d.ts +7 -0
- package/utils/http/index.js +69 -0
- package/utils/http/utils.d.ts +7 -0
- package/utils/http/utils.js +47 -0
- package/utils/options.d.ts +3 -0
- package/utils/options.js +25 -0
- package/utils/utils.d.ts +24 -0
- package/utils/utils.js +163 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildCommand = exports.buildOption = exports.registerLongName = exports.isOptionAlreadyRegistered = exports.getShortFlagForLongName = exports.createCommand = void 0;
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
const root_1 = require("../commands/handler/root");
|
|
7
|
+
const logger_1 = require("../logger");
|
|
8
|
+
const registeredOptions = {};
|
|
9
|
+
const getLogger = () => (0, logger_1.get)("utils.commands");
|
|
10
|
+
const checkEmpty = (name, value) => {
|
|
11
|
+
if (value.trim() === "") {
|
|
12
|
+
throw new Error(`missing value for parameter ${name}`);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const checkCommand = (command) => {
|
|
16
|
+
const { command: name, description } = command;
|
|
17
|
+
try {
|
|
18
|
+
checkEmpty("command", name);
|
|
19
|
+
checkEmpty("description", description);
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
const { trace } = getLogger();
|
|
23
|
+
trace(`failed to check command ${JSON.stringify(command, null, 2)} because of`, err);
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const createCommand = (name) => {
|
|
28
|
+
const { output } = getLogger();
|
|
29
|
+
const command = new commander_1.Command(name);
|
|
30
|
+
command.enablePositionalOptions();
|
|
31
|
+
command.exitOverride();
|
|
32
|
+
command.configureOutput({ writeErr: output, writeOut: output });
|
|
33
|
+
command.allowUnknownOption();
|
|
34
|
+
return command;
|
|
35
|
+
};
|
|
36
|
+
exports.createCommand = createCommand;
|
|
37
|
+
const getShortFlagForLongName = (longName) => {
|
|
38
|
+
for (const s of Object.keys(registeredOptions)) {
|
|
39
|
+
if (registeredOptions[s] === longName) {
|
|
40
|
+
return s;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`no short flag registered for long name ${longName}`);
|
|
44
|
+
};
|
|
45
|
+
exports.getShortFlagForLongName = getShortFlagForLongName;
|
|
46
|
+
const isOptionAlreadyRegistered = ({ longName }, command) => {
|
|
47
|
+
// @ts-expect-error ts(2551)
|
|
48
|
+
const opts = command.options.map((o) => o.long.replace("--", ""));
|
|
49
|
+
return opts.includes(longName);
|
|
50
|
+
};
|
|
51
|
+
exports.isOptionAlreadyRegistered = isOptionAlreadyRegistered;
|
|
52
|
+
const registerLongName = (longName) => {
|
|
53
|
+
try {
|
|
54
|
+
return (0, exports.getShortFlagForLongName)(longName);
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
for (const c of [
|
|
58
|
+
longName[0],
|
|
59
|
+
longName[0].toUpperCase() === longName[0]
|
|
60
|
+
? longName[0].toLowerCase()
|
|
61
|
+
: longName[0].toUpperCase(),
|
|
62
|
+
...types_1.CHARACTERS,
|
|
63
|
+
]) {
|
|
64
|
+
if (!registeredOptions[c]) {
|
|
65
|
+
registeredOptions[c] = longName;
|
|
66
|
+
return c;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`no short flag found for name ${longName}`);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
exports.registerLongName = registerLongName;
|
|
73
|
+
const buildOption = ({ longName, description, choices, required, default: defaultValue, args, }) => {
|
|
74
|
+
const mandatory = !!required;
|
|
75
|
+
const shortFlag = (0, exports.registerLongName)(longName);
|
|
76
|
+
let newDescr = description;
|
|
77
|
+
if (!mandatory) {
|
|
78
|
+
newDescr = newDescr ? `${newDescr} (optional)` : "(optional)";
|
|
79
|
+
}
|
|
80
|
+
const argStr = args
|
|
81
|
+
? args.reduce((p, c) => (c.optional ? `${p} [${c.name}]` : `${p} <${c.name}>`), "")
|
|
82
|
+
: "";
|
|
83
|
+
const option = new commander_1.Option(`-${shortFlag}, --${longName}${argStr}`, newDescr)
|
|
84
|
+
.makeOptionMandatory(mandatory)
|
|
85
|
+
.default(defaultValue);
|
|
86
|
+
if (choices && choices.length > 0) {
|
|
87
|
+
option.choices(choices);
|
|
88
|
+
}
|
|
89
|
+
return option;
|
|
90
|
+
};
|
|
91
|
+
exports.buildOption = buildOption;
|
|
92
|
+
const buildCommand = (topCommand, command) => {
|
|
93
|
+
[]
|
|
94
|
+
.concat([command], command.type === "topCommand" ? command.subCommands : [])
|
|
95
|
+
.forEach((c) => checkCommand(c));
|
|
96
|
+
const { command: name, description, options } = command;
|
|
97
|
+
const comm = (0, exports.createCommand)(name);
|
|
98
|
+
comm.description(description);
|
|
99
|
+
comm.enablePositionalOptions();
|
|
100
|
+
options?.forEach((o) => comm.addOption((0, exports.buildOption)(o)));
|
|
101
|
+
if (command.type === "topCommand") {
|
|
102
|
+
const { subCommands } = command;
|
|
103
|
+
subCommands.forEach((s) => (0, exports.buildCommand)(comm, s));
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// command.type === "command"
|
|
107
|
+
const { args, handler } = command;
|
|
108
|
+
if (args) {
|
|
109
|
+
args.forEach((a) => comm.argument(`<${a.argument}>`, a.description));
|
|
110
|
+
}
|
|
111
|
+
comm.action((0, root_1.create)(handler, `Failed to ${command.description}`)(comm));
|
|
112
|
+
}
|
|
113
|
+
topCommand.addCommand(comm);
|
|
114
|
+
};
|
|
115
|
+
exports.buildCommand = buildCommand;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fetch = exports.DEFAULTS = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const config_1 = require("../../config");
|
|
9
|
+
const logger_1 = require("../../logger");
|
|
10
|
+
const constants_1 = require("../../constants");
|
|
11
|
+
const utils_1 = require("./utils");
|
|
12
|
+
const core_1 = require("../../config/core");
|
|
13
|
+
exports.DEFAULTS = {
|
|
14
|
+
maxBodyLength: -1,
|
|
15
|
+
maxContentLength: -1,
|
|
16
|
+
maxRedirects: 0, // must be set to 0 because maxBodyLength: -1 won't work otherwise, see github.com/axios/axios/issues/4263
|
|
17
|
+
};
|
|
18
|
+
const HEADERS_ETAG = "x-sap-cli-core-discovery-etag";
|
|
19
|
+
const getLogger = () => (0, logger_1.get)("http");
|
|
20
|
+
const setEtag = (headers) => {
|
|
21
|
+
const { debug } = getLogger();
|
|
22
|
+
if (headers?.[HEADERS_ETAG]) {
|
|
23
|
+
(0, config_1.set)({ etag: headers[HEADERS_ETAG] });
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
debug("header %s is not available", HEADERS_ETAG);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const getCsrfTokenFromConfig = () => {
|
|
30
|
+
const cnfg = (0, config_1.get)();
|
|
31
|
+
return cnfg[constants_1.X_CSRF_TOKEN] ? { [constants_1.X_CSRF_TOKEN]: cnfg[constants_1.X_CSRF_TOKEN] } : {};
|
|
32
|
+
};
|
|
33
|
+
const fetch = async (config) => {
|
|
34
|
+
const cnfg = (0, config_1.get)();
|
|
35
|
+
const { debug, trace, error, output } = getLogger();
|
|
36
|
+
try {
|
|
37
|
+
if (cnfg.verbose) {
|
|
38
|
+
// eslint-disable-next-line
|
|
39
|
+
output("%s %s", config.method.toUpperCase(), config.url);
|
|
40
|
+
}
|
|
41
|
+
debug("http config: %s", JSON.stringify(config));
|
|
42
|
+
const res = await (0, axios_1.default)({
|
|
43
|
+
...config,
|
|
44
|
+
...exports.DEFAULTS,
|
|
45
|
+
headers: {
|
|
46
|
+
...getCsrfTokenFromConfig(),
|
|
47
|
+
...config.headers,
|
|
48
|
+
"User-Agent": `${(0, core_1.getPackageName)()}+${(0, core_1.getVersion)()}`,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
if (cnfg.verbose) {
|
|
52
|
+
output("%d %s", res.status, res.statusText);
|
|
53
|
+
(0, utils_1.printCorrelationId)(res);
|
|
54
|
+
}
|
|
55
|
+
trace("response", res);
|
|
56
|
+
setEtag(res.headers);
|
|
57
|
+
return res;
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
error("error while executing http request", err.toString(), err.response);
|
|
61
|
+
if (cnfg.verbose) {
|
|
62
|
+
(0, utils_1.printError)(err);
|
|
63
|
+
(0, utils_1.printCorrelationId)(err.response);
|
|
64
|
+
}
|
|
65
|
+
setEtag(err.response?.headers);
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
exports.fetch = fetch;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.printError = exports.printCorrelationId = void 0;
|
|
4
|
+
const logger_1 = require("../../logger");
|
|
5
|
+
const getLogger = () => (0, logger_1.get)("handler.http.utils");
|
|
6
|
+
const CORRELATION_ID_KEYS = [
|
|
7
|
+
"x-correlationid",
|
|
8
|
+
"x-vcap-request-id",
|
|
9
|
+
"x-request-id",
|
|
10
|
+
];
|
|
11
|
+
const printCorrelationId = (response) => {
|
|
12
|
+
if (!response) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
let corrId = Object.entries(response.headers || {}).find((e) => CORRELATION_ID_KEYS.includes(e[0]))?.[1];
|
|
16
|
+
if (!corrId && response.data?.details?.stack) {
|
|
17
|
+
const stack = response.data.details.stack;
|
|
18
|
+
if (stack.startsWith("See correlation id ")) {
|
|
19
|
+
corrId = stack.replace("See correlation id ", "");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const { output } = getLogger();
|
|
23
|
+
output(corrId ? `Correlation ID ${corrId}` : "No correlation ID available");
|
|
24
|
+
};
|
|
25
|
+
exports.printCorrelationId = printCorrelationId;
|
|
26
|
+
const printError = (err) => {
|
|
27
|
+
const { output, debug } = getLogger();
|
|
28
|
+
if (err.response) {
|
|
29
|
+
output("Request failed with %i %s", err.response.status, err.response.statusText);
|
|
30
|
+
if (err.response.data?.details?.message) {
|
|
31
|
+
output('Error: "%s"', err.response.data.details.message);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
debug("No error message available");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (err.reason) {
|
|
38
|
+
output('Request failed with reason "%s"', err.reason);
|
|
39
|
+
}
|
|
40
|
+
else if (err.message) {
|
|
41
|
+
output('Request failed with message "%s"', err.message);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
output("Request failed with an unknown error");
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.printError = printError;
|
package/utils/options.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOptionValueFromConfigGracefully = exports.getOptionValueFromConfig = void 0;
|
|
4
|
+
const config_1 = require("../config");
|
|
5
|
+
const getOptionValueFromConfig = (option, defaultValue) => {
|
|
6
|
+
const config = (0, config_1.get)();
|
|
7
|
+
if (!config.options[option.longName] &&
|
|
8
|
+
typeof defaultValue !== "string" &&
|
|
9
|
+
!defaultValue) {
|
|
10
|
+
throw new Error(`no value found for option ${option.longName}`);
|
|
11
|
+
}
|
|
12
|
+
return config.options[option.longName]
|
|
13
|
+
? config.options[option.longName]
|
|
14
|
+
: defaultValue;
|
|
15
|
+
};
|
|
16
|
+
exports.getOptionValueFromConfig = getOptionValueFromConfig;
|
|
17
|
+
const getOptionValueFromConfigGracefully = (option) => {
|
|
18
|
+
try {
|
|
19
|
+
return (0, exports.getOptionValueFromConfig)(option);
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.getOptionValueFromConfigGracefully = getOptionValueFromConfigGracefully;
|
package/utils/utils.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PackageJson } from "../types";
|
|
2
|
+
export declare const parseVersion: (version: string) => {
|
|
3
|
+
major: number;
|
|
4
|
+
minor: number;
|
|
5
|
+
};
|
|
6
|
+
export declare const requireFile: (p: string) => any;
|
|
7
|
+
export declare const readPackageJson: (cwd?: string, force?: boolean) => PackageJson;
|
|
8
|
+
export declare const getVersion: (cwd?: string) => string;
|
|
9
|
+
export declare const getName: (cwd?: string) => string;
|
|
10
|
+
export declare const getPackageName: (cwd?: string) => string;
|
|
11
|
+
export declare const getDescription: (cwd?: string) => string;
|
|
12
|
+
export declare const getBin: (cwd?: string) => string;
|
|
13
|
+
export declare const parseTenant: (tenant: string) => string;
|
|
14
|
+
export declare const getInfoFromTenant: (tenant: string, verbose: boolean, printOutput?: boolean) => {
|
|
15
|
+
host: string;
|
|
16
|
+
publicfqdn: string;
|
|
17
|
+
passcodeUrl: string;
|
|
18
|
+
tenantUrl: string;
|
|
19
|
+
};
|
|
20
|
+
export declare const sha256: (string: string) => string;
|
|
21
|
+
export declare const toConstantCase: (string: string) => string;
|
|
22
|
+
export declare const openUrlInBrowser: (url: string, queryParameters?: {
|
|
23
|
+
[key: string]: string;
|
|
24
|
+
}) => Promise<void>;
|
package/utils/utils.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.openUrlInBrowser = exports.toConstantCase = exports.sha256 = exports.getInfoFromTenant = exports.parseTenant = exports.getBin = exports.getDescription = exports.getPackageName = exports.getName = exports.getVersion = exports.readPackageJson = exports.requireFile = exports.parseVersion = void 0;
|
|
30
|
+
const crypto = __importStar(require("crypto"));
|
|
31
|
+
const path_1 = __importDefault(require("path"));
|
|
32
|
+
const lodash_1 = require("lodash");
|
|
33
|
+
const open_1 = __importDefault(require("open"));
|
|
34
|
+
const logger_1 = require("../logger");
|
|
35
|
+
let pgk;
|
|
36
|
+
const getLogger = () => (0, logger_1.get)("utils.utils");
|
|
37
|
+
const REGEX_HTTPS = /http(s)*:\/\//;
|
|
38
|
+
const parseVersion = (version) => {
|
|
39
|
+
const segments = version.split(".");
|
|
40
|
+
return { major: parseInt(segments[0], 10), minor: parseInt(segments[1], 10) };
|
|
41
|
+
};
|
|
42
|
+
exports.parseVersion = parseVersion;
|
|
43
|
+
// eslint-disable-next-line import/no-dynamic-require, global-require
|
|
44
|
+
const requireFile = (p) => require(p);
|
|
45
|
+
exports.requireFile = requireFile;
|
|
46
|
+
const readPackageJson = (cwd = "", force = false) => {
|
|
47
|
+
if (!pgk || force) {
|
|
48
|
+
const { trace } = getLogger();
|
|
49
|
+
try {
|
|
50
|
+
const p = path_1.default.join(cwd, "./package.json");
|
|
51
|
+
trace(`reading ${p}`);
|
|
52
|
+
pgk = (0, exports.requireFile)(p);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
trace("failed to read package.json first time", err.stack);
|
|
56
|
+
try {
|
|
57
|
+
const p = path_1.default.join(cwd, "../package.json");
|
|
58
|
+
trace(`reading ${p}`);
|
|
59
|
+
pgk = (0, exports.requireFile)(p);
|
|
60
|
+
}
|
|
61
|
+
catch (err2) {
|
|
62
|
+
trace("failed to read package.json second time", err.stack);
|
|
63
|
+
const p = path_1.default.join(cwd, "../../package.json");
|
|
64
|
+
trace(`reading ${p}`);
|
|
65
|
+
pgk = (0, exports.requireFile)(p);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return pgk;
|
|
70
|
+
};
|
|
71
|
+
exports.readPackageJson = readPackageJson;
|
|
72
|
+
const getVersion = (cwd = "") => {
|
|
73
|
+
return (0, exports.readPackageJson)(cwd, true).version;
|
|
74
|
+
};
|
|
75
|
+
exports.getVersion = getVersion;
|
|
76
|
+
const getName = (cwd = "") => {
|
|
77
|
+
const segments = (0, exports.readPackageJson)(cwd, true).name.split(/^@.*\//);
|
|
78
|
+
return segments[segments.length - 1];
|
|
79
|
+
};
|
|
80
|
+
exports.getName = getName;
|
|
81
|
+
const getPackageName = (cwd = "") => {
|
|
82
|
+
return (0, exports.readPackageJson)(cwd, true).name;
|
|
83
|
+
};
|
|
84
|
+
exports.getPackageName = getPackageName;
|
|
85
|
+
const getDescription = (cwd = "") => {
|
|
86
|
+
return (0, exports.readPackageJson)(cwd, true).description;
|
|
87
|
+
};
|
|
88
|
+
exports.getDescription = getDescription;
|
|
89
|
+
const getBin = (cwd = "") => {
|
|
90
|
+
const keys = Object.keys((0, exports.readPackageJson)(cwd, true).bin || {});
|
|
91
|
+
if (keys.length === 0) {
|
|
92
|
+
throw new Error("no bin property defined in package.json or bin property is empty");
|
|
93
|
+
}
|
|
94
|
+
return keys[0];
|
|
95
|
+
};
|
|
96
|
+
exports.getBin = getBin;
|
|
97
|
+
const removeProtocol = (tenant) => tenant.replace(REGEX_HTTPS, "");
|
|
98
|
+
const parseTenant = (tenant) => {
|
|
99
|
+
const t = REGEX_HTTPS.test(tenant) ? tenant : `https://${tenant}`;
|
|
100
|
+
/*
|
|
101
|
+
One can enter the tenant in different ways, for example
|
|
102
|
+
|
|
103
|
+
<protocol>://<fqdn>.<landscape>.cloud.sap/
|
|
104
|
+
<fqdn>.<landscape>.cloud.sap/
|
|
105
|
+
<fqdn>.<landscape>.cloud.sap
|
|
106
|
+
<protocol>://<fqdn>.<landscape>.cloud.sap/dwaas-ui/index.html#/
|
|
107
|
+
|
|
108
|
+
However, we want the tenant in this format, eventually:
|
|
109
|
+
|
|
110
|
+
<protocol>://<fqdn>.<landscape>.cloud.sap
|
|
111
|
+
*/
|
|
112
|
+
return new URL(t).host;
|
|
113
|
+
};
|
|
114
|
+
exports.parseTenant = parseTenant;
|
|
115
|
+
const getInfoFromTenant = (tenant, verbose, printOutput = true) => {
|
|
116
|
+
let protocol;
|
|
117
|
+
try {
|
|
118
|
+
protocol = new URL(tenant).protocol;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
protocol = "https:";
|
|
122
|
+
}
|
|
123
|
+
const publicfqdn = removeProtocol(tenant);
|
|
124
|
+
const segments = publicfqdn.split(".");
|
|
125
|
+
const hostname = segments[0];
|
|
126
|
+
const region = segments[1];
|
|
127
|
+
if (!region) {
|
|
128
|
+
const { output, error } = getLogger();
|
|
129
|
+
if (printOutput) {
|
|
130
|
+
output("error: invalid value '%s' for option '-H, --host <host>'", tenant);
|
|
131
|
+
}
|
|
132
|
+
if (verbose) {
|
|
133
|
+
output("value '%s' does not match pattern https://<prefix>.<landscape>.hcs.sap.cloud", tenant);
|
|
134
|
+
}
|
|
135
|
+
error("invalid region", region);
|
|
136
|
+
throw new Error("invalid region");
|
|
137
|
+
}
|
|
138
|
+
const parsedTenant = (0, exports.parseTenant)(tenant);
|
|
139
|
+
return {
|
|
140
|
+
host: `${protocol}//dwaas-core.sac${region}.cfapps.orca.net.sap`,
|
|
141
|
+
publicfqdn: parsedTenant,
|
|
142
|
+
passcodeUrl: `https://${hostname}.authentication.${region}.hana.ondemand.com/passcode`,
|
|
143
|
+
tenantUrl: `${protocol}//${parsedTenant}`,
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
exports.getInfoFromTenant = getInfoFromTenant;
|
|
147
|
+
const sha256 = (string) =>
|
|
148
|
+
// NOSONAR actually sha256 (or SHA-256) is considered secure according to SonarQube
|
|
149
|
+
crypto.createHash("sha256").update(string).digest("base64");
|
|
150
|
+
exports.sha256 = sha256;
|
|
151
|
+
const toConstantCase = (string) => (0, lodash_1.upperCase)(string).replace(/ /g, "_");
|
|
152
|
+
exports.toConstantCase = toConstantCase;
|
|
153
|
+
const openUrlInBrowser = async (url, queryParameters = {}) => {
|
|
154
|
+
const u = new URL(url);
|
|
155
|
+
for (const [key, value] of Object.entries(queryParameters)) {
|
|
156
|
+
u.searchParams.append(key, value);
|
|
157
|
+
}
|
|
158
|
+
const urlString = u.toString();
|
|
159
|
+
const { debug } = getLogger();
|
|
160
|
+
debug(`open browser at ${urlString}`);
|
|
161
|
+
await (0, open_1.default)(urlString);
|
|
162
|
+
};
|
|
163
|
+
exports.openUrlInBrowser = openUrlInBrowser;
|