Skip to content

Instantly share code, notes, and snippets.

@eirikb
Last active January 29, 2020 20:02
Show Gist options
  • Save eirikb/8d503cef9efa8513bad35e25b6437317 to your computer and use it in GitHub Desktop.
Save eirikb/8d503cef9efa8513bad35e25b6437317 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const head = () => {
Object.assign(process.env, require('../local.settings').Values);
const orchestrators = {};
console.log.error = console.error;
const context = { log: console.log };
const setInput = input => context.bindings = { input: { input } };
const df = {
orchestrator(cb) {
return (additionalContext) => cb({ ...context, ...additionalContext })
},
callActivity(name, arg) {
setInput(arg);
return require(`../${name}`)(context, arg);
},
callSubOrchestrator(name, arg) {
setInput(arg);
return orchestrators[name]();
},
Task: {
all(array) {
return Promise.all(array);
}
},
setCustomStatus(...args) {
console.log(' ** setCustomStatus', args);
}
};
context.df = df;
module.exports = orchestrators;
};
function fnToString(fn) {
const res = fn.toString();
return res.slice(res.indexOf('{') + 1, res.lastIndexOf('}'));
}
const out = [];
out.push(fnToString(head));
const orchestrators = fs.readdirSync('..').filter(p => p.includes('Orchestrator'));
orchestrators.forEach(orchestrator => {
const js = fs.readFileSync(`../${orchestrator}/index.js`, 'utf-8')
.replace(/yield/g, 'await')
.replace(/module.exports/, `orchestrators.${orchestrator}`)
.replace(/function\*/, 'async function')
.replace(/const.*durable.*/, '');
out.push(js);
});
fs.writeFileSync('./generated.js', out.join('\n'));
// Run like this:
// require('./generated').MyFirstOrchestrator({ bindingData: { input: { hello: 'world' } } }).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment