Automatically add export statements

This commit is contained in:
jiaming743 2019-07-05 16:18:29 +08:00
parent b755a487aa
commit 98536da7d1
1 changed files with 37 additions and 3 deletions

View File

@ -1,10 +1,12 @@
const { copyDir, fileForEach, readFile, writeFile, unlinkDirFileByExtname } = require('./plugin/fs') const { copyDir, fileForEach, readFile, writeFile, unlinkDirFileByExtname, dirForEach } = require('./plugin/fs')
const print = require('./plugin/print') const print = require('./plugin/print')
const path = require('path') const path = require('path')
const doExec = require('./plugin/exec') const doExec = require('./plugin/exec')
const PACKAGE_SRC = './src' const PACKAGE_SRC = './src'
const COMPILE_SRC = './lib' const COMPILE_SRC = './lib'
const COMPONENTS_DIR = '/components'
const ENTRANCE = '/index.js'
async function start () { async function start () {
const copyPackage = await copyDir(PACKAGE_SRC, COMPILE_SRC) const copyPackage = await copyDir(PACKAGE_SRC, COMPILE_SRC)
@ -51,8 +53,18 @@ async function start () {
print.success('Finish adding css import statement!') print.success('Finish adding css import statement!')
const componentsExport = await addComponentsExport()
if (!componentsExport) {
print.error('Exception in adding components export statement!')
return false
}
print.success('Finish adding components export statement!')
print.yellow('-------------------------------------') print.yellow('-------------------------------------')
print.success(' DataV lib Compile Success! ') print.success(' DataV Lib Compile Success! ')
print.yellow('-------------------------------------') print.yellow('-------------------------------------')
return true return true
@ -125,7 +137,7 @@ async function compileLessToCss () {
async function addCssImport () { async function addCssImport () {
let importSuccess = true let importSuccess = true
await fileForEach(COMPILE_SRC + '/components', async src => { await fileForEach(COMPILE_SRC + COMPONENTS_DIR, async src => {
if (path.extname(src) !== '.js') return if (path.extname(src) !== '.js') return
let content = await readFile(src) let content = await readFile(src)
@ -146,6 +158,28 @@ async function addCssImport () {
return importSuccess return importSuccess
} }
async function addComponentsExport () {
const components = []
await dirForEach(COMPILE_SRC + COMPONENTS_DIR, src => {
components.push(src.split('/').slice(-1)[0])
})
const importString = components.reduce((all, current) => {
return all + '\n' + `export { default as ${current} } from '.${COMPONENTS_DIR}/${current}/index'`
}, '/**\n * EXPORT COMPONENTS\n */') + '\n'
const targetSrc = COMPILE_SRC + ENTRANCE
let content = await readFile(targetSrc)
content = importString + content
let write = await writeFile(targetSrc, content)
return write
}
module.exports = start module.exports = start