Skip to content

Commit

Permalink
导入vuex模板
Browse files Browse the repository at this point in the history
  • Loading branch information
lyaaaa committed May 17, 2019
1 parent ab96a3f commit e9f3349
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 51 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}
});
Empty file added src/store/actions.js
Empty file.
Empty file added src/store/getters.js
Empty file.
16 changes: 16 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -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
})
Empty file added src/store/mutation-types.js
Empty file.
7 changes: 7 additions & 0 deletions src/store/mutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as types from './mutation-types'
const mutations = {

}


export default mutations
5 changes: 5 additions & 0 deletions src/store/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const state = {

}

export default state
89 changes: 40 additions & 49 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
})
});
}
}
Empty file added template/store/actions.js
Empty file.
Empty file added template/store/getters.js
Empty file.
16 changes: 16 additions & 0 deletions template/store/index.js
Original file line number Diff line number Diff line change
@@ -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
})
Empty file.
7 changes: 7 additions & 0 deletions template/store/mutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as types from './mutation-types'
const mutations = {

}


export default mutations
5 changes: 5 additions & 0 deletions template/store/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const state = {

}

export default state

0 comments on commit e9f3349

Please sign in to comment.