From dbf0828294ca754b5bef7391903a6a36015127a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isabella=20Sko=C5=99epov=C3=A1?= <isabella@skorepova.info> Date: Sat, 11 Jan 2020 14:29:32 +0100 Subject: [PATCH] Fix end and possible race conditions --- index.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 9453fdc8..83d99f47 100755 --- a/index.js +++ b/index.js @@ -2,33 +2,32 @@ const { spawnSync, spawn } = require('child_process') const config = require('./sitegin/config') -function onBabelExit(code) { - setTimeout(() => { - runBabel({ watch: true }) - }, 500) -} - -function runBabel({ watch = false }) { +function runBabel({ onExit, opts = [] }) { const babel = spawn( './node_modules/.bin/babel', - [ - 'theme-source', - '-d', - 'theme', - '--delete-dir-on-start', - watch ? '--watch' : null, - ].filter(a => a), + ['theme-source', '-d', 'theme', ...opts].filter(a => a), { stdio: 'inherit', cwd: __dirname }, ) - if (watch) babel.on('exit', onBabelExit) + if (onExit) babel.on('exit', onExit) +} + +function runBabelWatch() { + runBabel({ + onExit: () => { + setTimeout(() => runBabelWatch(), 500) + }, + opts: ['--skip-initial-build', '--watch'], + }) } ;(async () => { const opts = await config() - runBabel({ watch: !opts.nowatch }) - if (opts.config.options.nowatch) { - require('./sitegin/index.js') - } else { + await new Promise(res => + runBabel({ onExit: res, opts: ['--delete-dir-on-start'] }), + ) + const watch = !opts.config.options.nowatch + if (watch) { + runBabelWatch() spawnSync( process.argv[0], [ @@ -40,5 +39,7 @@ function runBabel({ watch = false }) { ], { stdio: ['inherit', 'inherit', 'inherit'] }, ) + } else { + require('./sitegin/index.js') } })() -- GitLab