add build error handling so builds don't silently fail

This commit is contained in:
Joe Pea 2020-05-22 23:59:25 -07:00
parent 5a5b5aa932
commit b72cb3d94c
4 changed files with 80 additions and 54 deletions

View File

@ -9,8 +9,8 @@ const version = process.env.VERSION || require('../package.json').version
const chokidar = require('chokidar') const chokidar = require('chokidar')
const path = require('path') const path = require('path')
const build = function (opts) { async function build(opts) {
rollup await rollup
.rollup({ .rollup({
input: opts.input, input: opts.input,
plugins: (opts.plugins || []).concat([ plugins: (opts.plugins || []).concat([
@ -27,31 +27,34 @@ const build = function (opts) {
var dest = 'lib/' + (opts.output || opts.input) var dest = 'lib/' + (opts.output || opts.input)
console.log(dest) console.log(dest)
bundle.write({ return bundle.write({
format: 'iife', format: 'iife',
file: dest, file: dest,
strict: false strict: false
}) })
}) })
.catch(function (err) {
console.error(err)
})
} }
const buildCore = function () {
build({ async function buildCore() {
const promises = []
promises.push(build({
input: 'src/core/index.js', input: 'src/core/index.js',
output: 'docsify.js' output: 'docsify.js'
}) }))
if (isProd) { if (isProd) {
build({ promises.push(build({
input: 'src/core/index.js', input: 'src/core/index.js',
output: 'docsify.min.js', output: 'docsify.min.js',
plugins: [uglify()] plugins: [uglify()]
}) }))
} }
await Promise.all(promises)
} }
const buildAllPlugin = function () {
async function buildAllPlugin() {
var plugins = [ var plugins = [
{name: 'search', input: 'search/index.js'}, {name: 'search', input: 'search/index.js'},
{name: 'ga', input: 'ga.js'}, {name: 'ga', input: 'ga.js'},
@ -64,8 +67,8 @@ const buildAllPlugin = function () {
{name: 'gitalk', input: 'gitalk.js'} {name: 'gitalk', input: 'gitalk.js'}
] ]
plugins.forEach(item => { const promises = plugins.map(item => {
build({ return build({
input: 'src/plugins/' + item.input, input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.js' output: 'plugins/' + item.name + '.js'
}) })
@ -73,47 +76,59 @@ const buildAllPlugin = function () {
if (isProd) { if (isProd) {
plugins.forEach(item => { plugins.forEach(item => {
build({ promises.push(build({
input: 'src/plugins/' + item.input, input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.min.js', output: 'plugins/' + item.name + '.min.js',
plugins: [uglify()] plugins: [uglify()]
}) }))
}) })
} }
await Promise.all(promises)
}
async function main() {
if (!isProd) {
chokidar
.watch(['src/core', 'src/plugins'], {
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
if (dirs[1] === 'core') {
buildCore()
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`
build({
input,
output: 'plugins/' + name + '.js'
})
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
buildAllPlugin()
})
} else {
await Promise.all([
buildCore(),
buildAllPlugin()
])
}
} }
if (!isProd) { main().catch((e) => {
chokidar console.error(e)
.watch(['src/core', 'src/plugins'], { process.exit(1)
atomic: true, })
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
if (dirs[1] === 'core') {
buildCore()
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`
build({
input,
output: 'plugins/' + name + '.js'
})
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
buildAllPlugin()
})
} else {
buildCore()
buildAllPlugin()
}

View File

@ -5,8 +5,8 @@ const {spawn} = require('child_process')
const args = process.argv.slice(2) const args = process.argv.slice(2)
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => { fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
if (err) { if (err) {
console.log('err', err) console.error('err', err)
return process.exit(1)
} }
files.map(async (file) => { files.map(async (file) => {
if (/\.styl/g.test(file)) { if (/\.styl/g.test(file)) {
@ -31,7 +31,14 @@ fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
}); });
stylusCMD.on('close', (code) => { stylusCMD.on('close', (code) => {
console.log(`[Stylus Build ] child process exited with code ${code}`); const message = `[Stylus Build ] child process exited with code ${code}`
if (code !== 0) {
console.error(message);
process.exit(code)
} else {
console.log(message);
}
}); });
} else { } else {
return return

View File

@ -8,5 +8,8 @@ files.forEach(file => {
file = path.resolve('lib/themes', file) file = path.resolve('lib/themes', file)
cssnano(fs.readFileSync(file)).then(result => { cssnano(fs.readFileSync(file)).then(result => {
fs.writeFileSync(file, result.css) fs.writeFileSync(file, result.css)
}).catch(e => {
console.error(e)
process.exit(1)
}) })
}) })

View File

@ -24,11 +24,12 @@ rollup
var dest = 'packages/docsify-server-renderer/build.js' var dest = 'packages/docsify-server-renderer/build.js'
console.log(dest) console.log(dest)
bundle.write({ return bundle.write({
format: 'cjs', format: 'cjs',
file: dest file: dest
}) })
}) })
.catch(function (err) { .catch(function (err) {
console.error(err) console.error(err)
process.exit(1)
}) })