Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid boilerplate error #278

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ USAGE
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> Choose a boilerplate from search list
--boilerplate=<value> Provide a boilerplate from the following options: App Boilerplate, DAM App Boilerplate or
Ecommerce App Boilerplate

DESCRIPTION
Create a new app in Developer Hub and optionally clone a boilerplate locally.
Expand All @@ -98,7 +99,11 @@ EXAMPLES

$ 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> --boilerplates <boilerplate-name>
$ 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.3.0/src/commands/app/create.ts)_
Expand Down
43 changes: 25 additions & 18 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
sanitizePath,
selectedBoilerplate,
validateBoilerplate,
validateAppName,
} from "../../util";

export default class Create extends BaseCommand<typeof Create> {
Expand All @@ -53,13 +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> --boilerplates <boilerplate-name>",
"$ <%= 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 @@ -75,7 +77,7 @@ export default class Create extends BaseCommand<typeof Create> {
char: "d",
description: commonMsg.CURRENT_WORKING_DIR,
}),
"boilerplate": flags.string({
boilerplate: flags.string({
description: appCreate.BOILERPLATE_TEMPLATES,
}),
};
Expand Down Expand Up @@ -150,27 +152,32 @@ 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)) {
const boilerplate: BoilerplateAppType = await selectedBoilerplate();
boilerplate = await selectedBoilerplate();
} else {
boilerplate = (await validateBoilerplate(
this.sharedConfig.boilerplateName
)) as BoilerplateAppType;
}

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

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

//Auto select org in case of oauth
this.sharedConfig.org =
Expand Down
2 changes: 1 addition & 1 deletion src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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: "Choose a boilerplate from search list"
BOILERPLATE_TEMPLATES: "Provide a boilerplate from the following options: App Boilerplate, DAM App Boilerplate or Ecommerce App Boilerplate"
};

const getAppMsg = {
Expand Down
15 changes: 9 additions & 6 deletions src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,19 @@ async function fetchBoilerplateDetails(): Promise<Record<string, any>[]> {
throw error;
}
}
async function validateBoilerplate(boilerplateName: string): Promise<void> {

async function validateBoilerplate(boilerplateName: string) {
const boilerplates = await fetchBoilerplateDetails();
const isValid = find(
const boilerplate = find(
boilerplates,
(boilerplate) => boilerplate.name.toLowerCase()
.replace(/ /g, "-") === boilerplateName
(boilerplate) => boilerplate.name === boilerplateName
);
if (!isValid) {
throw new Error("Invalid boilerplate. Please enter a valid boilerplate.");
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 {
Expand Down
10 changes: 7 additions & 3 deletions src/util/inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,7 @@ async function askConfirmation(): Promise<boolean> {
});
}

const askProjectName = async (
projectName: string,
): Promise<string> => {
const askProjectName = async (projectName: string): Promise<string> => {
return await cliux.inquire({
type: "input",
name: "name",
Expand Down Expand Up @@ -402,6 +400,11 @@ const selectedBoilerplate = async (): Promise<any> => {
});
};

const validateAppName = (name: string) => {
if (name.length < 3 || name.length > 20) {
throw new Error($t(errors.INVALID_NAME, { min: "3", max: "20" }));
}
};
export {
getOrg,
getAppName,
Expand All @@ -418,4 +421,5 @@ export {
selectProject,
askProjectName,
selectedBoilerplate,
validateAppName,
};
Loading