Skip to content

Functions:Commands

Emiel Wit edited this page Sep 11, 2023 · 4 revisions

Getting started

The bb functions namespace supports several commands.

  • init [identifier] initialize functions project
  • new [function-name] Initialize a new function
  • validate validate functions of current working directory
  • publish publish functions of current working directory
  • autoversion migrate existing projects to a project with versioning
  • bump bump the version of existing functions
  • test test your application functions within an IsolatedVM ("ActionsJS lite") environment

bb functions init

New projects can be initialised with: bb functions init <your-app-identifier> --app

Given an application cli-wiki.bettyblocks.com, the identifier will be cli-wiki.

Example:

$ bb functions init cli-wiki --app

Initialized app functions project in /Users/bettyblocks/application-functions/cli-wiki.
You can use "bb functions" to publish it:

    cd cli-wiki
    bb functions publish

Each new project comes with one example function that can be found in <path/to/project>/functions/say-hello/.


bb functions publish

Once you've got your function implemented and the function.json finished, you're ready to publish your function. All your functions will automatically be validated on publish.

Publishing can be done with: bb functions publish

Note: The first time you publish your functions for your application, you will be asked for your application's ID. See this post on how to find your application-id

Example:

$ cd /Users/bettyblocks/application-functions/cli-wiki
$ bb functions publish

Validating functions in /Users/bettyblocks/application-functions/cli-wiki/functions
✔ Validate: authenticateUser
✔ Validate: http
Publishing to https://cli-wiki.bettyblocks.com ...
✔ Please supply the ID for your application (cli-wiki) … 1234567890abcdef1234567890abcdef
✔ Fill in your e-mail address … [email protected]
✔ Fill in your password … ********
✔ Fill in your 2FA code … 000000
✔ Create sayGoodbye.
✔ Create sayHello.
Done.

Your function is now ready to be used in the IDE!

Publish "Say Hello" function


bb functions new

You can create a new function from the root of your project with: bb functions new <function name>

Example:

$ cd /Users/bettyblocks/application-functions/cli-wiki
$ bb functions new say-goodbye

functions/say-goodbye created

This will create two new files

  • cli-wiki/functions/say-goodbye/function.json
  • cli-wiki/functions/say-goodbye/index.js

The function.json describes the function following the JSON Schema. While the index.js implements the actual function. Checkout these functions that are predefined for each application.


bb functions validate

You can validate the function.json of your function with: bb functions validate <function-name>

Example:

$ cd /Users/bettyblocks/application-functions/cli-wiki
$ bb functions validate say-goodbye

✔ Validate: sayGoodbye

It is also possible to validate all the functions in your project by omitting the function name. Example:

$ cd /Users/bettyblocks/application-functions/cli-wiki
$ bb functions validate

✔ Validate: sayGoodbye
✔ Validate: sayHello

Debugging invalid function.json

A possible validation message of invalid function.json files may be:

bb functions validate say-goodbye

✖ Validate: sayGoodbye
	instance.options[0] requires property "label",instance requires property "category"

In this instance, the function.json appears to be missing a "category" key in the root of the json. It also appears to have a list of options of which the first instance ([0]) seems to be missing a "label" key.


bb functions bump

You can bump versions of existing functions from the root of your project with: bb functions bump

Example:

$ bb functions bump

? Which function do you want to bump?
    Authenticate User
    Condition
❯   Create Record
    Delete Record
    HTTP(S)
    Log Message
    Loop
    Update Record
✔ Which function do you want to bump? › Create Record

? To which version do you want to bump your function? › 1.1 / 2.0
✔ Version bumped to create/2.0

bb functions test

As of version 25.29.0, it is possible to test your application functions locally within an IsolatedVM environment (so in an "ActionJS lite" environment so to say).

How it works

This command basically does the following:

  1. It zips your application functions
  2. It creates a Webpack bundle based on the test files (*.test.js) you want to run
  3. It runs the tests in an IsolatedVM environment in which you can assert expected values

Writing your test files

It is advised to put your test files in the /test of your functions project. The test files need to match *.test.js (e.g. say-hello.test.js).

Provided are the following functions:

  • test(description, asyncFunction) - Provide a unique description and an async function
  • assert(left, right) - Use this function to assert expected values

Mock ActionsJS provided functions

You can define helper functions to mock ActionsJS provided functions (e.g. gql()) at test/helpers.js within your functions project.

Example

(in test/helpers.js)

const gql = async (query) => {
  return [{ id: 1, name: 'Bruce Wayne' }];
};

(in test/say-hello.test.js)

test('sayHello 1.0', async () => {
  const output = await $app['sayHello 1.0']({ name: 'Bruce' });
  assert(output, { greet: 'Hello, Bruce' });
});

(in your terminal)

$ bb functions test
› Building artifacts ...
✔ Build succeeded
› Running tests ...
✔ test "sayHello 1.0"

Finished in 3.426 seconds (build: 3.418s, tests: 0.008)
1 tests, 0 failures

Please note that you can use glob patterns to specify which test files to run.

Clone this wiki locally