Skip to content

Commit 90b7e42

Browse files
Merge pull request #52 from SAP/refactoring
fix: yeoman-environment version update + code refactor + tests
2 parents 9a74fed + d6d240d commit 90b7e42

File tree

11 files changed

+208
-189
lines changed

11 files changed

+208
-189
lines changed

backend/.nycrc.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"temp-dir": "./reports/.nyc_output",
99
"report-dir": "./reports/coverage",
1010
"check-coverage": true,
11-
"branches": 86.79,
12-
"lines": 89,
13-
"functions": 94,
14-
"statements": 89
11+
"branches": 98,
12+
"lines": 99,
13+
"functions": 97,
14+
"statements": 99
1515
}

backend/package.json

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippet",
3-
"version": "0.0.21",
4-
"displayName": "Code Snippet",
3+
"version": "0.0.22",
4+
"displayName": "Code Snippet",
55
"publisher": "SAPOSS",
66
"author": {
77
"name": "SAP SE"
@@ -117,18 +117,13 @@
117117
},
118118
"dependencies": {
119119
"@sap-devx/webview-rpc": "^0.2.2",
120-
"@sap-devx/yeoman-ui-types": "0.0.1",
121120
"@vscode-logging/logger": "1.1.0",
122-
"chalk": "^4.0.0",
123121
"datauri": "^2.0.0",
124122
"fs-extra": "^8.1.0",
125-
"humanize-string": "^1.0.2",
126123
"lodash": "^4.17.15",
127-
"object-hash": "^2.0.3",
128124
"strip-ansi": "^6.0.0",
129-
"titleize": "^1.0.1",
130125
"ws": "^7.2.5",
131-
"yeoman-environment": "2.8.0"
126+
"yeoman-environment": "2.10.3"
132127
},
133128
"devDependencies": {
134129
"@types/chai": "^4.2.9",
@@ -137,49 +132,46 @@
137132
"@types/lodash": "^4.14.150",
138133
"@types/mocha": "^5.2.7",
139134
"@types/node": "^10.17.21",
140-
"@types/object-hash": "^1.3.1",
141135
"@types/sinon": "^7.5.0",
142136
"@types/ws": "^6.0.3",
143-
"@types/yeoman-environment": "2.3.3",
137+
"@types/yeoman-environment": "2.10.2",
144138
"@typescript-eslint/parser": "^2.30.0",
145139
"@typescript-eslint/eslint-plugin": "^2.30.0",
146140
"eslint": "^6.8.0",
147141
"eslint-config-prettier": "^6.11.0",
148142
"prettier": "^2.0.5",
149143
"typescript": "^3.9.7",
150-
"bufferutil": "^4.0.1",
151144
"chai": "^4.2.0",
152145
"copy-webpack-plugin": "^5.0.5",
153146
"mocha": "^6.2.2",
154147
"nyc": "^14.1.1",
155148
"sinon": "^7.5.0",
156149
"ts-loader": "^6.2.1",
157150
"ts-node": "^8.9.1",
158-
"utf-8-validate": "^5.0.2",
159151
"string-replace-loader": "^2.1.1",
160152
"vsce": "^1.73.0",
161153
"webpack": "^4.43.0",
162154
"webpack-cli": "^3.3.11",
163155
"lcov-result-merger": "3.1.0",
164156
"coveralls": "2.11.16",
165-
"cz-conventional-changelog": "3.3.0",
166-
"@commitlint/cli": "11.0.0",
167-
"@commitlint/config-conventional": "11.0.0",
168-
"husky": "4.3.0"
157+
"cz-conventional-changelog": "3.3.0",
158+
"@commitlint/cli": "11.0.0",
159+
"@commitlint/config-conventional": "11.0.0",
160+
"husky": "4.3.0"
169161
},
170162
"husky": {
171-
"hooks": {
172-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
173-
}
174-
},
175-
"config": {
176-
"commitizen": {
177-
"path": "cz-conventional-changelog"
178-
}
179-
},
180-
"commitlint": {
181-
"extends": [
182-
"@commitlint/config-conventional"
183-
]
184-
}
163+
"hooks": {
164+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
165+
}
166+
},
167+
"config": {
168+
"commitizen": {
169+
"path": "cz-conventional-changelog"
170+
}
171+
},
172+
"commitlint": {
173+
"extends": [
174+
"@commitlint/config-conventional"
175+
]
176+
}
185177
}

backend/src/code-snippet.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AppEvents } from "./app-events";
66
import { IRpc } from "@sap-devx/webview-rpc/out.ext/rpc-common";
77
import Generator = require("yeoman-generator");
88
import { IChildLogger } from "@vscode-logging/logger";
9+
import TerminalAdapter = require("yeoman-environment/lib/adapter");
910

1011
export class CodeSnippet {
1112

@@ -20,7 +21,7 @@ export class CodeSnippet {
2021
private readonly logger: IChildLogger;
2122
private gen: Generator | undefined; // eslint-disable-line @typescript-eslint/prefer-readonly
2223
private promptCount: number;
23-
private currentQuestions: Environment.Adapter.Questions<any>;
24+
private currentQuestions: TerminalAdapter.Questions<any>;
2425
private snippetName: string;
2526
private readonly customQuestionEventHandlers: Map<string, Map<string, Function>>;
2627
private errorThrown = false;
@@ -131,12 +132,12 @@ export class CodeSnippet {
131132
return this.outputChannel.showOutput();
132133
}
133134

134-
public async showPrompt(questions: Environment.Adapter.Questions<any>): Promise<inquirer.Answers> {
135+
public async showPrompt(questions: TerminalAdapter.Questions<any>): Promise<inquirer.Answers> {
135136
this.promptCount++;
136137
const promptName = this.getPromptName(questions);
137138

138139
this.currentQuestions = questions;
139-
const mappedQuestions: Environment.Adapter.Questions<any> = this.normalizeFunctions(questions);
140+
const mappedQuestions: TerminalAdapter.Questions<any> = this.normalizeFunctions(questions);
140141
if (_.isEmpty(mappedQuestions)) {
141142
return {};
142143
}
@@ -152,7 +153,7 @@ export class CodeSnippet {
152153
}
153154
}
154155

155-
private getPromptName(questions: Environment.Adapter.Questions<any>): string {
156+
private getPromptName(questions: TerminalAdapter.Questions<any>): string {
156157
const firstQuestionName = _.get(questions, "[0].name");
157158
return (firstQuestionName ? _.startCase(firstQuestionName) : `Step ${this.promptCount}`);
158159
}
@@ -224,12 +225,12 @@ export class CodeSnippet {
224225
* Functions are lost when being passed to client (using JSON.Stringify)
225226
* Also functions cannot be evaluated on client)
226227
*/
227-
private normalizeFunctions(questions: Environment.Adapter.Questions<any>): Environment.Adapter.Questions<any> {
228+
private normalizeFunctions(questions: TerminalAdapter.Questions<any>): TerminalAdapter.Questions<any> {
228229
this.addCustomQuestionEventHandlers(questions);
229230
return JSON.parse(JSON.stringify(questions, CodeSnippet.funcReplacer));
230231
}
231232

232-
private addCustomQuestionEventHandlers(questions: Environment.Adapter.Questions<any>): void {
233+
private addCustomQuestionEventHandlers(questions: TerminalAdapter.Questions<any>): void {
233234
for (const question of (questions as any[])) {
234235
const guiType = _.get(question, "guiOptions.type", question.guiType);
235236
const questionHandlers = this.customQuestionEventHandlers.get(guiType);

backend/src/contributors.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,42 @@ import * as vscode from 'vscode';
22
import * as _ from 'lodash';
33

44
export class Contributors {
5-
private static readonly apiMap = new Map<string, any>();
6-
7-
public static getSnippet(uiOptions: any) {
8-
let snippet = undefined;
9-
const contributorId = _.get(uiOptions, "contributorId");
10-
const snippetName = _.get(uiOptions, "snippetName");
11-
const snippetContext = _.get(uiOptions, "context");
12-
if (contributorId && snippetName) {
13-
const api = Contributors.apiMap.get(contributorId);
14-
const snippets = api.getCodeSnippets(snippetContext);
15-
snippet = snippets.get(snippetName);
16-
}
17-
return snippet;
18-
}
19-
20-
public static add(extensionId: string, api: any) {
21-
Contributors.apiMap.set(extensionId, api);
22-
}
23-
24-
private static async getApi(extension: vscode.Extension<any>, extensionId: string) {
25-
let api: any;
26-
if (!extension.isActive) {
5+
public static async getSnippet(contributerInfo: any) {
6+
const contributorId = _.get(contributerInfo, "contributorId");
7+
const extension = Contributors.getContributorExtension(contributorId);
8+
if (extension) {
279
try {
28-
api = await extension.activate();
10+
const api = await this.getApiPromise(extension as vscode.Extension<any>);
11+
const snippetContext = _.get(contributerInfo, "context");
12+
const snippets = api.getCodeSnippets(snippetContext);
13+
const snippetName = _.get(contributerInfo, "snippetName");
14+
return snippets.get(snippetName);
2915
} catch (error) {
30-
console.error(error);
31-
// TODO: Add Logger.error here ("Failed to activate extension", {extensionId: extensionId})
16+
const errorMessage = _.get(error, "stack", _.get(error, "message", error));
17+
console.error(errorMessage);
18+
// TODO: Add Logger.error
3219
}
33-
} else {
34-
api = extension.exports;
3520
}
36-
return api;
37-
}
21+
}
3822

39-
public static async init() {
40-
const allExtensions: readonly vscode.Extension<any>[] = vscode.extensions.all;
41-
for (const extension of allExtensions) {
42-
const currentPackageJSON: any = _.get(extension, "packageJSON");
43-
const extensionDependencies: string[] = _.get(currentPackageJSON, "extensionDependencies");
44-
if (!_.isEmpty(extensionDependencies)) {
45-
const codeSnippetDependancy: boolean = _.includes (extensionDependencies,"saposs.code-snippet");
46-
if (codeSnippetDependancy) {
47-
const extensionName: string = _.get(currentPackageJSON, "name");
48-
const extensionPublisher: string = _.get(currentPackageJSON, "publisher");
49-
const extensionId: string = extensionPublisher + "." + extensionName;
50-
const api = await Contributors.getApi(extension, extensionId);
51-
Contributors.add(extensionId, api);
23+
private static getApiPromise(extension: vscode.Extension<any>) {
24+
return (extension.isActive ? extension.exports : extension.activate());
25+
}
26+
27+
private static getContributorExtension(contributorId: string) {
28+
return _.find(vscode.extensions.all, (extension: vscode.Extension<any>) => {
29+
const extensionDependencies: string[] = _.get(extension, "packageJSON.extensionDependencies");
30+
if (_.includes(extensionDependencies, "saposs.code-snippet")) {
31+
if (contributorId === Contributors.getExtensionId(extension)) {
32+
return extension;
5233
}
5334
}
54-
}
35+
});
5536
}
5637

57-
}
38+
private static getExtensionId(extension: vscode.Extension<any>) {
39+
const extensionName: string = _.get(extension, "packageJSON.name");
40+
const extensionPublisher: string = _.get(extension, "packageJSON.publisher");
41+
return `${extensionPublisher}.${extensionName}`;
42+
}
43+
}

backend/src/extension.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as _ from 'lodash';
22
import * as vscode from 'vscode';
33
import { createExtensionLoggerAndSubscribeToLogSettingsChanges } from "./logger/logger-wrapper";
4-
import { Contributors } from './contributors';
54
import { CodeSnippetPanel } from './panels/CodeSnippetPanel';
65
import { AbstractWebviewPanel } from './panels/AbstractWebviewPanel';
76

@@ -18,8 +17,6 @@ export function activate(context: vscode.ExtensionContext) {
1817
return;
1918
}
2019

21-
Contributors.init();
22-
2320
codeSnippetPanel = new CodeSnippetPanel(extContext);
2421
registerAndSubscribeCommand("loadCodeSnippet", codeSnippetPanel.loadWebviewPanel.bind(codeSnippetPanel));
2522
registerAndSubscribeCommand("codeSnippet.toggleOutput", codeSnippetPanel.toggleOutput.bind(codeSnippetPanel));

backend/src/panels/CodeSnippetPanel.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,27 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
2525
public setWebviewPanel(webViewPanel: vscode.WebviewPanel, uiOptions?: any) {
2626
super.setWebviewPanel(webViewPanel, uiOptions);
2727

28-
this.snippet = Contributors.getSnippet(uiOptions);
29-
30-
if (_.isNil(this.snippet)) {
31-
return vscode.window.showErrorMessage("Can not find snippet.");
32-
}
33-
34-
this.messages = _.assign({}, backendMessages, this.snippet.getMessages());
35-
const rpc = new RpcExtension(this.webViewPanel.webview);
36-
this.outputChannel = new OutputChannelLog(this.messages.channelName);
37-
const vscodeEvents: AppEvents = new VSCodeEvents(rpc, this.webViewPanel);
38-
this.codeSnippet = new CodeSnippet(rpc,
39-
vscodeEvents,
40-
this.outputChannel,
41-
this.logger,
42-
{messages: this.messages, snippet: this.snippet});
43-
this.codeSnippet.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
44-
this.codeSnippet.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));
45-
46-
this.initWebviewPanel();
28+
const contributerInfo = _.get(uiOptions, "contributerInfo", uiOptions);
29+
Contributors.getSnippet(contributerInfo).then(snippet => {
30+
if (_.isNil(snippet)) {
31+
this.webViewPanel.dispose();
32+
return vscode.window.showErrorMessage("Can not find snippet.");
33+
}
34+
35+
this.messages = _.assign({}, backendMessages, snippet.getMessages());
36+
const rpc = new RpcExtension(this.webViewPanel.webview);
37+
this.outputChannel = new OutputChannelLog(this.messages.channelName);
38+
const vscodeEvents: AppEvents = new VSCodeEvents(rpc, this.webViewPanel);
39+
this.codeSnippet = new CodeSnippet(rpc,
40+
vscodeEvents,
41+
this.outputChannel,
42+
this.logger,
43+
{ messages: this.messages, snippet, contributerInfo});
44+
this.codeSnippet.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
45+
this.codeSnippet.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));
46+
47+
this.initWebviewPanel();
48+
});
4749
}
4850

4951
public static getOutputChannel(channelName: string): vscode.OutputChannel {
@@ -55,7 +57,6 @@ export class CodeSnippetPanel extends AbstractWebviewPanel {
5557
}
5658

5759
private codeSnippet: CodeSnippet;
58-
private snippet: any;
5960
private messages: any;
6061
private outputChannel: AppLog;
6162

backend/src/webSocketServer/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CodeSnippetWebSocketServer {
3535
this.rpc = new RpcExtensionWebSockets(ws);
3636
//TODO: Use RPC to send it to the browser log (as a collapsed pannel in Vue)
3737
const logger: AppLog = new ServerLog(this.rpc);
38-
const childLogger = {debug: () => {}, error: () => {}, fatal: () => {}, warn: () => {}, info: () => {}, trace: () => {}, getChildLogger: () => {return {} as IChildLogger;}};
38+
const childLogger = {debug: () => "", error: () => "", fatal: () => "", warn: () => "", info: () => "", trace: () => "", getChildLogger: () => {return {} as IChildLogger;}};
3939
const appEvents: AppEvents = new ServerEvents(this.rpc);
4040
const snippet = {
4141
getQuestions() {

backend/src/webSocketServer/server-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export class ServerEvents implements AppEvents {
1717
}
1818

1919
public doClose(): void {
20-
20+
return;
2121
}
2222
}

backend/tests/code-snippet.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('codeSnippet unit test', () => {
9292
}
9393
}
9494

95-
const testLogger = {debug: () => {}, error: () => {}, fatal: () => {}, warn: () => {}, info: () => {}, trace: () => {}, getChildLogger: () => ({} as IChildLogger)};
95+
const testLogger = { debug: () => "", error: () => "", fatal: () => "", warn: () => "", info: () => "", trace: () => "", getChildLogger: () => ({} as IChildLogger)};
9696

9797
const snippet: any = {
9898
getMessages() {

0 commit comments

Comments
 (0)