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 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
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 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 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 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 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 utilities from './utilities';

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

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

var path = require('path');
var util = require('util');

function Utilities () {
}

Utilities.prototype.formatMilliseconds = function (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);
}

Utilities.prototype.exists = function (v) {
return !(typeof v == 'undefined' || v == null);
}

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

module.exports = new Utilities();
import * as util from 'util';

export default class Utilities {

static formatMilliseconds(ms) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seyeojumu does it make sense to format this file so java-like?

i think it also works if i make each method export function formatMilliseconds(ms) {}

In that case i can import only the methods i want... like...
import {formatMilliseconds, isDictionary} from './utilities'

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;
}
return hours + ':' + minutes + ':' + seconds;
}

static isDictionary(obj) {
return !(!obj || Array.isArray(obj) || obj.constructor != Object);
}

static exists(v) {
return !(typeof v == 'undefined' || v == null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return !(v === undefined || v === null)`
(from federico)

}

static 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 utilities from './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 utilities from './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 utilities from './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
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
"bugs": {
"url": "http://github.com/yahoo/guerilla/issues"
},
"engines": {
"node": ">=0.12"
},
"scripts": {
"test": "TEST=1 ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(find tests -name '*.test.js')",
"master": "node ./node_modules/.bin/babel-node server.js --master",
"worker": "node ./node_modules/.bin/babel-node server.js --worker",
"redis": "redis-server",
"test": "TEST=1 ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(find tests -name '*.test.js') --compilers js:babel-core/register",
"lint": "./node_modules/.bin/jshint tests"
},
"dependencies": {
Expand Down Expand Up @@ -58,10 +64,16 @@
"underscore": "^1.8.3",
"yargs": "^3.15.0"
},
"engines": {
"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",
Expand Down
4 changes: 3 additions & 1 deletion routes/master/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* Router for all job endpoints.
*/

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

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 server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

var git = require('git-rev');
var logger = require('./lib/logger');

git.long(function (str) {

require('./lib/globals');
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}
```