My objective was to develop a script that accomplishes the following tasks:
- Import all JS files from a directory excluding those ending in
_test.js
- Set up a
module.exports
containing an array of module names extracted from those imported files.
Initially, I believed I had succeeded with the code below:
// This script scans through the directory, identifies all javascript files within any subdirectory,
// excludes test files, imports the list of files, and assigns the exported module names as dependencies to the myApp.demoApp.views module.
var context = require.context('.', true, /\/.*\/.*\.js$/);
var moduleNames = _.chain(context.keys())
.filter(function(key) {
console.log(key, key.indexOf('_test.js') == -1);
return key.indexOf('_test.js') == -1;
})
.map(function(key) {
console.log("KEY", key);
return context(key)
})
.value();
module.exports = angular.module('myApp.demoApp.views', moduleNames).name;
#2 functions as intended
#1 Unfortunately, I made the mistake of overlooking a crucial detail. Despite successfully filtering out the module names, it still imports all files with _test
, resulting in test files being included in my final code.
I attempted to resolve this by adjusting the regex pattern, but JavaScript does not support negative lookbehind in regex, and my regex skills are not advanced enough to work around this limitation.