You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 lines
1.4 KiB

const path = require('path');
const fs = require('fs-extra');
const writeEntryFile = async (basePath, fileIds) => {
const imports = [];
const funcMaps = [];
fileIds.forEach((id, idx) => {
const funcName = `fn_${idx}`;
imports.push(`import ${funcName} from './${id}';`);
funcMaps.push(`'${id}': ${funcName}`);
});
const fileContent = [
imports.join('\n'),
`const nodeFns = {\n ${funcMaps.join(',\n ')}\n};`,
'export default nodeFns;',
].join('\n');
const entryFilePath = path.join(basePath, 'index.js');
await fs.outputFile(entryFilePath, fileContent, { encoding: 'utf8', flag: 'w' });
};
const writeNodeCodes = async (basePath, dsl) => {
const fileIds = [];
const { cells = [] } = dsl;
const nodes = cells.filter((cell) => cell.shape !== 'edge');
for (const {
id,
shape,
data: { label, code },
} of nodes) {
fileIds.push(id);
const filePath = path.join(basePath, id + '.js');
const preData = `// ${shape}: ${label}\n`;
const saveData = `${preData}\n${code}`;
await fs.outputFile(filePath, saveData, { encoding: 'utf8', flag: 'w' });
}
return fileIds;
};
const setup = async (dsl, logicBasePath) => {
const nodeCodesPath = path.join(logicBasePath, 'nodeFns');
await fs.remove(nodeCodesPath);
const fileIds = await writeNodeCodes(nodeCodesPath, dsl);
await writeEntryFile(nodeCodesPath, fileIds);
};
module.exports = setup;