-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from saulecabrera/javy-json-v1
Introduce Javy.JSON builtins
- Loading branch information
Showing
2 changed files
with
14 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import * as fs from "javy/fs"; | ||
|
||
export type ShopifyFunction<Input extends {}, Output extends {}> = ( | ||
input: Input | ||
) => Output; | ||
|
||
interface Javy { | ||
JSON: { | ||
fromStdin(): any; | ||
toStdout(val: any); | ||
} | ||
} | ||
|
||
declare global { | ||
const Javy: Javy; | ||
} | ||
|
||
export default function <I extends {}, O extends {}>(userfunction: ShopifyFunction<I, O>) { | ||
const input_data = fs.readFileSync(fs.STDIO.Stdin); | ||
const input_str = new TextDecoder("utf-8").decode(input_data); | ||
const input_obj = JSON.parse(input_str); | ||
const input_obj = Javy.JSON.fromStdin(); | ||
const output_obj = userfunction(input_obj); | ||
const output_str = JSON.stringify(output_obj); | ||
const output_data = new TextEncoder().encode(output_str); | ||
fs.writeFileSync(fs.STDIO.Stdout, output_data); | ||
Javy.JSON.toStdout(output_obj) | ||
} |