Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(grunt): create equivalent grunt tasks for all cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbcross committed Aug 14, 2014
1 parent 7484072 commit 258f3f1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
node_modules/
.DS_Store
benchpress-build/
benchpress-build/
build/
18 changes: 18 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,18 @@
var cli = require('./lib/cli');

/**
* This file is only for testing and demonstration of the included grunt tasks.
*/

module.exports = function (grunt) {
grunt.initConfig({
bp_build: {
options: {
buildPath: 'build/benchmarks',
benchmarksPath: 'grunt-benchmarks'
}
}
});

grunt.loadTasks('./tasks');
};
3 changes: 3 additions & 0 deletions grunt-benchmarks/sample-folder/bp.conf.js
@@ -0,0 +1,3 @@
module.exports = function(config) {

}
1 change: 1 addition & 0 deletions grunt-benchmarks/sample-folder/main.html
@@ -0,0 +1 @@
sample!
26 changes: 19 additions & 7 deletions lib/cli.js
Expand Up @@ -2,28 +2,32 @@ var fs = require('fs'),
path = require('path'),
benchpressBase = path.resolve(module.filename, '../../'),
rimraf = require('rimraf'),
mkdirp = require('mkdirp'),
defaultBuildPath = path.resolve('benchpress-build'),
benchmarksPath = path.resolve('benchmarks'),
defaultBenchmarksPath = path.resolve('benchmarks'),
underscorePath = path.resolve(benchpressBase, 'node_modules/underscore/underscore-min.js'),
bootstrapPath = path.resolve(benchpressBase, 'node_modules/bootStrap/dist/css/bootstrap.min.css'),
bpPath = path.resolve(benchpressBase, 'lib/bp.js'),
libPath = path.resolve(benchpressBase, 'lib'),
httpServer = require('http-server'),
minimist = require('minimist');

exports.launchChrome = function launchChrome() {
exports.launchChrome = function launchChrome(done) {
if (process.platform == 'linux') {
require('child_process').exec('google-chrome -incognito --js-flags="--expose-gc"', function(err) {
if (err) console.error('An error occurred trying to launch Chrome: ', err);
done && done();
});
}
else if (process.platform === 'darwin') {
require('child_process').exec('/Applications/Google\\ Chrome\\ Canary.app/Contents/MacOS/Google\\ Chrome\\ Canary --enable-memory-info --enable-precise-memory-info --enable-memory-benchmarking --js-flags="--expose-gc" -incognito', function(err) {
if (err) console.error('An error occurred trying to launch Chrome: ', err);
done && done();
});
}
else {
console.log('Cannot launch chrome for your platform: ', process.platform);
done && done();
}
};

Expand All @@ -40,15 +44,18 @@ exports.run = function run() {
});
};

exports.build = function build(buildPath) {
buildPath = buildPath || defaultBuildPath;
exports.build = function build(options, done) {
options = options || {};
buildPath = options.buildPath || defaultBuildPath;
benchmarksPath = options.benchmarksPath || defaultBenchmarksPath;

//Start fresh
rimraf(buildPath, buildIt);

function buildIt (err) {
var template, benchmarks;
if (err) throw err;
fs.mkdirSync(buildPath);
mkdirp.sync(buildPath);

//Get benchmark html template
template = fs.readFileSync(path.resolve(libPath, 'template.html'));
Expand All @@ -66,8 +73,9 @@ exports.build = function build(buildPath) {
require(path.resolve(benchmarkPath, 'bp.conf.js'))(config);

dependencies = fs.readdirSync(benchmarkPath);

dependencies.forEach(function(dependency) {
var dependencyPath = path.resolve('benchmarks', benchmark, dependency);
var dependencyPath = path.resolve(benchmarkPath, dependency);
if (dependency === 'main.html') {
//This is the main benchmark template
main = fs.readFileSync(dependencyPath).toString();
Expand All @@ -83,6 +91,7 @@ exports.build = function build(buildPath) {
main = main.replace('%%SCRIPTS%%', JSON.stringify(config.scripts));

fs.writeFileSync(path.resolve(buildPath, benchmark, 'index.html'), main);
done && done();
});
}

Expand All @@ -102,7 +111,10 @@ exports.exec = function() {
var args = minimist(process.argv.slice(2));
switch (process.argv[2]) {
case 'build':
exports.build(args['build-path']);
exports.build({
buildPath: args['build-path'],
benchmarksPath: args['benchmark-path']
});
break;
case 'run':
exports.run();
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -26,9 +26,11 @@
"homepage": "https://github.com/angular/benchpress",
"dependencies": {
"bootstrap": "^3.2.0",
"grunt": "^0.4.5",
"http-server": "^0.6.1",
"karma": "^0.12.21",
"minimist": "^1.1.0",
"mkdirp": "^0.5.0",
"rimraf": "^2.2.8",
"underscore": "^1.6.0"
}
Expand Down
7 changes: 7 additions & 0 deletions tasks/bp_build.js
@@ -0,0 +1,7 @@
var cli = require('../lib/cli');

module.exports = function (grunt) {
grunt.registerTask('bp_build', 'build benchmarks for project', function() {
cli.build(this.options(), this.async());
});
};
8 changes: 8 additions & 0 deletions tasks/bp_launch_chrome.js
@@ -0,0 +1,8 @@
var cli = require('../lib/cli');

module.exports = function (grunt) {
grunt.registerTask('bp_launch_chrome', 'launch chrome canary in incognito with flags',
function() {
cli.launchChrome(this.async());
});
};
7 changes: 7 additions & 0 deletions tasks/bp_run.js
@@ -0,0 +1,7 @@
var cli = require('../lib/cli');

module.exports = function (grunt) {
grunt.registerTask('bp_run', 'run a server from cwd', function() {
cli.run(this.async());
});
};

0 comments on commit 258f3f1

Please sign in to comment.