@contentstack/apps-cli 1.0.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/LICENSE +21 -0
- package/README.md +200 -0
- package/bin/dev +17 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +5 -0
- package/bin/run.cmd +3 -0
- package/lib/commands/app/base-command.d.ts +51 -0
- package/lib/commands/app/base-command.js +123 -0
- package/lib/commands/app/create.d.ts +68 -0
- package/lib/commands/app/create.js +288 -0
- package/lib/commands/app/delete.d.ts +10 -0
- package/lib/commands/app/delete.js +72 -0
- package/lib/commands/app/get.d.ts +12 -0
- package/lib/commands/app/get.js +57 -0
- package/lib/commands/app/index.d.ts +7 -0
- package/lib/commands/app/index.js +35 -0
- package/lib/commands/app/install.d.ts +11 -0
- package/lib/commands/app/install.js +77 -0
- package/lib/commands/app/uninstall.d.ts +11 -0
- package/lib/commands/app/uninstall.js +51 -0
- package/lib/commands/app/update.d.ts +34 -0
- package/lib/commands/app/update.js +190 -0
- package/lib/config/index.d.ts +16 -0
- package/lib/config/index.js +20 -0
- package/lib/config/manifest.json +69 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -0
- package/lib/messages/index.d.ts +83 -0
- package/lib/messages/index.js +101 -0
- package/lib/types/app.d.ts +101 -0
- package/lib/types/app.js +29 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/index.js +5 -0
- package/lib/types/utils.d.ts +18 -0
- package/lib/types/utils.js +2 -0
- package/lib/util/common-utils.d.ts +18 -0
- package/lib/util/common-utils.js +148 -0
- package/lib/util/fs.d.ts +2 -0
- package/lib/util/fs.js +36 -0
- package/lib/util/index.d.ts +4 -0
- package/lib/util/index.js +9 -0
- package/lib/util/inquirer.d.ts +34 -0
- package/lib/util/inquirer.js +198 -0
- package/lib/util/log.d.ts +41 -0
- package/lib/util/log.js +150 -0
- package/package.json +99 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Color } from "chalk";
|
|
2
|
+
import { PrintOptions } from "@contentstack/cli-utilities";
|
|
3
|
+
import config from "../config";
|
|
4
|
+
type LogFn = (message: string | any, logType?: LoggerType | PrintOptions | undefined) => void;
|
|
5
|
+
type ExitFn = (code?: number | undefined) => void;
|
|
6
|
+
type Partial<T> = {
|
|
7
|
+
[P in keyof T]?: T[P];
|
|
8
|
+
};
|
|
9
|
+
type ConfigType = {
|
|
10
|
+
config?: string;
|
|
11
|
+
} & typeof config & Record<string, any>;
|
|
12
|
+
export { LogFn, ExitFn, Partial, ConfigType };
|
|
13
|
+
export type LoggerType = "info" | "warn" | "error" | "debug";
|
|
14
|
+
export type PrintType = {
|
|
15
|
+
message: string;
|
|
16
|
+
bold?: boolean;
|
|
17
|
+
color?: typeof Color;
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ContentstackClient, FlagInput } from "@contentstack/cli-utilities";
|
|
2
|
+
import { Extension, LogFn } from "../types";
|
|
3
|
+
import { Stack } from "@contentstack/cli-utilities";
|
|
4
|
+
export type CommonOptions = {
|
|
5
|
+
log: LogFn;
|
|
6
|
+
managementSdk: ContentstackClient;
|
|
7
|
+
};
|
|
8
|
+
declare function getOrganizations(options: CommonOptions, skip?: number, organizations?: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
9
|
+
declare function getOrgAppUiLocation(): Extension[];
|
|
10
|
+
declare function fetchApps(flags: FlagInput, orgUid: string, options: CommonOptions, skip?: number, apps?: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
11
|
+
declare function fetchApp(flags: FlagInput, orgUid: string, options: CommonOptions): Promise<import("@contentstack/management/types/app").App>;
|
|
12
|
+
declare function fetchAppInstallations(flags: FlagInput, orgUid: string, options: CommonOptions): Promise<import("@contentstack/management/types/contentstackCollection").ContentstackCollection<import("@contentstack/management/types/app/installation").Installation>>;
|
|
13
|
+
declare function deleteApp(flags: FlagInput, orgUid: string, options: CommonOptions): Promise<import("@contentstack/management/types/app").App>;
|
|
14
|
+
declare function installApp(flags: FlagInput, orgUid: string, type: string, options: CommonOptions): Promise<import("@contentstack/management/types/app/installation").Installation>;
|
|
15
|
+
declare function fetchStack(flags: FlagInput, options: CommonOptions): Promise<import("@contentstack/management/types/stack").Stack>;
|
|
16
|
+
declare function getStacks(options: CommonOptions, orgUid: string, skip?: number, stacks?: Stack[]): Promise<Stack[]>;
|
|
17
|
+
declare function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions): Promise<import("@contentstack/management/types/utility/fields").AnyProperty>;
|
|
18
|
+
export { getOrganizations, getOrgAppUiLocation, fetchApps, fetchApp, fetchAppInstallations, deleteApp, installApp, getStacks, fetchStack, uninstallApp };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uninstallApp = exports.fetchStack = exports.getStacks = exports.installApp = exports.deleteApp = exports.fetchAppInstallations = exports.fetchApp = exports.fetchApps = exports.getOrgAppUiLocation = exports.getOrganizations = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
6
|
+
async function getOrganizations(options, skip = 0, organizations = []) {
|
|
7
|
+
const { log, managementSdk } = options;
|
|
8
|
+
const response = await managementSdk
|
|
9
|
+
.organization()
|
|
10
|
+
.fetchAll({ limit: 100, asc: "name", include_count: true, skip: skip })
|
|
11
|
+
.catch((error) => {
|
|
12
|
+
log("Unable to fetch organizations.", "warn");
|
|
13
|
+
log(error, "error");
|
|
14
|
+
throw error;
|
|
15
|
+
});
|
|
16
|
+
if (response) {
|
|
17
|
+
organizations = organizations.concat(response.items);
|
|
18
|
+
if (organizations.length < response.count) {
|
|
19
|
+
organizations = await getOrganizations(options, skip + 100);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return organizations;
|
|
23
|
+
}
|
|
24
|
+
exports.getOrganizations = getOrganizations;
|
|
25
|
+
function getOrgAppUiLocation() {
|
|
26
|
+
const orgConfigLocation = {
|
|
27
|
+
type: types_1.AppLocation.ORG_CONFIG,
|
|
28
|
+
meta: [
|
|
29
|
+
{
|
|
30
|
+
path: "/app-configuration",
|
|
31
|
+
signed: true,
|
|
32
|
+
enabled: true,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
return [orgConfigLocation];
|
|
37
|
+
}
|
|
38
|
+
exports.getOrgAppUiLocation = getOrgAppUiLocation;
|
|
39
|
+
async function fetchApps(flags, orgUid, options, skip = 0, apps = []) {
|
|
40
|
+
const { log, managementSdk } = options;
|
|
41
|
+
const response = await managementSdk
|
|
42
|
+
.organization(orgUid)
|
|
43
|
+
.app()
|
|
44
|
+
.findAll({
|
|
45
|
+
limit: 50,
|
|
46
|
+
asc: "name",
|
|
47
|
+
include_count: true,
|
|
48
|
+
skip: skip,
|
|
49
|
+
target_type: flags["app-type"],
|
|
50
|
+
})
|
|
51
|
+
.catch((error) => {
|
|
52
|
+
cli_utilities_1.cliux.loader("failed");
|
|
53
|
+
log("Some error occurred while fetching apps.", "warn");
|
|
54
|
+
log(error.errorMessage, "error");
|
|
55
|
+
throw error;
|
|
56
|
+
});
|
|
57
|
+
if (response) {
|
|
58
|
+
apps = apps.concat(response.items);
|
|
59
|
+
if (apps.length < response.count) {
|
|
60
|
+
apps = await fetchApps(flags, orgUid, options, skip + 50, apps);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return apps;
|
|
64
|
+
}
|
|
65
|
+
exports.fetchApps = fetchApps;
|
|
66
|
+
function fetchApp(flags, orgUid, options) {
|
|
67
|
+
const { managementSdk } = options;
|
|
68
|
+
const app = flags["app-uid"];
|
|
69
|
+
return managementSdk
|
|
70
|
+
.organization(orgUid)
|
|
71
|
+
.app(app)
|
|
72
|
+
.fetch();
|
|
73
|
+
}
|
|
74
|
+
exports.fetchApp = fetchApp;
|
|
75
|
+
function fetchAppInstallations(flags, orgUid, options) {
|
|
76
|
+
const { managementSdk } = options;
|
|
77
|
+
const app = flags["app-uid"];
|
|
78
|
+
return managementSdk
|
|
79
|
+
.organization(orgUid)
|
|
80
|
+
.app(app)
|
|
81
|
+
.installation()
|
|
82
|
+
.findAll()
|
|
83
|
+
.catch(error => {
|
|
84
|
+
const { log } = options;
|
|
85
|
+
cli_utilities_1.cliux.loader("failed");
|
|
86
|
+
log("Some error occurred while fetching app installations.", "warn");
|
|
87
|
+
throw error; // throwing error here instead of removing the catch block, as the loader needs to stopped in case there is an error.
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exports.fetchAppInstallations = fetchAppInstallations;
|
|
91
|
+
function deleteApp(flags, orgUid, options) {
|
|
92
|
+
const { managementSdk } = options;
|
|
93
|
+
const app = flags["app-uid"];
|
|
94
|
+
return managementSdk
|
|
95
|
+
.organization(orgUid)
|
|
96
|
+
.app(app)
|
|
97
|
+
.delete();
|
|
98
|
+
}
|
|
99
|
+
exports.deleteApp = deleteApp;
|
|
100
|
+
function installApp(flags, orgUid, type, options) {
|
|
101
|
+
const { managementSdk } = options;
|
|
102
|
+
return managementSdk
|
|
103
|
+
.organization(orgUid)
|
|
104
|
+
.app(flags["app-uid"])
|
|
105
|
+
.install({
|
|
106
|
+
targetUid: flags["stack-api-key"] || orgUid,
|
|
107
|
+
targetType: type,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
exports.installApp = installApp;
|
|
111
|
+
function fetchStack(flags, options) {
|
|
112
|
+
const { managementSdk } = options;
|
|
113
|
+
return managementSdk
|
|
114
|
+
.stack({ api_key: flags["stack-api-key"] })
|
|
115
|
+
.fetch();
|
|
116
|
+
}
|
|
117
|
+
exports.fetchStack = fetchStack;
|
|
118
|
+
async function getStacks(options, orgUid, skip = 0, stacks = []) {
|
|
119
|
+
const { log, managementSdk } = options;
|
|
120
|
+
const response = await managementSdk
|
|
121
|
+
.organization(orgUid)
|
|
122
|
+
.stacks({ include_count: true, limit: 100, asc: "name", skip: skip })
|
|
123
|
+
.catch((error) => {
|
|
124
|
+
cli_utilities_1.cliux.loader("failed");
|
|
125
|
+
log("Unable to fetch stacks.", "warn");
|
|
126
|
+
log((error === null || error === void 0 ? void 0 : error.errorMessage) || error, "error");
|
|
127
|
+
throw error;
|
|
128
|
+
});
|
|
129
|
+
if (response) {
|
|
130
|
+
stacks = stacks.concat(response.items);
|
|
131
|
+
if (stacks.length < response.count) {
|
|
132
|
+
stacks = await getStacks(options, orgUid, skip + 100, stacks);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return stacks;
|
|
136
|
+
}
|
|
137
|
+
exports.getStacks = getStacks;
|
|
138
|
+
function uninstallApp(flags, orgUid, options) {
|
|
139
|
+
const { managementSdk } = options;
|
|
140
|
+
const app = flags['app-uid'];
|
|
141
|
+
const installationUid = flags['installation-uid'];
|
|
142
|
+
return managementSdk
|
|
143
|
+
.organization(orgUid)
|
|
144
|
+
.app(app)
|
|
145
|
+
.installation(installationUid)
|
|
146
|
+
.uninstall();
|
|
147
|
+
}
|
|
148
|
+
exports.uninstallApp = uninstallApp;
|
package/lib/util/fs.d.ts
ADDED
package/lib/util/fs.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeFile = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const config_1 = tslib_1.__importDefault(require("../config"));
|
|
8
|
+
const messages_1 = tslib_1.__importStar(require("../messages"));
|
|
9
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
10
|
+
async function writeFile(dir = process.cwd(), force = false, data = {}, log = console.log) {
|
|
11
|
+
var _a;
|
|
12
|
+
await ensureDirectoryExists(dir);
|
|
13
|
+
const files = (0, fs_1.readdirSync)(dir);
|
|
14
|
+
const latestFileName = ((_a = files.filter(fileName => fileName.match(new RegExp(config_1.default.defaultAppFileName))).pop()) === null || _a === void 0 ? void 0 : _a.split('.')[0]) || config_1.default.defaultAppFileName;
|
|
15
|
+
let target = (0, path_1.resolve)(dir, `${latestFileName}.json`);
|
|
16
|
+
if ((0, fs_1.existsSync)(target)) {
|
|
17
|
+
const userConfirmation = force || (await cli_utilities_1.cliux.confirm((0, messages_1.$t)(messages_1.default.FILE_ALREADY_EXISTS, { file: `${config_1.default.defaultAppFileName}.json` })));
|
|
18
|
+
if (userConfirmation) {
|
|
19
|
+
target = (0, path_1.resolve)(dir, `${config_1.default.defaultAppFileName}.json`);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
target = (0, path_1.resolve)(dir, `${incrementName(latestFileName)}.json`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
await (0, fs_1.writeFileSync)(target, JSON.stringify(data));
|
|
26
|
+
log((0, messages_1.$t)(messages_1.default.FILE_WRITTEN_SUCCESS, { file: target }), "info");
|
|
27
|
+
}
|
|
28
|
+
exports.writeFile = writeFile;
|
|
29
|
+
async function ensureDirectoryExists(dir) {
|
|
30
|
+
if (!(0, fs_1.existsSync)(dir)) {
|
|
31
|
+
await (0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function incrementName(name) {
|
|
35
|
+
return `${config_1.default.defaultAppFileName}${Number(name.split(config_1.default.defaultAppFileName).pop()) + 1}`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./fs"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./inquirer"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./common-utils"), exports);
|
|
8
|
+
var log_1 = require("./log");
|
|
9
|
+
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return tslib_1.__importDefault(log_1).default; } });
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FlagInput, ContentstackClient } from "@contentstack/cli-utilities";
|
|
2
|
+
import { AppTarget } from '@contentstack/management/types/app/index';
|
|
3
|
+
import { CommonOptions } from "./common-utils";
|
|
4
|
+
/**
|
|
5
|
+
* @method getAppName
|
|
6
|
+
*
|
|
7
|
+
* @return {*} {(Promise<string | boolean>)}
|
|
8
|
+
*/
|
|
9
|
+
declare function getAppName(defaultName?: string): Promise<string | boolean>;
|
|
10
|
+
/**
|
|
11
|
+
* @method getDirName
|
|
12
|
+
*
|
|
13
|
+
* @param {string} [defaultName=""]
|
|
14
|
+
* @return {*} {(Promise<string | boolean>)}
|
|
15
|
+
*/
|
|
16
|
+
declare function getDirName(path: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* @method getOrg
|
|
19
|
+
*
|
|
20
|
+
* @param {FlagInput} flags
|
|
21
|
+
* @param {CommonOptions} options
|
|
22
|
+
* @return {*}
|
|
23
|
+
*/
|
|
24
|
+
declare function getOrg(flags: FlagInput, options: CommonOptions): Promise<import("@oclif/core/lib/interfaces/parser").CompletableFlag<any>>;
|
|
25
|
+
declare function getApp(flags: FlagInput, orgUid: string, options: CommonOptions): Promise<Record<string, any> | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
* @method getDeveloperHubUrl
|
|
28
|
+
*
|
|
29
|
+
* @return {*} {Promise<string>}
|
|
30
|
+
*/
|
|
31
|
+
declare function getDeveloperHubUrl(): Promise<string>;
|
|
32
|
+
declare function getStack(orgUid: string, options: CommonOptions): Promise<Record<string, any> | undefined>;
|
|
33
|
+
declare function getInstallation(flags: FlagInput, orgUid: string, managementSdkForStacks: ContentstackClient, appType: AppTarget, options: CommonOptions): Promise<string>;
|
|
34
|
+
export { getOrg, getAppName, getDirName, getDeveloperHubUrl, getApp, getStack, getInstallation };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInstallation = exports.getStack = exports.getApp = exports.getDeveloperHubUrl = exports.getDirName = exports.getAppName = exports.getOrg = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const find_1 = tslib_1.__importDefault(require("lodash/find"));
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
9
|
+
const config_1 = tslib_1.__importDefault(require("../config"));
|
|
10
|
+
const messages_1 = tslib_1.__importStar(require("../messages"));
|
|
11
|
+
const common_utils_1 = require("./common-utils");
|
|
12
|
+
/**
|
|
13
|
+
* @method getAppName
|
|
14
|
+
*
|
|
15
|
+
* @return {*} {(Promise<string | boolean>)}
|
|
16
|
+
*/
|
|
17
|
+
async function getAppName(defaultName = "") {
|
|
18
|
+
return cli_utilities_1.cliux.inquire({
|
|
19
|
+
type: "input",
|
|
20
|
+
name: "appName",
|
|
21
|
+
default: defaultName,
|
|
22
|
+
message: (0, messages_1.$t)(messages_1.default.NAME, { target: "App" }),
|
|
23
|
+
validate: (name) => {
|
|
24
|
+
if (name.length < 3 || name.length > 20) {
|
|
25
|
+
return (0, messages_1.$t)(messages_1.errors.INVALID_NAME, { min: "3", max: "20" });
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
exports.getAppName = getAppName;
|
|
32
|
+
/**
|
|
33
|
+
* @method getDirName
|
|
34
|
+
*
|
|
35
|
+
* @param {string} [defaultName=""]
|
|
36
|
+
* @return {*} {(Promise<string | boolean>)}
|
|
37
|
+
*/
|
|
38
|
+
async function getDirName(path) {
|
|
39
|
+
const basePath = (0, path_1.dirname)(path);
|
|
40
|
+
const defaultName = (0, path_1.basename)(path);
|
|
41
|
+
return cli_utilities_1.cliux
|
|
42
|
+
.inquire({
|
|
43
|
+
type: "input",
|
|
44
|
+
name: "dirName",
|
|
45
|
+
default: defaultName,
|
|
46
|
+
message: (0, messages_1.$t)(messages_1.default.NAME, { target: "Directory" }),
|
|
47
|
+
validate: (name) => {
|
|
48
|
+
if (name.length < 3 || name.length > 30) {
|
|
49
|
+
return (0, messages_1.$t)(messages_1.errors.INVALID_NAME, { min: "3", max: "50" });
|
|
50
|
+
}
|
|
51
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(basePath, name))) {
|
|
52
|
+
return messages_1.default.DIR_EXIST;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
.then((name) => (0, path_1.join)(basePath, name));
|
|
58
|
+
}
|
|
59
|
+
exports.getDirName = getDirName;
|
|
60
|
+
/**
|
|
61
|
+
* @method getOrg
|
|
62
|
+
*
|
|
63
|
+
* @param {FlagInput} flags
|
|
64
|
+
* @param {CommonOptions} options
|
|
65
|
+
* @return {*}
|
|
66
|
+
*/
|
|
67
|
+
async function getOrg(flags, options) {
|
|
68
|
+
const organizations = (await (0, common_utils_1.getOrganizations)(options)) || [];
|
|
69
|
+
if (!(flags.org && (0, find_1.default)(organizations, { uid: flags.org }))) {
|
|
70
|
+
if (flags.org) {
|
|
71
|
+
throw new Error(messages_1.default.ORG_UID_NOT_FOUND);
|
|
72
|
+
}
|
|
73
|
+
flags.org = await cli_utilities_1.cliux
|
|
74
|
+
.inquire({
|
|
75
|
+
type: "search-list",
|
|
76
|
+
name: "Organization",
|
|
77
|
+
choices: organizations,
|
|
78
|
+
message: messages_1.default.CHOOSE_ORG,
|
|
79
|
+
})
|
|
80
|
+
.then((name) => { var _a; return (_a = (0, find_1.default)(organizations, { name })) === null || _a === void 0 ? void 0 : _a.uid; });
|
|
81
|
+
}
|
|
82
|
+
return flags.org;
|
|
83
|
+
}
|
|
84
|
+
exports.getOrg = getOrg;
|
|
85
|
+
async function getApp(flags, orgUid, options) {
|
|
86
|
+
cli_utilities_1.cliux.loader("Loading Apps");
|
|
87
|
+
const apps = (await (0, common_utils_1.fetchApps)(flags, orgUid, options)) || [];
|
|
88
|
+
cli_utilities_1.cliux.loader("done");
|
|
89
|
+
if (apps.length === 0) {
|
|
90
|
+
throw new Error(messages_1.default.APPS_NOT_FOUND);
|
|
91
|
+
}
|
|
92
|
+
flags.app = await cli_utilities_1.cliux
|
|
93
|
+
.inquire({
|
|
94
|
+
type: "search-list",
|
|
95
|
+
name: "App",
|
|
96
|
+
choices: apps,
|
|
97
|
+
message: messages_1.default.CHOOSE_APP
|
|
98
|
+
})
|
|
99
|
+
.then((name) => { var _a; return (_a = apps.find(app => app.name === name)) === null || _a === void 0 ? void 0 : _a.uid; });
|
|
100
|
+
return apps.find(app => app.uid === flags.app);
|
|
101
|
+
}
|
|
102
|
+
exports.getApp = getApp;
|
|
103
|
+
/**
|
|
104
|
+
* @method getDeveloperHubUrl
|
|
105
|
+
*
|
|
106
|
+
* @return {*} {Promise<string>}
|
|
107
|
+
*/
|
|
108
|
+
async function getDeveloperHubUrl() {
|
|
109
|
+
const { cma, name } = cli_utilities_1.configHandler.get("region") || {};
|
|
110
|
+
let developerHubBaseUrl = config_1.default.developerHubUrls[cma];
|
|
111
|
+
if (!developerHubBaseUrl) {
|
|
112
|
+
developerHubBaseUrl = await cli_utilities_1.cliux.inquire({
|
|
113
|
+
type: "input",
|
|
114
|
+
name: "name",
|
|
115
|
+
validate: (url) => {
|
|
116
|
+
if (!url)
|
|
117
|
+
return messages_1.errors.BASE_URL_EMPTY;
|
|
118
|
+
return true;
|
|
119
|
+
},
|
|
120
|
+
message: (0, messages_1.$t)(messages_1.commonMsg.DEVELOPER_HUB_URL_PROMPT, { name }),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (developerHubBaseUrl.startsWith("http")) {
|
|
124
|
+
developerHubBaseUrl = developerHubBaseUrl.split("//")[1];
|
|
125
|
+
}
|
|
126
|
+
return developerHubBaseUrl;
|
|
127
|
+
}
|
|
128
|
+
exports.getDeveloperHubUrl = getDeveloperHubUrl;
|
|
129
|
+
async function getStack(orgUid, options) {
|
|
130
|
+
cli_utilities_1.cliux.loader("Loading Stacks");
|
|
131
|
+
const stacks = (await (0, common_utils_1.getStacks)(options, orgUid)) || [];
|
|
132
|
+
cli_utilities_1.cliux.loader("done");
|
|
133
|
+
if (stacks.length === 0) {
|
|
134
|
+
// change this to stacks not found
|
|
135
|
+
throw new Error(messages_1.default.APPS_NOT_FOUND);
|
|
136
|
+
}
|
|
137
|
+
const selectedStack = await cli_utilities_1.cliux
|
|
138
|
+
.inquire({
|
|
139
|
+
type: "search-list",
|
|
140
|
+
name: "Stack",
|
|
141
|
+
choices: stacks,
|
|
142
|
+
message: messages_1.default.CHOOSE_A_STACK
|
|
143
|
+
})
|
|
144
|
+
.then((name) => stacks.find(stack => stack.name === name));
|
|
145
|
+
return selectedStack;
|
|
146
|
+
}
|
|
147
|
+
exports.getStack = getStack;
|
|
148
|
+
async function getInstallation(flags, orgUid, managementSdkForStacks, appType, options) {
|
|
149
|
+
var _a;
|
|
150
|
+
const { log } = options;
|
|
151
|
+
if (appType === 'stack') {
|
|
152
|
+
cli_utilities_1.cliux.loader("Loading App Installations");
|
|
153
|
+
}
|
|
154
|
+
let { items: installations } = (await (0, common_utils_1.fetchAppInstallations)(flags, orgUid, options)) || [];
|
|
155
|
+
// console.log(installations)
|
|
156
|
+
if (!(installations === null || installations === void 0 ? void 0 : installations.length)) {
|
|
157
|
+
if (appType === "stack")
|
|
158
|
+
cli_utilities_1.cliux.loader("done");
|
|
159
|
+
throw new Error(messages_1.default.NO_INSTALLATIONS_FOUND);
|
|
160
|
+
}
|
|
161
|
+
let selectedInstallation;
|
|
162
|
+
if (appType === 'stack') {
|
|
163
|
+
// fetch stacks from where the app has to be uninstalled
|
|
164
|
+
cli_utilities_1.cliux.loader("done");
|
|
165
|
+
const stacks = await (0, common_utils_1.getStacks)({ managementSdk: managementSdkForStacks, log: options.log }, orgUid);
|
|
166
|
+
installations = populateMissingDataInInstallations(installations, stacks);
|
|
167
|
+
selectedInstallation = await cli_utilities_1.cliux
|
|
168
|
+
.inquire({
|
|
169
|
+
type: 'search-list',
|
|
170
|
+
name: 'appInstallation',
|
|
171
|
+
choices: installations,
|
|
172
|
+
message: messages_1.default.CHOOSE_AN_INSTALLATION
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
// as this is an organization app, and it is supposed to only be installed on the source organization
|
|
177
|
+
// it will be uninstalled from the selected organization
|
|
178
|
+
selectedInstallation = ((_a = installations.pop()) === null || _a === void 0 ? void 0 : _a.uid) || "";
|
|
179
|
+
}
|
|
180
|
+
log((0, messages_1.$t)(messages_1.uninstallAppMsg.UNINSTALLING_APP, {
|
|
181
|
+
type: appType
|
|
182
|
+
}), "info");
|
|
183
|
+
return selectedInstallation;
|
|
184
|
+
}
|
|
185
|
+
exports.getInstallation = getInstallation;
|
|
186
|
+
function populateMissingDataInInstallations(installations, stacks) {
|
|
187
|
+
let result = installations.map(installation => {
|
|
188
|
+
var _a;
|
|
189
|
+
let stack = (_a = stacks.filter(stack => stack.api_key === installation.target.uid)) === null || _a === void 0 ? void 0 : _a.pop();
|
|
190
|
+
installation.name = (stack === null || stack === void 0 ? void 0 : stack.name) || installation.target.uid;
|
|
191
|
+
installation.value = installation.uid;
|
|
192
|
+
return installation;
|
|
193
|
+
});
|
|
194
|
+
if (result.length > 0) {
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
return installations;
|
|
198
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
import { PrintOptions } from "@contentstack/cli-utilities";
|
|
3
|
+
import { LoggerType, PrintType } from "../types";
|
|
4
|
+
export default class Logger {
|
|
5
|
+
private infoLogger;
|
|
6
|
+
private errorLogger;
|
|
7
|
+
private config;
|
|
8
|
+
get loggerOptions(): winston.transports.FileTransportOptions;
|
|
9
|
+
constructor(config: Record<string, any>);
|
|
10
|
+
/**
|
|
11
|
+
* @method getLoggerInstance - init/generate new winston logger instance
|
|
12
|
+
*
|
|
13
|
+
* @param {LoggerType} logType
|
|
14
|
+
* @return {*} {winston.Logger}
|
|
15
|
+
* @memberof Logger
|
|
16
|
+
*/
|
|
17
|
+
getLoggerInstance(logType: LoggerType): winston.Logger;
|
|
18
|
+
/**
|
|
19
|
+
* @method log - log/print message with log type (error, info, warn)
|
|
20
|
+
*
|
|
21
|
+
* @param {(string | any)} message
|
|
22
|
+
* @param {(LoggerType | PrintOptions | undefined)} [logType]
|
|
23
|
+
* @memberof Logger
|
|
24
|
+
*/
|
|
25
|
+
log(message: string | any, logType?: LoggerType | PrintOptions | undefined): void;
|
|
26
|
+
/**
|
|
27
|
+
* @method returnString - formate error and return as string without any credentials
|
|
28
|
+
*
|
|
29
|
+
* @param {*} message
|
|
30
|
+
* @return {*} {string}
|
|
31
|
+
* @memberof Logger
|
|
32
|
+
*/
|
|
33
|
+
returnString(message: any): string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @method print - print message on UI
|
|
37
|
+
*
|
|
38
|
+
* @export print
|
|
39
|
+
* @param {Array<PrintType>} printInput
|
|
40
|
+
*/
|
|
41
|
+
export declare function print(printInput: Array<PrintType>): void;
|
package/lib/util/log.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.print = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const map_1 = tslib_1.__importDefault(require("lodash/map"));
|
|
6
|
+
const winston_1 = tslib_1.__importDefault(require("winston"));
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
9
|
+
const replace_1 = tslib_1.__importDefault(require("lodash/replace"));
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const isObject_1 = tslib_1.__importDefault(require("lodash/isObject"));
|
|
12
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
13
|
+
const ansiRegexPattern = [
|
|
14
|
+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
15
|
+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
|
|
16
|
+
].join("|");
|
|
17
|
+
const customLevels = {
|
|
18
|
+
levels: {
|
|
19
|
+
warn: 1,
|
|
20
|
+
info: 2,
|
|
21
|
+
debug: 3,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
class Logger {
|
|
25
|
+
get loggerOptions() {
|
|
26
|
+
return {
|
|
27
|
+
filename: "",
|
|
28
|
+
maxFiles: 20,
|
|
29
|
+
tailable: true,
|
|
30
|
+
maxsize: 1000000,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
constructor(config) {
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.infoLogger = this.getLoggerInstance("info");
|
|
36
|
+
this.errorLogger = this.getLoggerInstance("error");
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @method getLoggerInstance - init/generate new winston logger instance
|
|
40
|
+
*
|
|
41
|
+
* @param {LoggerType} logType
|
|
42
|
+
* @return {*} {winston.Logger}
|
|
43
|
+
* @memberof Logger
|
|
44
|
+
*/
|
|
45
|
+
getLoggerInstance(logType) {
|
|
46
|
+
const consoleOptions = {
|
|
47
|
+
format: winston_1.default.format.combine(winston_1.default.format.simple(), winston_1.default.format.colorize({ all: true })),
|
|
48
|
+
};
|
|
49
|
+
if (logType === "error") {
|
|
50
|
+
consoleOptions.level = logType;
|
|
51
|
+
}
|
|
52
|
+
if ((0, fs_1.existsSync)(this.config.projectBasePath)) {
|
|
53
|
+
const filename = (0, path_1.normalize)((0, path_1.join)(this.config.projectBasePath, "logs", `${logType}.log`)).replace(/^(\.\.(\/|\\|$))+/, "");
|
|
54
|
+
const loggerOptions = {
|
|
55
|
+
transports: [
|
|
56
|
+
new winston_1.default.transports.File(Object.assign(Object.assign({}, this.loggerOptions), { level: logType, filename })),
|
|
57
|
+
new winston_1.default.transports.Console(consoleOptions),
|
|
58
|
+
],
|
|
59
|
+
levels: customLevels.levels,
|
|
60
|
+
};
|
|
61
|
+
if (logType === "error") {
|
|
62
|
+
loggerOptions.levels = { error: 0 };
|
|
63
|
+
}
|
|
64
|
+
return winston_1.default.createLogger(loggerOptions);
|
|
65
|
+
}
|
|
66
|
+
winston_1.default
|
|
67
|
+
.createLogger({
|
|
68
|
+
transports: [new winston_1.default.transports.Console(consoleOptions)],
|
|
69
|
+
})
|
|
70
|
+
.error("Provided base path is not valid");
|
|
71
|
+
throw new Error("Provided base path is not valid");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @method log - log/print message with log type (error, info, warn)
|
|
75
|
+
*
|
|
76
|
+
* @param {(string | any)} message
|
|
77
|
+
* @param {(LoggerType | PrintOptions | undefined)} [logType]
|
|
78
|
+
* @memberof Logger
|
|
79
|
+
*/
|
|
80
|
+
log(message, logType) {
|
|
81
|
+
const logString = this.returnString(message);
|
|
82
|
+
switch (logType) {
|
|
83
|
+
case "info":
|
|
84
|
+
case "debug":
|
|
85
|
+
case "warn":
|
|
86
|
+
this.infoLogger.log(logType, logString);
|
|
87
|
+
break;
|
|
88
|
+
case "error":
|
|
89
|
+
this.errorLogger.error(logString);
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
cli_utilities_1.cliux.print(logString, logType || {});
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @method returnString - formate error and return as string without any credentials
|
|
98
|
+
*
|
|
99
|
+
* @param {*} message
|
|
100
|
+
* @return {*} {string}
|
|
101
|
+
* @memberof Logger
|
|
102
|
+
*/
|
|
103
|
+
returnString(message) {
|
|
104
|
+
let returnStr = "";
|
|
105
|
+
const replaceCredentials = (item) => {
|
|
106
|
+
try {
|
|
107
|
+
return JSON.stringify(item).replace(/authtoken":"blt................/g, 'authtoken":"blt....');
|
|
108
|
+
}
|
|
109
|
+
catch (error) { }
|
|
110
|
+
return item;
|
|
111
|
+
};
|
|
112
|
+
if (Array.isArray(message) && message.length) {
|
|
113
|
+
returnStr = (0, map_1.default)(message, (item) => {
|
|
114
|
+
if (item && typeof item === "object") {
|
|
115
|
+
return replaceCredentials(item);
|
|
116
|
+
}
|
|
117
|
+
return item;
|
|
118
|
+
})
|
|
119
|
+
.join(" ")
|
|
120
|
+
.trim();
|
|
121
|
+
}
|
|
122
|
+
else if ((0, isObject_1.default)(message)) {
|
|
123
|
+
return replaceCredentials(message);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
returnStr = message;
|
|
127
|
+
}
|
|
128
|
+
returnStr = (0, replace_1.default)(returnStr, new RegExp(ansiRegexPattern, "g"), "").trim();
|
|
129
|
+
return returnStr;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.default = Logger;
|
|
133
|
+
/**
|
|
134
|
+
* @method print - print message on UI
|
|
135
|
+
*
|
|
136
|
+
* @export print
|
|
137
|
+
* @param {Array<PrintType>} printInput
|
|
138
|
+
*/
|
|
139
|
+
function print(printInput) {
|
|
140
|
+
const str = (0, map_1.default)(printInput, ({ message, bold, color }) => {
|
|
141
|
+
let chalkFn = chalk_1.default;
|
|
142
|
+
if (color)
|
|
143
|
+
chalkFn = chalkFn[color];
|
|
144
|
+
if (bold)
|
|
145
|
+
chalkFn = chalkFn.bold;
|
|
146
|
+
return chalkFn(message);
|
|
147
|
+
}).join(" ");
|
|
148
|
+
cli_utilities_1.cliux.print(str);
|
|
149
|
+
}
|
|
150
|
+
exports.print = print;
|