Node's Glob module allows you to write a glob rule using * and other symbols, like in the shell, to get files that match the corresponding rules.
This Glob tool is based on JavaScript. It uses the Minimatch library to match
Usage:
First download the Glob package:
NPM Install Glob
Call Format:
var glob = require ("Glob")//Options is an optional glob ("**/*.js", Options, Function (er, files) { //files) is an array of matches to the file. If the ' nonull ' option is set to True and no files are found, then files is the glob rule itself, not an empty array /ER is an error encountered during the search process})
"Globs" is a model, such as when you enter LS *.js in the command line, or you write bulid/* in the. gitignore file.
When parsing a path model, the contents of the curly braces separated by commas are expanded, and the part can contain "/", such as A{/B/C, BCD, which will be expanded into a/b/c and ABCD
A segment of the path can be represented by the following characters, each of which has a very cool effect:
- 1. *: Match 0 or more arbitrary characters in the path segment:
*: Matches a portion of the path: 0 or more characters glob ("Js/*.js", Function (er, files) { console.log (files)})
Get all the JS files in the JS directory. (not included with '. ') Files that begin with)
- 2.? : Matches 1 arbitrary characters in the path segment:
?: Match a part of the path: 1 characters glob ("js/?"). JS ", Function (er, files) { console.log (files)})
Get all the names in JS directory with only 1 characters of JS.
- 3. [...]: matches the character within the specified range in the path segment:
Note cannot be combined, only one of the characters
[]: Match part of Path: Specified range Glob ("Js/a[0-3].js", Function (er, files) { console.log (files)})
Get JS directory under a beginning, the second character is between 0-3 (including 0 and 3) of JavaScript (A03.js cannot be matched to)
- 4. * (Pattern|pattern|pattern): matches 0 or more or any combination of multiple models in parentheses
Note | There can be no spaces before and after
* (Pattern|pattern|pattern): matches a portion of a path: 0 or more in multiple models.//In addition to three models themselves, if it is a combination can also, such as Ab.js, but only to include a model is not possible, such as A4.js.glob ("JS /* (a|a1|b). js ", Function (er, files) { console.log (files)})
Get JS directory under A.js,a1.js,b.js, or a,a1,b a combination of these characters JS, such as Ab.js
- 5.! (Pattern|pattern|pattern): Match does not contain any model
Need attention:! (pattern|pattern|pattern) Not equal! (* (Pattern|pattern|pattern))
//! (Pattern|pattern|pattern): Matches a part of a path: does not contain any models.//With a or B, are excluded. It is important to note that it is not an inverse glob (a|b) ("js/!" ( a|b). js ", Function (er, files) { console.log (files)})
Gets the JS directory under the name does not contain a, also does not contain all files of B.
6.? (Pattern|pattern|pattern): matches 0 or any 1 in multiple models.
The difference between it and 4 is that it cannot be combined. Must match exactly
//? (Pattern|pattern|pattern): matches a portion of a path: 0 or 1 in multiple models.//Exact match model, cannot be combined. Glob ("js/?" ( a|a2|b). js ", Function (er, files) { console.log (files)})
Get JS directory under A.js,a2.js,b.js
- 7. + (Pattern|pattern|pattern): matches 1 or more of multiple models.
The difference between it and 4 is that there must be an empty mismatch
+ (Pattern|pattern|pattern): matches a portion of a path: 1 or more of multiple models.//Can be either a model or a combination of them, such as Ab.jsglob ("js/+ (a|a1|b). js", function (er, files) { console.log (files)})
Get JS directory under A.js,a1.js,b.js, or a,a1,b a combination of these characters JS, such as Ab.js
- 8. @ (Pattern|pat*|pat?ern): matches any 1 of multiple models.
@ (Pattern|pattern|pattern): matches a portion of a path: 1 in multiple models.//Exact match model, can not be combined. The difference between a and? Is that it cannot be empty. Must be one of them. Glob ("js/@ (a|a1|b). js", function (er, files) { console.log (files)})
The difference between the and 6 is that the mismatch is empty for the case
- 9. * *: As with 1, any content can be matched, but * * not only matches a segment in the path, but also matches the '/' content of ' a/b/c ', so it can also match files under subfolders.
* *: Not a single part of the path, but can be with '/', so all current folders and subfolders are matched Glob ("**/@ (a|a1|b). js", Function (er, files) { Console.log ( Files)})
Get a.js,a1.js,b.js under All folders and subfolders of the current directory
Another way to do this is to set the Matchbase property to True, which can also be used to search for all subfolders under the current path:
Matchbase: After set to true, look for matching files in all folders and subfolders in the current directory Glob ("@ (a|a1|b). js", {matchbase:true},function (er, files) { Console.log (Files)})
No matching files were obtained:
When Glob does not get any matching files, and does not return the model itself as in the shell, the files parameter returns an empty array, and if you need to let files return the model itself, you need to set the Nonull property to True
Nonull: After set to true, if no matching file is found, no empty string is returned, but the original GLOB statement is returned Glob ("@ (c|d|e). js", {nonull:true},function (er, files) { Console.log (Files)})
To get a list of matching files synchronously:
All that is said is asynchronous, passing in a callback that executes the callback when it gets to the matching file. If you need to synchronize the Get file list, you can do this:
var files = Glob.sync (pattern, [options])
Glob class:
By instantiating a glob. Glob class, you can get a Glob object:
var Glob = require ("Glob"). Globvar mg = new Glob (pattern, options, CB)
The arguments passed in when instantiated are the same as Glob (PATTERN,OPTIONS,CB).
It can get a return value, and this return value is a eventemitter.
If you set the Sync property to True in the options, it means that synchronization gets. The CB callback cannot be passed in. To get the matching result, you can get it by G.found:
var globinstance = new Glob. Glob ("@ (a|a1|b). js", {nonull:true,matchbase:true,sync:true}); Console.log (Globinstance.found);
Event:
- The End:end event is triggered at the end of the file match, when all matching results are found, and the parameters that it accepts are the array of files
- The Match:match event is triggered every time a file is matched, and the parameters it accepts are the matching files.
- The Error:error event is triggered when a match encounters an error. The accepted parameter is the error message
- Abort: The Abort event is triggered when the instance calls the. Abort () method
Method:
pause
Pause Match Search
resume
Continue matching Search
abort
Stop matching search forever, can't continue
var globinstance = new Glob. Glob ("js/@ (a|a1|b). js", {nonull:true}); Globinstance.on (' Match ', function (file) { console.log (file)}); Globinstance.on (' End ', function (files) { console.log (Files)}), Globinstance.on (' Abort ', function () { Console.log (' Abort ')}); Globinstance.pause (); Globinstance.resume (); Globinstance.abort ();
Property:
minimatch
The Minimatch object used by the Glob.
options
Options that are passed to the function.
aborted
abort()
after the function has been called, its value is true.
cache
- Statcache
- Symlinks
- Realpathcache
Options option:
Options are used to configure how matches are matched when the model is matched. All parameters that can be passed into the minimatch can also be passed to Glob, and Node-glob has added some additional configuration items.
All options if there is no special description, the default value is False
The
All options also apply to the Glob class.
-
cwd
-
root
-
dot
-
Nomount
-
Mark
-
nosort
-
stat
-
silent
-
strict
-
cache
-
statcache
-
symlinks
-
sync
-
nounique
-
nonull
-
debug
-
nobrace
-
noglobstar
-
noext
-
nocase
-
matchbase
-
nonull
-
nodir
-
ignore
-
follow
-
realpath
-
nonegate
-
nocomment
Forgive me so many options really do not want to be a translation over ........ ..... After all, most of them don't, if you have special needs, check the documentation ...
Reference Original: Https://github.com/isaacs/node-glob
Node-glob Module