A very simple loader to require() based on glob pattern matching.
A very simple loader to
require() based on glob pattern matching. Please contribute by writing tests and examples.
$ npm install node-glob-loader
- supports glob/multi glob matching ala node-glob, multi-glob
- supports deferrals ala promised-io
- suports asynchronous loading via async.
- pattern: a glob pattern or an array of patterns
- options: an optional object
- strict: When set to true, glob will yield an error if a pattern matches no files.
- limit: ensures synchronised loading. Note that under most use-cases files will be loaded 1 at a time
- callback: an optional callback that is called once per matched file with its
exportsand the file’s name.
var loader = require'node-glob-loader';loaderload'./foo.js'console.log'Loaded ' + filename;exportsfoo;;
Load a directory... When you're DONE – YEAH!
var loader = require'node-glob-loader';loaderload'./foo/*.js'console.log'Loaded ' + filename;exportsfoo;done// Yeah!;
var loader = require'node-glob-loader';loaderload'./foo/[!bar]*.js'console.log'Loaded ' + filename;exportsbar;;
.js files in
foo's tree THEN do something nice
var loader = require'node-glob-loader';loaderload'./foo/**/*.js'console.log'Loaded ' + filename;exportsbar;thenfuntion// Do Something Nice;
var loader = require'node-glob-loader';loaderload'./foo/**/*.js';
var loader = require'node-glob-loader';loaderload'./foo.js' './foo/**/*.js'exportsfoo;;
Make the @hueniverse Hapi with a real world example
var config = require"./config/" + processenvNODE_ENVpckg = require"./package.json"bunyan = require"bunyan"hapi = require"hapi"serverlogger;logger = bunyancreateLoggername: pckgnameserializers: bunyanstdSerializers;server = hapicreateServerconfigapplicationhostconfigapplicationportconfigapplicationoptions;serverapplogger = logger;serverappconfiguration = config;serverstartvar loader;if errorreturn logger.errorerror;logger.info"Server started @" + server.info;loader = require"node-glob-loader";loaderload"./application/routes/*.*"logger.info"Loading routes from " + name;serverrouteroutes;;;
Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. A backslash escapes the following character; the escaping backslash is discarded when matching. The special pattern characters must be quoted if they are to be matched literally.
The special pattern characters have the following meanings:
###* Matches any string, including the null string. When the globstar shell option is enabled, and ‘’ is used in a filename expansion context, two adjacent ‘’s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a ‘/’, two adjacent ‘*’s will match only directories and subdirectories.
###? Matches any single character.
###[…] Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a range expression; any character that sorts between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ may be matched by including it as the first or last character in the set. A ‘]’ may be matched by including it as the first character in the set. The sorting order of characters in range expressions is determined by the current locale and the value of the LC_COLLATE shell variable, if set.
For example, in the default C locale, ‘[a-dx-z]’ is equivalent to ‘[abcdxyz]’. Many locales sort characters in dictionary order, and in these locales ‘[a-dx-z]’ is typically not equivalent to ‘[abcdxyz]’; it might be equivalent to ‘[aBbCcDdxXyYz]’, for example. To obtain the traditional interpretation of ranges in bracket expressions, you can force the use of the C locale by setting the LC_COLLATE or LC_ALL environment variable to the value ‘C’.
Within ‘[’ and ‘]’, character classes can be specified using the syntax [:class:], where class is one of the following classes defined in the POSIX standard:
A character class matches any character belonging to that class. The word character class matches letters, digits, and the character ‘_’.
Within ‘[’ and ‘]’, an equivalence class can be specified using the syntax [=c=], which matches all characters with the same collation weight (as defined by the current locale) as the character c.
Within ‘[’ and ‘]’, the syntax [.symbol.] matches the collating symbol symbol.
If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a ‘|’. Composite patterns may be formed using one or more of the following sub-patterns:
###?(pattern-list) Matches zero or one occurrence of the given patterns.
###*(pattern-list) Matches zero or more occurrences of the given patterns.
###+(pattern-list) Matches one or more occurrences of the given patterns.
###@(pattern-list) Matches one of the given patterns.
###!(pattern-list) Matches anything except one of the given patterns.
(The MIT License)
Copyright (c) 2013 Droppe <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.