Skip to content

Commit

Permalink
Make cleaning async and deal with the consequences of that
Browse files Browse the repository at this point in the history
Use rimraf as a function, fixes #181

Includes a bit of refactoring to reduce duplicated spinner management
  • Loading branch information
insin committed Dec 31, 2016
1 parent fc6e550 commit 4f9cc4b
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 202 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**Fixed:**

* Fix `clean` commands in paths with spaces [[#181](https://github.com/insin/nwb/issues/181)]

**Changed:**

* The default build for a React component demo app now supports use of a `demo/public/` directory for static content.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"eslint-config-jonnybuchanan": "4.7.0",
"eventsource": "0.2.1",
"nyc": "10.0.0",
"run-series": "1.1.4",
"shelljs": "0.7.5",
"tree-kill": "1.1.0"
},
Expand Down
18 changes: 5 additions & 13 deletions src/commands/build-demo.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'path'

import glob from 'glob'
import ora from 'ora'
import runSeries from 'run-series'

import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanDemo from './clean-demo'

/**
Expand Down Expand Up @@ -43,15 +42,8 @@ export default function buildDemo(args, cb) {
config.plugins.copy = [{from: path.resolve('demo/public/'), to: dist}]
}

cleanDemo(args)

let spinner = ora('Building demo').start()
webpackBuild(args, config, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanDemo(args, cb),
(cb) => webpackBuild('demo', args, config, cb),
], cb)
}
18 changes: 5 additions & 13 deletions src/commands/build-inferno-app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path'

import glob from 'glob'
import ora from 'ora'
import runSeries from 'run-series'

import {getDefaultHTMLConfig} from '../appConfig'
import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanApp from './clean-app'

// Using a config function as webpackBuild() sets NODE_ENV to production if it
Expand Down Expand Up @@ -52,15 +51,8 @@ function buildConfig(args) {
export default function buildInfernoApp(args, cb) {
let dist = args._[2] || 'dist'

cleanApp({_: ['clean-app', dist]})

let spinner = ora(`Building Inferno app`).start()
webpackBuild(args, buildConfig, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild('Inferno app', args, buildConfig, cb),
], cb)
}
18 changes: 5 additions & 13 deletions src/commands/build-preact-app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path'

import glob from 'glob'
import ora from 'ora'
import runSeries from 'run-series'

import {getDefaultHTMLConfig} from '../appConfig'
import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanApp from './clean-app'

// Using a config function as webpackBuild() sets NODE_ENV to production if it
Expand Down Expand Up @@ -52,15 +51,8 @@ function buildConfig(args) {
export default function buildPreactApp(args, cb) {
let dist = args._[2] || 'dist'

cleanApp({_: ['clean-app', dist]})

let spinner = ora(`Building Preact app`).start()
webpackBuild(args, buildConfig, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild('Preact app', args, buildConfig, cb),
], cb)
}
18 changes: 5 additions & 13 deletions src/commands/build-react-app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path'

import glob from 'glob'
import ora from 'ora'
import runSeries from 'run-series'

import {getDefaultHTMLConfig} from '../appConfig'
import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanApp from './clean-app'

// Using a config function as webpackBuild() sets NODE_ENV to production if it
Expand Down Expand Up @@ -73,19 +72,12 @@ function buildConfig(args) {
export default function buildReactApp(args, cb) {
let dist = args._[2] || 'dist'

cleanApp({_: ['clean-app', dist]})

let library = 'React'
if (args.inferno) library = 'Inferno (React compat)'
else if (args.preact) library = 'Preact (React compat)'

let spinner = ora(`Building ${library} app`).start()
webpackBuild(args, buildConfig, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild(`${library} app`, args, buildConfig, cb),
], cb)
}
19 changes: 9 additions & 10 deletions src/commands/build-react-component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glob from 'glob'
import runSeries from 'run-series'

import moduleBuild from '../moduleBuild'
import buildDemo from './build-demo'
Expand Down Expand Up @@ -32,14 +33,12 @@ export default function buildModule(args, cb) {
}
}

moduleBuild(args, config, (err) => {
if (err) return cb(err)
// Disable demo build with --no-demo or --no-demo-build
if (args.demo === false ||
args['demo-build'] === false ||
glob.sync('demo/').length === 0) {
return cb(null)
}
buildDemo(args, cb)
})
let tasks = [(cb) => moduleBuild(args, config, cb)]
// Disable demo build with --no-demo or --no-demo-build
if (args.demo !== false &&
args['demo-build'] !== false &&
glob.sync('demo/').length !== 0) {
tasks.push((cb) => buildDemo(args, cb))
}
runSeries(tasks, cb)
}
18 changes: 5 additions & 13 deletions src/commands/build-react.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'path'

import ora from 'ora'
import runSeries from 'run-series'

import {UserError} from '../errors'
import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanApp from './clean-app'

// Using a config function as webpackBuild() sets NODE_ENV to production if it
Expand Down Expand Up @@ -77,19 +76,12 @@ export default function buildReact(args, cb) {

let dist = args._[2] || 'dist'

cleanApp({_: ['clean-app', dist]})

let library = 'React'
if (args.inferno) library = 'Inferno (React compat)'
else if (args.preact) library = 'Preact (React compat)'

let spinner = ora(`Building ${library} app`).start()
webpackBuild(args, buildConfig, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild(`${library} app`, args, buildConfig, cb),
], cb)
}
18 changes: 5 additions & 13 deletions src/commands/build-web-app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path'

import glob from 'glob'
import ora from 'ora'
import runSeries from 'run-series'

import {getDefaultHTMLConfig} from '../appConfig'
import webpackBuild from '../webpackBuild'
import {logBuildResults} from '../webpackUtils'
import cleanApp from './clean-app'

// Using a config function as webpackBuild() sets NODE_ENV to production if it
Expand Down Expand Up @@ -51,15 +50,8 @@ function buildConfig(args) {
export default function buildWebApp(args, cb) {
let dist = args._[2] || 'dist'

cleanApp({_: ['clean-app', dist]})

let spinner = ora('Building app').start()
webpackBuild(args, buildConfig, (err, stats) => {
if (err) {
spinner.fail()
return cb(err)
}
logBuildResults(stats, spinner)
cb()
})
runSeries([
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild(`app`, args, buildConfig, cb),
], cb)
}
17 changes: 3 additions & 14 deletions src/commands/clean-app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import exec from '../exec'
import {clean} from '../utils'

import ora from 'ora'

export default function cleanApp(args) {
export default function cleanApp(args, cb) {
let dist = args._[1] || 'dist'

let spinner = ora('Cleaning app').start()
try {
exec('rimraf', ['coverage', dist])
spinner.succeed()
}
catch (err) {
spinner.fail()
throw err
}
clean('app', ['coverage', dist], cb)
}
16 changes: 3 additions & 13 deletions src/commands/clean-demo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import exec from '../exec'
import {clean} from '../utils'

import ora from 'ora'

export default function cleanModule(args) {
let spinner = ora('Cleaning demo').start()
try {
exec('rimraf', ['demo/dist'])
spinner.succeed()
}
catch (err) {
spinner.fail()
throw err
}
export default function cleanDemo(args, cb) {
clean('demo', ['demo/dist'], cb)
}
16 changes: 3 additions & 13 deletions src/commands/clean-module.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import exec from '../exec'
import {clean} from '../utils'

import ora from 'ora'

export default function cleanModule(args) {
let spinner = ora('Cleaning module').start()
try {
exec('rimraf', ['coverage', 'es', 'lib', 'umd'])
spinner.succeed()
}
catch (err) {
spinner.fail()
throw err
}
export default function cleanModule(args, cb) {
clean('module', ['coverage', 'es', 'lib', 'umd'], cb)
}
4 changes: 2 additions & 2 deletions src/commands/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CLEAN_COMMANDS = {
/**
* Generic clean command, invokes the appropriate project type-specific command.
*/
export default function clean(args) {
export default function clean(args, cb) {
let userConfig = getUserConfig(args, {required: true})
CLEAN_COMMANDS[userConfig.type](args)
CLEAN_COMMANDS[userConfig.type](args, cb)
}
Loading

0 comments on commit 4f9cc4b

Please sign in to comment.