perfect prepublish process

This commit is contained in:
JM 2019-07-03 22:00:43 +08:00
parent ca392ba2f7
commit 070a47011b
6 changed files with 199 additions and 47 deletions

View File

@ -1,3 +1,3 @@
const { start: step1 } = require('./publish/copyAndAbstractLess')
const start = require('./publish/index')
step1()
start()

View File

@ -1,36 +0,0 @@
const { copyDir, fileForEach, readFile } = require('./plugin/fs')
const print = require('./plugin/print')
const path = require('path')
const PACKAGE_SRC = './src'
const COMPILE_SRC = './lib'
async function start () {
const copyPackage = await copyDir(PACKAGE_SRC, COMPILE_SRC)
if (!copyPackage) {
print.error('Exception in copyPackage!')
return false
}
const abstract = await abstractLessFromVue()
}
async function abstractLessFromVue () {
fileForEach(COMPILE_SRC, async src => {
if (path.extname(src) !== '.vue') return
const data = await readFile(src).split('<style>')
const style = data
console.warn(data)
})
}
module.exports = {
start
}

151
publish/index.js Normal file
View File

@ -0,0 +1,151 @@
const { copyDir, fileForEach, readFile, writeFile, unlinkDirFileByExtname } = require('./plugin/fs')
const print = require('./plugin/print')
const path = require('path')
const doExec = require('./plugin/exec')
const PACKAGE_SRC = './src'
const COMPILE_SRC = './lib'
async function start () {
const copyPackage = await copyDir(PACKAGE_SRC, COMPILE_SRC)
if (!copyPackage) {
print.error('Exception in file copy!')
return false
}
print.success('Complete file copy!')
const abstract = await abstractLessFromVue()
if (!abstract) {
print.error('Exception in less file extraction!')
return false
}
print.success('Complete less file extraction!')
await compileLessToCss()
print.success('Complete less compilation to css!')
const unlink = await unlinkDirFileByExtname(COMPILE_SRC, ['.less'])
if (!unlink) {
print.error('Exception in less file deletion!')
return false
}
print.success('Complete less file deletion!')
const addImport = await addCssImport()
if (!addImport) {
print.error('Exception in adding css import statement!')
return false
}
print.success('Finish adding css import statement!')
print.yellow('-------------------------------------')
print.success(' DataV lib Compile Success! ')
print.yellow('-------------------------------------')
return true
}
async function abstractLessFromVue () {
let abstractSuccess = true
await fileForEach(COMPILE_SRC, async src => {
if (path.extname(src) !== '.vue') return
let template = await readFile(src)
let style = template.match(/<style[ \S\n\r]*/g)
if (style) style = style[0]
if (!style) return
style = style.replace(/<style[ a-z="']*>(\n|\r)?|<\/style>/g, '')
style = style.replace(/[\n\r]*$/, '')
const styleSrc = src.replace('.vue', '.less')
let write = await writeFile(styleSrc, style)
if (!write) {
print.error(styleSrc + ' write error!')
abstractSuccess = false
}
template = template.replace(/<style[ \S\n\r]*/g, '')
template = template.replace(/[\n\r]*$/, '')
write = await writeFile(src, template)
if (!write) {
print.error(src + ' rewrite error!')
abstractSuccess = false
}
})
return abstractSuccess
}
async function compileLessToCss () {
let compileSuccess = true
await fileForEach(COMPILE_SRC, async src => {
if (path.extname(src) !== '.less') return
src = src.replace('./', '')
const execString = `lessc ${src} ${src.replace('less', 'css')}`
print.yellow(execString, {
maxBuffer: 1024 ** 5
})
const compile = await doExec(execString)
if (!compile) {
print.error(execString + ' Error!')
compileSuccess = false
}
})
return compileSuccess
}
async function addCssImport () {
let importSuccess = true
await fileForEach(COMPILE_SRC + '/components', async src => {
if (path.extname(src) !== '.js') return
let content = await readFile(src)
if (content.search(/import[ \S]* from '[\S]*\.vue'/) === -1) return
content = `import './src/main.css'\n` + content
let write = await writeFile(src, content)
if (!write) {
print.error(src + ' write import error!')
importSuccess = false
}
})
return importSuccess
}
module.exports = start

19
publish/plugin/exec.js Normal file
View File

@ -0,0 +1,19 @@
const { exec } = require('child_process')
function doExec (execString, maxBuffer = 1024 ** 5) {
return new Promise(resolve => {
exec(execString, {
maxBuffer
}, err => {
if (err) {
console.error(err)
resolve(false)
} else {
resolve(true)
}
})
})
}
module.exports = doExec

View File

@ -33,7 +33,7 @@ function mkdir (src) {
return new Promise(resolve => {
fs.mkdir(src, err => {
if (err) {
console.warn(err)
console.error(err)
resolve(false)
}
@ -186,7 +186,7 @@ async function unlinkDirFileByExtname (src, extnames = []) {
async function copyDir (src, target) {
if (!src || !target) {
console.warn('copyDir missing parameters!')
console.error('copyDir missing parameters!')
return false
}
@ -243,7 +243,7 @@ async function copyDir (src, target) {
async function fileForEach (src, callback) {
if (!src || !callback) {
console.warn('fileForEach missing parameters!')
console.error('fileForEach missing parameters!')
return false
}
@ -285,7 +285,7 @@ async function readFile (src, encoding = 'utf8') {
return new Promise(resolve => {
fs.readFile(src, encoding, (err, data) => {
if (err) {
console.warn(err)
console.error(err)
resolve(false)
} else {
@ -295,6 +295,20 @@ async function readFile (src, encoding = 'utf8') {
})
}
async function writeFile (src, string, encoding = 'utf8') {
return new Promise(resolve => {
fs.writeFile(src, string, encoding, err => {
if (err) {
console.error(err)
resolve(false)
} else {
resolve(true)
}
})
})
}
module.exports = {
readDir,
stat,
@ -304,5 +318,6 @@ module.exports = {
unlinkDirFileByExtname,
copyDir,
fileForEach,
readFile
readFile,
writeFile
}

View File

@ -3,16 +3,19 @@ const print = {
console.log(info)
},
warn (info) {
console.log('\033[31;33m' + info)
console.log('\033[31;33m' + info + '\033[0m')
},
error (info) {
console.log('\033[31;30m' + info)
console.log('\033[31;40m' + info + '\033[0m')
},
tip (info) {
console.log('\033[40;32m' + info)
console.log('\033[40;32m' + info + '\033[0m')
},
success (info) {
console.log('\033[42;30m' + info)
console.log('\033[42;30m' + info + '\033[0m')
},
yellow (info) {
console.log('\033[31;33m' + info + '\033[0m')
}
}