Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
EpocSquadron committed Jul 9, 2013
2 parents 85dea49 + d78d7e3 commit 294dc20
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 83 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ Use [npm](https://npmjs.org/) to install claymate globally or locally:

Here are short descriptions of the commands available. Use the command without any arguments (`$ claymate`) to see full details.

- `build` - Concatenates and minifies all gumby javascript assets in the proper order utilizing [uglify-js](https://github.com/mishoo/UglifyJS2). Accepts further options to specify which modules to include.
- `build` - Concatenates and minifies all gumby javascript assets in the proper order utilizing [uglify-js](https://github.com/mishoo/UglifyJS2). Accepts further options to specify which modules to include. By default, this will look for the gumby files starting from the current directory, and output them to the current directory. You will have to specify the path to gumby or desired output path if they are different. Furthermore, a sourcemap can be built to accompany the minified file.

##Configuration

All command line options for a given subcommand can be set in a JSON configuration file called `gumby.json`, typically located at the root of your project.
All command line options for a given subcommand can be set in a JSON configuration file called `gumby.json`, typically located at the root of your project.

> Note that claymate will look for this file in the directory in which you run claymate, so keeping it in the root of the project is not a requirement.
> Note that claymate will look for this file in the directory in which you run claymate, so keeping it in the root of the project is not a requirement.
As an example, the following command:

$ claymate build --gumbyPath public_html \
--outPath public_html/js \
--modules fittext,retina \
--extraModules public_html/gumby-parallax/gumby.parallax.js

Is equivalent to running `$ claymate build` with the following JSON in `gumby.json`:

{
Expand Down
21 changes: 18 additions & 3 deletions bin/claymate
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ var usage =
"$0 [subcommand] [arguments]\n\
\n\
Use one of the following subcommands:\n\
build - Concatenate and minify the core gumby scripts and its source map.\n\
build - Concatenate and minify the core gumby scripts.\n\
\n\
The options below will have which subcommand(s) they apply to in brackets before the description.\n\
The options below will have which subcommand(s) they apply to in brackets\n\
before the description.\n\
";

// Set up argument parsing
Expand All @@ -28,6 +29,10 @@ var argv = require('optimist')
.describe('extraModules', '[build] A comma separated list of paths to additional non-core gumby module files.')
.alias('extraModules', 'e')

.describe('buildSourceMap', '[build] Boolean flag, pass it to build the sourcemap.')
.boolean('buildSourceMap')
.alias('buildSourceMap', 's')

.describe('outPath', '[build] Where to write the minified and sourcemap files. If omitted, defaults to current directory.')
.alias('outPath', 'o')

Expand All @@ -45,7 +50,17 @@ var argv = require('optimist')
// Handle the subcommands
switch (argv._[0]) {
case 'build':
Claymate.build(getArg('modules', 'build', 'array'), getArg('extraModules', 'build', 'array'), getArg('gumbyPath', 'build'), getArg('outPath', 'build'));

var buildOpts = {
gumbyPath: getArg('gumbyPath', 'build'),
outPath: getArg('outPath', 'build'),
modules: getArg('modules', 'build', 'array'),
extraModules: getArg('extraModules', 'build', 'array'),
includeSourceMap: getArg('buildSourceMap', 'build')
};

Claymate.build(buildOpts);

break;
}

Expand Down
173 changes: 98 additions & 75 deletions lib/claymate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function Claymate() {
var fs = require("fs"),
path = require("path"),
util = require('util'),
workingDir = process.cwd(),
gumbyPathDefault = path.join(workingDir, '/components/gumby/');
workingDir = process.cwd();

/**
* Simple error handler for fs.write
Expand All @@ -23,18 +22,71 @@ function Claymate() {
}

/**
* Ensures gumby directories are present in the provided path
* @param {String} rootPath What path to check.
* @return {void} Throws an error.
* Validates input to the build method.
* @param {Object} buildInput Object literal of options to be cleaned.
* @return {Object} The cleaned options.
*/
function checkGumbyDirs(rootPath) {
function validateBuildInput(buildInput) {

var opts = {
gumbyPath: '',
outPath: '',
modules: [],
extraModules: [],
includeSourceMap: false
};

// Check for a passed directory
if (typeof buildInput.gumbyPath === 'string') {

// If the ui dir doesnt exist, the rest don't since
// the rest are all parents of that dir.
if (!fs.existsSync(path.join(buildInput.gumbyPath, 'js/libs/ui'))) {
throw new Error('Path ' + buildInput.gumbyPath + ' does not exist or does not contain gumby.');
}

opts.gumbyPath = buildInput.gumbyPath;

}

// Check for path to write output to
// Fall back to current directory.
if (typeof buildInput.outPath === 'string') {

if (fs.existsSync(buildInput.outPath)) {
opts.outPath = buildInput.outPath;
}

}

// Check for passed modules, fall back
// to all modules in ui directory
if (util.isArray(buildInput.modules)) {

opts.modules = buildInput.modules;

} else {

opts.modules = fs.readdirSync(path.join(opts.gumbyPath, 'js/libs/ui'));

// If the ui dir doesnt exist, the rest don't since
// the rest are all parents of that dir.
if (!fs.existsSync(path.join(rootPath, 'js/libs/ui'))) {
throw new Error('Path ' + rootPath + ' does not exist or does not contain gumby.');
}

// Check for passed extra modules.
if (util.isArray(buildInput.extraModules)) {

opts.extraModules = buildInput.extraModules;

}

// Force boolean
if (typeof buildInput.includeSourceMap !== 'undefined') {

opts.includeSourceMap = !!buildInput.includeSourceMap;

}

return opts;

}

/**
Expand All @@ -57,69 +109,34 @@ function Claymate() {

/**
* This method will build the gumby js assets.
* @param {Object} opts An object literal of options including the following params:
* @param {Array} modules An array of the modules that will be included, defaults to all.
* @param {Array} extraModules An array of any external modules that will be included.
* @param {String} gumbyPath The path to gumby that will be used, defaults to current dir + /components/gumby
* @return {void} Writes minified file and its sourcemap.
* @param {String} outPath What path to write minified/sourcemap files to.
* @param {Boolean} includeSourceMap Whether to write a source map. Defaults to true
* @return {void} Writes minified file and optionally its sourcemap.
*/
this.build = function(modules, extraModules, gumbyPath, outPath) {
this.build = function(opts) {

var UglifyJS = require('uglify-js'),
files = [],
minOpts = {},
minified, tmp, tmp2;

// Check for a passed directory
if (typeof gumbyPath === 'string') {

// Normalize relative paths to the current working
// directory
tmp = path.resolve(workingDir, gumbyPath);

// If it exists, set it. Or fall back
gumbyPath = (fs.existsSync(tmp)) ?
tmp :
gumbyPathDefault;

} else {

gumbyPath = gumbyPathDefault;

}

// Double check that we can acess gumby files
checkGumbyDirs(gumbyPath);

// Check for passed modules, fall back
// to all modules in ui directory
if (!util.isArray(modules)) {

modules = fs.readdirSync(path.join(gumbyPath, 'js/libs/ui'));

}

// Check for path to write output to
// Fall back to current directory.
if (typeof outPath === 'string') {

if (!fs.existsSync(outPath)) {
outPath = workingDir;
}

} else {

outPath = workingDir;

}
// Validate input
opts = validateBuildInput(opts);

// Master gumby script
files.push(path.join(gumbyPath, 'js/libs/gumby.js'));
files.push(path.join(opts.gumbyPath, 'js/libs/gumby.js'));

// Modules, configurable which shouldbe added.
for (var i = 0; i < modules.length; i++) {
for (var i = 0; i < opts.modules.length; i++) {

tmp = path.basename(modules[i], '.js');
tmp = path.basename(opts.modules[i], '.js');

tmp2 = path.join(
gumbyPath,
opts.gumbyPath,
'js/libs/ui/',
// Look if there is a gumby. or jquery. in front of the module
// name, if not, assume gumby. should be prepended.
Expand All @@ -133,31 +150,37 @@ function Claymate() {
}

// External modules, which will be passed as paths
if (util.isArray(extraModules)) {

for (var j = 0; j < extraModules.length; j++) {

tmp = path.resolve(workingDir, extraModules[j]);

if (fs.existsSync(tmp)) {
files.push(tmp);
}
for (var j = 0; j < opts.extraModules.length; j++) {

if (fs.existsSync(opts.extraModules[j])) {
files.push(opts.extraModules[j]);
}

}

// Final init script
files.push(path.join(gumbyPath, 'js/libs/gumby.init.js'));
files.push(path.join(opts.gumbyPath, 'js/libs/gumby.init.js'));

// Optionally request sourcemap
if (opts.includeSourceMap) {
minOpts.outSourceMap = 'gumby.js.map';
}

minified = UglifyJS.minify(files, minOpts);

minified = UglifyJS.minify(files, {
outSourceMap: "gumby.js.map"
});
// Write the minified file.
fs.writeFile(path.join(opts.outPath, 'gumby.min.js'), minified.code, writeErrorHandler);

// Need to print minified.code, minified.map to js/libs/gumby.min.js js/libs/gumby.js.map
// Write the minified and sourcemap files.
fs.writeFile(path.join(outPath, 'gumby.min.js'), minified.code, writeErrorHandler);
fs.writeFile(path.join(outPath, 'gumby.js.map'), minified.map, writeErrorHandler);
// Write the sourcemap file.
if (opts.includeSourceMap) {

// Minified.code apparently doesn't already contain this comment
// pointing to the sourcemap file. Let's append it if theyre on.
minified.code += '\n//# sourceMappingURL=' + minOpts.outSourceMap;

fs.writeFile(path.join(opts.outPath, 'gumby.js.map'), minified.map, writeErrorHandler);

}

};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "claymate",
"version": "0.2.0",
"version": "1.0.0",
"description": "Official helper scripts for the Gumby responsive framework.",
"main": "lib/claymate.js",
"bin": "bin/claymate",
Expand Down

0 comments on commit 294dc20

Please sign in to comment.