Skip to content

Commit

Permalink
Merge pull request #199 from kuzzleio/0.27.0-proposal
Browse files Browse the repository at this point in the history
Release 0.27.0
  • Loading branch information
rolljee committed Dec 13, 2023
2 parents 05e0832 + e68b5ef commit 0c2e17b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $ npm install -g kourou
$ kourou COMMAND
running command...
$ kourou (-v|--version|version)
kourou/0.26.2 darwin-arm64 node-v18.17.1
kourou/0.27.0 darwin-arm64 node-v18.17.1
$ kourou --help [COMMAND]
USAGE
$ kourou COMMAND
Expand Down
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 | const index: string = "nyc-open-data"; const collection: string = "yellow-taxi"; const id: string = "document-ricky"; return await sdk.document.create(index, collection, {}, id); |
Then The document "document-ricky" should exist
And I should match stdout with "document-ricky"
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kourou",
"description": "The CLI that helps you manage your Kuzzle instances",
"version": "0.26.2",
"version": "0.27.0",
"author": "The Kuzzle Team <[email protected]>",
"bin": {
"kourou": "./bin/run"
Expand Down
33 changes: 24 additions & 9 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 ts from "typescript";
import vm from 'node:vm'

class SdkExecute extends Kommand {
public static description = `
Expand Down Expand Up @@ -71,31 +73,44 @@ Other

async beforeConnect() {
this.code = this.stdin || this.args.code || "// paste your code here";

try {
vm.runInContext(this.code, vm.createContext({}));
} catch (e: any) {
if (e.name === "SyntaxError") {
const result = ts.transpileModule(this.code, { compilerOptions: { module: ts.ModuleKind.CommonJS } });
this.code = result.outputText;
}
}
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 +152,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 0c2e17b

Please sign in to comment.