Skip to content

Commit 6f61a37

Browse files
committed
Allow typescript file to be used by sdk:execute
1 parent 05e0832 commit 6f61a37

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

features/Sdk.feature

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
Feature: SDK commands
22

3-
# sdk:query ==================================================================
4-
53
@mappings
64
Scenario: Send a query to Kuzzle
75
Given an existing collection "nyc-open-data":"yellow-taxi"
@@ -21,12 +19,18 @@ Feature: SDK commands
2119
Then I should match stdout with:
2220
| "_id": "gordon" |
2321

24-
# sdk:execute ================================================================
25-
2622
@mappings
2723
Scenario: Execute code in the SDK context
2824
Given an existing collection "nyc-open-data":"yellow-taxi"
2925
When I run the command "sdk:execute" with:
3026
| arg | return await sdk.document.create("nyc-open-data", "yellow-taxi", {}, "document-adrien"); |
3127
Then The document "document-adrien" should exist
3228
And I should match stdout with "document-adrien"
29+
30+
@mappings
31+
Scenario: Execute Typescript code in the SDK context
32+
Given an existing collection "nyc-open-data":"yellow-taxi"
33+
When I run the command "sdk:execute" with:
34+
| arg | return const index: string = "nyc-open-data"; const collection: string = "yellow-taxi"; const id: string = "document-ricky"; await sdk.document.create(index, collection, {}, id); |
35+
Then The document "document-ricky" should exist
36+
And I should match stdout with "document-ricky"

src/commands/sdk/execute.ts

+47-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { isEmpty } from "lodash";
44
import { Editor } from "../../support/editor";
55
import { Kommand } from "../../common";
66
import { kuzzleFlags } from "../../support/kuzzle";
7+
import { exec } from "child_process";
8+
import fs from "fs";
79

810
class SdkExecute extends Kommand {
911
public static description = `
@@ -69,33 +71,70 @@ Other
6971

7072
static readStdin = true;
7173

74+
async toJavascript(filename: string): Promise<boolean> {
75+
return new Promise((resolve, reject) => {
76+
exec(
77+
`npx tsc -t es5 ${filename}`,
78+
() => {
79+
// We do not care for TS errors, user should now if this will work or not
80+
resolve(true);
81+
});
82+
});
83+
}
84+
7285
async beforeConnect() {
7386
this.code = this.stdin || this.args.code || "// paste your code here";
87+
try {
88+
eval(this.code);
89+
} catch (e) {
90+
if (e instanceof SyntaxError) {
91+
const tsFile = "kourou-sdk-execute-tmp.ts";
92+
const jsFile = "kourou-sdk-execute-tmp.js";
93+
94+
// Write to file, transpile it and read it back
95+
fs.writeFileSync(tsFile, this.code);
96+
await this.toJavascript(tsFile);
97+
const file = fs.readFileSync(jsFile, "utf8");
98+
this.code = file;
99+
100+
// Clean up
101+
fs.unlinkSync(tsFile);
102+
fs.unlinkSync(jsFile);
103+
}
104+
}
105+
106+
console.log(this.code);
74107

75108
if (this.haveSubscription) {
76109
this.sdkOptions.protocol = "ws";
77110
}
78111
}
79112

113+
getVariables() {
114+
return (this.flags.var || [])
115+
.map((nameValue: string) => {
116+
const [name, value] = nameValue.split("=");
117+
118+
return ` let ${name} = ${value};`;
119+
})
120+
.join("\n");
121+
122+
123+
}
124+
80125
async runSafe() {
81126
if (isEmpty(this.code)) {
82127
throw new Error("No code provided.");
83128
}
84129

85130
let userError: Error | null = null;
86131

87-
const variables = (this.flags.var || [])
88-
.map((nameValue: string) => {
89-
const [name, value] = nameValue.split("=");
90132

91-
return ` let ${name} = ${value};`;
92-
})
93-
.join("\n");
94133

95134
this.code = `
96135
(async () => {
97136
try {
98-
${variables}
137+
${this.getVariables()}
99138
${this.code}
100139
}
101140
catch (error) {
@@ -137,7 +176,7 @@ ${variables}
137176
this.logInfo("Keep alive for realtime notifications ...");
138177

139178
// eslint-disable-next-line @typescript-eslint/no-empty-function
140-
await new Promise(() => {});
179+
await new Promise(() => { });
141180
}
142181
}
143182

0 commit comments

Comments
 (0)