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