Skip to content
This repository has been archived by the owner on Sep 21, 2018. It is now read-only.

Babel and ES6 Utilities Class #54

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": ["es2015"] }
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ config/worker/*
!config/worker/config.json.sample

# intellij Webstorm
.idea/
.idea/

# babel
*-compiled.js
*-compiled.js.map
3 changes: 2 additions & 1 deletion lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Sets up middleware, routers, and error handlers for the Express.js application.
*/

import * as utilities from './utilities';

var fs = require('fs-extra');
var path = require('path');
var morgan = require('morgan');
Expand All @@ -16,7 +18,6 @@ var express = require('express');
var less = require('less-middleware');
var browserify = require('browserify-middleware');
var config = require('./config');
var utilities = require('./utilities');

//setting up app
var app = express();
Expand Down
5 changes: 4 additions & 1 deletion lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
* Cleans up worker file system of any data from deleted jobs.
*/

// TODO: Implement.
// TODO: Implement.
if (require.main === module) {
// do nothing
}
3 changes: 2 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* Provides data and functionality to be used by tasks.
*/

import * as utilities from './utilities';

var path = require('path');
var callsite = require('callsite');
var Task = require('./task');
var utilities = require('./utilities');

function Context (executor, job, result, device) {
var self = this;
Expand Down
3 changes: 2 additions & 1 deletion lib/job-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Entry point for a new forked node process to execute a job.
*/

import * as utilities from './utilities';

require('./globals');

var path = require('path');
var async = require('async');
var argv = require('yargs').argv;

var config = require('./config');
var utilities = require('./utilities');
var db = require('./db');

var result;
Expand Down
3 changes: 2 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* A logger class.
*/

import * as utilities from './utilities';

var colors = require('colors');
var util = require('util');
var utilities = require('./utilities');
var argv = require('yargs').argv;

function Logger () {
Expand Down
3 changes: 2 additions & 1 deletion lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Sends emails for job result status changes.
*/

import * as utilities from './utilities';

var nodemailer = require('nodemailer');
var fs = require('fs-extra');
var path = require('path');
var ejs = require('ejs');
var utilities = require('./utilities');
var config = require('./config');

function Mailer () {
Expand Down
1 change: 0 additions & 1 deletion lib/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function isValidCronTime (cronTime) {
try {
var c = new CronJob(cronTime, function () {
c.stop();
delete c;
});
}
catch (ex) {
Expand Down
3 changes: 2 additions & 1 deletion lib/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* An abstraction of a task.
*/

import * as utilities from './utilities';

var path = require('path');
var async = require('async');
var utilities = require('./utilities');

function Task (params) {
this.errors = [];
Expand Down
48 changes: 21 additions & 27 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,34 @@
* Utility and helper functions used across the application.
*/

var path = require('path');
var util = require('util');
import * as util from 'util';

function Utilities () {
}

Utilities.prototype.formatMilliseconds = function (ms) {
var secNum = parseInt(ms / 1000, 10);
export function formatMilliseconds(ms) {
var secNum = parseInt(ms / 1000, 10);
var hours = Math.floor(secNum / 3600);
var minutes = Math.floor((secNum - (hours * 3600)) / 60);
var seconds = secNum - (hours * 3600) - (minutes * 60);

if (minutes < 10)
minutes = '0' + minutes;
if (seconds < 10)
seconds = '0' + seconds;

var time = hours + ':' + minutes + ':' + seconds;
return time;
}

Utilities.prototype.isDictionary = function (obj) {
return !(!obj || Array.isArray(obj) || obj.constructor != Object);
if (minutes < 10) {
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
return hours + ':' + minutes + ':' + seconds;
}

Utilities.prototype.exists = function (v) {
return !(typeof v == 'undefined' || v == null);
export function isDictionary(obj) {
return !(!obj || Array.isArray(obj) || obj.constructor != Object);
}

Utilities.prototype.stringify = function (obj) {
if (typeof obj === 'object')
return util.inspect(obj, { showHidden: true, depth: null });
else
return obj;
export function exists(v) {
return !(v === undefined || v === null);
}

module.exports = new Utilities();
export function stringify(obj) {
if (typeof obj === 'object') {
return util.inspect(obj, {showHidden: true, depth: null});
} else {
return obj;
}
}
3 changes: 2 additions & 1 deletion models/Job.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* Models a configured job.
*/

import * as utilities from '../lib/utilities';

var async = require('async');
var path = require('path');
var childProcess = require('child_process');
var config = require(path.join(__rootdir, 'lib', 'config'));
var Report = require(path.join(__rootdir, 'lib', 'report'));
var utilities = require(path.join(__rootdir, 'lib', 'utilities'));

var Job;

Expand Down
3 changes: 2 additions & 1 deletion models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* Models a collection of jobs.
*/

import * as utilities from '../lib/utilities';

var async = require('async');
var path = require('path');
var utilities = require(path.join(__rootdir, 'lib', 'utilities'));

var Project;

Expand Down
3 changes: 2 additions & 1 deletion models/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* Models the results from an individual job run.
*/

import * as utilities from '../lib/utilities';

var moment = require('moment');
var path = require('path');
var util = require('util');
var async = require('async');
var fs = require('fs-extra');
var utilities = require(path.join(__rootdir, 'lib', 'utilities'));

var Result;

Expand Down
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"url": "http://github.com/yahoo/guerilla/issues"
},
"scripts": {
"test": "TEST=1 ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(find tests -name '*.test.js')",
"master": "node_modules/.bin/babel-node server.js --master",
"worker": "node_modules/.bin/babel-node server.js --worker",
"redis": "redis-server",
"test": "TEST=1 ./node_modules/.bin/babel-node ./node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --timeout 5000 $(find tests -name '*.test.js') ",
"lint": "./node_modules/.bin/jshint tests"
},
"dependencies": {
Expand Down Expand Up @@ -62,10 +65,19 @@
"node": ">=0.12"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-eslint": "^5.0.0-beta6",
"babel-loader": "^6.2.1",
"babel-plugin-react-transform": "^2.0.0",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.1.0",
"babel-register": "^6.4.3",
"chai": "^3.5.0",
"debug": "^2.2.0",
"glob": "^7.0.0",
"istanbul": "^0.4.2",
"istanbul": "^1.0.0-alpha.2",
"jshint": "^2.9.1",
"lodash": "^4.6.0",
"mocha": "^2.4.5",
Expand Down
2 changes: 1 addition & 1 deletion routes/master/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var router = require('express').Router();
var path = require('path');
var models = require(path.join(__rootdir, 'lib', 'db')).models();
var utilities = require(path.join(__rootdir, 'lib', 'utilities'));

var Job = models.Job;
var ConfigLocation = models.ConfigLocation;

Expand Down
2 changes: 2 additions & 0 deletions tests/coverage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ describe('Coverage baseline', function () {
});
mockery.registerSubstitute('./logger',
path.join(__ROOTDIR, 'tests', 'mocks', 'logger'));
mockery.registerSubstitute('./lib/logger',
path.join(__ROOTDIR, 'tests', 'mocks', 'logger'));
mockery.registerSubstitute('git-rev',
path.join(__ROOTDIR, 'tests', 'mocks', 'git-rev'));
mockery.registerSubstitute('fs-extra',
Expand Down
22 changes: 18 additions & 4 deletions wiki/Launching.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

Navigate to the Guerilla project directory and start the redis server:

`redis-server`
```sh
npm run redis
# or the following legacy way
# redis-server
```

### Install NPM Packages

Expand Down Expand Up @@ -41,6 +45,16 @@ temporary directory.

#### Starting syntax

Master: `node server.js --master [--config configFilePath] {--saveTempFiles}`

Worker: `node server.js --worker [--config configFilePath] {--saveTempFiles}`
Master:
```sh
npm run master
# or the following legacy way
# node server.js --master [--config configFilePath] {--saveTempFiles}`
```

Worker:
```sh
npm run worker
# or the following legacy way
# node server.js --worker [--config configFilePath] {--saveTempFiles}
```