UNPKG

muffin-cli

Version:

The command line utility for building sites using muffin

84 lines (60 loc) 2.25 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _broccoli = require('broccoli'); var _broccoli2 = _interopRequireDefault(_broccoli); var _ncp = require('ncp'); var _ncp2 = _interopRequireDefault(_ncp); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _broccoliSaneWatcher = require('broccoli-sane-watcher'); var _broccoliSaneWatcher2 = _interopRequireDefault(_broccoliSaneWatcher); var _utils = require('../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class Builder { constructor(tree, watch) { this.shouldWatch = watch; this.tree = tree; this.createBuilder(); } createBuilder() { this.builder = new _broccoli2.default.Builder(this.tree); this.builder.build().then(this.doneBuilding.bind(this)).catch(err => utils.log(err)); } doneBuilding(results) { const dir = typeof results === 'string' ? results : results.directory; const buildTime = results.totalTime; // Copy files from tmp folder to the destination directory // And make sure to follow symlinks while doing so (0, _ncp2.default)(dir, process.cwd() + '/dist', { dereference: true }, function (err) { if (err) throw err; if (buildTime) { // The original built time is in nanoseconds, so we need to convert it to milliseconds (0, _utils.log)(_chalk2.default.green(`Finished building after ${ Math.floor(buildTime / 1e6) }ms.`)); } else { (0, _utils.log)(_chalk2.default.green('Finished building.')); } if (this.shouldWatch && !this.watching) { return this.startWatching.bind(this)(); } if (!this.watching) { this.builder.cleanup().catch(err => (0, _utils.log)(err)); } }.bind(this)); } startWatching() { const watch = new _broccoliSaneWatcher2.default(this.builder); this.watching = true; process.on('SIGINT', () => { watch.close(); process.exit(0); }); watch.on('change', function (results) { if (!results.filePath) return; this.createBuilder(); }.bind(this)); watch.on('error', err => (0, _utils.log)(err)); } } exports.default = Builder;