Permalink
Browse files

--client-lib option to override default 'ender-js'

  • Loading branch information...
1 parent 488e84e commit 9ba05d7125bb0e26802b546ba8ae4e7cb6233af2 @rvagg rvagg committed Apr 8, 2012
View
@@ -66,19 +66,20 @@ var UnknownMainError = require('./errors').UnknownMainError
arr.push(o)
}
- // --foo -f [type]
- add('packages' , '' , Array)
- add('output' , 'o' , String)
- add('use' , 'u' , String)
- add('max' , Number)
- add('sandbox' , Array)
- add('noop' , 'x' , Boolean)
- add('silent' , 's' , Boolean)
- add('help' , 'h' , Boolean, true)
- add('sans' , Boolean)
- add('debug' , Boolean)
- add('version' , 'v' , Boolean, true)
- add('externs' , Array)
+ // --foo -f [type] [alternate main cmd]
+ add('packages' , '' , Array)
+ add('output' , 'o' , String)
+ add('use' , 'u' , String)
+ add('max' , Number)
+ add('sandbox' , Array)
+ add('noop' , 'x' , Boolean)
+ add('silent' , 's' , Boolean)
+ add('help' , 'h' , Boolean, true)
+ add('sans' , Boolean)
+ add('debug' , Boolean)
+ add('version' , 'v' , Boolean, true)
+ add('externs' , Array)
+ add('client-lib' , String)
return arr
}())
@@ -103,7 +104,7 @@ var UnknownMainError = require('./errors').UnknownMainError
, findOption = function (s) {
var i
, option
- , match = s.match(/^(--?)(\w+)$/)
+ , match = s.match(/^(--?)([a-z\-]+)$/)
if (!match) return
View
@@ -34,11 +34,10 @@
var async = require('async')
, path = require('path')
, packageUtil = require('./package-util')
-
-var defaultRootPackage = 'ender-js'
+ , util = require('./util')
// unique packages by proper name
- , uniquePackages = function (packages) {
+var uniquePackages = function (packages) {
var ret = []
, have = []
@@ -55,14 +54,14 @@ var defaultRootPackage = 'ender-js'
, isRootPackage = function (options, p) {
if (options.noop || options.sans) return false
- return packageUtil.cleanName(p) === defaultRootPackage
+ return packageUtil.cleanName(p) === util.getRootPackageName(options)
}
// given a list of packages, provide a sanitised list without duplicates and with
- // ender-js at the start.
+ // the root package at the start.
, packageList = function (options) {
var packages = options.packages && options.packages.length ? options.packages : [ '.' ]
- if (!options.noop && !options.sans) packages = [ defaultRootPackage ].concat(packages)
+ if (!options.noop && !options.sans) packages = [ util.getRootPackageName(options) ].concat(packages)
return uniquePackages(packages)
}
@@ -190,12 +189,12 @@ var defaultRootPackage = 'ender-js'
// this is used for dependency tree printing and may trigger the callback on
// duplicate packages (although the last argument of the callback is a uniqueness
// indicator)
- , forEachOrderedDependency = function (packages, tree, callback, _unique, _pkglist) {
+ , forEachOrderedDependency = function (options, packages, tree, callback, _unique, _pkglist) {
var ejs
// _unique, _pkglist is for internal use
if (!_pkglist) { // first call, to top-level stuff
- ejs = packages.indexOf('ender-js')
- // take ender-js from where it is and put it at the front
+ ejs = packages.indexOf(util.getRootPackageName(options))
+ // take root package from where it is and put it at the front
if (ejs > 0) packages.splice(0, 0, packages.splice(ejs, 1)[0])
_pkglist = []
}
@@ -205,7 +204,8 @@ var defaultRootPackage = 'ender-js'
if (isUnique || !_unique) {
if (tree[p].dependencies) {
forEachOrderedDependency(
- Object.keys(tree[p].dependencies)
+ options
+ , Object.keys(tree[p].dependencies)
, tree[p].dependencies
, callback
, _unique
@@ -221,11 +221,11 @@ var defaultRootPackage = 'ender-js'
// does the same as the above but doesn't trigger the callback for packages that have
// already been passed.
// this is for SourceBuild assembling
- , forEachUniqueOrderedDependency = function (packages, tree, callback) {
- return forEachOrderedDependency(packages, tree, callback, true)
+ , forEachUniqueOrderedDependency = function (options, packages, tree, callback) {
+ return forEachOrderedDependency(options, packages, tree, callback, true)
}
- // gives a list of packages by pro1per name from package.json, turns a path into a package name
+ // gives a list of packages by proper name from package.json, turns a path into a package name
, localizePackageList = function (packages, tree) {
var newList = []
packages.forEach(function (p) {
View
@@ -58,13 +58,18 @@ var util = require('./util')
buildUtil.constructDependencyTree(packages, function (err, tree) {
if (err) return callback(err) // wrapped in package-utils.js
- var srcBuild = SourceBuild.create(options) // new SourceBuild object to store each package in
- packages = buildUtil.localizePackageList(packages, tree) // sanitise and localise the names from relative paths
+ // new SourceBuild object to store each package in
+ var srcBuild = SourceBuild.create(options)
+ // sanitise and localise the names from relative paths
+ , localizedPackages = buildUtil.localizePackageList(packages, tree)
+ , rootPackageName = util.getRootPackageName(options)
//TODO: warn if this has > 0 elements: packages.map(function (p) { return packageUtil.isPath(p) })
- buildUtil.forEachUniqueOrderedDependency(packages, tree, function (packageName, parents, data) {
+ buildUtil.forEachUniqueOrderedDependency(options, localizedPackages, tree, function (packageName, parents, data) {
+ var pidx = localizedPackages.indexOf(packageName)
+ , isRoot = (pidx != -1 && packages[pidx] == rootPackageName) || packageName == rootPackageName
// each package that we need, add it to SourceBuild as a SourcePackage object
- srcBuild.addPackage(SourcePackage.create(parents, packageName, data.packageJSON, options))
+ srcBuild.addPackage(SourcePackage.create(packageName, parents, isRoot, data.packageJSON, options))
})
// write the output files!
@@ -79,7 +84,7 @@ var util = require('./util')
, out
, util.getOutputFilenameFromOptions(options)
, options
- , packages
+ , localizedPackages
, tree
, callback
)
View
@@ -81,11 +81,11 @@ var zlib = require('zlib')
// dependency tree into an archy-compatible tree.
// we only do a 1/2 job here and leave it to main-info-output to complete the
// process and make it perdy.
- , buildArchyTree = function (packages, tree) {
+ , buildArchyTree = function (options, packages, tree) {
var archyTree = { label: 'Active packages:', nodes: [] }
, localPackages = mainBuildUtil.localizePackageList(packages, tree)
- mainBuildUtil.forEachOrderedDependency(localPackages, tree, function (packageName, parents, data, index, first) {
+ mainBuildUtil.forEachOrderedDependency(options, localPackages, tree, function (packageName, parents, data, index, first) {
var archyTreeNode = archyTree
, found
, newNode
View
@@ -72,7 +72,7 @@ var async = require('async')
var tree = data.spec.tree
, context = data.spec.context
// build an `archy` compatible tree representing the packages in the build
- , archyTree = mainInfoUtil.buildArchyTree(context.options.packages, tree)
+ , archyTree = mainInfoUtil.buildArchyTree(context.options, context.options.packages, tree)
out.buildInfo(filename, context.options, context.packages, data.sizes, archyTree)
callback()
View
@@ -44,7 +44,7 @@ var async = require('async')
, exec = function (options, out, callback) {
var filename = util.getInputFilenameFromOptions(options)
, packages = options.packages.filter(function (p) {
- return !mainBuildUtil.isRootPackage(options, p) // can't remove ender-js
+ return !mainBuildUtil.isRootPackage(options, p) // can't remove root package
})
, finish = function (err) {
repository.packup(err)
View
@@ -38,11 +38,12 @@ var fs = require('fs')
fs.readFile(file, 'utf-8', function (err, data) {
if (err) return callback(new FilesystemError(err))
try {
- out.version(JSON.parse(data).version)
- callback()
+ data = JSON.parse(data)
} catch (err) {
- callback(new JSONParseError(err.message + ' [' + file + ']', err))
+ return callback(new JSONParseError(err.message + ' [' + file + ']', err))
}
+ out.version(data.version)
+ callback()
})
}
View
@@ -75,7 +75,7 @@ var colors = require('colors')
}
, error: function (err) {
- var name = (err.name || 'Error').replace(/(\w)([A-Z])/g, '$1 $2')
+ var name = (err.name || 'Error').replace(/([a-z])([A-Z])/g, '$1 $2')
this.log((name + ': ').red.bold + err.message.red)
if (this.isDebug) {
this.log(err.stack)
View
@@ -78,10 +78,11 @@ var fs = require('fs')
fs.readFile(file, 'utf-8', function (err, data) {
if (err) return callback(new FilesystemError(err))
try {
- callback(null, JSON.parse(data))
+ data = JSON.parse(data)
} catch (err) {
- callback(new JSONParseError(err.message + ' [' + file + ']', err))
+ return callback(new JSONParseError(err.message + ' [' + file + ']', err))
}
+ callback(null, data)
})
}
View
@@ -40,13 +40,12 @@ var fs = require('fs')
, templateFiles = {
'standard' : '../resources/source-package.mustache'
- , 'ender-js' : '../resources/ender-js-package.mustache' // a special template for the root package
+ , 'root' : '../resources/root-package.mustache' // a special template for the root package
}
// pass the source through
- , generateSource = function (type, data, callback) {
- if (!templateFiles[type]) type = 'standard'
- template.generateSource(templateFiles[type], data, callback)
+ , generateSource = function (root, data, callback) {
+ template.generateSource(templateFiles[root ? 'root' : 'standard'], data, callback)
}
, indent = function (str) {
@@ -63,9 +62,10 @@ var fs = require('fs')
}
, SourcePackage = {
- init: function (packageName, parents, packageJSON, options) {
+ init: function (packageName, parents, isRoot, packageJSON, options) {
this.parents = parents
this.options = options
+ this.isRoot = isRoot
this.packageName = packageName
this.packageJSON = packageJSON
@@ -109,10 +109,10 @@ var fs = require('fs')
noop : this.options.noop
, sandbox :
// if this package is in the `--sandbox <packages>` list, or, if we are
- // an ender-js package and a --sandbox option is passed, then set this to true
+ // the root package and a --sandbox option is passed, then set this to true
Array.isArray(this.options.sandbox) && (
this.options.sandbox.indexOf(this.packageJSON.name) != -1
- || this.packageJSON.name == 'ender-js')
+ || this.isRoot)
}
// these objects have lazy methods so we don't do unnecessary indent()ing
, mainSource: sources.main && {
@@ -140,7 +140,7 @@ var fs = require('fs')
if (err) return callback(err)
generateSource(
- packageName
+ this.isRoot
, this.makeTemplateData(sources)
, callback
)
@@ -155,6 +155,6 @@ var fs = require('fs')
}
}
-module.exports.create = function (parents, packageName, packageJSON, options) {
- return Object.create(SourcePackage).init(packageName, parents, packageJSON, options)
+module.exports.create = function (packageName, parents, isRoot, packageJSON, options) {
+ return Object.create(SourcePackage).init(packageName, parents, isRoot, packageJSON, options)
}
View
@@ -37,6 +37,8 @@ var tmpDir = process.env.TMPDIR
|| process.env.TEMP
|| (process.platform === "win32" ? "c:\\windows\\temp" : "/tmp")
+ , defaultRootPackage = 'ender-js'
+
, homeDir = process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME
, extend = function (src, dst) {
@@ -73,6 +75,10 @@ var tmpDir = process.env.TMPDIR
return getFilenameFromOptions(options.output)
}
+ , getRootPackageName = function (options) {
+ return options['client-lib'] || defaultRootPackage
+ }
+
, toKb = function (size) {
size = Math.round(size / 1024 * 10) / 10
return size + ' kB'
@@ -86,4 +92,5 @@ module.exports = {
, homeDir : homeDir
, getInputFilenameFromOptions : getInputFilenameFromOptions
, getOutputFilenameFromOptions : getOutputFilenameFromOptions
+ , getRootPackageName : getRootPackageName
}
@@ -7,4 +7,4 @@ var require, provide, $, ender;
/* Set Local API */
require = this.require;
provide = this.provide;
-ender = $ = this.ender;{{/options.sandbox}}
+ender = $ = this.ender;{{/options.sandbox}}
@@ -108,7 +108,9 @@ buster.testCase('Args parser', {
}
, 'test parse returns expected object (-- long form)': function () {
- var actual = argsParse.parse(buildargs('build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug'))
+ var actual = argsParse.parse(buildargs(
+ 'build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug --externs what tha --client-lib BOOM'
+ ))
assert.equals(
actual
, {
@@ -123,6 +125,8 @@ buster.testCase('Args parser', {
, help: true
, sans: true
, debug: true
+ , externs: [ 'what', 'tha' ]
+ , 'client-lib': 'BOOM'
}
)
}
@@ -277,9 +281,11 @@ buster.testCase('Args parser', {
, 'test "--" long form': function () {
var ctx = argsParse.toContextString(
argsParse.parse(
- buildargs('build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug')))
+ buildargs(
+ 'build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug --externs what tha --client-lib BOOM'
+ )))
assert(ctx)
- assert.equals(ctx.split(' ').length, 19)
+ assert.equals(ctx.split(' ').length, 24)
ctx += ' ' // for convenience so we can match spaces around each element, even at the end
assert.match(ctx, /^build fee fie foe fum /)
assert.match(ctx, / --output foobar /)
@@ -291,6 +297,8 @@ buster.testCase('Args parser', {
assert.match(ctx, / --help /)
assert.match(ctx, / --sans /)
assert.match(ctx, / --debug /)
+ assert.match(ctx, / --externs what tha /)
+ assert.match(ctx, / --client-lib BOOM /)
}
}
})
@@ -38,7 +38,6 @@ testCase('Build', {
'test exec() calls setup(), install() and packup() on repository': function (done) {
var mock = this.mock(repository)
, mockUtil = this.mock(util)
- , mockOut = this.mock(buildOutput)
mockUtil.expects('mkdir').once().withArgs('node_modules').callsArg(1)
@@ -73,7 +72,7 @@ testCase('Build', {
, optionsArg = { options: 1 }
, packagesArg = { packages: 1 }
- , localizedArg = { localized: 1 }
+ , localizedArg = [ 'foobar' ]
, installedArg = [ { installed: 1 } ]
, npmTreeArg = { tree: 1 }
, prettyArg = { pretty: 1 }
@@ -98,12 +97,12 @@ testCase('Build', {
mockBuildUtil
.expects('forEachUniqueOrderedDependency')
.once()
- .withArgs(localizedArg, depTreeArg)
- .callsArgWith(2, packageNameArg, parentsArg, dataArg)
+ .withArgs(optionsArg, localizedArg, depTreeArg)
+ .callsArgWith(3, packageNameArg, parentsArg, dataArg)
SourcePackageMock
.expects('create')
.once()
- .withExactArgs(parentsArg, packageNameArg, dataArg.packageJSON, optionsArg)
+ .withExactArgs(packageNameArg, parentsArg, false, dataArg.packageJSON, optionsArg)
.returns(sourcePackage)
sourceBuildMock.expects('addPackage').once().withArgs(sourcePackage)
outMock.expects('finishedAssembly').once()
Oops, something went wrong.

0 comments on commit 9ba05d7

Please sign in to comment.