Skip to content

Commit

Permalink
refactor: format and lint (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
puria authored Jun 13, 2024
1 parent ece1b9b commit 1990bb4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
"sandboxed",
"transpiled",
"typedoc",
"untracked"
"untracked",
"slangroom",
"matteo",
"cristino",
"rngseed",
"qrcode",
"pocketbase"
],
"flagWords": [],
"ignorePaths": [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Zenroom and zencode are part of the [DECODE project](https://decodeproject.eu) a
## 🎮 Quick start

In many use-cases you want to chain execution of different slangroom and
pass the output as keys/data to other slangrooms.
pass the output as keys/data to other slangroom contracts.
This small library helps to achieve that by putting your slangroom in an
array of steps.

Expand Down
14 changes: 7 additions & 7 deletions src/lib/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@ test('callbacks should work', async (t) => {
});

test('read from file', async (t) => {
process.env['FILES_DIR'] = ".";
process.env['FILES_DIR'] = '.';
const steps = {
steps: [
{
id: 'from file',
zencodeFromFile: 'test_contracts/hello.zen',
dataFromFile: 'test_contracts/hello.data.json',
keysFromFile: 'test_contracts/hello.keys.json',
}
]
},
],
};
const result = await execute(steps);
t.deepEqual(JSON.parse(result), {hello: "world", bonjour: 'monde'});
t.deepEqual(JSON.parse(result), { hello: 'world', bonjour: 'monde' });
});

test('read input data', async (t) => {
Expand All @@ -277,9 +277,9 @@ test('read input data', async (t) => {
id: 'from input data',
zencodeFromFile: 'test_contracts/hello.zen',
keysFromFile: 'test_contracts/hello.keys.json',
}
]
},
],
};
const result = await execute(steps, '{"hello": "world"}');
t.deepEqual(JSON.parse(result), {hello: "world", bonjour: 'monde'});
t.deepEqual(JSON.parse(result), { hello: 'world', bonjour: 'monde' });
});
50 changes: 39 additions & 11 deletions src/lib/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ import { timestamp } from '@slangroom/timestamp';
import { wallet } from '@slangroom/wallet';
import { zencode } from '@slangroom/zencode';

const slang = new Slangroom(fs, git, helpers, http, JSONSchema, oauth, pocketbase, qrcode, redis, shell, timestamp, wallet, zencode)
const slang = new Slangroom(
fs,
git,
helpers,
http,
JSONSchema,
oauth,
pocketbase,
qrcode,
redis,
shell,
timestamp,
wallet,
zencode,
);

const readFromFileContract = `Rule unknown ignore
Given I send path 'path' and read verbatim file content and output into 'content'
Given I have a 'string' named 'content'
Then print the 'content'
`
`;
const readFromFile = async (path: string): Promise<string> => {
const { result } = await slang.execute(readFromFileContract, {data: {path: path}});
const { result } = await slang.execute(readFromFileContract, {
data: { path: path },
});
return result.content as string;
}
};

type Step = {
readonly id: string;
Expand Down Expand Up @@ -82,20 +98,32 @@ type Results = {
[x: string]: string;
};

export const execute = async (steps: Steps, inputData?: string): Promise<string> => {
export const execute = async (
steps: Steps,
inputData?: string,
): Promise<string> => {
const results: Results = {};
let final = '';

var firstIteration = true;
let firstIteration = true;
for (const step of steps.steps) {
let data = step.dataFromFile ? await readFromFile(step.dataFromFile) : step.dataFromStep ? results[step.dataFromStep] : step.data;
let data = step.dataFromFile
? await readFromFile(step.dataFromFile)
: step.dataFromStep
? results[step.dataFromStep]
: step.data;
if (firstIteration) {
if (typeof(data) == 'undefined') data = inputData;
if (typeof data == 'undefined') data = inputData;
firstIteration = false;
}
let keys = step.keysFromFile ? await readFromFile(step.keysFromFile) : step.keysFromStep ? results[step.keysFromStep] : step.keys;
let keys = step.keysFromFile
? await readFromFile(step.keysFromFile)
: step.keysFromStep
? results[step.keysFromStep]
: step.keys;
const conf = step.conf ? step.conf : steps.conf;
const zencode = step.zencodeFromFile ? await readFromFile(step.zencodeFromFile) : step.zencode || '';
const zencode = step.zencodeFromFile
? await readFromFile(step.zencodeFromFile)
: step.zencode || '';
if (steps.verbose) {
console.log(`Executing contract ${step.id} `);
console.log(`DATA: ${data}`);
Expand Down

0 comments on commit 1990bb4

Please sign in to comment.