Skip to content

Commit

Permalink
Support multiple file tokens (#84)
Browse files Browse the repository at this point in the history
- Expand multiple globs
- Accept mutiple file tokens - file names or globs
  • Loading branch information
nikolay-borzov committed Apr 13, 2019
1 parent 5e018d3 commit 31630ba
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 38 deletions.
8 changes: 3 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,13 @@ interface WriteConfig {
/**
* Explore multiple bundles and write html output to file.
*
* @param codePath Path to bundle file or glob matching bundle files
* @param [mapPath] Path to bundle map file
* @param fileTokens List of file paths or glob patterns
*/
export async function exploreBundlesAndWriteHtml(
writeConfig: WriteConfig,
codePath: string,
mapPath?: string
fileTokens: string[]
): Promise<void> {
const bundles = getBundles(codePath, mapPath);
const bundles = getBundles(fileTokens);

return exploreBundles(bundles).then(results => {
const successResults = results.filter(
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function writeToHtml(html?: string): void {
if (require.main === module) {
const argv = parseArguments();

const bundles = getBundles(argv._[0], argv._[1]);
const bundles = getBundles(argv._);

if (bundles.length === 0) {
throw new Error('No file(s) found');
Expand Down
55 changes: 34 additions & 21 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,42 @@ export interface Bundle {
mapPath?: string;
}

function expandGlob(pattern: string): string[] {
// Make sure pattern match `.map` files as well
if (pattern.endsWith('.js')) {
pattern = `${pattern}?(.map)`;
}

return glob.sync(pattern);
}

/**
* Expand codePath and mapPath into a list of { codePath, mapPath } pairs
* @see https://github.com/danvk/source-map-explorer/issues/52
* @param codePath Path to bundle file or glob matching bundle files
* @param [mapPath] Path to bundle map file
* Expands list of file token into a list of { codePath, mapPath } pairs
*/
export function getBundles(codePath: string, mapPath?: string): Bundle[] {
if (codePath && mapPath) {
return [
{
codePath,
mapPath,
},
];
}
export function getBundles(fileTokens: string[]): Bundle[] {
const filenames = fileTokens.reduce<string[]>((result, filePath) => {
if (glob.hasMagic(filePath)) {
result.push(...expandGlob(filePath));
} else {
result.push(filePath);
}

const filenames = glob.sync(codePath);
const mapFilenames = glob.sync(codePath + '.map');
return result;
}, []);

const codeFilenames: string[] = [];
const mapFilenames: string[] = [];

return filenames
.filter(filename => !filename.endsWith('.map'))
.map<Bundle>(filename => ({
codePath: filename,
mapPath: mapFilenames.find(mapFilename => mapFilename === `${filename}.map`),
}));
filenames.forEach(filename => {
if (filename.endsWith('.map')) {
mapFilenames.push(filename);
} else {
codeFilenames.push(filename);
}
});

return codeFilenames.map<Bundle>(codePath => ({
codePath,
mapPath: mapFilenames.find(filename => filename === `${codePath}.map`),
}));
}
4 changes: 2 additions & 2 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('Public API', function() {
fileName: 'bundle-out.tmp.html',
};

await exploreBundlesAndWriteHtml(writeConfig, 'data/*.*');
await exploreBundlesAndWriteHtml(writeConfig, ['data/*.*']);

const data = fs.readFileSync(writeConfigToPath(writeConfig), 'utf8');

Expand All @@ -135,7 +135,7 @@ describe('Public API', function() {
it('should explore multiple bundles and write a html file to current directory if path is undefined in writeConfig', async function() {
const writeConfig = { fileName: 'bundle-out.tmp.html' };

await exploreBundlesAndWriteHtml(writeConfig, 'data/*.*');
await exploreBundlesAndWriteHtml(writeConfig, ['data/*.*']);

const data = fs.readFileSync(writeConfigToPath(writeConfig), 'utf8');

Expand Down
42 changes: 33 additions & 9 deletions tests/common..test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { expect } from 'chai';

import { getBundles } from '../src/common';

describe('getBundles - command line parsing', function() {
describe('getBundles - file tokens parsing', function() {
const tests = [
{
name: 'should expand glob',
args: ['data/foo.min.js*'],
fileTokens: ['data/foo.min.js*'],
expected: [
{
codePath: 'data/foo.min.js',
Expand All @@ -16,7 +16,7 @@ describe('getBundles - command line parsing', function() {
},
{
name: 'should return one bundle if map file specified',
args: ['foo.min.js', 'foo.min.js.map'],
fileTokens: ['foo.min.js', 'foo.min.js.map'],
expected: [
{
codePath: 'foo.min.js',
Expand All @@ -26,7 +26,7 @@ describe('getBundles - command line parsing', function() {
},
{
name: 'should expand glob into all bundles in directory',
args: ['data/*.*'],
fileTokens: ['data/*.*'],
expected: [
{
codePath: 'data/foo.1234.js',
Expand All @@ -47,18 +47,42 @@ describe('getBundles - command line parsing', function() {
],
},
{
name: 'should support single file glob',
args: ['data/foo.1*.js'],
name: 'should expand glob including .map files',
fileTokens: ['data/foo.1*.js'],
expected: [
{
codePath: 'data/foo.1234.js',
mapPath: 'data/foo.1234.js.map',
},
],
},
{
name: 'should expand glob into code and map files',
fileTokens: ['data/foo.1*.js?(.map)'],
expected: [
{
codePath: 'data/foo.1234.js',
mapPath: 'data/foo.1234.js.map',
},
],
},
{
name: 'should expand multiple globs',
fileTokens: ['data/foo.1*.js', 'data/foo.mi?.js'],
expected: [
{
codePath: 'data/foo.1234.js',
mapPath: 'data/foo.1234.js.map',
},
{
codePath: 'data/foo.min.js',
mapPath: 'data/foo.min.js.map',
},
],
},
{
name: 'should support single file glob when inline map',
args: ['data/foo.min.inline*.js'],
fileTokens: ['data/foo.min.inline*.js'],
expected: [
{
codePath: 'data/foo.min.inline-map.js',
Expand All @@ -68,9 +92,9 @@ describe('getBundles - command line parsing', function() {
},
];

tests.forEach(function({ name, args, expected }) {
tests.forEach(function({ name, fileTokens, expected }) {
it(name, function() {
expect(getBundles(...args)).to.deep.equal(expected);
expect(getBundles(fileTokens)).to.deep.equal(expected);
});
});
});

0 comments on commit 31630ba

Please sign in to comment.