Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively exclude files and folders that have names beginning with specific characters (using nodejs and glob)

Tags:

Glob documentation (I'll refer to it):

  • * Matches 0 or more characters in a single path portion
  • ? Matches 1 character
  • [...] Matches a range of characters, similar to a RegExp range. If the first character of the range is - ! or ^ then it matches any character not in the range.
  • !(pattern|pattern|pattern) Matches anything that does not match any of the patterns provided.
  • ?(pattern|pattern|pattern) Matches zero or one occurrence of the patterns provided.
  • +(pattern|pattern|pattern) Matches one or more occurrences of the patterns provided.
  • *(a|b|c) Matches zero or more occurrences of the patterns provided
  • @(pattern|pat*|pat?erN) Matches exactly one of the patterns provided
  • ** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches.

Recursively exclude FILES which name begins from underscore

Solution attempt

  1. First we need to match zero or more directories (I suppose, it means "in this directory and below"). As far as I understood the documentation, ** should do it.
  2. Next we need to exclude the files which name begins from underscore. AFAIK it is [^_]*.
  3. The filename extension left. Maybe .pug will will be enough, but .+(pug) allow to scale the solution to n filename extensions like .+(pug|haml).
/Pages/Open/**/[^_]*.+(pug)

enter image description here

The EntryPoint.pug was excluded, however it's name does not begin from underscore.

🌎 Fiddle

Recursively exclude DIRECTORIES which name begins from underscore

Solution attempt

All that I know is "globstar matches zero or more directories and subdirectories searching for matches" and "[^_]" excludes underscore. Looks like it's not enough to build the correct solution. Below solution in intuitive and does not work.

/Pages/Open/[^_]**/*.+(pug)

enter image description here

🌎 Fiddle

Recursively exclude files AND directories which name begins from underscore

Can we reach this effect just by combining the solution for files and directories?

like image 483
Takeshi Tokugawa YD Avatar asked Aug 23 '20 05:08

Takeshi Tokugawa YD


1 Answers

Use ignore option of glob to exclude directories and files starting from character underscore

var glob = require('glob');

glob("**/*",{"ignore":['**/_**.pug', "**/_**/**"]}, function (err, files) {
  console.log(files);
})

// Output
[
  'Pages',
  'Pages/Open',
  'Pages/Open/EntryPoint.pug',
  'Pages/Open/Top',
  'Pages/Open/Top/EntryPoint.pug',
  'Pages/Open/Top/SubDirectory1',
  'Pages/Open/Top/SubDirectory1/NonPartial.pug',
  'test.js'
]
like image 198
Hardik Sondagar Avatar answered Sep 30 '22 19:09

Hardik Sondagar