Skip to content

Commit

Permalink
Merge 05ac40b into 7a4efaf
Browse files Browse the repository at this point in the history
  • Loading branch information
haganbmj committed Mar 20, 2018
2 parents 7a4efaf + 05ac40b commit 518fad6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/bundle-manager.js
Expand Up @@ -27,6 +27,11 @@ const watcher = chokidar.watch([
followSymlinks: true
});

const blacklistedBundleDirectories = [
'node_modules',
'bower_components'
];

const emitter = new EventEmitter();
const bundles = [];
let log;
Expand Down Expand Up @@ -70,6 +75,7 @@ module.exports = emitter;
* @param Logger {Function} - A preconfigured @nodecg/logger constructor.
* @return {Object} - A bundle-manager instance.
*/
// eslint-disable-next-line max-params
module.exports.init = function (bundlesPath, cfgPath, nodecgVersion, nodecgConfig, Logger) {
if (initialized) {
throw new Error('Cannot initialize when already initialized');
Expand Down Expand Up @@ -140,6 +146,11 @@ module.exports.init = function (bundlesPath, cfgPath, nodecgVersion, nodecgConfi
return;
}

// Prevent attempting to load unwanted directories. Those specified above and all dot-prefixed.
if (blacklistedBundleDirectories.indexOf(bundleFolderName) > -1 || /(?:^|[\\/])(\.(?!\.)[^\\/]+)$/.test(bundleFolderName)) {
return;
}

if (nodecgConfig && nodecgConfig.bundles && nodecgConfig.bundles.disabled &&
nodecgConfig.bundles.disabled.indexOf(bundleFolderName) > -1) {
log.debug('Not loading bundle ' + bundleFolderName + ' as it is disabled in config');
Expand All @@ -155,6 +166,7 @@ module.exports.init = function (bundlesPath, cfgPath, nodecgVersion, nodecgConfi
// Parse each bundle and push the result onto the bundles array
let bundle;
const bundleCfgPath = path.join(cfgPath, `${bundleFolderName}.json`);

if (fs.existsSync(bundleCfgPath)) {
bundle = parseBundle(bundlePath, bundleCfgPath);
} else {
Expand All @@ -170,14 +182,6 @@ module.exports.init = function (bundlesPath, cfgPath, nodecgVersion, nodecgConfi
return;
}

// This block can probably be removed in 0.8, but let's leave it for 0.7 just in case.
/* istanbul ignore next: Given how strict nodecg-bundle-parser is,
it should not be possible for "bundle" to be undefined. */
if (typeof bundle === 'undefined') {
log.error('Could not load bundle in directory', bundleFolderName);
return;
}

bundles.push(bundle);
});

Expand Down
5 changes: 5 additions & 0 deletions test/bundle-manager.js
Expand Up @@ -72,6 +72,11 @@ test.serial('loader - should not load a bundle that has been disabled', t => {
t.is(bundle, undefined);
});

test.serial('loader - should not crash or load an invalid bundle', t => {
const bundle = bundleManager.find('node_modules');
t.is(bundle, undefined);
});

test.cb.serial('watcher - hould emit a change event when the manifest file changes', t => {
const manifest = JSON.parse(fs.readFileSync(`${tempFolder}/bundles/change-manifest/package.json`));
bundleManager.once('bundleChanged', bundle => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 518fad6

Please sign in to comment.