Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/node_modules/**
liquid.min.js
liquid.min.js
jsonpath.min.js
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"fetch": "readonly",
"smtp": "readonly",
"storeFile": "readonly",
"parseToGqlFragment": "readonly"
"parseToGqlFragment": "readonly",
"runAction": "readonly"
}
}
27 changes: 16 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ name: Main
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ '**' ]
branches: ['**']
pull_request:
branches: [ '**' ]
branches: ['**']

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -19,21 +19,26 @@ jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

env:
npm_token: ${{ secrets.NPM_TOKEN }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install packages
run: yarn install
run: bun install --frozen-lockfile

- name: Lint
run: yarn lint
run: bun run lint

- name: Prettier
run: yarn prettier:check
run: bun run prettier:check

- name: Test
run: yarn test
run: bun run test
69 changes: 10 additions & 59 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,19 @@ on:
jobs:
publish:
runs-on: ubuntu-latest
env:
npm_token: ${{ secrets.NPM_TOKEN }}
steps:
- run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: branch
- run: npm config set '//registry.npmjs.org/:_authToken' '${{ secrets.NPM_TOKEN }}'
- run: npm install -g @betty-blocks/cli @betty-blocks/jaws form-data fs-extra shelljs
- uses: actions/checkout@v2
- uses: actions/github-script@v6
- uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
script: |
const global = '/usr/local/lib/node_modules';
const cli = `${global}/@betty-blocks/cli/build`;
bun-version: latest

const Jaws = require(`${global}/@betty-blocks/jaws`);
const FormData = require(`${global}/form-data`);
const fs = require(`${global}/fs-extra`);
const path = require('path');
const shell = require(`${global}/shelljs`);
- name: Install npm dependencies
run: bun install --frozen-lockfile

const run = (cmd) => shell.exec(cmd, {silent: true}).stdout.trim();

const URL = '${{ secrets.API_URL }}';
const CONFIG = JSON.parse('${{ secrets.JAWS_SECRETS }}');
const jaws = Jaws.default.getInstance(CONFIG);
const zones = CONFIG.services;

const {
functionDefinitions,
stringifyDefinitions,
zipFunctionDefinitions
} = require(
`${global}/@betty-blocks/cli/build/functions/functionDefinitions.js`
);

const functionsDir = path.join(run('pwd'), 'functions');
const functions = functionDefinitions(functionsDir);
const functionsJson = stringifyDefinitions(functions);

const appFile = zipFunctionDefinitions(functionsDir);
const nativeFile = appFile.replace('app', 'native');
run(`mv ${appFile} ${nativeFile}`);

const currentBranch = '${{ steps.branch.outputs.branch }}';

Object.keys(zones).forEach(zone => {
const url = URL.replace('{ZONE}', zone);
const jwt = jaws.sign(zone, {application_id: 'native'}).jwt;
const zoneBranch = zone.match(/(edge|acceptance)/) ? zone : 'master';

if (currentBranch === zoneBranch) {
const formData = new FormData();
formData.append('functions', functionsJson);
formData.append('options', JSON.stringify({compile: false}));
formData.append('file', fs.createReadStream(nativeFile));

fetch(url, {
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${jwt}`,
},
}).then(response => {
console.log(`[${zone.slice(0, 3)}] ${response.status} - ${response.statusText}`);
});
}
});
- name: Publish to Betty Blocks
run: bun run publish.ts ${{ secrets.API_URL }} ${{ secrets.JAWS_SECRETS }} ${{ steps.branch.outputs.branch }}
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/node_modules/**
**/node_modules/**
jsonpath.min.js
111 changes: 111 additions & 0 deletions __tests__/jsonpath/1.0/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import jsonpath from '../../../functions/jsonpath/1.0';

const TEST_DATA = `
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95,
"sold": true
}
}
}
`;

describe('Jsonpath value', () => {
describe('with input type string and return type', () => {
test('object', async () => {
const { text } = await jsonpath({
data: TEST_DATA,
path: '$..book[(@.length-1)]',
outputType: 'text',
});
expect(text).toStrictEqual({
author: 'J. R. R. Tolkien',
category: 'fiction',
isbn: '0-395-19395-8',
price: 22.99,
title: 'The Lord of the Rings',
});
});

test('text', async () => {
const { text } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.color',
outputType: 'text',
});
expect(text).toBe('red');
});

test('number', async () => {
const { number } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.price',
outputType: 'number',
});
expect(number).toBe(19.95);
});

test('boolean', async () => {
const { boolean } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.sold',
outputType: 'boolean',
});
expect(boolean).toBe(true);
});

test('list returns the first item', async () => {
const { text } = await jsonpath({
data: TEST_DATA,
path: '$.store.book[*].author',
outputType: 'text',
});
expect(text).toStrictEqual('Nigel Rees');
});
});

test('with input type object return type text', async () => {
const { text } = await jsonpath({
data: JSON.parse(TEST_DATA),
path: '$.store.bicycle.color',
outputType: 'text',
});
expect(text).toBe('red');
});

test('with input type integer', async () =>
expect(async () =>
jsonpath({
data: 12345,
path: '$.store.bicycle.color',
outputType: 'text',
}),
).rejects.toThrow(/obj needs to be an object/));
});
104 changes: 104 additions & 0 deletions __tests__/jsonpath/1.1/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import jsonpath from '../../../functions/jsonpath/1.1';

const TEST_DATA = `
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95,
"sold": true
}
}
}
`;

describe('Jsonpath value', () => {
describe('with input type string and return type', () => {
test('object', async () => {
const { result } = await jsonpath({
data: TEST_DATA,
path: '$..book[(@.length-1)]',
});
expect(result).toStrictEqual({
author: 'J. R. R. Tolkien',
category: 'fiction',
isbn: '0-395-19395-8',
price: 22.99,
title: 'The Lord of the Rings',
});
});

test('text', async () => {
const { result } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.color',
});
expect(result).toBe('red');
});

test('number', async () => {
const { result } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.price',
});
expect(result).toBe(19.95);
});

test('boolean', async () => {
const { result } = await jsonpath({
data: TEST_DATA,
path: '$.store.bicycle.sold',
});
expect(result).toBe(true);
});

test('list returns the first item', async () => {
const { result } = await jsonpath({
data: TEST_DATA,
path: '$.store.book[*].author',
});
expect(result).toStrictEqual('Nigel Rees');
});
});

test('with input type object return type text', async () => {
const { result } = await jsonpath({
data: JSON.parse(TEST_DATA),
path: '$.store.bicycle.color',
});
expect(result).toBe('red');
});

test('with input type integer', async () =>
expect(async () =>
jsonpath({
data: 12345,
path: '$.store.bicycle.color',
}),
).rejects.toThrow(/obj needs to be an object/));
});
Loading