Convert script to a package function
You can convert a script to a package function to:
- use external libraries in your code,
- save the script in the version control system (e.g., Git).
Follow these steps to achieve it:
- Create a TypeScript package.
grok init package_name --ts
- Install required NodeJs modules:
npm install
-
Add your script to the package.
- If you are writing in Javascript/Typescript,
you can convert your script to the JS function, annotate it, and add directly to
src
folder:
//name: DemoScript
//tags: demo
//language: javascript
//input: int input1
...
export async function DemoScript(input1: int, ...) {
...
}- Alternatively, create the
scripts
folder in your package directory and place your script file in it. This approach works for all script languages.
- If you are writing in Javascript/Typescript,
you can convert your script to the JS function, annotate it, and add directly to
-
Add the application function to the
src/package.ts
:
//name: ApplicationFromScript
//tags: app, demo
//description: Demo application created from a script
//language: javascript
export async function DemoAppCreate() {
const func = await grok.functions.eval(`${_package.name}:DemoScript`);
const call = func.prepare();
call.edit();
}
Substitute the DemoScript
in the grok.functions.eval
call with the name of your script
(what you used in the //name:
annotation, not the file name).
- Compile and publish the package:
npm run build && grok publish your_environment
To make your package publicly available, use the --release
publishing mode.
npm run build && grok publish your_environment --release