UNPKG

paranormal

Version:
195 lines (139 loc) 5.34 kB
'use strict'; exports.__esModule = true; var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _map = require('babel-runtime/core-js/map'); var _map2 = _interopRequireDefault(_map); var _from = require('babel-runtime/core-js/array/from'); var _from2 = _interopRequireDefault(_from); var _Watcher = require('./Watcher'); var _Watcher2 = _interopRequireDefault(_Watcher); var _Example = require('./Example'); var _Example2 = _interopRequireDefault(_Example); var _App = require('./App'); var _App2 = _interopRequireDefault(_App); var _fs = require('./utils/fs'); var fs = _interopRequireWildcard(_fs); var _constants = require('./utils/constants'); var constants = _interopRequireWildcard(_constants); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _parcel = require('./utils/parcel'); var parcel = _interopRequireWildcard(_parcel); var _stripIndent = require('strip-indent'); var _stripIndent2 = _interopRequireDefault(_stripIndent); var _lodash = require('lodash.debounce'); var _lodash2 = _interopRequireDefault(_lodash); require('../types'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class Runner { constructor(opts) { this.update = (0, _lodash2.default)(async () => { if (!this.ready || this.updating || !this.queue.length) { return; } this.updating = true; let queue = this.queue.splice(0); for (let action of queue) { if (action.kind === 'add') { await this.addExample(action.filePath); } else if (action.kind === 'remove') { await this.removeExample(action.filePath); } else if (action.kind === 'change') { await this.changeExample(action.filePath); } } await this.app.build((0, _from2.default)(this.examples.values())); this.updating = false; if (this.queue.length) { await this.update(); } }, 0); this.onAdd = filePath => { if (this.matches(filePath)) { this.queue.push({ kind: 'add', filePath }); this.update(); } }; this.onRemove = async filePath => { if (this.matches(filePath)) { this.queue.push({ kind: 'remove', filePath }); await this.update(); } }; this.onChange = async filePath => { if (this.matches(filePath)) { this.queue.push({ kind: 'change', filePath }); await this.update(); } }; this.cwd = opts.cwd; this.dirName = _path2.default.dirname(this.cwd); this.tempDir = fs.tempdir(); this.outDir = _path2.default.join(this.cwd, 'dist'); this.watcher = new _Watcher2.default(); this.examples = new _map2.default(); this.app = new _App2.default({ tempDir: this.tempDir }); this.queue = []; this.updating = false; this.watching = false; this.ready = false; } async run(opts) { if (opts.watch) { this.watching = true; this.watcher.on('add', this.onAdd); this.watcher.on('remove', this.onRemove); this.watcher.on('change', this.onChange); this.watcher.watch(this.cwd); } let examplePaths = await fs.findGlobPatterns(this.cwd, this.getExampleGlobPatterns()); await _promise2.default.all(examplePaths.map(async examplePath => { await this.addExample(examplePath); })); await this.app.build((0, _from2.default)(this.examples.values())); this.ready = true; if (opts.watch) { await this.update(); await parcel.serve(this.app.indexPath, _path2.default.join(this.cwd, 'dist')); } else { await parcel.build(this.app.indexPath, _path2.default.join(this.cwd, 'dist')); } } async addExample(examplePath) { let example = new _Example2.default({ cwd: this.cwd, tempDir: this.tempDir, filePath: examplePath }); if (this.ready) { console.log(_chalk2.default.green(`Example: "${example.title}" (added)`)); } else { console.log(_chalk2.default.cyan(`Example: "${example.title}"`)); } await example.build(); this.examples.set(examplePath, example); } async removeExample(examplePath) { let example = this.examples.get(examplePath); if (!example) return; console.log(_chalk2.default.red(`Example: "${example.title}" (removed)`)); this.examples.delete(examplePath); await example.delete(); } async changeExample(examplePath) { let example = this.examples.get(examplePath); if (!example) return; console.log(_chalk2.default.cyan(`Example: "${example.title}" (changed)`)); } matches(filePath) { return fs.matchesGlobPatterns(this.cwd, filePath, this.getExampleGlobPatterns()); } getExampleGlobPatterns() { return [constants.DEFAULT_EXAMPLES_GLOB, constants.IGNORE_NODE_MODULES_GLOB, `!${_path2.default.relative(this.cwd, this.outDir)}/**`, `!.cache/**`]; } } exports.default = Runner;