1 | const emoji = require('node-emoji')
|
2 |
|
3 | const chalk = require('chalk')
|
4 | const rimrafSync = require('rimraf').sync
|
5 |
|
6 | const getBabelConfig = require('../utils/getBabelConfig')
|
7 | const paths = require('../utils/paths')
|
8 | const BabelCompiler = require('../utils/babelCompiler')
|
9 |
|
10 | const babelConfig = getBabelConfig()
|
11 |
|
12 | module.exports = cmd => {
|
13 | console.log(`Cleaning directory ${paths.lib}`)
|
14 | rimrafSync(`${paths.lib}/*`)
|
15 |
|
16 | const compiler = new BabelCompiler(paths.src, paths.lib, {
|
17 | babel: babelConfig,
|
18 | watch: cmd.watch,
|
19 | sourceMaps: cmd.sourceMaps
|
20 | })
|
21 |
|
22 | let ready = false
|
23 | let errors = false
|
24 |
|
25 | if (cmd.watch) {
|
26 | console.log(emoji.get('eyes'), ' Starting watch...\n')
|
27 | } else {
|
28 | console.log(emoji.get('package'), ' Compiling files...\n')
|
29 | }
|
30 |
|
31 | compiler.on('copy', filepath => {
|
32 | if (ready) console.log(`File '${filepath}' copied to build dir`)
|
33 | })
|
34 | compiler.on('success', filepath => {
|
35 | if (ready) console.log(`File '${filepath}' compiled successfully`)
|
36 | })
|
37 | compiler.on('error', (filepath, error) => {
|
38 | if (!ready) errors = true
|
39 |
|
40 | console.log()
|
41 | console.log(error.toString())
|
42 | if (error.codeFrame) console.log(`\n${error.codeFrame}\n`)
|
43 | })
|
44 | compiler.on('ready', () => {
|
45 | ready = true
|
46 | if (errors) {
|
47 | console.log(chalk.red('Compiled with errors.\n'))
|
48 | } else {
|
49 | console.log(chalk.green('Compiled successfully.\n'))
|
50 | }
|
51 | if (!cmd.watch) {
|
52 | compiler.close()
|
53 | process.exit(errors ? 1 : 0)
|
54 | }
|
55 | })
|
56 | }
|