diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45fe6b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules +.DS_Store +.idea +.vscode +package-lock.json \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..59274a0 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,4 @@ +const pagesEntry = require( './pagesEntry' ) +module.exports = { + pagesEntry +} \ No newline at end of file diff --git a/lib/pagesEntry.js b/lib/pagesEntry.js new file mode 100644 index 0000000..3281f3e --- /dev/null +++ b/lib/pagesEntry.js @@ -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 +} \ No newline at end of file diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..a5faa2f --- /dev/null +++ b/lib/util.js @@ -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 +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..a19e7dd --- /dev/null +++ b/package.json @@ -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 ", + "license": "ISC", + "bugs": { + "url": "https://github.com/megalojs/megalo-entry/issues" + }, + "homepage": "https://github.com/megalojs/megalo-entry#readme", + "dependencies": { + "json5": "^2.1.0" + } +}