Skip to content

Commit 631d3cf

Browse files
authored
Merge pull request #188 from contentstack/development
Merge development to staging
2 parents 3e362c8 + 97215a1 commit 631d3cf

File tree

6 files changed

+256
-150
lines changed

6 files changed

+256
-150
lines changed

src/commands/app/create.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as tmp from "tmp";
22
import AdmZip from "adm-zip";
3+
import omit from "lodash/omit";
34
import pick from "lodash/pick";
45
import * as shell from "shelljs";
56
import merge from "lodash/merge";
@@ -14,7 +15,13 @@ import {
1415
writeFileSync,
1516
createWriteStream,
1617
} from "fs";
17-
import { ux, cliux, flags, HttpClient, configHandler } from "@contentstack/cli-utilities";
18+
import {
19+
ux,
20+
cliux,
21+
flags,
22+
HttpClient,
23+
configHandler,
24+
} from "@contentstack/cli-utilities";
1825

1926
import { BaseCommand } from "../../base-command";
2027
import { AppManifest, AppType } from "../../types";
@@ -28,6 +35,11 @@ import {
2835

2936
export default class Create extends BaseCommand<typeof Create> {
3037
private appData!: AppManifest;
38+
private tempAppData = {
39+
name: "",
40+
target_type: "",
41+
ui_location: { locations: undefined },
42+
} as any;
3143

3244
static description =
3345
"Create a new app in Developer Hub and optionally clone a boilerplate locally.";
@@ -62,15 +74,14 @@ export default class Create extends BaseCommand<typeof Create> {
6274
async run(): Promise<void> {
6375
this.sharedConfig.org = this.flags.org;
6476
this.sharedConfig.appName = this.flags.name;
65-
this.appData = require(this.sharedConfig.manifestPath);
6677

6778
await this.flagsPromptQueue();
6879

69-
this.appData.name = this.sharedConfig.appName;
70-
this.appData.target_type = this.flags["app-type"] as AppType;
80+
this.tempAppData.name = this.sharedConfig.appName;
81+
this.tempAppData.target_type = this.flags["app-type"] as AppType;
7182

7283
if (this.flags["app-type"] === AppType.ORGANIZATION) {
73-
this.appData.ui_location.locations = getOrgAppUiLocation();
84+
this.tempAppData.ui_location.locations = getOrgAppUiLocation();
7485
}
7586

7687
try {
@@ -84,6 +95,7 @@ export default class Create extends BaseCommand<typeof Create> {
8495
) {
8596
await this.boilerplateFlow();
8697
} else {
98+
this.manageManifestToggeling();
8799
await this.registerTheAppOnDeveloperHub(false);
88100
}
89101
} catch (error: Error | any) {
@@ -105,6 +117,8 @@ export default class Create extends BaseCommand<typeof Create> {
105117
await this.unZipBoilerplate(await this.cloneBoilerplate());
106118
tmp.setGracefulCleanup(); // NOTE If graceful cleanup is set, tmp will remove all controlled temporary objects on process exit
107119

120+
this.manageManifestToggeling();
121+
108122
// NOTE Step 2: Registering the app
109123
await this.registerTheAppOnDeveloperHub();
110124

@@ -114,7 +128,7 @@ export default class Create extends BaseCommand<typeof Create> {
114128
ux.action.stop();
115129
this.log(
116130
this.$t(this.messages.START_APP_COMMAND, {
117-
command: `cd ${this.sharedConfig.folderPath} && npm run start`,
131+
command: `cd "${this.sharedConfig.folderPath}" && npm run start`,
118132
}),
119133
"info"
120134
);
@@ -133,10 +147,12 @@ export default class Create extends BaseCommand<typeof Create> {
133147
}
134148

135149
//Auto select org in case of oauth
136-
this.sharedConfig.org = configHandler.get('oauthOrgUid') ?? (await getOrg(this.flags, {
137-
log: this.log,
138-
managementSdk: this.managementSdk,
139-
}));
150+
this.sharedConfig.org =
151+
configHandler.get("oauthOrgUid") ??
152+
(await getOrg(this.flags, {
153+
log: this.log,
154+
managementSdk: this.managementSdk,
155+
}));
140156
}
141157

142158
/**
@@ -208,6 +224,34 @@ export default class Create extends BaseCommand<typeof Create> {
208224
});
209225
}
210226

227+
/**
228+
* @method manageManifestToggeling
229+
*
230+
* The function manages toggling of the manifest file based on the app type, removing the
231+
* "ui_location" property if the app type is an organization.
232+
*/
233+
manageManifestToggeling() {
234+
// NOTE Use boilerplate manifest if exist
235+
const manifestPath = resolve(
236+
this.sharedConfig.folderPath || "",
237+
"manifest.json"
238+
);
239+
240+
if (existsSync(manifestPath)) {
241+
this.sharedConfig.manifestPath = manifestPath;
242+
}
243+
244+
let manifest = require(this.sharedConfig.manifestPath);
245+
246+
if (this.flags["app-type"] === AppType.ORGANIZATION) {
247+
manifest = omit(manifest, ["ui_location"]);
248+
} else {
249+
this.tempAppData = omit(this.tempAppData, ["ui_location"]);
250+
}
251+
252+
this.appData = merge(manifest, this.tempAppData);
253+
}
254+
211255
/**
212256
* @method registerTheAppOnDeveloperHub
213257
*

src/commands/app/update.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs";
77

88
import { $t, appUpdate } from "../../messages";
99
import { fetchApp, getApp, getOrg } from "../../util";
10-
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
10+
import { AppCLIBaseCommand } from "../../app-cli-base-coomand";
1111

1212
export default class Update extends AppCLIBaseCommand {
1313
private orgUid!: string;
@@ -28,12 +28,14 @@ export default class Update extends AppCLIBaseCommand {
2828

2929
async run(): Promise<void> {
3030
try {
31-
//if working directory isn't app directory
32-
if(!this.manifestData){
31+
// if working directory isn't app directory
32+
if (!this.manifestData) {
3333
await this.validateManifest();
3434
}
35-
this.flags["app-manifest"] = this.manifestPath ?? this.flags["app-manifest"];
36-
this.orgUid = this.flags.org ?? this.manifestData?.organization_uid;
35+
36+
this.flags["app-manifest"] =
37+
this.manifestPath ?? this.flags["app-manifest"];
38+
this.orgUid = this.flags.org ?? this.manifestData?.organization_uid;
3739
this.sharedConfig.org = await getOrg(
3840
{ org: this.orgUid as any },
3941
{

src/config/manifest.json

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2-
"icon": "",
32
"description": "",
4-
"target_type": "stack",
3+
"icon": "",
54
"name": "",
6-
"visibility": "private",
75
"organization_uid": "",
6+
"target_type": "stack",
7+
"visibility": "private",
8+
"uid": "",
89
"ui_location": {
910
"signed": false,
1011
"base_url": "http://localhost:3000",
@@ -13,20 +14,22 @@
1314
"type": "cs.cm.stack.custom_field",
1415
"meta": [
1516
{
16-
"path": "/custom-field",
17-
"signed": true,
17+
"multiple": false,
18+
"path": "/#/custom-field",
19+
"signed": false,
1820
"enabled": true,
19-
"data_type": "number"
21+
"data_type": "json"
2022
}
2123
]
2224
},
2325
{
24-
"type": "cs.cm.stack.config",
26+
"type": "cs.cm.stack.dashboard",
2527
"meta": [
2628
{
27-
"path": "/app-configuration",
28-
"signed": true,
29-
"enabled": true
29+
"path": "/#/stack-dashboard",
30+
"signed": false,
31+
"enabled": true,
32+
"default_width": "half"
3033
}
3134
]
3235
},
@@ -35,35 +38,68 @@
3538
"meta": [
3639
{
3740
"blur": false,
38-
"path": "/asset-sidebar",
39-
"signed": true,
41+
"path": "/#/asset-sidebar",
42+
"signed": false,
4043
"enabled": true,
4144
"width": 500
4245
}
4346
]
4447
},
4548
{
46-
"type": "cs.cm.stack.dashboard",
49+
"type": "cs.cm.stack.sidebar",
4750
"meta": [
4851
{
49-
"path": "/stack-dashboard",
50-
"signed": true,
52+
"path": "/#/entry-sidebar",
53+
"signed": false,
54+
"enabled": true
55+
}
56+
]
57+
},
58+
{
59+
"type": "cs.cm.stack.full_page",
60+
"meta": [
61+
{
62+
"path": "/#/full-page",
63+
"signed": false,
64+
"enabled": true
65+
}
66+
]
67+
},
68+
{
69+
"type": "cs.cm.stack.field_modifier",
70+
"meta": [
71+
{
72+
"path": "/#/field-modifier",
73+
"signed": false,
5174
"enabled": true,
52-
"default_width": "half"
75+
"allowed_types": ["$all"]
5376
}
5477
]
5578
},
5679
{
57-
"type": "cs.cm.stack.sidebar",
80+
"type": "cs.cm.stack.config",
81+
"meta": [
82+
{
83+
"path": "/#/app-configuration",
84+
"signed": false,
85+
"enabled": true
86+
}
87+
]
88+
},
89+
{
90+
"type": "cs.cm.stack.rte",
5891
"meta": [
5992
{
60-
"path": "/entry-sidebar",
61-
"signed": true,
93+
"path": "/json-rte.js",
94+
"signed": false,
6295
"enabled": true
6396
}
6497
]
6598
}
6699
]
67100
},
68-
"uid": ""
101+
"hosting": {
102+
"provider": "external",
103+
"deployment_url": "http://localhost:3000"
104+
}
69105
}

src/util/common-utils.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ function fetchAppInstallations(
9797
const { managementSdk } = options;
9898
const app: any = flags["app-uid"];
9999
return managementSdk
100-
.organization(orgUid)
101-
.app(app as string)
102-
.installation()
103-
.findAll()
104-
.catch(error => {
105-
const {log} = options;
106-
cliux.loader("failed");
107-
log("Some error occurred while fetching app installations.", "warn");
108-
throw error // throwing error here instead of removing the catch block, as the loader needs to stopped in case there is an error.
109-
})
100+
.organization(orgUid)
101+
.app(app as string)
102+
.installation()
103+
.findAll()
104+
.catch((error) => {
105+
const { log } = options;
106+
cliux.loader("failed");
107+
log("Some error occurred while fetching app installations.", "warn");
108+
throw error; // throwing error here instead of removing the catch block, as the loader needs to stopped in case there is an error.
109+
});
110110
}
111111

112112
function deleteApp(flags: FlagInput, orgUid: string, options: CommonOptions) {
@@ -144,10 +144,10 @@ function fetchStack(flags: FlagInput, options: CommonOptions) {
144144
async function getStacks(
145145
options: CommonOptions,
146146
orgUid: string,
147-
skip: number= 0,
148-
stacks: Stack[] = [],
147+
skip: number = 0,
148+
stacks: Stack[] = []
149149
): Promise<Stack[]> {
150-
const {log, managementSdk} = options;
150+
const { log, managementSdk } = options;
151151
const response = await managementSdk
152152
.organization(orgUid)
153153
.stacks({ include_count: true, limit: 100, asc: "name", skip: skip })
@@ -168,17 +168,26 @@ async function getStacks(
168168
return stacks;
169169
}
170170

171-
function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions, installationUid: string) {
172-
const {managementSdk} = options;
173-
const app: unknown = flags['app-uid'];
171+
function uninstallApp(
172+
flags: FlagInput,
173+
orgUid: string,
174+
options: CommonOptions,
175+
installationUid: string
176+
) {
177+
const { managementSdk } = options;
178+
const app: unknown = flags["app-uid"];
174179
return managementSdk
175-
.organization(orgUid)
176-
.app(app as string)
177-
.installation(installationUid as string)
178-
.uninstall()
180+
.organization(orgUid)
181+
.app(app as string)
182+
.installation(installationUid as string)
183+
.uninstall();
179184
}
180185

181-
async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) {
186+
async function fetchInstalledApps(
187+
flags: FlagInput,
188+
orgUid: string,
189+
options: CommonOptions
190+
) {
182191
const { managementSdk, log } = options;
183192
const apps = (await fetchApps(flags, orgUid, options)) || [];
184193
let batchRequests = [];
@@ -196,8 +205,7 @@ async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: Com
196205
.installation()
197206
.findAll();
198207
return installations.items.length ? installations.items : null;
199-
}
200-
catch (error) {
208+
} catch (error) {
201209
log("Unable to fetch installations.", "warn");
202210
log(error, "error");
203211
throw error;
@@ -217,10 +225,10 @@ async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: Com
217225
return batchRequests.flat();
218226
}
219227

220-
export {
221-
getOrganizations,
222-
getOrgAppUiLocation,
223-
fetchApps,
228+
export {
229+
getOrganizations,
230+
getOrgAppUiLocation,
231+
fetchApps,
224232
fetchApp,
225233
fetchAppInstallations,
226234
deleteApp,

0 commit comments

Comments
 (0)