Skip to content

Commit

Permalink
Merge pull request #15 from nickthesing/14-enable-src
Browse files Browse the repository at this point in the history
#14 Enable watching the src & prebuilt directory
  • Loading branch information
nickthesing authored Aug 11, 2017
2 parents f1dfe28 + de46b16 commit 29c26ad
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 65 deletions.
14 changes: 8 additions & 6 deletions dist/lib/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ var Package = function () {
this.options = {
excludeDefault: true,
buildPlugins: 'sass'

// if watching prebuilt item:
};if (process.cwd().indexOf('prebuilt') !== -1) {
this.options.builtDirs = [process.cwd()];
}
};
}

/**
Expand All @@ -44,7 +40,13 @@ var Package = function () {

_createClass(Package, [{
key: 'createPackage',
value: function createPackage() {
value: function createPackage(directory) {
// if watching prebuilt item:
if (directory.indexOf('prebuilt') !== -1) {
this.options.builtDirs = [directory];
}

this.sourceFolder = [directory];
this.options.output = Config.packageName;

this.startTime = process.hrtime();
Expand Down
11 changes: 11 additions & 0 deletions dist/lib/Verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _tcpPing = require('tcp-ping');

var _tcpPing2 = _interopRequireDefault(_tcpPing);
Expand All @@ -20,11 +24,18 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @return {boolean}
*/
var model = function model() {
// only execute model.xml-check if not inside the src or prebuilt directory
var basename = _path2.default.basename(process.cwd());
if (basename === 'src' || basename === 'prebuilt') {
return true;
}

if (!_fs2.default.existsSync(process.cwd() + '/model.xml')) {
_Notify2.default.welcome();
_Notify2.default.error('model.xml not found. Are you running the watcher from the correct folder? \n');
return false;
}

return true;
};

Expand Down
112 changes: 69 additions & 43 deletions dist/lib/Watcher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

var _watch = require('watch');
Expand Down Expand Up @@ -30,6 +30,14 @@ var _config = require('../config');

var _config2 = _interopRequireDefault(_config);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var monitor = void 0;
Expand All @@ -44,13 +52,13 @@ var _import = new _Import2.default();
*/
var Watcher = function Watcher(config) {

// create global config
global.Config = config ? Object.assign({}, _config2.default, config) : _config2.default;
// create global config
global.Config = config ? Object.assign({}, _config2.default, config) : _config2.default;

checkRequirements();
checkRequirements();

// create watcher
start();
// create watcher
start();
};

/**
Expand All @@ -59,9 +67,9 @@ var Watcher = function Watcher(config) {
* @return {void}
*/
var start = function start() {
_Notify2.default.welcome();
_Notify2.default.welcome();

createWatcher();
createWatcher();
};

/**
Expand All @@ -70,16 +78,16 @@ var start = function start() {
* @return {void}
*/
var createWatcher = function createWatcher() {
var options = {
filter: _helpers.filterExtentions,
interval: 1, // 1 second
wait: 2
};

// create the actual monitor
_watch2.default.createMonitor(process.cwd(), options, function (monitor) {
return watcherCallback(monitor);
});
var options = {
filter: _helpers.filterExtentions,
interval: 1, // 1 second
wait: 2
};

// create the actual monitor
_watch2.default.createMonitor(process.cwd(), options, function (monitor) {
return watcherCallback(monitor);
});
};

/**
Expand All @@ -88,11 +96,11 @@ var createWatcher = function createWatcher() {
* @return {void}
*/
var watcherCallback = function watcherCallback(_monitor) {
monitor = _monitor;
createHandlers();
monitor = _monitor;
createHandlers();

// notify that everything is setup.
_Notify2.default.start();
// notify that everything is setup.
_Notify2.default.start();
};

/**
Expand All @@ -101,26 +109,44 @@ var watcherCallback = function watcherCallback(_monitor) {
* @return {void}
*/
var createHandlers = function createHandlers() {
monitor.on("changed", function (file, curr, prev) {
_Notify2.default.changed(file);
monitor.on("changed", function (file, curr, prev) {
_Notify2.default.changed(file);

// make sure we pass a widget directory to the package & import command
var fileDirectory = _path2.default.parse(file).dir;
var widgetDirectory = traverseUntilModelIsFound(fileDirectory);

runPackageAndImport(widgetDirectory);
});
};

/**
* Traverses a directory all the way up until a directory contains a model.xml file
*
* @return {string} path for the directory that contains a model.xml file
*/
var traverseUntilModelIsFound = function traverseUntilModelIsFound(dir) {
var modelFile = _fs2.default.readdirSync(dir) // can be improved upon by making it async
.find(function (file) {
return file === 'model.xml';
});

runPackageAndImport();
});
return modelFile ? dir : traverseUntilModelIsFound(_path2.default.join(dir, '..'));
};

/**
* Run package and import
*
* @return {void}
*/
var runPackageAndImport = function runPackageAndImport() {
_package.createPackage().then(function () {
return _import.doImport().then(function () {
return notifySuccess();
}).catch(function (error) {
return notifyError(error);
});
});
var runPackageAndImport = function runPackageAndImport(widgetDirectory) {
_package.createPackage(widgetDirectory).then(function () {
return _import.doImport();
}).then(function () {
return notifySuccess();
}).catch(function (error) {
return notifyError(error);
});
};

/**
Expand All @@ -129,8 +155,8 @@ var runPackageAndImport = function runPackageAndImport() {
* @return {void}
*/
var notifyError = function notifyError(error) {
_Notify2.default.log(error);
_Notify2.default.notify('ERROR: Importing package failed', 'fail');
_Notify2.default.log(error);
_Notify2.default.notify('ERROR: Importing package failed', 'fail');
};

/**
Expand All @@ -139,22 +165,22 @@ var notifyError = function notifyError(error) {
* @return {vois}
*/
var notifySuccess = function notifySuccess() {
var timing = [_package.getTime(), _import.getTime()];
var timing = [_package.getTime(), _import.getTime()];

_Notify2.default.importEnd.apply(_Notify2.default, timing);
_Notify2.default.notify('Successfully packaged and imported');
_Notify2.default.importEnd.apply(_Notify2.default, timing);
_Notify2.default.notify('Successfully packaged and imported');
};

/**
* Check if requirements are met before we start anything
*
*/
var checkRequirements = function checkRequirements() {
if (!_Verify2.default.model()) {
process.exit();
}
if (!_Verify2.default.model()) {
process.exit();
}

_Verify2.default.online();
_Verify2.default.online();
};

exports.default = Watcher;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nickthesing/bb-watch",
"version": "0.1.1",
"version": "0.2.0",
"description": "BB-watch for automatic watching and running bb-package and bb-import.",
"main": "src/index.js",
"scripts": {
Expand Down
13 changes: 7 additions & 6 deletions src/lib/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ class Package {
excludeDefault: true,
buildPlugins: 'sass'
}

// if watching prebuilt item:
if ( process.cwd().indexOf('prebuilt') !== -1 ) {
this.options.builtDirs = [process.cwd()];
}
}

/**
* Handles the packageProject function
*
* @return {Promise}
*/
createPackage() {
createPackage(directory) {
// if watching prebuilt item:
if ( directory.indexOf('prebuilt') !== -1 ) {
this.options.builtDirs = [directory];
}

this.sourceFolder = [directory];
this.options.output = Config.packageName;

this.startTime = process.hrtime();
Expand Down
12 changes: 10 additions & 2 deletions src/lib/Verify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import path from 'path';
import ping from 'tcp-ping';
import Notify from './Notify';

Expand All @@ -8,11 +9,18 @@ import Notify from './Notify';
* @return {boolean}
*/
const model = () => {
if ( ! fs.existsSync(process.cwd() + '/model.xml') ) {
Notify.welcome();
// only execute model.xml-check if not inside the src or prebuilt directory
let basename = path.basename(process.cwd());
if ( basename === 'src' || basename === 'prebuilt' ) {
return true;
}

if ( ! fs.existsSync(process.cwd() + '/model.xml') ) {
Notify.welcome();
Notify.error('model.xml not found. Are you running the watcher from the correct folder? \n');
return false;
}

return true;
}

Expand Down
33 changes: 27 additions & 6 deletions src/lib/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { filterExtentions } from './helpers';
import Package from './Package';
import Import from './Import';
import defaultConfig from '../config';
import path from 'path';
import fs from 'fs';

let monitor;

Expand Down Expand Up @@ -80,20 +82,39 @@ const createHandlers = () => {
monitor.on("changed", (file, curr, prev) => {
Notify.changed(file);

runPackageAndImport();
// make sure we pass a widget directory to the package & import command
const fileDirectory = path.parse(file).dir;
const widgetDirectory = traverseUntilModelIsFound(fileDirectory);

runPackageAndImport(widgetDirectory);
});
}

/**
* Traverses a directory all the way up until a directory contains a model.xml file
*
* @return {string} path for the directory that contains a model.xml file
*/
const traverseUntilModelIsFound = (dir) => {
const modelFile =
fs
.readdirSync(dir) // can be improved upon by making it async
.find(file => file === 'model.xml');

return modelFile ? dir : traverseUntilModelIsFound(path.join(dir, '..'));
}

/**
* Run package and import
*
* @return {void}
*/
const runPackageAndImport = () => {
_package.createPackage()
.then(() => _import.doImport()
.then(() => notifySuccess())
.catch((error) => notifyError(error)));
const runPackageAndImport = widgetDirectory => {
_package
.createPackage(widgetDirectory)
.then(() => _import.doImport())
.then(() => notifySuccess())
.catch(error => notifyError(error));
}

/**
Expand Down

0 comments on commit 29c26ad

Please sign in to comment.