Skip to content

Commit e2c83d6

Browse files
author
Ben Force
committed
feat: use package to render templates
1 parent 6d3d2f8 commit e2c83d6

File tree

4 files changed

+63
-41
lines changed

4 files changed

+63
-41
lines changed

.vscodeignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ vsc-extension-quickstart.md
99
**/.eslintrc.json
1010
**/*.map
1111
**/*.ts
12+
test_templates/**
13+
.github/**

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"vscode-test": "^1.4.0"
139139
},
140140
"dependencies": {
141-
"aws-sdk": "^2.755.0",
142-
"velocityjs": "^2.0.1"
141+
"appsync-template-tester": "^1.0.9",
142+
"aws-sdk": "^2.755.0"
143143
}
144144
}

src/previewContentProvider.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from "vscode";
2-
import {Converter} from "aws-sdk/clients/dynamodb";
3-
import {render as renderVelocity} from "velocityjs";
2+
import { Converter } from "aws-sdk/clients/dynamodb";
3+
import Parser from "appsync-template-tester";
44

55
function isVelocityFile(document?: vscode.TextDocument): boolean {
6-
return document?.languageId === "velocity";
7-
}
6+
return document?.languageId === "velocity";
7+
}
88

99
export class VelocityPreviewProvider
1010
implements vscode.TextDocumentContentProvider {
@@ -14,48 +14,34 @@ export class VelocityPreviewProvider
1414
private contentProviderRegistration: vscode.Disposable;
1515

1616
static SCHEME = `appsyncvtl`;
17-
static URI = vscode.Uri.parse(`${VelocityPreviewProvider.SCHEME}:preview.json`, true);
17+
static URI = vscode.Uri.parse(
18+
`${VelocityPreviewProvider.SCHEME}:preview.json`,
19+
true
20+
);
1821

1922
constructor() {
20-
this.contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(VelocityPreviewProvider.SCHEME, this);
23+
this.contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(
24+
VelocityPreviewProvider.SCHEME,
25+
this
26+
);
2127
}
2228

2329
provideTextDocumentContent(
2430
uri: vscode.Uri,
2531
token: vscode.CancellationToken
2632
): vscode.ProviderResult<string> {
27-
const editor = vscode.window.activeTextEditor;
33+
const editor = vscode.window.activeTextEditor;
2834

29-
if(!isVelocityFile(editor?.document)) {
30-
console.error(`Wrong document type`);
31-
return `Unknown document type "${editor?.document?.languageId}"`;
35+
if (!isVelocityFile(editor?.document)) {
36+
console.error(`Wrong document type`);
37+
return `Unknown document type "${editor?.document?.languageId}"`;
3238
}
3339

3440
const content = editor?.document?.getText() ?? ``;
3541

36-
const renderContext = {
37-
util: {
38-
toJson(vmString: any, context: any): string {
39-
return JSON.stringify(vmString);
40-
},
41-
dynamodb: {
42-
toMap(vmString: any, context: any): object {
43-
return Converter.input(vmString);
44-
},
45-
toMapValues(vmString: any, context: any): object {
46-
return Converter.marshall(vmString);
47-
},
48-
toMapJson(vmString: any, context: any): string {
49-
return JSON.stringify(Converter.input(vmString));
50-
},
51-
toMapValuesJson(vmString: any, context: any): string {
52-
return JSON.stringify(Converter.marshall(vmString));
53-
}
54-
}
55-
}
56-
};
57-
58-
return renderVelocity(content, renderContext);
42+
const parser = new Parser(content);
43+
44+
return parser.resolve({});
5945
}
6046

6147
async updatePreview() {}
@@ -76,15 +62,23 @@ export class VelocityPreviewProvider
7662
const provider = new VelocityPreviewProvider();
7763

7864
context.subscriptions.push(
79-
vscode.workspace.onDidChangeTextDocument(provider.onContentUpdated.bind(provider))
65+
vscode.workspace.onDidChangeTextDocument(
66+
provider.onContentUpdated.bind(provider)
67+
)
8068
);
8169
context.subscriptions.push(
82-
vscode.window.onDidChangeActiveTextEditor(provider.onContentUpdated.bind(provider))
70+
vscode.window.onDidChangeActiveTextEditor(
71+
provider.onContentUpdated.bind(provider)
72+
)
8373
);
8474

85-
vscode.commands.registerCommand('appsyncVtl.showPreview', async () => {
86-
let doc = await vscode.workspace.openTextDocument(this.URI);
87-
await vscode.window.showTextDocument(doc, {preview: false, viewColumn: vscode.ViewColumn.Beside, preserveFocus: true});
75+
vscode.commands.registerCommand("appsyncVtl.showPreview", async () => {
76+
let doc = await vscode.workspace.openTextDocument(this.URI);
77+
await vscode.window.showTextDocument(doc, {
78+
preview: false,
79+
viewColumn: vscode.ViewColumn.Beside,
80+
preserveFocus: true,
81+
});
8882
});
8983

9084
return provider;

yarn.lock

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,15 @@ anymatch@~3.1.1:
607607
normalize-path "^3.0.0"
608608
picomatch "^2.0.4"
609609

610+
appsync-template-tester@^1.0.9:
611+
version "1.0.9"
612+
resolved "https://registry.yarnpkg.com/appsync-template-tester/-/appsync-template-tester-1.0.9.tgz#dcc792ff22de5512292bb55f3c9243a397d1e515"
613+
integrity sha512-+aWbJFyw1Ovqgz1IH1HsYNM2+Oho1KCMDWroJpndv4vOGdH9diNKeFEzU3McKBCp9GzswUZMXbyb3s+6wPvjsw==
614+
dependencies:
615+
moment-timezone "^0.5.28"
616+
uuid "^8.0.0"
617+
velocityjs "^2.0.0"
618+
610619
aproba@^1.0.3, aproba@^1.1.1, aproba@^1.1.2:
611620
version "1.2.0"
612621
resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a"
@@ -4299,6 +4308,18 @@ modify-values@^1.0.0:
42994308
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
43004309
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==
43014310

4311+
moment-timezone@^0.5.28:
4312+
version "0.5.31"
4313+
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.31.tgz#9c40d8c5026f0c7ab46eda3d63e49c155148de05"
4314+
integrity sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA==
4315+
dependencies:
4316+
moment ">= 2.9.0"
4317+
4318+
"moment@>= 2.9.0":
4319+
version "2.29.0"
4320+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.0.tgz#fcbef955844d91deb55438613ddcec56e86a3425"
4321+
integrity sha512-z6IJ5HXYiuxvFTI6eiQ9dm77uE0gyy1yXNApVHqTcnIKfY9tIwEjlzsZ6u1LQXvVgKeTnv9Xm7NDvJ7lso3MtA==
4322+
43024323
move-concurrently@^1.0.1:
43034324
version "1.0.1"
43044325
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
@@ -6740,6 +6761,11 @@ uuid@^3.3.2, uuid@^3.3.3:
67406761
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
67416762
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
67426763

6764+
uuid@^8.0.0:
6765+
version "8.3.0"
6766+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea"
6767+
integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==
6768+
67436769
v8-compile-cache@^2.0.3:
67446770
version "2.1.0"
67456771
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"
@@ -6760,7 +6786,7 @@ validate-npm-package-name@^3.0.0, validate-npm-package-name@~3.0.0:
67606786
dependencies:
67616787
builtins "^1.0.3"
67626788

6763-
velocityjs@^2.0.1:
6789+
velocityjs@^2.0.0:
67646790
version "2.0.1"
67656791
resolved "https://registry.yarnpkg.com/velocityjs/-/velocityjs-2.0.1.tgz#078c73d22a53f085cf3a52a56a8271c820ac23dd"
67666792
integrity sha512-KXVGRAOPCm8DhMdrhHqXyELIZNs3CTe2Vz1Kg09zzSycEmE9VzjiSnG50n3rs3FuiL9ek7c36sYJg+R+8EO2Zg==

0 commit comments

Comments
 (0)