Browse Source

feat: add logic plugin templates

master
smallstonesk 4 years ago
parent
commit
712e5252f6
  1. 24
      packages/cli/src/cmd/dev/addPlugins.js
  2. 3
      packages/cli/src/cmd/dev/index.js
  3. 4
      packages/cli/src/cmd/dev/template/index.js

24
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;

3
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();

4
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;

Loading…
Cancel
Save