Skip to content

Commit

Permalink
Merge pull request #280 from contentstack/staging
Browse files Browse the repository at this point in the history
DX | 31-07-2024 | Release
  • Loading branch information
cs-raj authored Jul 31, 2024
2 parents 0b4ab90 + f72cb75 commit abd9a0d
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 33 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $ npm install -g @contentstack/apps-cli
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/apps-cli/1.2.1 darwin-arm64 node-v18.12.1
@contentstack/apps-cli/1.3.0 darwin-arm64 node-v18.16.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -67,22 +67,24 @@ EXAMPLES
$ csdx app:reinstall
```

_See code: [src/commands/app/index.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/index.ts)_
_See code: [src/commands/app/index.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/index.ts)_

## `csdx app:create`

Create a new app in Developer Hub and optionally clone a boilerplate locally.

```
USAGE
$ csdx app:create [-n <value>] [--app-type stack|organization] [-c <value>] [-d <value>]
$ csdx app:create [-n <value>] [--app-type stack|organization] [-c <value>] [-d <value>] [--boilerplate
<value>]
FLAGS
-c, --config=<value> Path of the external config
-d, --data-dir=<value> Current working directory.
-n, --name=<value> [default: app-boilerplate] Name of the app to be created
-n, --name=<value> Name of the app to be created
--app-type=<option> [default: stack] Type of app
<options: stack|organization>
--boilerplate=<value> Provide a boilerplate. <options: App Boilerplate|DAM App Boilerplate|Ecommerce App Boilerplate>
DESCRIPTION
Create a new app in Developer Hub and optionally clone a boilerplate locally.
Expand All @@ -95,9 +97,15 @@ EXAMPLES
$ csdx app:create --name App-2 --app-type stack -d ./boilerplate
$ csdx app:create --name App-3 --app-type organization --org <UID> -d ./boilerplate -c ./external-config.json
$ csdx app:create --name App-4 --app-type organization --org <UID> --boilerplate <App Boilerplate>
$ csdx app:create --name App-4 --app-type organization --org <UID> --boilerplate <DAM App Boilerplate>
$ csdx app:create --name App-4 --app-type organization --org <UID> --boilerplate <Ecommerce App Boilerplate>
```

_See code: [src/commands/app/create.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/create.ts)_
_See code: [src/commands/app/create.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/create.ts)_

## `csdx app:delete`

Expand All @@ -121,7 +129,7 @@ EXAMPLES
$ csdx app:delete --app-uid <value> --org <value> -d ./boilerplate
```

_See code: [src/commands/app/delete.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/delete.ts)_
_See code: [src/commands/app/delete.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/delete.ts)_

## `csdx app:deploy`

Expand Down Expand Up @@ -158,7 +166,7 @@ EXAMPLES
$ csdx app:deploy --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <new> --config <config-path>
```

_See code: [src/commands/app/deploy.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/deploy.ts)_
_See code: [src/commands/app/deploy.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/deploy.ts)_

## `csdx app:get`

Expand Down Expand Up @@ -187,7 +195,7 @@ EXAMPLES
$ csdx app:get --org <value> --app-uid <value> --app-type organization
```

_See code: [src/commands/app/get.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/get.ts)_
_See code: [src/commands/app/get.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/get.ts)_

## `csdx app:install`

Expand All @@ -212,7 +220,7 @@ EXAMPLES
$ csdx app:install --org <UID> --app-uid <APP-UID-1> --stack-api-key <STACK-API-KEY-1>
```

_See code: [src/commands/app/install.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/install.ts)_
_See code: [src/commands/app/install.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/install.ts)_

## `csdx app:reinstall`

Expand All @@ -237,7 +245,7 @@ EXAMPLES
$ csdx app:reinstall --org <UID> --app-uid <APP-UID-1> --stack-api-key <STACK-API-KEY-1>
```

_See code: [src/commands/app/reinstall.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/reinstall.ts)_
_See code: [src/commands/app/reinstall.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/reinstall.ts)_

## `csdx app:uninstall`

Expand All @@ -263,7 +271,7 @@ EXAMPLES
$ csdx app:uninstall --org <UID> --app-uid <APP-UID-1> --installation-uid <INSTALLATION-UID-1>
```

_See code: [src/commands/app/uninstall.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/uninstall.ts)_
_See code: [src/commands/app/uninstall.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/uninstall.ts)_

## `csdx app:update`

Expand All @@ -285,5 +293,5 @@ EXAMPLES
$ csdx app:update --app-manifest ./boilerplate/manifest.json
```

_See code: [src/commands/app/update.ts](https://github.com/contentstack/apps-cli/blob/v1.2.1/src/commands/app/update.ts)_
_See code: [src/commands/app/update.ts](https://github.com/contentstack/apps-cli/blob/v1.3.0/src/commands/app/update.ts)_
<!-- commandsstop -->
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/apps-cli",
"version": "1.2.1",
"version": "1.3.0",
"description": "App ClI",
"author": "Contentstack CLI",
"homepage": "https://github.com/contentstack/contentstack-apps-cli",
Expand Down
57 changes: 44 additions & 13 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import {
flags,
HttpClient,
configHandler,
FlagInput
FlagInput,
} from "@contentstack/cli-utilities";

import { BaseCommand } from "../../base-command";
import { AppManifest, AppType } from "../../types";
import { AppManifest, AppType, BoilerplateAppType } from "../../types";
import { appCreate, commonMsg } from "../../messages";
import {
getOrg,
getAppName,
getDirName,
getOrgAppUiLocation,
sanitizePath
sanitizePath,
selectedBoilerplate,
validateBoilerplate,
validateAppName,
} from "../../util";

export default class Create extends BaseCommand<typeof Create> {
Expand All @@ -51,12 +54,14 @@ export default class Create extends BaseCommand<typeof Create> {
"$ <%= config.bin %> <%= command.id %> --name App-1 --app-type stack",
"$ <%= config.bin %> <%= command.id %> --name App-2 --app-type stack -d ./boilerplate",
"$ <%= config.bin %> <%= command.id %> --name App-3 --app-type organization --org <UID> -d ./boilerplate -c ./external-config.json",
"$ <%= config.bin %> <%= command.id %> --name App-4 --app-type organization --org <UID> --boilerplate <App Boilerplate>",
"$ <%= config.bin %> <%= command.id %> --name App-4 --app-type organization --org <UID> --boilerplate <DAM App Boilerplate>",
"$ <%= config.bin %> <%= command.id %> --name App-4 --app-type organization --org <UID> --boilerplate <Ecommerce App Boilerplate>",
];

static flags: FlagInput = {
name: flags.string({
char: "n",
default: "app-boilerplate",
description: appCreate.NAME_DESCRIPTION,
}),
"app-type": flags.string({
Expand All @@ -72,11 +77,15 @@ export default class Create extends BaseCommand<typeof Create> {
char: "d",
description: commonMsg.CURRENT_WORKING_DIR,
}),
boilerplate: flags.string({
description: appCreate.BOILERPLATE_TEMPLATES,
}),
};

async run(): Promise<void> {
this.sharedConfig.org = this.flags.org;
this.sharedConfig.appName = this.flags.name;
this.sharedConfig.boilerplateName = this.flags.boilerplate;

await this.flagsPromptQueue();

Expand Down Expand Up @@ -143,10 +152,31 @@ export default class Create extends BaseCommand<typeof Create> {
* @memberof Create
*/
async flagsPromptQueue() {
if (isEmpty(this.sharedConfig.appName)) {
this.sharedConfig.appName = await getAppName(
this.sharedConfig.defaultAppName
);
if (this.sharedConfig.appName) {
validateAppName(this.sharedConfig.appName);
}

let boilerplate: BoilerplateAppType | null = null;
if (isEmpty(this.sharedConfig.boilerplateName)) {
boilerplate = await selectedBoilerplate();
} else {
boilerplate = (await validateBoilerplate(
this.sharedConfig.boilerplateName
)) as BoilerplateAppType;
}

if (boilerplate) {
let boilerplateName = this.sharedConfig.appName || boilerplate.name;
if (isEmpty(this.sharedConfig.appName)) {
boilerplateName = boilerplateName
.toLowerCase()
.replace(/ /g, "-")
.substring(0, 20);
}

this.sharedConfig.boilerplateName = boilerplateName;
this.sharedConfig.appBoilerplateGithubUrl = boilerplate.link;
this.sharedConfig.appName = boilerplateName;
}

//Auto select org in case of oauth
Expand Down Expand Up @@ -198,7 +228,11 @@ export default class Create extends BaseCommand<typeof Create> {
const zip = new AdmZip(filepath);
const dataDir = this.flags["data-dir"] ?? process.cwd();
let targetPath = resolve(dataDir, this.sharedConfig.appName);
const sourcePath = resolve(dataDir, this.sharedConfig.boilerplateName);

// Get the directory inside the zip file
const zipEntries = zip.getEntries();
const firstEntry = zipEntries[0];
const sourcePath = resolve(sanitizePath(dataDir), sanitizePath(firstEntry.entryName.split("/")[0]));

if (this.flags["data-dir"] && !existsSync(this.flags["data-dir"])) {
mkdirSync(this.flags["data-dir"], { recursive: true });
Expand Down Expand Up @@ -235,10 +269,7 @@ export default class Create extends BaseCommand<typeof Create> {
*/
manageManifestToggeling() {
// NOTE Use boilerplate manifest if exist
const manifestPath = resolve(
this.sharedConfig.folderPath || "",
"manifest.json"
);
const manifestPath = resolve(this.sharedConfig.folderPath, "manifest.json");

if (existsSync(manifestPath)) {
this.sharedConfig.manifestPath = manifestPath;
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { resolve } from "path";
const config = {
defaultAppName: "app-boilerplate",
manifestPath: resolve(__dirname, "manifest.json"),
boilerplateName: "marketplace-app-boilerplate-main",
developerHubBaseUrl: "",
appBoilerplateGithubUrl:
"https://codeload.github.com/contentstack/marketplace-app-boilerplate/zip/refs/heads/main",
defaultAppFileName: "manifest",
boilerplatesUrl: 'https://marketplace-artifacts.contentstack.com/cli/starter-template.json'
};

export default config;
2 changes: 2 additions & 0 deletions src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const appCreate = {
REGISTER_THE_APP_ON_DEVELOPER_HUB:
"Registering the app with the name {appName} on the Developer Hub...",
START_APP_COMMAND: "Start the app using the following command: {command}",
BOILERPLATE_TEMPLATES: "Provide a boilerplate. <options: App Boilerplate|DAM App Boilerplate|Ecommerce App Boilerplate>",
SELECT_BOILERPLATE: "Select one from the following boilerplates:"
};

const getAppMsg = {
Expand Down
9 changes: 9 additions & 0 deletions src/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ export interface LaunchProjectRes {
environmentUid: any;
developerHubAppUid: any;
}

export interface BoilerplateAppType {
name: string;
description?: string;
link: string;
tags?: string[];
created_at?: string;
updated_at?: string;
}
27 changes: 27 additions & 0 deletions src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
cliux,
Stack,
FsUtility,
HttpClient,
} from "@contentstack/cli-utilities";
import { projectsQuery } from "../graphql/queries";
import { apiRequestHandler } from "./api-request-handler";
Expand All @@ -19,6 +20,8 @@ import {
} from "../types";
import { askProjectName } from "./inquirer";
import { deployAppMsg } from "../messages";
import config from "../config";
import find from "lodash/find";

export type CommonOptions = {
log: LogFn;
Expand Down Expand Up @@ -396,6 +399,28 @@ const handleProjectNameConflict = async (
}
return projectName;
};
async function fetchBoilerplateDetails(): Promise<Record<string, any>[]> {
try {
const content = await new HttpClient().get(config.boilerplatesUrl);
return content?.data?.templates ?? [];
} catch (error) {
throw error;
}
}

async function validateBoilerplate(boilerplateName: string) {
const boilerplates = await fetchBoilerplateDetails();
const boilerplate = find(
boilerplates,
(boilerplate) => boilerplate.name === boilerplateName
);
if (!boilerplate) {
throw new Error(
"Invalid boilerplate! Please select a boilerplate from the following options: App Boilerplate, DAM App Boilerplate or Ecommerce App Boilerplate"
);
}
return boilerplate;
}

export {
getOrganizations,
Expand All @@ -418,4 +443,6 @@ export {
disconnectApp,
formatUrl,
handleProjectNameConflict,
fetchBoilerplateDetails,
validateBoilerplate,
};
Loading

0 comments on commit abd9a0d

Please sign in to comment.