18 changed files with 225 additions and 107 deletions
@ -1,46 +0,0 @@ |
|||
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; |
@ -1,35 +0,0 @@ |
|||
const path = require('path'); |
|||
const fs = require('fs-extra'); |
|||
|
|||
const extractObj = (obj = {}, keys = []) => { |
|||
const ret = {}; |
|||
keys.forEach((key) => { |
|||
if (obj[key]) { |
|||
ret[key] = obj[key]; |
|||
} |
|||
}); |
|||
return ret; |
|||
}; |
|||
|
|||
const simplify = (dsl) => { |
|||
const { cells = [] } = dsl; |
|||
return { |
|||
cells: cells.map((cell) => { |
|||
if (cell.shape === 'edge') { |
|||
return extractObj(cell, ['id', 'shape', 'source', 'target']); |
|||
} else { |
|||
const newCell = extractObj(cell, ['id', 'shape']); |
|||
newCell.data = extractObj(cell.data, ['trigger', 'configData']); |
|||
return newCell; |
|||
} |
|||
}), |
|||
}; |
|||
}; |
|||
|
|||
const setup = async (dsl, logicBasePath) => { |
|||
const simpilifiedDSL = simplify(dsl); |
|||
const dslFilePath = path.join(logicBasePath, 'dsl.json'); |
|||
await fs.writeFile(dslFilePath, JSON.stringify(simpilifiedDSL, null, 2)); |
|||
}; |
|||
|
|||
module.exports = setup; |
@ -0,0 +1,3 @@ |
|||
{ |
|||
"extends": "../../.babelrc" |
|||
} |
@ -0,0 +1 @@ |
|||
node_modules |
@ -0,0 +1 @@ |
|||
# @imove/compile-code |
@ -0,0 +1,41 @@ |
|||
{ |
|||
"name": "@imove/compile-code", |
|||
"version": "0.0.1", |
|||
"description": "imove compile code", |
|||
"main": "dist/core.common.js", |
|||
"module": "dist/core.esm.js", |
|||
"types": "dist/types/index.d.ts", |
|||
"directories": { |
|||
"dist": "dist" |
|||
}, |
|||
"files": [ |
|||
"dist" |
|||
], |
|||
"publishConfig": { |
|||
"access": "public", |
|||
"registry": "http://registry.npmjs.org/" |
|||
}, |
|||
"keywords": [ |
|||
"imove", |
|||
"compile code" |
|||
], |
|||
"scripts": { |
|||
"prepublishOnly": "node scripts/prepublish.js", |
|||
"declare-type": "tsc --emitDeclarationOnly", |
|||
"build": "rollup -c & npm run declare-type", |
|||
"watch": "watch 'npm run build' ./src" |
|||
}, |
|||
"repository": { |
|||
"type": "git", |
|||
"url": "git+https://github.com/imgcook/iMove.git" |
|||
}, |
|||
"author": "smallstonesk", |
|||
"license": "MIT", |
|||
"dependencies": { |
|||
"@antv/x6": "^1.5.1" |
|||
}, |
|||
"devDependencies": { |
|||
"rollup": "^2.7.3", |
|||
"watch": "^1.0.2" |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
import path from 'path'; |
|||
import rollupBaseConfig from '../../rollup.config'; |
|||
import pkg from './package.json'; |
|||
|
|||
export default Object.assign(rollupBaseConfig, { |
|||
input: path.join(__dirname, './src/index.ts'), |
|||
output: [ |
|||
{ |
|||
file: pkg.main, |
|||
format: 'cjs', |
|||
}, |
|||
{ |
|||
file: pkg.module, |
|||
format: 'es', |
|||
}, |
|||
] |
|||
}); |
@ -0,0 +1,6 @@ |
|||
#!/usr/bin/env node
|
|||
|
|||
const path = require('path'); |
|||
const prePublish = require('../../../scripts/prepublish'); |
|||
|
|||
prePublish('@imove/plugin-store', path.join(__dirname, '../')); |
@ -1,20 +1,16 @@ |
|||
const path = require('path'); |
|||
const fs = require('fs-extra'); |
|||
|
|||
const INSERT_IMPORT_PLUGINS_COMMENT = '// import plugins here'; |
|||
const INSERT_USE_PLUGINS_COMMENT = '// use plugins here'; |
|||
|
|||
const setup = async (plugins = [], logicBasePath) => { |
|||
const entryFilePath = path.join(logicBasePath, 'index.js'); |
|||
const codes = await fs.readFile(entryFilePath, 'utf8'); |
|||
const modifiedContent = codes |
|||
const addPlugins = (originalCode: string = '', plugins: string[] = []): string => { |
|||
const modifiedContent: string = originalCode |
|||
.replace(new RegExp(INSERT_IMPORT_PLUGINS_COMMENT), () => { |
|||
return plugins.map((plugin, index) => `import plugin${index} from '${plugin}';`).join('\n'); |
|||
}) |
|||
.replace(new RegExp(INSERT_USE_PLUGINS_COMMENT), () => { |
|||
return plugins.map((_, index) => `logic.use(plugin${index});`).join('\n'); |
|||
}); |
|||
await fs.outputFile(entryFilePath, modifiedContent); |
|||
return modifiedContent; |
|||
}; |
|||
|
|||
module.exports = setup; |
|||
export default addPlugins; |
@ -0,0 +1,52 @@ |
|||
import {Cell} from '@antv/x6'; |
|||
|
|||
interface DSL { |
|||
cells: Cell.Properties[]; |
|||
} |
|||
|
|||
interface INodesFns { |
|||
[fileName: string]: string; |
|||
} |
|||
|
|||
const genEntryFile = (nodeIds: string[]): string => { |
|||
const imports: string[] = []; |
|||
const funcMaps: string[] = []; |
|||
nodeIds.forEach((id, idx) => { |
|||
const funcName = `fn_${idx}`; |
|||
imports.push(`import ${funcName} from './${id}';`); |
|||
funcMaps.push(`'${id}': ${funcName}`); |
|||
}); |
|||
const fileContent: string = [ |
|||
imports.join('\n'), |
|||
`const nodeFns = {\n ${funcMaps.join(',\n ')}\n};`, |
|||
'export default nodeFns;', |
|||
].join('\n'); |
|||
return fileContent; |
|||
}; |
|||
|
|||
const genNodeFns = (dsl: DSL): INodesFns => { |
|||
const nodeFns: INodesFns = {}; |
|||
const { cells = [] } = dsl; |
|||
const nodes = cells.filter((cell) => cell.shape !== 'edge'); |
|||
for (const { |
|||
id, |
|||
shape, |
|||
data: { label, code }, |
|||
} of nodes) { |
|||
const fileName: string = id + '.js'; |
|||
const descData: string = `// ${shape}: ${label}\n`; |
|||
const saveData: string = `${descData}\n${code}`; |
|||
nodeFns[fileName] = saveData; |
|||
} |
|||
return nodeFns; |
|||
}; |
|||
|
|||
const extract = (dsl: DSL): INodesFns => { |
|||
const nodeFns = genNodeFns(dsl); |
|||
const nodeIds = Object.keys(nodeFns).map(fileName => fileName.slice(0, -3)); |
|||
const entryFileContent = genEntryFile(nodeIds); |
|||
nodeFns['index.js'] = entryFileContent; |
|||
return nodeFns; |
|||
}; |
|||
|
|||
export default extract; |
@ -0,0 +1,34 @@ |
|||
import {Cell} from '@antv/x6'; |
|||
import addPlugins from './addPlugins'; |
|||
import simplifyDSL from './simplifyDSL'; |
|||
import extractNodeFns from './extractNodeFns'; |
|||
import logicTpl from './template/logic'; |
|||
import indexTpl from './template/index'; |
|||
import contextTpl from './template/context'; |
|||
|
|||
interface DSL { |
|||
cells: Cell.Properties[]; |
|||
} |
|||
|
|||
interface IOutput { |
|||
'nodeFns': { |
|||
[fileName: string]: string |
|||
}; |
|||
'context.js': string; |
|||
'dsl.json': string; |
|||
'index.js': string; |
|||
'logic.js': string; |
|||
} |
|||
|
|||
const compile = (dsl: DSL, plugins = []): IOutput => { |
|||
const output: IOutput = { |
|||
'nodeFns': extractNodeFns(dsl), |
|||
"context.js": contextTpl, |
|||
'dsl.json': JSON.stringify(simplifyDSL(dsl), null, 2), |
|||
'index.js': addPlugins(indexTpl, plugins), |
|||
'logic.js': logicTpl |
|||
}; |
|||
return output; |
|||
}; |
|||
|
|||
export default compile; |
@ -0,0 +1,32 @@ |
|||
import {Cell} from '@antv/x6'; |
|||
|
|||
interface DSL { |
|||
cells: Cell.Properties[]; |
|||
} |
|||
|
|||
const extractObj = (obj: Cell.Properties = {}, keys: string[] = []): Cell.Properties => { |
|||
const ret: Cell.Properties = {}; |
|||
keys.forEach((key) => { |
|||
if (obj[key]) { |
|||
ret[key] = obj[key]; |
|||
} |
|||
}); |
|||
return ret; |
|||
}; |
|||
|
|||
const simplifyDSL = (dsl: DSL): Cell.Properties => { |
|||
const { cells = [] } = dsl; |
|||
return { |
|||
cells: cells.map((cell) => { |
|||
if (cell.shape === 'edge') { |
|||
return extractObj(cell, ['id', 'shape', 'source', 'target']); |
|||
} else { |
|||
const newCell = extractObj(cell, ['id', 'shape', 'data']); |
|||
newCell.data = extractObj(cell.data, ['trigger', 'configData', 'ports']); |
|||
return newCell; |
|||
} |
|||
}), |
|||
}; |
|||
}; |
|||
|
|||
export default simplifyDSL; |
@ -0,0 +1,7 @@ |
|||
{ |
|||
"extends": "../../tsconfig", |
|||
"compilerOptions": { |
|||
"outDir": "dist/types" |
|||
}, |
|||
"include": ["./src", "./types"] |
|||
} |
Loading…
Reference in new issue