Skip to content

Commit

Permalink
feat: function pagesEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
zwwill committed Nov 26, 2018
1 parent a7de312 commit d695988
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.DS_Store
.idea
.vscode
package-lock.json
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const pagesEntry = require( './pagesEntry' )
module.exports = {
pagesEntry
}
40 changes: 40 additions & 0 deletions lib/pagesEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'
const fs = require('fs')
const path = require( 'path' )
const { walk4Obj } = require('./util')
const exp4parse = /\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\r\n]*|\s/g

// 获取指定目录下符合glob的所有文件
module.exports = function pagesEntry(file) {
let entries = {},
txt = '',
mainObj = {},
pages,
subpackages

try {
txt = fs.readFileSync(file,'utf8')
txt = txt.replace(exp4parse,'')
mainObj = walk4Obj(txt,'exportdefault')['config'] || {}

pages = mainObj.pages || []
subpackages = mainObj.subpackages || mainObj.subPackages || []

pages.forEach(p=>{
entries[p] = path.resolve(`src/${p}.js`)
})
subpackages.forEach(sp=>{
let {root, pages} = sp
if(root && pages.length>0){
pages.forEach(p=>{
entries[`${root}/${p}`] = path.resolve(`src/${root}/${p}.js`)
})
}
})

} catch (e) {
console.log(e)
}

return entries
}
44 changes: 44 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const path = require( 'path' )
const json5 = require('json5')

function resolve (...args) {
return path.resolve( __dirname, '../', ...args)
}

function walk4Obj (txt, startStr) {
if(typeof txt !== 'string' || typeof startStr !== 'string'){
return console.log('Props error')
}
let index = txt.indexOf(startStr),
resault = {},
begin = index + startStr.length,
end = begin

try{
if(index>0){
for(let _i = begin, _l=txt.length, _a=0;_i<_l;_i++) {
switch(txt.charAt(_i) || '') {
case '{': _a ++
break
case '}': _a --
break
default : break
}
if(_a <= 0) {
end = _i
break
}
}
resault = json5.parse(txt.substring(begin,end+1))
}

} catch (e) {
console.log(e)
}
return resault
}

module.exports = {
resolve,
walk4Obj
}
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@megalo/entry",
"version": "0.0.1",
"description": "",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/megalojs/megalo-entry.git"
},
"keywords": [
"megalo"
],
"files": [
"lib"
],
"author": "zwwill <https://github.com/zwwill>",
"license": "ISC",
"bugs": {
"url": "https://github.com/megalojs/megalo-entry/issues"
},
"homepage": "https://github.com/megalojs/megalo-entry#readme",
"dependencies": {
"json5": "^2.1.0"
}
}

0 comments on commit d695988

Please sign in to comment.