Skip to content

Commit

Permalink
Allow typescript file to be used by sdk:execute
Browse files Browse the repository at this point in the history
  • Loading branch information
rolljee committed Nov 23, 2023
1 parent 05e0832 commit 6f61a37
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
12 changes: 8 additions & 4 deletions features/Sdk.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Feature: SDK commands

# sdk:query ==================================================================

@mappings
Scenario: Send a query to Kuzzle
Given an existing collection "nyc-open-data":"yellow-taxi"
Expand All @@ -21,12 +19,18 @@ Feature: SDK commands
Then I should match stdout with:
| "_id": "gordon" |

# sdk:execute ================================================================

@mappings
Scenario: Execute code in the SDK context
Given an existing collection "nyc-open-data":"yellow-taxi"
When I run the command "sdk:execute" with:
| arg | return await sdk.document.create("nyc-open-data", "yellow-taxi", {}, "document-adrien"); |
Then The document "document-adrien" should exist
And I should match stdout with "document-adrien"

@mappings
Scenario: Execute Typescript code in the SDK context
Given an existing collection "nyc-open-data":"yellow-taxi"
When I run the command "sdk:execute" with:
| 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); |
Then The document "document-ricky" should exist
And I should match stdout with "document-ricky"
55 changes: 47 additions & 8 deletions src/commands/sdk/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { isEmpty } from "lodash";
import { Editor } from "../../support/editor";
import { Kommand } from "../../common";
import { kuzzleFlags } from "../../support/kuzzle";
import { exec } from "child_process";
import fs from "fs";

class SdkExecute extends Kommand {
public static description = `
Expand Down Expand Up @@ -69,33 +71,70 @@ Other

static readStdin = true;

async toJavascript(filename: string): Promise<boolean> {
return new Promise((resolve, reject) => {

Check warning on line 75 in src/commands/sdk/execute.ts

View workflow job for this annotation

GitHub Actions / Lint

'reject' is defined but never used
exec(
`npx tsc -t es5 ${filename}`,
() => {
// We do not care for TS errors, user should now if this will work or not
resolve(true);
});
});
}

async beforeConnect() {
this.code = this.stdin || this.args.code || "// paste your code here";
try {
eval(this.code);
} catch (e) {
if (e instanceof SyntaxError) {
const tsFile = "kourou-sdk-execute-tmp.ts";
const jsFile = "kourou-sdk-execute-tmp.js";

// Write to file, transpile it and read it back
fs.writeFileSync(tsFile, this.code);
await this.toJavascript(tsFile);
const file = fs.readFileSync(jsFile, "utf8");
this.code = file;

// Clean up
fs.unlinkSync(tsFile);
fs.unlinkSync(jsFile);
}
}

console.log(this.code);

if (this.haveSubscription) {
this.sdkOptions.protocol = "ws";
}
}

getVariables() {
return (this.flags.var || [])
.map((nameValue: string) => {
const [name, value] = nameValue.split("=");

return ` let ${name} = ${value};`;
})
.join("\n");


}

async runSafe() {
if (isEmpty(this.code)) {
throw new Error("No code provided.");
}

let userError: Error | null = null;

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

return ` let ${name} = ${value};`;
})
.join("\n");

this.code = `
(async () => {
try {
${variables}
${this.getVariables()}
${this.code}
}
catch (error) {
Expand Down Expand Up @@ -137,7 +176,7 @@ ${variables}
this.logInfo("Keep alive for realtime notifications ...");

// eslint-disable-next-line @typescript-eslint/no-empty-function
await new Promise(() => {});
await new Promise(() => { });
}
}

Expand Down

0 comments on commit 6f61a37

Please sign in to comment.