Skip to content

Commit

Permalink
Merge pull request box#57 from Box/buildupdate
Browse files Browse the repository at this point in the history
Switch to ShellJS and create dist files
  • Loading branch information
Nicholas Zakas committed Mar 25, 2015
2 parents 5a359d2 + cf20ae3 commit 3e9c109
Show file tree
Hide file tree
Showing 28 changed files with 2,487 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
_site
node_modules
dist
doc
*.swp
*.iml
.idea
.DS_Store
npm-debug.log
dist/t3-*.js
75 changes: 0 additions & 75 deletions Gruntfile.js

This file was deleted.

196 changes: 196 additions & 0 deletions Makefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* @fileoverview Build file
* @author nzakas
*/
/*global target, exec, echo, find, which, test, exit, mkdir*/

'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

require('shelljs/make');

var util = require('util'),
nodeCLI = require('shelljs-nodecli');

//------------------------------------------------------------------------------
// Data
//------------------------------------------------------------------------------

var NODE = 'node ', // intentional extra space
NODE_MODULES = './node_modules/',
BUILD_DIR = './build/',
DIST_DIR = './dist/',
LIB_DIR = './lib/',

// Utilities - intentional extra space at the end of each string
ISTANBUL = NODE + NODE_MODULES + 'istanbul/lib/cli.js ',
MOCHA = NODE_MODULES + 'mocha/bin/_mocha ',
JSDOC = NODE + NODE_MODULES + 'jsdoc/jsdoc.js ',

// Directories
JS_DIRS = getSourceDirectories(),
SRC_FILES = ['lib/box.js', 'lib/event-target.js', 'lib/context.js', 'lib/application.js'],

// Files
JS_FILES = find(JS_DIRS).filter(fileType('js')).join(' '),
TEST_FILES = find('tests/').filter(fileType('js')).join(' ');

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Executes a Node CLI and exits with a non-zero exit code if the
* CLI execution returns a non-zero exit code. Otherwise, it does
* not exit.
* @param {...string} [args] Arguments to pass to the Node CLI utility.
* @returns {void}
* @private
*/
function nodeExec(args) {
args = arguments; // make linting happy
var code = nodeCLI.exec.apply(nodeCLI, args).code;
if (code !== 0) {
exit(code);
}
}

/**
* Runs exec() but exits if the exit code is non-zero.
* @param {string} cmd The command to execute.
* @returns {void}
* @private
*/
function execOrExit(cmd) {
var code = exec(cmd).code;
if (code !== 0) {
exit(code);
}
}

/**
* Generates a function that matches files with a particular extension.
* @param {string} extension The file extension (i.e. 'js')
* @returns {Function} The function to pass into a filter method.
* @private
*/
function fileType(extension) {
return function(filename) {
return filename.substring(filename.lastIndexOf('.') + 1) === extension;
};
}

/**
* Determines which directories are present that might have JavaScript files.
* @returns {string[]} An array of directories that exist.
* @private
*/
function getSourceDirectories() {
var dirs = [ 'lib', 'src', 'app' ],
result = [];

dirs.forEach(function(dir) {
if (test('-d', dir)) {
result.push(dir);
}
});

return result;
}

/**
* Creates a release version tag and pushes to origin.
* @param {string} type The type of release to do (patch, minor, major)
* @returns {void}
*/
function release(type) {
target.test();

target.dist();

execOrExit('git add -A');
execOrExit('git commit --amend --no-edit');

execOrExit('npm version ' + type);

// ...and publish
execOrExit('git push origin master --tags');
}


//------------------------------------------------------------------------------
// Tasks
//------------------------------------------------------------------------------

target.all = function() {
target.test();
};

target.lint = function() {
echo('Validating JavaScript files');
nodeExec('eslint', JS_FILES);
};

target.test = function() {
target.lint();

echo('Running browser tests');
var code = exec('node ./node_modules/karma/bin/karma start config/karma-conf.js').code;
if (code !== 0) {
exit(code);
}
};

target.docs = function() {
echo('Generating documentation');
exec(JSDOC + '-d jsdoc ' + JS_DIRS.join(' '));
echo('Documentation has been output to /jsdoc');
};

target.dist = function() {
var pkg = require('./package.json'),
distFilename = DIST_DIR + pkg.name + '.js',
minDistFilename = distFilename.replace(/\.js$/, '.min.js');

if (test('-d', DIST_DIR)) {
rm('-r', DIST_DIR + '*');
} else {
mkdir(DIST_DIR);
}

// concatenate files together
cat(SRC_FILES).to(distFilename);

// create minified version
nodeExec('uglifyjs', distFilename, '-o', minDistFilename);

// Add copyrights and version info
var versionComment = '/*! ' + pkg.name + ' v ' + pkg.version + '*/\n',
copyrightComment = cat('./config/copyright.txt');

(copyrightComment + versionComment + cat(distFilename)).to(distFilename);
(copyrightComment + versionComment + cat(minDistFilename)).to(minDistFilename);

// ensure there's a newline at the end of each file
(cat(distFilename) + '\n').to(distFilename);
(cat(minDistFilename) + '\n').to(minDistFilename);

// create filenames with version in them
cp(distFilename, distFilename.replace('.js', '-' + pkg.version + '.js'));
cp(minDistFilename, minDistFilename.replace('.min.js', '-' + pkg.version + '.min.js'));
};

target.patch = function() {
release('patch');
};

target.minor = function() {
release('minor');
};

target.major = function() {
release('major');
};
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ Clone the repo:
git clone https://gitenterprise.inside-box.net/Box/T3.git
```

Install the [grunt-cli](http://gruntjs.com/getting-started#installing-the-cli) globally:

```bash
sudo npm install -g grunt-cli
```

Install dependencies:

```bash
cd T3 && npm install
```

Build concatenated file (dist/t3-\<version\>.js) and docs (doc/index.html):
Build concatenated file (dist/t3-\<version\>.js):

```bash
grunt build
npm run dist
```

Developing
Expand All @@ -36,6 +30,6 @@ Developing
Lint and run unit tests:

```bash
grunt
npm test
```

15 changes: 15 additions & 0 deletions config/copyright.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*!
Copyright 2015 Box, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Loading

0 comments on commit 3e9c109

Please sign in to comment.