Permalink
Browse files

load source files as filename/contents pairs

  • Loading branch information...
1 parent 472c27d commit d78bd38c5d756820d6daa82da495344864d9de2b @rvagg rvagg committed May 13, 2012
Showing with 50 additions and 33 deletions.
  1. +5 −7 lib/source-package-util.js
  2. +9 −2 lib/source-package.js
  3. +36 −24 test/unit/source-package-util-test.js
View
@@ -54,7 +54,7 @@ var fs = require('fs')
}
// utility to read multiple files in order and append them
- , loadFilesAsString = function (root, files, callback) {
+ , loadFiles = function (root, files, callback) {
if (!Array.isArray(files)) files = [ files ]
if (!files.length || (files.length == 1 && files[0] == 'noop')) return callback()
@@ -65,18 +65,16 @@ var fs = require('fs')
files
, function (file, callback) {
file = path.join(root, file).replace(/(\.js)?$/, '.js')
- fs.readFile(file, 'utf-8', function (err) {
+ fs.readFile(file, 'utf-8', function (err, contents) {
if (err) return callback(new FilesystemError(err))
- callback.apply(null, arguments)
+ callback(null, { file: file, contents: contents })
})
}
- , function (err, sources) {
- callback(err, sources && sources.join('\n\n'))
- }
+ , callback
)
})
}
module.exports = {
- loadFilesAsString: loadFilesAsString
+ loadFiles: loadFiles
}
View
@@ -117,6 +117,13 @@ var fs = require('fs')
, handleSourceData = function (err, sources) {
if (err) return callback(err)
+ // take the array of { file: 'path', contents: 'source' } and just join the contents
+ // for more complex handling later
+ sources.ender =
+ sources.ender && sources.ender.map(function (s) { return s.contents }).join('\n\n')
+ sources.main =
+ sources.main && sources.main.map(function (s) { return s.contents }).join('\n\n')
+
generateSource(
this.isRoot
, makeTemplateData.bind(this)(sources)
@@ -125,8 +132,8 @@ var fs = require('fs')
}.bind(this)
, sourceLoaders = {
- main: sourcePackageUtil.loadFilesAsString.bind(this, root, mainSources)
- , ender: sourcePackageUtil.loadFilesAsString.bind(this, root, enderBridgeSources)
+ main: sourcePackageUtil.loadFiles.bind(this, root, mainSources)
+ , ender: sourcePackageUtil.loadFiles.bind(this, root, enderBridgeSources)
}
async.parallel(sourceLoaders, handleSourceData)
@@ -36,7 +36,7 @@ testCase('Source package util', {
mkfiletree.cleanUp(done)
}
- , 'loadFilesAsString': {
+ , 'loadFiles': {
// note: we expect globs to return a sorted list of files (dirname(s) included)
'test single file with exact name': function (done) {
@@ -45,9 +45,12 @@ testCase('Source package util', {
, { 'foo.js': 'fooish!' }
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ 'foo.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ 'foo.js' ], function (err, contents) {
refute(err)
- assert.equals(contents, 'fooish!')
+ assert.equals(
+ contents
+ , [ { file: path.join(dir, 'foo.js'), contents: 'fooish!' } ]
+ )
done()
})
}
@@ -60,9 +63,12 @@ testCase('Source package util', {
, { 'foo.js': 'fooish!' }
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ '**/*.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ '**/*.js' ], function (err, contents) {
refute(err)
- assert.equals(contents, 'fooish!')
+ assert.equals(
+ contents
+ , [ { file: path.join(dir, 'foo.js'), contents: 'fooish!' } ]
+ )
done()
})
}
@@ -83,23 +89,24 @@ testCase('Source package util', {
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ '**/*.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ '**/*.js' ], function (err, contents) {
refute(err)
assert.equals(
contents
- , 'bang!\n\n'
- + 'baz!\n\n'
- + 'boo!!\n\n'
- + 'boo!!!\n\n'
- + 'fooish!'
+ , [
+ { file: path.join(dir, 'bar/bang.js'), contents: 'bang!' }
+ , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
+ , { file: path.join(dir, 'bar/boo/1.js'), contents: 'boo!!' }
+ , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
+ , { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
+ ]
)
done()
})
}
)
}
-
, 'test lots of files, cherry pick specifics': function (done) {
mkfiletree.makeTemp(
'ender-test-source-package-util'
@@ -114,14 +121,16 @@ testCase('Source package util', {
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ 'foo.js', './bar/bang.js', '/bar/boo/2.js', 'bar/baz.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ 'foo.js', './bar/bang.js', '/bar/boo/2.js', 'bar/baz.js' ], function (err, contents) {
refute(err)
assert.equals(
contents
- , 'fooish!\n\n'
- + 'bang!\n\n'
- + 'boo!!!\n\n'
- + 'baz!'
+ , [
+ { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
+ , { file: path.join(dir, 'bar/bang.js'), contents: 'bang!' }
+ , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
+ , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
+ ]
)
done()
})
@@ -143,12 +152,14 @@ testCase('Source package util', {
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ '*.js', 'bar/baz.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ '*.js', 'bar/baz.js' ], function (err, contents) {
refute(err)
assert.equals(
contents
- , 'fooish!\n\n'
- + 'baz!'
+ , [
+ { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
+ , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
+ ]
)
done()
})
@@ -170,13 +181,14 @@ testCase('Source package util', {
, function (err, dir) {
refute(err)
- sourcePackageUtil.loadFilesAsString(dir, [ '*/*/*.js' ], function (err, contents) {
+ sourcePackageUtil.loadFiles(dir, [ '*/*/*.js' ], function (err, contents) {
refute(err)
assert.equals(
contents
- ,
- 'boo!!\n\n'
- + 'boo!!!'
+ , [
+ { file: path.join(dir, 'bar/boo/1.js'), contents: 'boo!!' }
+ , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
+ ]
)
done()
})

0 comments on commit d78bd38

Please sign in to comment.