Comparing version 2.3.2 to 2.3.3
271
bin/get.js
#!/usr/bin/env node | ||
var fs = require('fs'), | ||
amdetective = require('amdetective'); | ||
os = require('os'), | ||
path = require('path'), | ||
util = require('util'), | ||
Minilog = require('minilog'), | ||
DetectiveList = require('../lib/detective-list.js'), | ||
AmdList = require('../lib/amd-list.js'), | ||
amdetective = require('amdetective'), | ||
amdresolve = require('amd-resolve'), | ||
SortDependencies = require('../lib/sort-dependencies.js'), | ||
Cache = require('minitask').Cache; | ||
function List() { | ||
this.files = []; | ||
var opts = require('optimist') | ||
.usage('Usage: $0 --include <file/dir ...>') | ||
.options({ | ||
'amd': { }, | ||
'include': { }, | ||
'main': { }, | ||
}) | ||
.boolean('amd'), | ||
argv = opts.parse(process.argv); | ||
if(!argv['include']) { | ||
console.log('Usage: --include <file/dir>'); | ||
console.log('Options:'); | ||
console.log(' --amd'); | ||
console.log(' --main <file>'); | ||
process.exit(1); | ||
} | ||
List.prototype.include = function(filepath) { | ||
var deps = amdetective(fs.readFileSync(process.argv[2]).toString()); | ||
Minilog.enable(); | ||
opts = { | ||
'cache-method': 'stat', | ||
'cache-path': os.tmpDir() + '/gluejs-' + new Date().getTime() | ||
}; | ||
var list = new List(); | ||
// determine main | ||
var main = argv.main || Array.isArray(argv.include) ? argv.include[0] : argv.include, | ||
basepath = path.dirname(main); | ||
console.log('Reading file from first argument: ' + process.argv[2]); | ||
list.include(process.argv[2]); | ||
function loadAMDConfig(filepath) { | ||
// the config specification for RJS is painful to parse as it's not a JSON file | ||
// but rather a JS file that defines as specifically named variable | ||
var sandbox = {}; | ||
require('vm').runInNewContext( | ||
fs.readFileSync(filepath).toString(), sandbox); | ||
console.log(list.files); | ||
return sandbox.require; | ||
} | ||
if(argv.amd) { | ||
opts.amdresolve = loadAMDConfig(argv.config); | ||
} | ||
if(argv.amd && main) { | ||
// baseDir is required for AMD | ||
opts.amdresolve.baseDir = path.dirname(main); | ||
} | ||
var list = (argv.amd ? new AmdList(opts) : new DetectiveList(opts)); | ||
var cache = Cache.instance({ | ||
method: opts['cache-method'], | ||
path: opts['cache-path'] | ||
}); | ||
cache.begin(); | ||
function lookupDeps(filepath) { | ||
var key = opts['cache-hash'] + '-amdependencies', | ||
noCache = false; // for easy dev | ||
return cache.file(filepath).data(key); | ||
} | ||
function wrapAMD(filepath, basePath) { | ||
var deps = lookupDeps(filepath), | ||
// the substr here will not be correct for files under folders which have been mapped unless the path length | ||
// happens to be identical e.g. app and lib | ||
relativeName = (path.dirname(filepath) + '/' + path.basename(filepath, path.extname(filepath))).substr(basePath.length + 1); | ||
return fs.readFileSync(filepath).toString().replace('define(', 'define(' + | ||
JSON.stringify(relativeName) + ', ' + | ||
JSON.stringify(deps) + ', ' | ||
); | ||
} | ||
var template = fs.readFileSync(__dirname + '/../lib/amd-vendor-wrap.js').toString(); | ||
function wrapAMDVendor(name, filepath, deps, globalName) { | ||
var result = ''; | ||
if(!filepath) { | ||
return ''; | ||
} | ||
if(globalName) { | ||
result += fs.readFileSync(filepath).toString(); | ||
result += template.replace('%name%', JSON.stringify(name)).replace('%deps%', JSON.stringify(deps)).replace('%global%', globalName); | ||
} else { | ||
if(!deps) { | ||
try { | ||
deps = amdetective(fs.readFileSync(filepath).toString()); | ||
} catch(e) { | ||
} | ||
console.log(deps); | ||
} | ||
// assuming: define(function (require, exports, module) { | ||
// -> define('decode',['require','exports','module'],function (require, exports, module) { | ||
result += fs.readFileSync(filepath).toString().replace('define(', 'define(' + | ||
JSON.stringify(name) + ', ' + | ||
JSON.stringify(deps || ['require','exports','module']) + ', ' | ||
); | ||
} | ||
return result; | ||
} | ||
function uniq() { | ||
var last = null; | ||
return function(item) { | ||
// to make a set of sorted keys unique, just check that consecutive keys are different | ||
var isDuplicate = (item == last); | ||
last = item; | ||
return !isDuplicate; | ||
}; | ||
} | ||
console.log('Reading files: '); | ||
(Array.isArray(argv.include) ? argv.include : [ argv.include ]).map(function(filepath) { | ||
console.log(' ' + filepath); | ||
list.add(filepath); | ||
}); | ||
list.exec(function(err, files) { | ||
cache.end(); | ||
var errs = list.resolveErrors(); | ||
console.log('done!'); | ||
/* | ||
console.log(errs.map(function(item) { | ||
return item.dep; | ||
}).sort()); | ||
*/ | ||
console.log(files.length); | ||
files = files.filter(function(e) { | ||
return path.basename(e.name) != 'application.js'; | ||
}); | ||
var sorter = new SortDependencies(); | ||
var basePath = path.dirname(argv.config); | ||
var configjs = loadAMDConfig(argv.config); | ||
// find the paths of the vendor files | ||
var vendorNames = Object.keys(configjs.paths); | ||
vendorPaths = { }; | ||
var missing = errs.map(function(item) { | ||
return item.dep; | ||
}).sort().filter(uniq()); | ||
missing.concat(vendorNames).forEach(function(name) { | ||
if(vendorPaths[name]) { | ||
return; | ||
} | ||
var fullpath = ''; | ||
configjs.relDir = basePath; | ||
configjs.baseDir = basePath; | ||
try { | ||
fullpath = amdresolve.sync(name, configjs); | ||
} catch(err) { } | ||
if(fullpath) { | ||
fullpath = path.normalize(fullpath); | ||
stat = fs.statSync(fullpath); | ||
if(stat.isFile()) { | ||
vendorPaths[name] = fullpath; | ||
} | ||
} | ||
}); | ||
missing = missing.filter(function(name) { | ||
return !vendorPaths[name]; | ||
}); | ||
// list the still not found items | ||
var failed = missing.sort().filter(uniq()); | ||
// console.log(vendorPaths, failed); | ||
// TODO at this point, plug in the config data | ||
// figure out the actual 3rd party dependencies (from the list) and extract names from paths key | ||
// Note: the CANONICAL names are from the paths array | ||
vendorNames.forEach(function(name) { | ||
sorter.add({ name: name, deps: configjs.shim && configjs.shim[name] && configjs.shim[name].deps || [] }); | ||
}); | ||
var result = ''; | ||
// Use the resolver to output the vendor files first | ||
sorter.resolve('require'); | ||
console.log('Vendor'); | ||
while(!sorter.isEmpty()) { | ||
var next = sorter.next(); | ||
console.log('\t' + next.name + (vendorPaths[next.name] ? ' OK' : '')); | ||
// due to unfilled dep on /v2/config | ||
if(next.name != 'triconf') { | ||
var vendorShimEntry = configjs.shim[next.name] || {}; | ||
result += wrapAMDVendor(next.name, vendorPaths[next.name], vendorShimEntry.deps, vendorShimEntry.exports || ''); | ||
} | ||
sorter.resolve(next); | ||
} | ||
// now add the other package files | ||
files.forEach(function(file) { | ||
sorter.add({ name: file.name, deps: lookupDeps(file.name) || [] }); | ||
}); | ||
sorter.resolve('require'); | ||
console.log('App'); | ||
while(!sorter.isEmpty()) { | ||
var next = sorter.next(); | ||
console.log('\t' + next.name); | ||
result += wrapAMD(next.name, basePath); | ||
sorter.resolve(next); | ||
} | ||
fs.writeFileSync(path.resolve(process.cwd(), './bundle.js'), result); | ||
/* | ||
console.log(deps); | ||
console.log(files.map(function(file) { return file.name + ' ' + lookupDeps(file.name); })); | ||
console.log(files.length); | ||
files.map(); | ||
*/ | ||
/* | ||
var inferPackages = require('../lib/list-tasks/infer-packages.js'), | ||
filterPackages = require('../lib/list-tasks/filter-packages.js'); | ||
inferPackages(list, { main: main, basepath: basepath }); | ||
// - for each package, apply excludes (package.json.files, .npmignore, .gitignore) | ||
filterPackages(list); | ||
console.log(util.inspect(list.packages, false, 1, true)); | ||
*/ | ||
}); |
@@ -57,3 +57,3 @@ USAGE | ||
--cache-method Sets the cache method: stat | hash algorighm name. | ||
--cache-method Sets the cache method: stat | hash algorithm name. | ||
@@ -60,0 +60,0 @@ REPORTING |
103
index.js
var os = require('os'), | ||
path = require('path'), | ||
List = require('minitask').list, | ||
DetectiveList = require('./lib/detective-list.js'), | ||
packageCommonJs = require('./lib/runner/package-commonjs'), | ||
Capture = require('./lib/file-tasks/capture.js'), | ||
Minilog = require('minilog'); | ||
Minilog = require('minilog'), | ||
Cache = require('minitask').Cache; | ||
var homePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']; | ||
homePath = (typeof homePath === 'string' ? path.normalize(homePath) : process.cwd()); | ||
// API wrapper | ||
function API() { | ||
this.files = new List(); | ||
// exclude matching paths from traversal - this is applied during the | ||
// initial traversal because going into source control directories is | ||
// potentially very expensive | ||
this.files.exclude([ | ||
function(p) { return p.match(/\/.svn/); }, | ||
function(p) { return p.match(/\/.git/); }, | ||
function(p) { return p.match(/\/.hg/); }, | ||
function(p) { return p.match(/\/CVS/); } | ||
]); | ||
// default options | ||
@@ -28,3 +20,4 @@ this.options = { | ||
cache: true, | ||
'cache-path': path.normalize(homePath) + path.sep + '.gluejs-cache' + path.sep | ||
'cache-path': homePath + path.sep + '.gluejs-cache' + path.sep, | ||
include: [] | ||
}; | ||
@@ -35,9 +28,3 @@ } | ||
if(!filepath) return this; | ||
var self = this, | ||
paths = (Array.isArray(filepath) ? filepath : [ filepath ]), | ||
base = this.options.basepath || process.cwd(); | ||
paths.forEach(function(p) { | ||
self.files.add(path.resolve(base, p)); | ||
}); | ||
this.options.include.push(filepath); | ||
return this; | ||
@@ -47,2 +34,3 @@ }; | ||
API.prototype.render = function(dest) { | ||
var self = this; | ||
// if the cache is disabled, then use a temp path | ||
@@ -53,2 +41,37 @@ if(!this.options.cache) { | ||
if(!this.options['cache-method']) { | ||
this.options['cache-method'] = 'stat'; | ||
} | ||
// LIST | ||
// only instantiate the list just before running the code | ||
// this avoids issues with the order between `.set('parse', true)` and .include() calls | ||
var opts = { | ||
'cache-path': this.options['cache-path'], | ||
'cache-method': this.options['cache-method'], | ||
'cache-hash': Cache.hash(JSON.stringify(this.options)) | ||
}, | ||
list = (this.options['parse'] ? new DetectiveList(opts) : new List()); | ||
// set the cache mode to transactional and begin a single cache scope | ||
var cache = Cache.instance({ | ||
method: opts['cache-method'], | ||
path: opts['cache-path'] | ||
}); | ||
cache.begin(); | ||
// --reset-exclude should also reset the pre-processing exclusion | ||
if(this.options['reset-exclude']) { | ||
list.exclude(null); | ||
} | ||
// --basepath | ||
if(this.options['basepath']) { | ||
list.basepath(this.options['basepath']); | ||
} | ||
this.options['include'].map(function(filepath) { | ||
list.add(filepath); | ||
}); | ||
// END LIST | ||
if(typeof dest == 'function') { | ||
@@ -66,11 +89,13 @@ var capture = new Capture(); | ||
packageCommonJs(this.files, this.options, capture, function() { | ||
// NOP | ||
list.exec(function(err, files) { | ||
packageCommonJs({ files: files }, self.options, capture, function() { | ||
cache.end(); | ||
}); | ||
}); | ||
} else if(dest.write) { | ||
// writable stream | ||
packageCommonJs(this.files, this.options, dest, function() { | ||
// if(dest !== process.stdout) { | ||
// dest.end(); | ||
// } | ||
list.exec(function(err, files) { | ||
packageCommonJs({ files: files }, self.options, dest, function() { | ||
cache.end(); | ||
}); | ||
}); | ||
@@ -81,3 +106,2 @@ } | ||
// setters | ||
API.defaults = API.prototype.defaults = function(opts) {}; | ||
API.prototype.set = function(key, value) { | ||
@@ -88,7 +112,2 @@ this.options[key] = value; | ||
} | ||
// --reset-exclude should also reset the pre-processing exclusion | ||
// which prevent | ||
if(key == 'reset-exclude' && value) { | ||
this.files.exclude(null); | ||
} | ||
return this; | ||
@@ -144,3 +163,2 @@ }; | ||
API.prototype.exclude = function(path) { | ||
@@ -160,3 +178,5 @@ if(!this.options['exclude']) { | ||
opts.include = opts.include || './lib'; | ||
opts.basepath = opts.basepath || (Array.isArray(opts.include) ? opts.include[0] : opts.include); | ||
if(!opts.basepath) { | ||
opts.basepath = Array.isArray(opts.include) ? opts.include[0] : opts.include; | ||
} | ||
opts.main = opts.main || 'index.js'; | ||
@@ -166,4 +186,4 @@ | ||
var glue = new API() | ||
.include(opts.include) // Use API function to define | ||
.basepath(opts.basepath); // Use API function to define | ||
.include(opts.include) | ||
.basepath(opts.basepath); | ||
@@ -182,3 +202,3 @@ // -- All other options are set by clobbering the glue.options hash | ||
// -- Set content-type | ||
res.set('Content-Type', 'application/javascript'); | ||
res.setHeader('Content-Type', 'application/javascript'); | ||
@@ -190,11 +210,2 @@ // -- Render file and pipe to response | ||
API.prototype.handler = function(regex, fn) {}; | ||
API.prototype.define = function(module, code) {}; | ||
API.prototype.watch = function(onDone) {}; | ||
// deprecated | ||
API.prototype.npm = function(name, pathTo) {}; | ||
API.prototype.reqpath = function(value) {}; | ||
API.concat = function(arr, callback) {}; | ||
module.exports = API; |
@@ -213,3 +213,4 @@ var fs = require('fs'), | ||
var flow = new Flow(item.tasks).input(fs.createReadStream(fullpath)); | ||
// wrap in a function to reduce file handle usage | ||
var flow = new Flow(item.tasks).input(function() { return fs.createReadStream(fullpath); } ); | ||
@@ -308,3 +309,3 @@ // these are used to disambiguate cached results | ||
cachePath: options['cache-path'], | ||
cacheMethod: (options.cacheMethod ? options.cacheMethod : 'stat'), | ||
cacheMethod: options['cache-method'], | ||
output: (out ? out : process.stdout), | ||
@@ -311,0 +312,0 @@ limit: options.jobs, |
{ | ||
"name": "gluejs", | ||
"description": "Build CommonJS modules for the browser via a chainable API", | ||
"version": "2.3.2", | ||
"version": "2.3.3", | ||
"author": { | ||
@@ -31,8 +31,9 @@ "name": "Mikito Takada", | ||
"minimatch": "~0.2.12", | ||
"minitask": "~0.1.2", | ||
"minitask": "~0.2.0", | ||
"optimist": "~0.5.2", | ||
"bytes": "~0.2.1", | ||
"minilog": "~2.0.3", | ||
"resolve": "~0.5.1", | ||
"progress": "~1.1.2" | ||
"progress": "~1.1.2", | ||
"browser-resolve": "~1.2.2", | ||
"detective": "~2.4.0" | ||
}, | ||
@@ -39,0 +40,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
104604
44
1935
9
14
+ Addedbrowser-resolve@~1.2.2
+ Addeddetective@~2.4.0
+ Addedamdefine@1.0.1(transitive)
+ Addedbrowser-resolve@1.2.4(transitive)
+ Addeddetective@2.4.1(transitive)
+ Addedescodegen@1.1.0(transitive)
+ Addedesprima@1.0.4(transitive)
+ Addedestraverse@1.5.1(transitive)
+ Addedesutils@1.0.0(transitive)
+ Addedmicroee@0.0.6(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedminiq@0.1.2(transitive)
+ Addedminitask@0.2.3(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedresolve@0.6.3(transitive)
+ Addedsource-map@0.1.43(transitive)
- Removedresolve@~0.5.1
- Removedminitask@0.1.2(transitive)
- Removedmkdirp@0.3.5(transitive)
- Removedresolve@0.5.1(transitive)
Updatedminitask@~0.2.0