Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Add ExternalResolver to filter modules which should not be processed #129

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 16 additions & 8 deletions lib/cli/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var Path = require('path');
var recast = require('recast');

var formatters = require('../formatters');
var FileResolver = require('../file_resolver');
var resolvers = require('../resolvers');
var Container = require('../container');
var FileResolver = require('../file_resolver');

var getopt = require('posix-getopt');
var exe = Path.basename(process.argv[1]);
Expand All @@ -20,10 +19,11 @@ exports.run = function(args, puts, eputs) {
var includePaths = [process.cwd()];
var output;
var formatter = formatters[formatters.DEFAULT];
var resolverClasses = [FileResolver];
var resolverClasses = [resolvers.file];
var ignoreExternal = false;

while (offset < args.length) {
var parser = new getopt.BasicParser('h(help)o:(output)I:(include)f:(format)r:(resolver)', ['', ''].concat(args.slice(offset)));
var parser = new getopt.BasicParser('h(help)o:(output)I:(include)f:(format)r:(resolver)e(external)', ['', ''].concat(args.slice(offset)));
var option;

while ((option = parser.getopt()) !== undefined) {
Expand Down Expand Up @@ -73,6 +73,10 @@ exports.run = function(args, puts, eputs) {
return 1;
}
break;

case 'e':
ignoreExternal = true;
break;
}
}

Expand All @@ -90,14 +94,17 @@ exports.run = function(args, puts, eputs) {
formatter = new formatter();
}

var resolvers = resolverClasses.map(function(resolverClass) {
return new resolverClass(includePaths);
});
var container = new Container({
formatter: formatter,
resolvers: resolvers
resolvers: resolverClasses.map(function(resolverClass) {
return new resolverClass(includePaths);
})
});

if (ignoreExternal) {
container.resolvers.push(new resolvers.external());
}

files.forEach(function(file) {
container.getModule(file);
});
Expand Down Expand Up @@ -134,6 +141,7 @@ function usage(puts) {
puts(' -o, --output <path> File or directory to output converted files.');
puts(' -f, --format <path|name> Path to custom formatter or choose from built-in formats.');
puts(' -r, --resolver <path> Path to custom resolver (usable multiple times).');
puts(' -e, --external Flag to ignore external modules from parsing/validation.');
puts(' -h, --help Show this help message.');
puts();
puts(bold('Formats'));
Expand Down
2 changes: 2 additions & 0 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ Container.prototype.getModules = function() {
var modules = this.modules;
return Object.keys(modules).map(function(key) {
return modules[key];
}).filter(function (module) {
return !module.external;
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* jshint node:true, undef:true, unused:true */

var Container = require('./container');
var FileResolver = require('./file_resolver');
var formatters = require('./formatters');
var resolvers = require('./resolvers');

exports.FileResolver = FileResolver;
exports.Container = Container;
exports.formatters = formatters;
exports.resolvers = resolvers;
4 changes: 4 additions & 0 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* jshint node:true, undef:true, unused:true */

exports.file = require('./resolvers/file_resolver');
exports.external = require('./resolvers/external_resolver');
50 changes: 50 additions & 0 deletions lib/resolvers/external_resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* jshint node:true, undef:true, unused:true */

var Module = require('../module');

/**
* Provides "mock" Module object for external modules which should be ignored.
*
* @constructor
*/
function ExternalResolver(externalModules) {
// When `externalModules` is specificed, restrict to modules named in list.
if (Array.isArray(externalModules) && externalModules.length) {
this.externalModules = externalModules;
}
}

/**
* Resolves `importedPath` imported by the given module `fromModule` to a
* an external module.
*
* @param {string} importedPath
* @param {?Module} fromModule
* @param {Container} container
* @return {?Module}
*/
ExternalResolver.prototype.resolveModule = function (importedPath, fromModule, container) {
var isExternal, module;

if (this.externalModules) {
isExternal = this.externalModules.indexOf(importedPath) >= 0;
} else {
isExternal = importedPath.charAt(0) !== '.';
}

if (isExternal) {
module = container.getCachedModule(importedPath);

if (!module) {
module = new Module(importedPath, importedPath, container);
module.external = true;
console.log('INFO: External module detected: "%s"', importedPath);
}

return module;
}

return null;
};

module.exports = ExternalResolver;
2 changes: 1 addition & 1 deletion lib/file_resolver.js → lib/resolvers/file_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var assert = require('assert');
var Path = require('path');
var fs = require('fs');

var Module = require('./module');
var Module = require('../module');

/**
* Provides resolution of absolute paths from module import sources.
Expand Down
2 changes: 1 addition & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function runTestDir(testDir) {
var testName = Path.basename(testDir);

var options = {
resolvers: [new modules.FileResolver([testDir])],
resolvers: [new modules.resolvers.file([testDir])],
formatter: formatters[formatter]
};
var container = new modules.Container(options);
Expand Down