Skip to content

Commit

Permalink
Async fix (#124)
Browse files Browse the repository at this point in the history
* editor events

* get error info

* update coverage

* update coverage

* update tests

* update version
  • Loading branch information
slavik-lvovsky authored Jan 26, 2020
1 parent 50f8a25 commit e6724c2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 73 deletions.
2 changes: 1 addition & 1 deletion backend/.nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 26,
"lines": 39,
"lines": 38,
"functions": 29,
"statements": 39
}
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache 2.0",
"description": "Provide rich user experience for Yeoman generators using VSCode extension or the browser",
"repository": "https://github.com/SAP/yeoman-ui",
"version": "0.0.31",
"version": "0.0.32",
"engines": {
"vscode": "^1.39.2"
},
Expand Down
21 changes: 12 additions & 9 deletions backend/src/yeomanui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ export class YeomanUI {
this.doGeneratorDone(true, message, destinationRoot);
});
} catch (error) {
this.handleError(error);
const errorMessage = `Error info ---->\n ${this.getErrorInfo(error)}`;
this.showMessageInOutput(errorMessage);
return Promise.reject(errorMessage);
}
}

Expand All @@ -192,10 +194,8 @@ export class YeomanUI {
return `name: ${name}\n message: ${message}\n stack: ${stack}\n string: ${string}\n`;
}

handleError(error: any) {
const errorMessage = `Error info ---->\n ${this.getErrorInfo(error)}`;
console.error(errorMessage);
this.logMessage(errorMessage);
async showMessageInOutput(errorMessage: string) {
await this.logMessage(errorMessage);
this.toggleLog();
}

Expand All @@ -212,20 +212,23 @@ export class YeomanUI {
* @param answers - partial answers for the current prompt -- the input parameter to the method to be evaluated
* @param method
*/
public evaluateMethod(params: any[], questionName: string, methodName: string): any {
public async evaluateMethod(params: any[], questionName: string, methodName: string): Promise<any> {
try {
if (this.currentQuestions) {
const relevantQuestion: any = _.find(this.currentQuestions, question => {
return (_.get(question, "name") === questionName);
});
if (relevantQuestion) {
return relevantQuestion[methodName].apply(this.gen, params);
return await relevantQuestion[methodName].apply(this.gen, params);
}
}
} catch (error) {
this.handleError(error);
const questionInfo = `Could not update method '${methodName}' in '${questionName}' question in generator '${this.gen.options.namespace}'`;
const errorMessage = `${questionInfo}\nError info ---->\n ${this.getErrorInfo(error)}`;
this.showMessageInOutput(errorMessage);
return Promise.reject(errorMessage);
}
}
}

public async receiveIsWebviewReady() {
// TODO: loading generators takes a long time; consider prefetching list of generators
Expand Down
108 changes: 50 additions & 58 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ export default {
let showBusy = true
const that = this
const finished = relevantQuestionsToUpdate.reduce((p, question) => {
return p.then(() => that.updateQuestion(question))
return p.then(() => that.updateQuestion(question)).catch(error => {
// eslint-disable-next-line no-console
console.error(error);
})
}, Promise.resolve());
setTimeout(() => {
Expand All @@ -188,70 +191,59 @@ export default {
},
async updateQuestion(question) {
const newAnswers = this.currentPrompt.answers
try {
if (question.when === FUNCTION) {
question.isWhen = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
if (question.when === FUNCTION) {
question.isWhen = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"when"
]);
}
if (question.isWhen === true) {
if (question.filter === FUNCTION) {
question.answer = await this.rpc.invoke("evaluateMethod", [
[question.answer],
question.name,
"when"
"filter"
]);
}
if (question.isWhen === true) {
if (question.filter === FUNCTION) {
question.answer = await this.rpc.invoke("evaluateMethod", [
[question.answer],
question.name,
"filter"
]);
}
if (question._default === FUNCTION) {
question.default = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"default"
]);
if (question.answer === undefined) {
question.answer = question.default;
}
}
if (question._message === FUNCTION) {
question.message = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"message"
]);
}
if (question._choices === FUNCTION) {
question.choices = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"choices"
]);
}
if (question.validate === FUNCTION) {
const response = await this.rpc.invoke("evaluateMethod", [
[question.answer, newAnswers],
question.name,
"validate"
]);
question.isValid = _.isString(response) ? false : response;
question.validationMessage = _.isString(response)
? response
: undefined;
if (question._default === FUNCTION) {
question.default = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"default"
]);
if (question.answer === undefined) {
question.answer = question.default;
}
}
} catch (error) {
const errorMessage = this.getErrorMessageOnException(question, error);
// eslint-disable-next-line no-console
console.error(errorMessage);
await this.rpc.invoke("logMessage", [errorMessage]);
this.rpc.invoke("toggleLog", [{}]);
if (question._message === FUNCTION) {
question.message = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"message"
]);
}
if (question._choices === FUNCTION) {
question.choices = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
question.name,
"choices"
]);
}
if (question.validate === FUNCTION) {
const response = await this.rpc.invoke("evaluateMethod", [
[question.answer, newAnswers],
question.name,
"validate"
]);
question.isValid = _.isString(response) ? false : response;
question.validationMessage = _.isString(response)
? response
: undefined;
}
}
},
getErrorMessageOnException(question) {
return `Could not update the '${question.name}' question in generator '${this.generatorName}`;
},
next() {
if (this.resolve) {
try {
Expand Down
4 changes: 0 additions & 4 deletions frontend/tests/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ describe('App.vue', () => {
const invokeSpy = jest.spyOn(wrapper.vm.rpc, 'invoke')
await wrapper.vm.updateQuestionsFromIndex(0)
expect(invokeSpy).toHaveBeenCalledWith('evaluateMethod', [["validateAnswer", {"validateQ": "validateAnswer"}], 'validateQ', 'validate'])
expect(invokeSpy).toHaveBeenCalledWith('toggleLog', [{}])

invokeSpy.mockRestore();
})

Expand All @@ -184,7 +182,6 @@ describe('App.vue', () => {
const invokeSpy = jest.spyOn(wrapper.vm.rpc, 'invoke')
await wrapper.vm.updateQuestionsFromIndex(0)
expect(invokeSpy).toHaveBeenCalledWith('evaluateMethod', [["validateAnswer", {"validateQ": "validateAnswer"}], 'validateQ', 'validate'])
expect(invokeSpy).toHaveBeenCalledWith('toggleLog', [{}])

invokeSpy.mockRestore();
})
Expand All @@ -205,7 +202,6 @@ describe('App.vue', () => {
const invokeSpy = jest.spyOn(wrapper.vm.rpc, 'invoke')
await wrapper.vm.updateQuestionsFromIndex(0)
expect(invokeSpy).toHaveBeenCalledWith('evaluateMethod', [["validateAnswer", {"validateQ": "validateAnswer"}], 'validateQ', 'validate'])
expect(invokeSpy).toHaveBeenCalledWith('toggleLog', [{}])

invokeSpy.mockRestore();
})
Expand Down

0 comments on commit e6724c2

Please sign in to comment.