Skip to content

Commit

Permalink
refactor: client store code style (#3683)
Browse files Browse the repository at this point in the history
  • Loading branch information
manniL authored and pi0 committed Sep 23, 2018
1 parent bc1cecf commit 054ea79
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/app/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ import Vuex from 'vuex'

Vue.use(Vuex)

// Recursive find files in {srcDir}/{dir.store}
const files = require.context('@/<%= dir.store %>', true, /^\.\/(?!<%= ignorePrefix %>)[^.]+\.(<%= extensions %>)$/)
const filenames = files.keys()

// Store
let storeData = {}

// Check if {dir.store}/index.js exists
let indexFilename
filenames.forEach((filename) => {
if (filename.indexOf('./index.') !== -1) {
indexFilename = filename
}
})
const indexFilename = filenames.find(name => name.includes('./index.'))

if (indexFilename) {
storeData = getModule(indexFilename)
}
Expand All @@ -35,21 +30,21 @@ if (typeof storeData !== 'function') {
const namePath = name.split(/\//)

name = namePath[namePath.length - 1]
if (name === 'state' || name === 'getters' || name === 'actions' || name === 'mutations') {
if (['state', 'getters', 'actions', 'mutations'].includes(name)) {
const module = getModuleNamespace(storeData, namePath, true)
appendModule(module, filename, name)
continue
}

// if file is foo/index.js
// it should save as foo
// If file is foo/index.js, it should be saved as foo
const isIndex = (name === 'index')
if (isIndex) {
namePath.pop()
}

const module = getModuleNamespace(storeData, namePath)
const fileModule = getModule(filename)

name = namePath.pop()
module[name] = module[name] || {}

Expand Down Expand Up @@ -100,7 +95,9 @@ function getModule (filename) {

function getModuleNamespace (storeData, namePath, forAppend = false) {
if (namePath.length === 1) {
if (forAppend) { return storeData }
if (forAppend) {
return storeData
}
return storeData.modules
}
const namespace = namePath.shift()
Expand Down

0 comments on commit 054ea79

Please sign in to comment.