Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use current working directory by default; add tests

  • Loading branch information...
commit 58f37cbcf845fd807c7df3bb187d61a4103d3e52 1 parent 5af4dbf
@powmedia authored
View
1  .gitignore
@@ -1,3 +1,4 @@
+.DS_Store
lib-cov
*.seed
*.log
View
5 README.md
@@ -9,7 +9,6 @@ Builder for creating distributable JavaScript files from source. Concatenate, wr
var buildify = require('buildify');
buildify()
- .setDir(__dirname + '/src')
.load('base.js')
.concat(['part1.js', 'part2.js'])
.wrap('../lib/template.js', { version: '1.0' })
@@ -20,9 +19,11 @@ Builder for creating distributable JavaScript files from source. Concatenate, wr
##API
-###buildify([options])
+###buildify([dir, options])
Create a new Builder instance.
+Takes the starting directory as the first argument, e.g. __dirname. If this is not set, the current working directory is used.
+
Options:
- `dir` Base directory file operations start in.
- `interpolate` Underscore template settings. Default to mustache {{var}} style interpolation tags.
View
18 index.js
@@ -5,14 +5,16 @@ var fs = require('fs'),
uglifyJS = require('uglify-js');
/**
- * @param {Object} options
- * @param {String} [options.dir] Base directory file operations start in.
+ * @param {String} [dir] Starting directory absolute path. Default: current working dir
+ * @param {Object} [options]
* @param {Object} [options.interpolate] Underscore template settings. Default to mustache {{var}} style interpolation tags.
* @param {String} [options.encoding] File encoding ('utf-8')
* @param {String} [options.eol] End of line character ('\n')
* @param {Boolean} [options.quiet] Whether to silence console output
*/
-function Builder(options) {
+function Builder(dir, options) {
+ dir = dir || process.cwd();
+
this.options = _.extend({
encoding: 'utf-8',
eol: '\n',
@@ -22,7 +24,7 @@ function Builder(options) {
_.templateSettings.interpolate = this.options.interpolate;
//The current directory
- this.setDir(this.options.dir || __dirname);
+ this.setDir(dir);
//The content being acted on
this.content = '';
@@ -90,7 +92,7 @@ Builder.prototype.load = function(file) {
* @param {String} [eol] Join character. Default: '\n'
*/
Builder.prototype.concat = function(files, eol) {
- eol = eol || this.options.eol;
+ eol = (_.isUndefined(eol)) ? this.options.eol : eol;
if (!_.isArray(files)) files = [files];
@@ -103,6 +105,8 @@ Builder.prototype.concat = function(files, eol) {
return fs.readFileSync(file, encoding);
});
+ if (this.content) contents.unshift(this.content);
+
this.content = contents.join(eol);
return this;
@@ -182,6 +186,6 @@ Builder.prototype.clear = function() {
*
* @param {Object} [options] Constructor options
*/
-module.exports = function(options) {
- return new Builder(options);
+module.exports = function(dir, options) {
+ return new Builder(dir, options);
};
View
5 package.json
@@ -13,7 +13,10 @@
"uglify-js": "1.3.0",
"underscore": "1.3.3"
},
- "devDependencies": {},
+ "devDependencies": {
+ "nodeunit": "latest",
+ "sinon": "latest"
+ },
"optionalDependencies": {},
"engines": {
"node": ">=0.4.0"
View
246 test/index.test.js
@@ -0,0 +1,246 @@
+//Run these tests with nodeunit
+
+var builder = require('../index.js'),
+ mkdirp = require('mkdirp'),
+ fs = require('fs'),
+ sinon = require('sinon');
+
+
+exports['constructor - dir defaults to working directory'] = function(test) {
+ var b = builder();
+
+ test.same(b.dir, process.cwd());
+
+ test.done();
+};
+
+
+exports['constructor option defaults'] = function(test) {
+ var b = builder();
+
+ test.same(b.options, {
+ encoding: 'utf-8',
+ eol: '\n',
+ interpolate: /\{\{(.+?)\}\}/g
+ });
+
+ test.same(b.content, '');
+
+ test.done();
+};
+
+
+exports['constructor options'] = function(test) {
+ var b = builder(null, {
+ eol: '\r\n',
+ interpolate: /\<\<(.+?)\?\>/g,
+ quiet: true
+ });
+
+ test.same(b.options, {
+ encoding: 'utf-8',
+ eol: '\r\n',
+ interpolate: /\<\<(.+?)\?\>/g,
+ quiet: true
+ });
+
+ test.done();
+};
+
+
+exports['setDir'] = function(test) {
+ var b = builder('/');
+
+ b.setDir('/foo/bar');
+
+ test.same(b.dir, '/foo/bar');
+
+ test.same(b, b.setDir('/'), 'Returns self for chaining');
+
+ test.done();
+};
+
+
+exports['changeDir'] = function(test) {
+ var b = builder('/foo');
+
+ test.same(b.dir, '/foo');
+
+ b.changeDir('bar');
+ test.same(b.dir, '/foo/bar');
+
+ b.changeDir('..');
+ test.same(b.dir, '/foo');
+
+ b.changeDir('./bar/baz/..');
+ test.same(b.dir, '/foo/bar');
+
+ test.same(b, b.changeDir('.'), 'Returns self for chaining');
+
+ test.done();
+};
+
+
+exports['setContent'] = function(test) {
+ var b = builder(__dirname);
+
+ test.same(b.content, '');
+
+ b.setContent('foo');
+ test.same(b.content, 'foo');
+
+ test.same(b, b.setContent(''), 'Returns self for chaining');
+
+ test.done();
+};
+
+
+exports['getContent'] = function(test) {
+ var b = builder(__dirname);
+
+ b.setContent('bla');
+
+ test.same(b.getContent(), 'bla');
+
+ test.done();
+};
+
+
+exports['load'] = function(test) {
+ var b = builder(__dirname + '/support');
+
+ b.load('foo.txt');
+ test.same(b.content, 'FOO');
+
+ b.load('./bar.txt');
+ test.same(b.content, 'BAR');
+
+ test.same(b, b.load('foo.txt'), 'Returns self for chaining');
+
+ test.done();
+};
+
+
+exports['concat'] = {
+ 'default eol': function(test) {
+ var b = builder(__dirname + '/support');
+
+ b.concat(['./foo.txt', 'bar.txt']);
+ test.same(b.content, 'FOO\nBAR');
+
+ test.done();
+ },
+
+ 'custom eol': function(test) {
+ var b = builder(__dirname + '/support');
+
+ b.setContent('Hello.');
+
+ b.concat(['bar.txt', 'foo.txt'], '');
+ test.same(b.content, 'Hello.BARFOO');
+
+ test.done();
+ },
+
+ 'passing single file': function(test) {
+ var b = builder(__dirname + '/support');
+
+ b.setContent('Test!')
+
+ b.concat('foo.txt');
+ test.same(b.content, 'Test!\nFOO');
+
+ test.done();
+ },
+
+ 'returns self for chaining': function(test) {
+ var b = builder(__dirname + '/support');
+
+ test.same(b, b.concat('foo.txt'), 'Returns self for chaining');
+
+ test.done();
+ }
+};
+
+
+exports['wrap'] = function(test) {
+ var b = builder(__dirname + '/support');
+
+ b.setContent('FOO');
+
+ var returned = b.wrap('template.txt', { version: '0.1.0' });
+
+ test.same(b.content, 'Module v0.1.0: FOO!');
+
+ test.same(returned, b, 'Returns self for chaining');
+
+ test.done();
+};
+
+
+exports['uglify'] = function(test) {
+ var b = builder();
+
+ b.setContent(' foo = 123; ')
+
+ b.uglify();
+
+ test.same(b.content, 'foo=123');
+
+ test.done();
+};
+
+
+exports['save'] = {
+ setUp: function(done) {
+ this.sinon = sinon.sandbox.create();
+
+ this.sinon.stub(fs, 'writeFileSync');
+ this.sinon.stub(mkdirp, 'sync');
+
+ done();
+ },
+
+ tearDown: function(done) {
+ this.sinon.restore();
+ done();
+ },
+
+ main: function(test) {
+ var b = builder(__dirname + '/support', { quiet: true });
+
+ b.setContent('test');
+
+ var returned = b.save('path/to/file/output.txt');
+
+ //Test make directory recursively
+ test.same(mkdirp.sync.lastCall.args, [
+ __dirname + '/support/path/to/file'
+ ]);
+
+ //Test wrote file OK
+ test.same(fs.writeFileSync.lastCall.args, [
+ __dirname + '/support/path/to/file/output.txt',
+ 'test'
+ ]);
+
+ test.same(returned, b, 'Returns self for chaining');
+
+ test.done();
+ }
+};
+
+
+exports['clear'] = function(test) {
+ var b = builder();
+
+ b.setContent('foo');
+
+ var returned = b.clear();
+
+ test.same(b.content, '');
+
+ test.same(returned, b, 'Returns self for chaining');
+
+ test.done();
+};
View
1  test/support/bar.txt
@@ -0,0 +1 @@
+BAR
View
1  test/support/foo.txt
@@ -0,0 +1 @@
+FOO
View
1  test/support/template.txt
@@ -0,0 +1 @@
+Module v{{version}}: {{body}}!
Please sign in to comment.
Something went wrong with that request. Please try again.