Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Update code from internal repo
Browse files Browse the repository at this point in the history
  • Loading branch information
randing89 committed Nov 28, 2014
1 parent 784ef4c commit bbc5a1a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 80 deletions.
63 changes: 16 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var meta = {};
// {Provider -> file} mapping for fast lookup
var providerCache = {};

// {Module name -> file[]} mapping for fast lookup
// {Module name -> file} mapping for fast lookup
var moduleCache = {};

// Shared options
Expand Down Expand Up @@ -94,8 +94,6 @@ module.exports = {
if (metaData === false || !metaData.angular) {
return skipped.push(file);
}

var moduleName = metaData.moduleName;

// Do not apply naming check on external modules
if (!externalModulesRegexp.test(file)) {
Expand All @@ -104,8 +102,8 @@ module.exports = {
if (options.ensureModuleName) {
var expectedModuleName = path.dirname(file).replace(/\//g, '.');

if (!s.nullOrEmpty(moduleName) && expectedModuleName.indexOf(moduleName) === -1) {
throw new Error('Module "{0}" should follow folder path for {1}'.f(moduleName, file));
if (!s.nullOrEmpty(metaData.moduleName) && expectedModuleName.indexOf(metaData.moduleName) === -1) {
throw new Error('Module "{0}" should follow folder path for {1}'.f(metaData.moduleName, file));
}
}

Expand All @@ -115,7 +113,6 @@ module.exports = {

} else if (metaData.namedProviders.length === 1 && !s.nullOrEmpty(metaData.namedProviders[0])) {
var baseName = path.basename(file, '.js');
var providerName = metaData.namedProviders[0].toLowerCase().replace(/[\-_]+/g, '');

var expectedProviderNames = [];
// like example
Expand All @@ -124,16 +121,15 @@ module.exports = {
// like ExampleController
expectedProviderNames.push((baseName + metaData.providerTypes[0]).toLowerCase());

// also skip ones start with $
if (providerName[0] !== '$' && expectedProviderNames.indexOf(providerName) === -1) {
if (expectedProviderNames.indexOf(metaData.namedProviders[0].toLowerCase()) === -1) {
throw new Error('Provider "{0}" is not matching file name at {1}'.f(metaData.namedProviders[0], file));
}
}
}
}

// Add to cache
moduleCache[moduleName] = _.union(moduleCache[moduleName] || [], [ file ]);
moduleCache[metaData.moduleName] = file;

_.each(metaData.namedProviders, function (namedProvider) {
providerCache[namedProvider] = file;
Expand Down Expand Up @@ -198,15 +194,11 @@ module.exports = {
*/
var loadedFileMeta;

/*
* Best effort to parse required file
*/
if (!(filePath in meta) && path.extname(filePath) === '.js' && fs.existsSync(filePath)) {
if (!(filePath in meta)) {
// attempt to parse file
self._parse(filePath);
}


loadedFileMeta = meta[filePath];
if (loadedFileMeta) {
var isDependency = fileMeta.dependencies.indexOf(loadedFileMeta.moduleName) !== -1;
Expand All @@ -223,7 +215,6 @@ module.exports = {
}
});


// Find out what is missing
var missingInjectedProviders = _.difference(fileMeta.injectedProviders, _.keys(loadedProviders));

Expand All @@ -247,47 +238,25 @@ module.exports = {
}

// If still can't find
if (options.ignoreProviders && options.ignoreProviders.length > 0) {
missingInjectedProviders = _.filter(missingInjectedProviders, function (missingInjectedProvider) {
var ignore = false;

_.each(options.ignoreProviders, function (ignoreExpr) {
if (helpers.isString(ignoreExpr)) {
ignore = ignoreExpr === missingInjectedProvider;
}

if (helpers.isRegexp(ignoreExpr)) {
ignore = ignoreExpr.test(missingInjectedProvider);
}

if (ignore) return false;
});

return !ignore;
});
}

if (missingInjectedProviders.length > 0) {
throw new Error('Can not find provider "{0}" in {1}'.f(missingInjectedProviders.join(', '), file));
throw new Error('Can not find providers "{0}" in {1}'.f(missingInjectedProviders.join(', '), file));
}

// Include modules that user explicit specified
_.each(fileMeta.dependencies, function (dependency) {
if (!(dependency in loadedModules)) {
// Absolute to relative path
var absolutePaths = moduleCache[dependency];
var absolutePath = moduleCache[dependency];

// Only process known modules
if (absolutePaths) {
_.each(absolutePaths, function (absolutePath) {
var relativePath = helpers.absolutePathToRelative(fileBase, absolutePath);

result.push({
providerName: '',
moduleName: dependency,
path: absolutePath,
relativePath: relativePath
});
if (absolutePath) {
var relativePath = helpers.absolutePathToRelative(fileBase, absolutePath);

result.push({
providerName: '',
moduleName: dependency,
path: absolutePath,
relativePath: relativePath
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngrequire",
"version": "2.0.26",
"version": "2.0.19",
"description": "Analyse AngularJS module dependencies",
"main": "index.js",
"repository": {
Expand Down
8 changes: 0 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,5 @@ module.exports = {
}

return relativePath;
},

isString: function (obj) {
return Object.prototype.toString.call(obj) === '[object String]';
},

isRegexp: function (obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
};
24 changes: 0 additions & 24 deletions test/spec_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var _ = require('lodash');
var ngRequire = require('../index');

var fixtures = {
simple: './fixtures/simple/a.js',
valid: './fixtures/followPath/**/allValid.js',
moduleNameNotValid: './fixtures/followPath/**/moduleNameNotValid.js',
providerNameNotValid: './fixtures/followPath/**/providerNameNotValid.js',
Expand Down Expand Up @@ -63,27 +62,4 @@ describe('Options test', function () {
done();
}
});

it('should ignore', function (done) {
try {
ngRequire.update(fixtures.simple);
ngRequire.getMissingDependencies(fixtures.simple);
} catch (e) {
// Should fail without ignore
expect(e.message).to.match(/Can not find provider "b, c"/);

ngRequire.clean();
ngRequire.update(fixtures.simple, {
ignoreProviders: [
'b',
/^c/
]
});

// Should be ok
ngRequire.getMissingDependencies(fixtures.simple);

done();
}
});
});

0 comments on commit bbc5a1a

Please sign in to comment.