Skip to content

Commit

Permalink
fix select project by name (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
BagusAK95 authored Nov 11, 2020
1 parent 5e069a8 commit 81ff04c
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## [2.6.3] - 2020/11/11

- Fix select project by name

## [2.6.1] - 2020/08/25

- Fix when create deployment not set cmsId, nluId, botId
Expand Down
77 changes: 36 additions & 41 deletions components/projects/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ export default class Project {
) { }

public async create() {
let projectData: Record<string, any> = {name : ""};
let projectData: Record<string, any> = { name: "" };
while (true) {
projectData = await this.helper.inquirerPrompt([
{
type: "text",
name: "name",
message: "Project name:",
},
]);
if (projectData.name.length > 20) {
console.error("error: Project name length can not exceed 20 characters");
}
else if (!/^[A-Za-z][A-Za-z0-9_-]*[A-Za-z0-9]$/.test(projectData.name)) {
console.error("error: Project name must start with alphabet characters and contains only aplhanumeric character, dash, or underscore");
}
else {
break;
}
projectData = await this.helper.inquirerPrompt([
{
type: "text",
name: "name",
message: "Project name:",
},
]);
if (projectData.name.length > 20) {
console.error("error: Project name length can not exceed 20 characters");
}
else if (!/^[A-Za-z][A-Za-z0-9_-]*[A-Za-z0-9]$/.test(projectData.name)) {
console.error("error: Project name must start with alphabet characters and contains only aplhanumeric character, dash, or underscore");
}
else {
break;
}
}
const inquiredOptions = await this.helper.inquirerPrompt([
{
Expand Down Expand Up @@ -87,15 +87,15 @@ export default class Project {
}
}

public async update(projectName?:string) {
public async update(projectName?: string) {
let chosen = null

if (projectName) {
chosen = await this.getDataByName(projectName)
} else {
chosen = await this.choose()
}

if (chosen) {
const { description, privateNlu } = await this.helper.inquirerPrompt([
{
Expand All @@ -110,9 +110,9 @@ export default class Project {
default: true,
}
]);

const nluVisibility = privateNlu ? 'private' : 'public';

const requestBody = {
id: chosen.id,
name: chosen.name,
Expand All @@ -124,7 +124,7 @@ export default class Project {
nluId: chosen.options.nluId
}
}

const { response } = await this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsProjectIdPut, chosen.id, requestBody);
if (response && response.body) {
console.log(`Project ${chosen.name} has been updated.`)
Expand Down Expand Up @@ -156,26 +156,21 @@ export default class Project {
}

private async getDataByName(projectName: string) {
const { response } = await this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsGet, {});

if (response && response.body && response.body.data) {
const projects = response.body.data
const sameName = projects.find((project: any) => project.name === projectName);
if (sameName) {
return sameName
}
const { response } = await this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsGetProjectByNameGet, { name: projectName });
if (response && response.body && response.body.id) {
return response.body;
}
}

private async choose(){
private async choose() {
try {
let page = 1;
let page = 1;
const pageLimit = 10;
while (true) {
const { response } = await this.helper.toPromise(
this.api.projectApi,
this.api.projectApi.projectsGet,
{ limit: pageLimit, page}
{ limit: pageLimit, page }
);

if (response && response.body && response.body.data) {
Expand All @@ -191,14 +186,14 @@ export default class Project {
const body = response.body;

if (body.total > body.page * body.limit) {
choices.push({name: "(Load More)", value: -1});
choices.push({ name: "(Load More)", value: -1 });
}

const { project } = await inquirer.prompt<any>([
{
type: "list",
name: "project",
message: `Select project (page ${ page } / ${ maxPage })`,
message: `Select project (page ${page} / ${maxPage})`,
paginated: false,
pageSize: pageLimit + 1,
choices
Expand Down Expand Up @@ -246,18 +241,18 @@ export default class Project {
this.helper.setProp("projectName", chosen.name);
}
}
console.log(colors.green(`Project "${ chosen.name }" (${ chosen.id }) is successfully selected`));
console.log(colors.green(`Project "${chosen.name}" (${chosen.id}) is successfully selected`));

if (!chosen || chosen === undefined) {
chosen = await this.choose()
if (chosen) {
this.helper.setProp("projectId", chosen.id);
this.helper.setProp("projectName", chosen.name);
console.log(colors.green(`Project "${ chosen.name }" (${ chosen.id }) is successfully selected`));
console.log(colors.green(`Project "${chosen.name}" (${chosen.id}) is successfully selected`));
}
}


}

public async delete(projectName?: string) {
Expand All @@ -277,12 +272,12 @@ export default class Project {
const deleteProject = await this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsProjectIdDelete, chosen.id);
if (deleteProject && deleteProject.response && deleteProject.response.body) {
if (deleteProject.response.body) {
console.log("Project has been deleted.")
console.log("Project has been deleted.")
} else {
console.log("Failed when trying delete project")
}
}
}
}
}
} else {
console.log(`Project ${projectName} is not found`)
}
Expand Down
3 changes: 2 additions & 1 deletion lib/components/bots/bot.js

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

7 changes: 4 additions & 3 deletions lib/components/bots/channel.js

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

3 changes: 2 additions & 1 deletion lib/components/bots/deployment.js

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

3 changes: 2 additions & 1 deletion lib/components/bots/environment.js

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

3 changes: 2 additions & 1 deletion lib/components/bots/session.js

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

3 changes: 2 additions & 1 deletion lib/components/main.js

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

9 changes: 5 additions & 4 deletions lib/components/nlus/nlu.js

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

15 changes: 6 additions & 9 deletions lib/components/projects/project.js

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

3 changes: 2 additions & 1 deletion lib/components/scripts/helper.js

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

Loading

0 comments on commit 81ff04c

Please sign in to comment.