Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Add a --lib option to include .js files from a package lib directory. #126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/ender.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ module.exports = {
option = 'use'
value = cmdOptions[i + 1] && cmdOptions[i + 1].replace(/\.js$/, '')
size = 2
} else if (option == '--lib') {
option = 'includeLib'
value = true
size = 1
} else if (option == '--max') {
option = 'max'
value = cmdOptions[i + 1]
Expand Down
6 changes: 5 additions & 1 deletion lib/ender.docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var ENDER = {
' + example: $ ' + 'ender build domready qwery bean\n'.cyan +
' + note: alternatively you can specify the -b flag\n' +
' + options:\n'.red +
' - ' + '--lib'.yellow + ' (--lib) Tells ender to include .js files for modules containing a lib directory specified on their package.json file.\n' +
' + example: ' + '$ ender build --lib coffee-script\n'.cyan +
' - ' + '--sandbox'.yellow + ' (--sandbox) Prevents global leakage of ender lib variables. Passing additional arguments will define the specified packages directly on the window.\n' +
' + example: ' + '$ ender build jeesh backbone --sandbox backbone\n'.cyan +
' - ' + '--noop'.yellow + ' (--noop, -x) build without the ender-js client library\n' +
Expand All @@ -28,6 +30,8 @@ var ENDER = {
' + adds a package to the current ender build\n' +
' + example: $ ender add underscore backbone\n' +
' + options:\n'.red +
' - ' + '--lib'.yellow + ' (--lib) Tells ender to include .js files for modules containing a lib directory specified on their package.json file.\n' +
' + example: ' + '$ ender add --lib coffee-script\n'.cyan +
' - ' + '--use'.yellow + ' (--use, -u) target a specific ender package (without use, ender defaults to local ender.js || ender.min.js)\n' +
' + example: ' + '$ ender add -u ../public/js/mootools.js\n\n'.cyan

Expand Down Expand Up @@ -106,4 +110,4 @@ module.exports = ENDER.util.merge(ENDER.docs, {

, 'list': ENDER.docs.info

});
});
156 changes: 113 additions & 43 deletions lib/ender.file.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ ENDER.file = module.exports = {
}
})
}

, glob: function(dir, regex, root) {
var list = []
fs.readdirSync(dir).forEach(function(name){
var file = path.join(dir, name)
if (ENDER.file.isDir(file)) {
list = list.concat(glob(file, regex, path.join(root, name)))
} else if (regex.test(file)) {
list.push(path.join(root, name))
}
})
return list
}

, isFile: function(file) {
return path.existsSync(file) && fs.statSync(file).isFile()
}

, isDir: function(file) {
return path.existsSync(file) && fs.statSync(file).isDirectory()
}

, gzip: function (_data, callback) {
gzip(_data, function (err, data) {
Expand Down Expand Up @@ -427,48 +448,80 @@ ENDER.file = module.exports = {

packageJSON = JSON.parse(data)

if (!packageJSON.main) {
packageJSON.main = []
var main
, lib

if (!packageJSON.main && ENDER.file.isFile(path.join(packagePath, 'index.js'))) {
main = 'index.js'
} else if (typeof packageJSON.main == 'string') {
packageJSON.main = [packageJSON.main]
main = packageJSON.main
}

if (options.includeLib && packageJSON.directories) {
lib = path.normalize(packageJSON.directories.lib)
}


var wrapSource = function(code, modName, modPath, inner) {
var source = [ 'require.def('+ modName +', '+modPath+', function(require){\n\n'
, 'var __dirname = require.dirname;'
, 'var __filename = require.filename'
, 'var module = { exports: {} }, exports = module.exports;'
, code.replace(/\n/g, '\n ')
, 'require.provide(module.exports);'
]

if (options.sandbox && ~options.sandbox.indexOf(modName)) {
source.push('typeof window != "undefined" && window["' + modName + '"] = module.exports;')
}
if (typeof inner === 'function') source.push(inner())
return source.join('\n\n ') + '\n\n return module.exports;\n\n});'
}

parallelQue = {
source: async.apply(ENDER.file.constructSource, packagePath, packageJSON.main)
source: async.apply(ENDER.file.constructSource, packagePath, main, lib)
, content: async.apply(ENDER.file.constructBridge, packagePath, packageJSON.ender)
}

async.parallel(parallelQue, function (err, results) {
if (err && options.debug) throw err
var source = results.source
var source = []
, files = results.source
, content = results.content
, strippedName = packageName.replace(/.*(?=\/)\//, '')

if (source && packageName != 'ender-js' && !options.noop) {
source = [ '!function () {\n\n var module = { exports: {} }, exports = module.exports;'
, source.replace(/\n/g, '\n ')
, 'provide("' + strippedName + '", module.exports);'
]
if (options.noop) { return callback && callback(null, '') }

if (options.sandbox && ~options.sandbox.indexOf(strippedName)) {
source.push('window["' + strippedName + '"] = module.exports;')
if (strippedName == 'ender-js') {
source.push(files.main)
if(options.sandbox) {
source = ['/* Declare local API */\nvar require, provide, $, ender;\n'
, source.join('\n')
, '\n/* Set Local API */\nrequire = this.require\nprovide = this.provide\nender = $ = this.ender'
]
}
}

if (packageJSON.ender && content) {
source.push(content.replace(/\n/g, '\n '))
} else if (!packageJSON.ender) {
source.push('$.ender(module.exports);')
if (strippedName != 'ender-js' && files.main) {
var filePath = path.normalize(path.join(strippedName, main))
source.push(wrapSource(files.main, '"'+strippedName+'"', '"'+filePath+'"', function(){
if (packageJSON.ender && content) {
return content.replace(/\n/g, '\n ')
} else if (!packageJSON.ender) {
return '$.ender(module.exports);'
}
}))

for(var i = 0, l = files.lib.length; i < l; i++) {
var name = files.libs[i]
, code = files.lib[i]
, modPath = path.normalize(path.join(strippedName, name))
, filePath = path.normalize(path.join(strippedName, lib, name))
source.push(wrapSource(code, 'null', '"'+filePath+'"'))
}

source = source.join('\n\n ') + '\n\n}();'
}

if (options.sandbox && packageName == 'ender-js') {
source = ['/* Declare local API */\nvar require, provide, $, ender;\n'
, source
, '\n/* Set Local API */\nrequire = this.require\nprovide = this.provide\nender = $ = this.ender'
].join('\n')
}
source = source.join('\n')

result[index] = source

Expand All @@ -478,28 +531,45 @@ ENDER.file = module.exports = {
})
}
}

, constructSource: function(packagePath, filePaths, callback) {
var result = []

if (!filePaths.length) {
return callback && callback(null, '')

, constructSource: function(packagePath, main, lib, callback) {
var result
, parallelQue
, mainFiles = []
, libFiles = []
, mainPath = path.join(packagePath, main)

var readFiles = function(dir, files, callback) {
async.map(files, function(f, cb) { fs.readFile(path.join(packagePath, dir, f), 'utf-8', cb) }, callback)
}

if (ENDER.file.isDir(mainPath)) {
console.error('Ignoring ' + mainPath + ' as it is a directory.')
} else if (ENDER.file.isFile(mainPath)) {
mainFiles = [main]
} else if (!(/\.js$/.test(mainPath)) && ENDER.file.isFile(mainPath+'.js')) {
mainFiles = [main + '.js']
}

if (lib && ENDER.file.isDir(path.join(packagePath, lib))) {
libFiles = ENDER.file.glob(path.join(packagePath, lib), /\.js$/)
}

filePaths.forEach(function (filePath) {
if (!(/\.js$/.test(filePath))) {
filePath += '.js'
parallelQue = {
main: async.apply(readFiles, null, mainFiles)
, lib: async.apply(readFiles, lib, libFiles)
}

async.parallel(parallelQue, function(err, results) {
if (err) throw err

result = {
main: results.main[0]
, lib: results.lib
, libs: libFiles.map(function(n){ return n.replace(/\.js$/, '') })
}
fs.readFile(path.join(packagePath, filePath), 'utf-8', function (err, data) {
if (err) {
callback(err)
return console.log('something went wrong trying to read ' + path.join(packagePath, filePath))
}
result.push(data)
if (filePaths.length == result.length) {
callback && callback(null, result.join('\n\n'))
}
})

callback(err, result)
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ender.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ module.exports = {
return true
}

}
}