diff --git a/packages/cli/src/cmd/dev/addPlugins.js b/packages/cli/src/cmd/dev/addPlugins.js new file mode 100644 index 0000000..0064cef --- /dev/null +++ b/packages/cli/src/cmd/dev/addPlugins.js @@ -0,0 +1,24 @@ +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); + const modifiedContent = codes + .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); +}; + +module.exports = setup; \ No newline at end of file diff --git a/packages/cli/src/cmd/dev/index.js b/packages/cli/src/cmd/dev/index.js index 5419f9f..262cadd 100644 --- a/packages/cli/src/cmd/dev/index.js +++ b/packages/cli/src/cmd/dev/index.js @@ -15,7 +15,7 @@ class Dev extends Base { save = async (req, res) => { - const {outputPath} = this.config; + const {outputPath, plugins} = this.config; // check outputPath whether exsited await fs.ensureDir(outputPath); @@ -32,6 +32,7 @@ class Dev extends Base { await simplifyDSL(dsl, outputPath); await extractCodes(dsl, outputPath); await fs.copy(TPL_PATH, outputPath); + await addPlugons(plugins, outputPath); await mergePkg(dsl, this.projectPath); await fs.outputFile(CACHE_DSL_FILE, JSON.stringify(dsl, null, 2)); res.status(200).json({isCompiled: true}).end(); diff --git a/packages/cli/src/cmd/dev/template/index.js b/packages/cli/src/cmd/dev/template/index.js index 3f60d68..c09d49a 100644 --- a/packages/cli/src/cmd/dev/template/index.js +++ b/packages/cli/src/cmd/dev/template/index.js @@ -1,6 +1,10 @@ import Logic from './logic'; import dsl from './dsl.json'; +// import plugins here + const logic = new Logic({dsl}); +// use plugins here + export default logic;