diff --git a/index.js b/index.js index b9bdfc4..d342209 100755 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ var doc = [ ].join('\n'); var fs = require('fs'), + glob = require('glob'), path = require('path'), sourcemap = require('source-map'), convert = require('convert-source-map'), @@ -81,9 +82,9 @@ function loadSourceMap(jsFile, mapFile) { var jsData; try { jsData = fs.readFileSync(jsFile).toString(); - } catch(e) { - if( err.code === "ENOENT" ) { - console.error( "File not found! -- "+err.message ); + } catch(err) { + if (err.code === "ENOENT" ) { + console.error("File not found! -- ", err.message); return null; } else { throw err; @@ -161,10 +162,35 @@ function validateArgs(args) { } } +// On Windows, it's helpful if source-map-explorer can expand globs itself. +// See https://github.com/danvk/source-map-explorer/issues/52 +function expandGlob(args) { + var arg1 = args['']; + var arg2 = args['']; + if (arg1 && !arg2) { + var files = glob.sync(arg1); + if (files.length > 2) { + throw new Error( + 'Glob should match exactly 2 files but matched ' + files.length + ' ' + arg1); + } else if (files.length === 2) { + // allow the JS and source map file to match in either order. + if (files[0].indexOf('.map') >= 0) { + var tmp = files[0]; + files[0] = files[1]; + files[1] = tmp; + } + args[''] = files[0]; + args[''] = files[1]; + } + } + return args; +} + if (require.main === module) { var args = docopt(doc, {version: '1.3.3'}); +expandGlob(args); validateArgs(args); var data = loadSourceMap(args[''], args['']); if (!data) { @@ -235,5 +261,6 @@ module.exports = { computeGeneratedFileSizes: computeGeneratedFileSizes, adjustSourcePaths: adjustSourcePaths, mapKeys: mapKeys, - commonPathPrefix: commonPathPrefix + commonPathPrefix: commonPathPrefix, + expandGlob: expandGlob }; diff --git a/package.json b/package.json index 0ad4c8c..0dfb2c9 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "convert-source-map": "^1.1.1", "docopt": "^0.6.2", "file-url": "^1.0.1", + "glob": "^7.1.2", "open": "0.0.5", "source-map": "^0.5.1", "temp": "^0.8.3", diff --git a/test.js b/test.js index 1aa0dae..e3ac2ec 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,8 @@ var expect = require('chai').expect; var sourceMapExplorer = require('./index'), adjustSourcePaths = sourceMapExplorer.adjustSourcePaths, mapKeys = sourceMapExplorer.mapKeys, - commonPathPrefix = sourceMapExplorer.commonPathPrefix; + commonPathPrefix = sourceMapExplorer.commonPathPrefix, + expandGlob = sourceMapExplorer.expandGlob; describe('source-map-explorer', function() { describe('commonPathPrefix', function() { @@ -48,4 +49,19 @@ describe('source-map-explorer', function() { .to.deep.equal({'/bar/foo.js': 10, '/bar/foodle.js': 20}); }); }); + + describe('command line parsing', function() { + expect(expandGlob({'': 'testdata/foo.min.js*'})).to.deep.equal({ + '': 'testdata/foo.min.js', + '': 'testdata/foo.min.js.map', + }); + + expect(expandGlob({ + '': 'foo.min.js', + '': 'foo.min.js.map' + })).to.deep.equal({ + '': 'foo.min.js', + '': 'foo.min.js.map' + }); + }); });