From e9f33491aa9b4633344f9e2a2add9df6eab72ad7 Mon Sep 17 00:00:00 2001 From: lyaaaa <1007634863@qq.com> Date: Fri, 17 May 2019 15:29:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5vuex=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 8 ++- src/store/actions.js | 0 src/store/getters.js | 0 src/store/index.js | 16 ++++++ src/store/mutation-types.js | 0 src/store/mutations.js | 7 +++ src/store/state.js | 5 ++ src/util.js | 89 ++++++++++++++------------------ template/store/actions.js | 0 template/store/getters.js | 0 template/store/index.js | 16 ++++++ template/store/mutation-types.js | 0 template/store/mutations.js | 7 +++ template/store/state.js | 5 ++ 14 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 src/store/actions.js create mode 100644 src/store/getters.js create mode 100644 src/store/index.js create mode 100644 src/store/mutation-types.js create mode 100644 src/store/mutations.js create mode 100644 src/store/state.js create mode 100644 template/store/actions.js create mode 100644 template/store/getters.js create mode 100644 template/store/index.js create mode 100644 template/store/mutation-types.js create mode 100644 template/store/mutations.js create mode 100644 template/store/state.js diff --git a/src/index.js b/src/index.js index 25db45a..c684fdb 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,8 @@ const commander = require('commander'); const inquirer = require('inquirer'); const shell = require('shelljs'); -const util = require('./util'); +const path = require('path'); +const { __root__, existsFile, copyFile } = require('./util'); commander.version('0.0.1') .description('init extension project') @@ -21,6 +22,9 @@ inquirer // 是否需要vuex; if(answers.vuex){ shell.exec('npm install vuex --save'); - util.makeStore(); + var url = path.join(__root__, '/src/store'); + var src = path.join(__root__, '/template/store'); + // 将vuex中的store模板直接拷贝到src下面 + existsFile(src, url, copyFile); } }); \ No newline at end of file diff --git a/src/store/actions.js b/src/store/actions.js new file mode 100644 index 0000000..e69de29 diff --git a/src/store/getters.js b/src/store/getters.js new file mode 100644 index 0000000..e69de29 diff --git a/src/store/index.js b/src/store/index.js new file mode 100644 index 0000000..c939787 --- /dev/null +++ b/src/store/index.js @@ -0,0 +1,16 @@ +import Vue from 'vue' +import Vuex from 'vuex' +import * as actions from './actions' +import * as getters from './getters' +import state from './state' +import mutations from './mutations' + +Vue.use(Vuex) + +export default new Vuex.Store({ + actions, + getters, + state, + mutations, + strict: true +}) diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js new file mode 100644 index 0000000..e69de29 diff --git a/src/store/mutations.js b/src/store/mutations.js new file mode 100644 index 0000000..3e0d7d0 --- /dev/null +++ b/src/store/mutations.js @@ -0,0 +1,7 @@ +import * as types from './mutation-types' +const mutations = { + +} + + +export default mutations \ No newline at end of file diff --git a/src/store/state.js b/src/store/state.js new file mode 100644 index 0000000..6adc86e --- /dev/null +++ b/src/store/state.js @@ -0,0 +1,5 @@ +const state = { + +} + +export default state \ No newline at end of file diff --git a/src/util.js b/src/util.js index e7270dd..8dbd317 100644 --- a/src/util.js +++ b/src/util.js @@ -1,57 +1,48 @@ const path = require('path'); const fs = require('fs'); -const __root__ = process.cwd() +const stat = fs.stat; module.exports = { - makeStore: function () { - var url = path.join(__root__, '/src/store'); -var VUEXJS = [{ - name: '/actions.js', - content: '' - }, - { - name: '/getters.js', - content: '' - }, - { - name: '/index.js', - content: `import Vue from 'vue' -import Vuex from 'vuex' -import * as actions from './actions' -import * as getters from './getters' -import state from './state' -import mutations from './mutations' -Vue.use(Vuex) -export default new Vuex.Store({ -actions, -getters, -state, -mutations, -strict: true})` - }, - { - name: '/mutation-types.js', - content: '' - }, - { - name: '/mutation.js', - content: `import * as types from './mutation-types'\n -const mutations = {}\n -export default mutations` + __root__: process.cwd(), + // 复制文件夹到指定目录下 + copyFile: function (src, dst) { + // 读取目录中的所有文件/目录 + fs.readdir(src, function (err, paths) { + if (err) { + throw err; + } + paths.forEach(function (path) { + var _src = src + '/' + path, + _dst = dst + '/' + path, + readable, writable; + stat(_src, function (err, st) { + if (err) { + throw err; + } + // 判断是否为文件 + if (st.isFile()) { + readable = fs.createReadStream(_src); + writable = fs.createWriteStream(_dst); + readable.pipe(writable); + } + // 如果是目录则递归调用自身 + else if (st.isDirectory()) { + exists(_src, _dst, copyFile); + } + }); + }); + }); }, - { - name: '/state.js', - content: `const state = {}\n -export default state` - } -] - fs.mkdir(url, (err) => { - if (err) throw err; - for (let i = 0; i < VUEXJS.length; i++) { - fs.writeFile(path.join(__root__, '/src/store', VUEXJS[i].name), VUEXJS[i].content, err => { - if (err) throw err; - }) + // 判断文件夹是否存在,不存在需要先创建目录 + existsFile: function (src, dst, callback) { + fs.exists(dst, function (exists) { + if (exists) { + callback(src, dst); + } else { + fs.mkdir(dst, function () { + callback(src, dst); + }); } - }) + }); } } \ No newline at end of file diff --git a/template/store/actions.js b/template/store/actions.js new file mode 100644 index 0000000..e69de29 diff --git a/template/store/getters.js b/template/store/getters.js new file mode 100644 index 0000000..e69de29 diff --git a/template/store/index.js b/template/store/index.js new file mode 100644 index 0000000..c939787 --- /dev/null +++ b/template/store/index.js @@ -0,0 +1,16 @@ +import Vue from 'vue' +import Vuex from 'vuex' +import * as actions from './actions' +import * as getters from './getters' +import state from './state' +import mutations from './mutations' + +Vue.use(Vuex) + +export default new Vuex.Store({ + actions, + getters, + state, + mutations, + strict: true +}) diff --git a/template/store/mutation-types.js b/template/store/mutation-types.js new file mode 100644 index 0000000..e69de29 diff --git a/template/store/mutations.js b/template/store/mutations.js new file mode 100644 index 0000000..3e0d7d0 --- /dev/null +++ b/template/store/mutations.js @@ -0,0 +1,7 @@ +import * as types from './mutation-types' +const mutations = { + +} + + +export default mutations \ No newline at end of file diff --git a/template/store/state.js b/template/store/state.js new file mode 100644 index 0000000..6adc86e --- /dev/null +++ b/template/store/state.js @@ -0,0 +1,5 @@ +const state = { + +} + +export default state \ No newline at end of file