Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for globs #53

Merged
merged 1 commit into from
Jul 3, 2017
Merged
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
35 changes: 31 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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['<script.js>'];
var arg2 = args['<script.js.map>'];
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['<script.js>'] = files[0];
args['<script.js.map>'] = files[1];
}
}
return args;
}


if (require.main === module) {

var args = docopt(doc, {version: '1.3.3'});
expandGlob(args);
validateArgs(args);
var data = loadSourceMap(args['<script.js>'], args['<script.js.map>']);
if (!data) {
Expand Down Expand Up @@ -235,5 +261,6 @@ module.exports = {
computeGeneratedFileSizes: computeGeneratedFileSizes,
adjustSourcePaths: adjustSourcePaths,
mapKeys: mapKeys,
commonPathPrefix: commonPathPrefix
commonPathPrefix: commonPathPrefix,
expandGlob: expandGlob
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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({'<script.js>': 'testdata/foo.min.js*'})).to.deep.equal({
'<script.js>': 'testdata/foo.min.js',
'<script.js.map>': 'testdata/foo.min.js.map',
});

expect(expandGlob({
'<script.js>': 'foo.min.js',
'<script.js.map>': 'foo.min.js.map'
})).to.deep.equal({
'<script.js>': 'foo.min.js',
'<script.js.map>': 'foo.min.js.map'
});
});
});