Skip to content

Commit 294dc20

Browse files
committed
Merge branch 'release/1.0.0'
2 parents 85dea49 + d78d7e3 commit 294dc20

File tree

4 files changed

+121
-83
lines changed

4 files changed

+121
-83
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ Use [npm](https://npmjs.org/) to install claymate globally or locally:
1515

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

18-
- `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.
18+
- `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.
1919

2020
##Configuration
2121

22-
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.
22+
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.
2323

24-
> 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.
24+
> 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.
2525
2626
As an example, the following command:
2727

2828
$ claymate build --gumbyPath public_html \
2929
--outPath public_html/js \
3030
--modules fittext,retina \
3131
--extraModules public_html/gumby-parallax/gumby.parallax.js
32-
32+
3333
Is equivalent to running `$ claymate build` with the following JSON in `gumby.json`:
3434

3535
{

bin/claymate

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ var usage =
1111
"$0 [subcommand] [arguments]\n\
1212
\n\
1313
Use one of the following subcommands:\n\
14-
build - Concatenate and minify the core gumby scripts and its source map.\n\
14+
build - Concatenate and minify the core gumby scripts.\n\
1515
\n\
16-
The options below will have which subcommand(s) they apply to in brackets before the description.\n\
16+
The options below will have which subcommand(s) they apply to in brackets\n\
17+
before the description.\n\
1718
";
1819

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

32+
.describe('buildSourceMap', '[build] Boolean flag, pass it to build the sourcemap.')
33+
.boolean('buildSourceMap')
34+
.alias('buildSourceMap', 's')
35+
3136
.describe('outPath', '[build] Where to write the minified and sourcemap files. If omitted, defaults to current directory.')
3237
.alias('outPath', 'o')
3338

@@ -45,7 +50,17 @@ var argv = require('optimist')
4550
// Handle the subcommands
4651
switch (argv._[0]) {
4752
case 'build':
48-
Claymate.build(getArg('modules', 'build', 'array'), getArg('extraModules', 'build', 'array'), getArg('gumbyPath', 'build'), getArg('outPath', 'build'));
53+
54+
var buildOpts = {
55+
gumbyPath: getArg('gumbyPath', 'build'),
56+
outPath: getArg('outPath', 'build'),
57+
modules: getArg('modules', 'build', 'array'),
58+
extraModules: getArg('extraModules', 'build', 'array'),
59+
includeSourceMap: getArg('buildSourceMap', 'build')
60+
};
61+
62+
Claymate.build(buildOpts);
63+
4964
break;
5065
}
5166

lib/claymate.js

Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ function Claymate() {
66
var fs = require("fs"),
77
path = require("path"),
88
util = require('util'),
9-
workingDir = process.cwd(),
10-
gumbyPathDefault = path.join(workingDir, '/components/gumby/');
9+
workingDir = process.cwd();
1110

1211
/**
1312
* Simple error handler for fs.write
@@ -23,18 +22,71 @@ function Claymate() {
2322
}
2423

2524
/**
26-
* Ensures gumby directories are present in the provided path
27-
* @param {String} rootPath What path to check.
28-
* @return {void} Throws an error.
25+
* Validates input to the build method.
26+
* @param {Object} buildInput Object literal of options to be cleaned.
27+
* @return {Object} The cleaned options.
2928
*/
30-
function checkGumbyDirs(rootPath) {
29+
function validateBuildInput(buildInput) {
30+
31+
var opts = {
32+
gumbyPath: '',
33+
outPath: '',
34+
modules: [],
35+
extraModules: [],
36+
includeSourceMap: false
37+
};
38+
39+
// Check for a passed directory
40+
if (typeof buildInput.gumbyPath === 'string') {
41+
42+
// If the ui dir doesnt exist, the rest don't since
43+
// the rest are all parents of that dir.
44+
if (!fs.existsSync(path.join(buildInput.gumbyPath, 'js/libs/ui'))) {
45+
throw new Error('Path ' + buildInput.gumbyPath + ' does not exist or does not contain gumby.');
46+
}
47+
48+
opts.gumbyPath = buildInput.gumbyPath;
49+
50+
}
51+
52+
// Check for path to write output to
53+
// Fall back to current directory.
54+
if (typeof buildInput.outPath === 'string') {
55+
56+
if (fs.existsSync(buildInput.outPath)) {
57+
opts.outPath = buildInput.outPath;
58+
}
59+
60+
}
61+
62+
// Check for passed modules, fall back
63+
// to all modules in ui directory
64+
if (util.isArray(buildInput.modules)) {
65+
66+
opts.modules = buildInput.modules;
67+
68+
} else {
69+
70+
opts.modules = fs.readdirSync(path.join(opts.gumbyPath, 'js/libs/ui'));
3171

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

74+
// Check for passed extra modules.
75+
if (util.isArray(buildInput.extraModules)) {
76+
77+
opts.extraModules = buildInput.extraModules;
78+
79+
}
80+
81+
// Force boolean
82+
if (typeof buildInput.includeSourceMap !== 'undefined') {
83+
84+
opts.includeSourceMap = !!buildInput.includeSourceMap;
85+
86+
}
87+
88+
return opts;
89+
3890
}
3991

4092
/**
@@ -57,69 +109,34 @@ function Claymate() {
57109

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

66122
var UglifyJS = require('uglify-js'),
67123
files = [],
124+
minOpts = {},
68125
minified, tmp, tmp2;
69126

70-
// Check for a passed directory
71-
if (typeof gumbyPath === 'string') {
72-
73-
// Normalize relative paths to the current working
74-
// directory
75-
tmp = path.resolve(workingDir, gumbyPath);
76-
77-
// If it exists, set it. Or fall back
78-
gumbyPath = (fs.existsSync(tmp)) ?
79-
tmp :
80-
gumbyPathDefault;
81-
82-
} else {
83-
84-
gumbyPath = gumbyPathDefault;
85-
86-
}
87-
88-
// Double check that we can acess gumby files
89-
checkGumbyDirs(gumbyPath);
90-
91-
// Check for passed modules, fall back
92-
// to all modules in ui directory
93-
if (!util.isArray(modules)) {
94-
95-
modules = fs.readdirSync(path.join(gumbyPath, 'js/libs/ui'));
96-
97-
}
98-
99-
// Check for path to write output to
100-
// Fall back to current directory.
101-
if (typeof outPath === 'string') {
102-
103-
if (!fs.existsSync(outPath)) {
104-
outPath = workingDir;
105-
}
106-
107-
} else {
108-
109-
outPath = workingDir;
110-
111-
}
127+
// Validate input
128+
opts = validateBuildInput(opts);
112129

113130
// Master gumby script
114-
files.push(path.join(gumbyPath, 'js/libs/gumby.js'));
131+
files.push(path.join(opts.gumbyPath, 'js/libs/gumby.js'));
115132

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

119-
tmp = path.basename(modules[i], '.js');
136+
tmp = path.basename(opts.modules[i], '.js');
120137

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

135152
// External modules, which will be passed as paths
136-
if (util.isArray(extraModules)) {
137-
138-
for (var j = 0; j < extraModules.length; j++) {
139-
140-
tmp = path.resolve(workingDir, extraModules[j]);
141-
142-
if (fs.existsSync(tmp)) {
143-
files.push(tmp);
144-
}
153+
for (var j = 0; j < opts.extraModules.length; j++) {
145154

155+
if (fs.existsSync(opts.extraModules[j])) {
156+
files.push(opts.extraModules[j]);
146157
}
147158

148159
}
149160

150161
// Final init script
151-
files.push(path.join(gumbyPath, 'js/libs/gumby.init.js'));
162+
files.push(path.join(opts.gumbyPath, 'js/libs/gumby.init.js'));
163+
164+
// Optionally request sourcemap
165+
if (opts.includeSourceMap) {
166+
minOpts.outSourceMap = 'gumby.js.map';
167+
}
168+
169+
minified = UglifyJS.minify(files, minOpts);
152170

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

157-
// Need to print minified.code, minified.map to js/libs/gumby.min.js js/libs/gumby.js.map
158-
// Write the minified and sourcemap files.
159-
fs.writeFile(path.join(outPath, 'gumby.min.js'), minified.code, writeErrorHandler);
160-
fs.writeFile(path.join(outPath, 'gumby.js.map'), minified.map, writeErrorHandler);
174+
// Write the sourcemap file.
175+
if (opts.includeSourceMap) {
176+
177+
// Minified.code apparently doesn't already contain this comment
178+
// pointing to the sourcemap file. Let's append it if theyre on.
179+
minified.code += '\n//# sourceMappingURL=' + minOpts.outSourceMap;
180+
181+
fs.writeFile(path.join(opts.outPath, 'gumby.js.map'), minified.map, writeErrorHandler);
182+
183+
}
161184

162185
};
163186

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "claymate",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"description": "Official helper scripts for the Gumby responsive framework.",
55
"main": "lib/claymate.js",
66
"bin": "bin/claymate",

0 commit comments

Comments
 (0)