From f778a3ee17a00a8fff6fe723ff35e2f7123e02fb Mon Sep 17 00:00:00 2001
From: monkeycf <2548772327@qq.com>
Date: Tue, 3 Dec 2019 22:37:43 +0800
Subject: [PATCH] new generate store

---
 plop-templates/store/index.hbs | 16 +++++++++
 plop-templates/store/prompt.js | 62 ++++++++++++++++++++++++++++++++++
 plopfile.js                    |  2 ++
 3 files changed, 80 insertions(+)
 create mode 100644 plop-templates/store/index.hbs
 create mode 100644 plop-templates/store/prompt.js

diff --git a/plop-templates/store/index.hbs b/plop-templates/store/index.hbs
new file mode 100644
index 00000000..4f8e2dc0
--- /dev/null
+++ b/plop-templates/store/index.hbs
@@ -0,0 +1,16 @@
+{{#if state}}
+const state = {}
+{{/if}}
+
+{{#if mutations}}
+const mutations = {}
+{{/if}}
+
+{{#if actions}}
+const actions = {}
+{{/if}}
+
+export default {
+  namespaced: true,
+  {{options}}
+}
diff --git a/plop-templates/store/prompt.js b/plop-templates/store/prompt.js
new file mode 100644
index 00000000..bcbc11d1
--- /dev/null
+++ b/plop-templates/store/prompt.js
@@ -0,0 +1,62 @@
+const { notEmpty } = require('../utils.js')
+
+module.exports = {
+  description: 'generate store',
+  prompts: [{
+    type: 'input',
+    name: 'name',
+    message: 'store name please',
+    validate: notEmpty('name')
+  },
+  {
+    type: 'checkbox',
+    name: 'blocks',
+    message: 'Blocks:',
+    choices: [{
+      name: 'state',
+      value: 'state',
+      checked: true
+    },
+    {
+      name: 'mutations',
+      value: 'mutations',
+      checked: true
+    },
+    {
+      name: 'actions',
+      value: 'actions',
+      checked: true
+    }
+    ],
+    validate(value) {
+      if (!value.includes('state') || !value.includes('mutations')) {
+        return 'store require at least state and mutations'
+      }
+      return true
+    }
+  }
+  ],
+  actions(data) {
+    const name = '{{name}}'
+    const { blocks } = data
+    const options = ['state', 'mutations']
+    const joinFlag = `,
+  `
+    if (blocks.length === 3) {
+      options.push('actions')
+    }
+
+    const actions = [{
+      type: 'add',
+      path: `src/store/modules/${name}.js`,
+      templateFile: 'plop-templates/store/index.hbs',
+      data: {
+        options: options.join(joinFlag),
+        state: blocks.includes('state'),
+        mutations: blocks.includes('mutations'),
+        actions: blocks.includes('actions')
+      }
+    }]
+    return actions
+  }
+}
diff --git a/plopfile.js b/plopfile.js
index 9f3147e2..57387bf1 100644
--- a/plopfile.js
+++ b/plopfile.js
@@ -1,7 +1,9 @@
 const viewGenerator = require('./plop-templates/view/prompt')
 const componentGenerator = require('./plop-templates/component/prompt')
+const storeGenerator = require('./plop-templates/store/prompt.js')
 
 module.exports = function(plop) {
   plop.setGenerator('view', viewGenerator)
   plop.setGenerator('component', componentGenerator)
+  plop.setGenerator('store', storeGenerator)
 }