Skip to content

Commit

Permalink
Merge branch 'nightmare'
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhill committed Aug 17, 2017
2 parents 511597e + c590d1b commit 8db162d
Show file tree
Hide file tree
Showing 20 changed files with 6,726 additions and 9,026 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Expand Up @@ -9,7 +9,7 @@ javascript/karma/ export-ignore
.gitignore export-ignore
.travis.yml export-ignore
.codeclimate.yml export-ignore
gulpfile.js export-ignore
gulpfile.babel.js export-ignore
package.json export-ignore
phpunit.xml
javascript/lava.spec.js export-ignore
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Expand Up @@ -2,9 +2,6 @@ vendor/
build/
.idea/

javascript/node_modules/
javascript/phantomjs/renders/

composer.phar

*.log
Expand Down
3 changes: 3 additions & 0 deletions javascript/.gitignore
@@ -0,0 +1,3 @@
.idea/
node_modules/
renders/*.png
File renamed without changes.
2 changes: 1 addition & 1 deletion javascript/dist/lava.js

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions javascript/gulp-functions/Compile.js
@@ -0,0 +1,87 @@
import args from 'yargs';
import gulpif from 'gulp-if';
import source from 'vinyl-source-stream';
import notifier from 'node-notifier';
import browserify from 'browserify';
import uglify from 'gulp-uglify';
import babelify from 'babelify';
// import stripify from 'stripify';
import watchify from 'watchify';
import streamify from 'gulp-streamify';
import versionify from 'browserify-versionify';
import { dest } from 'gulp';
import { log } from 'gulp-util';
import { red, green } from 'chalk';
import { create } from 'browser-sync';

const browserSync = create();

export default function compile(prod, watch, sync) {
let bundler = browserify({
debug: true,
entries: ['./src/lava.entry.es6'],
cache: {},
packageCache: {}
});

bundler.transform(babelify, { presets: ['es2015'] });
bundler.transform(versionify);

if (watch) {
bundler = watchify(bundler);

if (sync) {
browserSync.init({
proxy: "localhost:" + args.port || 8000
});
}
}

if (prod) {
bundler.transform('stripify');
}

function rebundle() {
return bundler.bundle()
.on('error', err => {
if (err instanceof SyntaxError) {
log(red('Syntax Error'));
log(err.message);
// log(err.filename+":"+err.loc.line);
log(err.codeFrame);
} else {
log(red('Error'), err.message);
}

this.emit('end');
})
.pipe(source('lava.js'))
.pipe(gulpif(prod, streamify(uglify())))
.pipe(dest('dist'));
}

if (watch) {
bundler.on('update', () => {
const msg = 'lava.js re-bundling...';

log(green(msg));

notifier.notify({
title: 'Browserify',
message:msg
});

rebundle();
});

bundler.on('log', msg => {
log(green(msg));

if (sync) {
browserSync.reload();
}
});
}

return rebundle();
}
20 changes: 20 additions & 0 deletions javascript/gulp-functions/GetChartTypes.js
@@ -0,0 +1,20 @@
import { map } from 'lodash';
import { cwd } from 'process';
import { resolve } from 'path';
import { sync as globSync } from 'glob';

export default function getChartTypes() {
let chartTypes = globSync('*.php', {
cwd: resolve(cwd(), '../src/Charts/'),
nomount: true,
ignore: [
'Chart.php',
'ChartBuilder.php',
'ChartFactory.php',
]
});

return map(chartTypes, chartType => {
return chartType.slice(0, -4);
});
}
29 changes: 29 additions & 0 deletions javascript/gulp-functions/PhpServer.js
@@ -0,0 +1,29 @@
import PortFinder from 'portfinder';
import PhpServer from 'gulp-connect-php';
import { cwd } from 'process';
import { resolve } from 'path';

function _createServer(port) {
const base = resolve(cwd(), '../tests/Examples');
const server = new PhpServer();

return new Promise(resolve => {
server.server({
base: base,
port: port,
ini: base + '/php.ini',
router: base + '/renderer.php'
});

resolve(server);
});
}

export default function getPhpServer() {
return PortFinder
.getPortPromise()
.then(_createServer)
.catch(err => {
console.log(err);
});
}
36 changes: 36 additions & 0 deletions javascript/gulp-functions/Renderer.js
@@ -0,0 +1,36 @@
import Nightmare from 'nightmare';
import getPhpServer from './PhpServer';
import { cwd } from 'process';
import { resolve } from 'path';

function _getNightmare(timeout) {
return new Nightmare({
gotoTimeout: timeout,
waitTimeout: timeout,
loadTimeout: timeout,
executionTimeout: timeout
});
}

export default function renderChart(chartType) {
return getPhpServer()
.then(server => {
let chartUrl = 'http://localhost:' + server.port + '/' + chartType;
let renderDir = resolve(cwd(), 'renders');
let chartImg = renderDir + '/' + chartType + '.png';

console.log('Nightmare opening ' + chartUrl);

return _getNightmare(5000)
.viewport(800, 600)
.goto(chartUrl)
.wait(3000)
.screenshot(chartImg)
.end(() => {
console.log('Saved screenshot to ' + chartImg);

server.closeServer();
});
});

}
111 changes: 111 additions & 0 deletions javascript/gulpfile.babel.js
@@ -0,0 +1,111 @@
/* jshint node:true */

import gulp from 'gulp';
import yargs from 'yargs';
import bump from 'gulp-bump';
import replace from 'gulp-replace';
import compile from './gulp-functions/Compile';
import renderChart from './gulp-functions/Renderer';
import getChartTypes from './gulp-functions/GetChartTypes';
import { cpus } from 'os'
import { map } from 'bluebird';

gulp.task('default', ['dev']);

/**
* Lava.js compilation tasks.
*
* The compile method accepts three boolean flags for the following signature:
* compile(prod, watch, sync)
*/
gulp.task('dev', () => { compile(false, false, false) });
gulp.task('prod', () => { compile(true, false, false) });
gulp.task('watch', () => { compile(false, true, false) });
gulp.task('sync', () => { compile(false, true, true) });

/**
* Render a specific chart.
*
* Specify the type as the php class name
*
* Syntax:
* gulp render --type [ AreaChart | LineChart | GeoChart | etc... ]
*/
gulp.task('render', done => {
const chartTypes = getChartTypes();
const args = yargs
.fail(msg => {
throw new Error(msg);
})
.alias('t', 'type')
.describe('t', 'choose the type of chart to render')
.choices('t', chartTypes)
.wrap(70)
.help('help')
.argv;

renderChart(args.t)
.then(() => {
done();
})
.catch(err => {
console.log(err);
});
});

/**
* Render all of the available charts.
*
* The renders will be ran in batches equal to the number of processors.
*
* Syntax:
* gulp renderAll
*/
gulp.task('renderAll', done => {
let batchSize = cpus().length;

console.log('Rendering charts in batches of '+batchSize);

map(getChartTypes(), chartType => {
return renderChart(chartType);
}, {concurrency: batchSize})
.then(() => {
done();
})
.catch(err => {
console.log(err);
});
});

/**
* Get all available chart types
*
* Syntax:
* gulp charts
*/
gulp.task('charts', done => {
console.log('Available charts for rendering:');
console.log(getChartTypes().join(', '));
done();
});

/**
* Render all of the available charts.
*
* Syntax:
* gulp version -v 4.0.0
*/
// gulp.task('version', done => {
// let version = args.v;
// let minorVersion = version.slice(0, -2);
//
// gulp.src('./package.json')
// .pipe(bump({version:args.v}))
// .pipe(gulp.dest('./'));
//
// gulp.src(['./README.md', './.travis.yml'])
// .pipe(replace(/(["=\/-])[0-9]+\.[0-9]+/g, '$1'+minorVersion))
// .pipe(gulp.dest('./'));
//
// done();
// });

0 comments on commit 8db162d

Please sign in to comment.