How to use the globSync function from glob

Find comprehensive JavaScript glob.globSync code examples handpicked from public code repositorys.

glob.globSync is a method in the glob library that returns an array of file paths that match a specified pattern.

43
44
45
46
47
48
49
50
51
52
};
/** 获取样式 */
const getLibraryStyle = (scanDir, extension) => {
    const id = getHash(JSON.stringify({ scanDir, extension }));
    const createLibraryCache = () => {
        const files = glob.globSync(extension, {
            matchBase: true,
            cwd: scanDir
        }).map((path) => vite.normalizePath(path));
        cache.set(id, files);
fork icon0
star icon0
watch icon1

67
68
69
70
71
72
73
74
75
const baseDir =
  lookupBaseDir && typeof lookupBaseDir === 'string' ? lookupBaseDir : new BladeContainerOptions().lookupBaseDir;

const pattern = path.join(baseDir.startsWith('/') ? baseDir.substring(1) : baseDir, lookupGlobPattern);

globSync(pattern).forEach((mapFileName) => {
  // Add BladeBox instance to the container
  this.#bladeBoxes.push(new BladeBox(require(`${appDir}/${mapFileName}`)));
});
fork icon0
star icon0
watch icon2

How does glob.globSync work?

glob.globSync is a method in the glob library that searches for files and directories that match a specified pattern and returns their paths as an array. When you call glob.globSync(pattern, options), glob searches the file system for any files or directories that match the specified pattern. The pattern can include special characters like *, ?, and [] to match specific file names or patterns. By default, glob.globSync searches the entire file system starting from the current working directory, but you can specify a cwd (current working directory) option to limit the search to a specific directory. glob.globSync also provides several other options to customize the search behavior, including the ability to ignore certain files or directories, follow symbolic links, and more. Once glob.globSync has found all the files and directories that match the specified pattern, it returns an array of their paths. The paths are returned as strings, and can be used to perform further operations on the matching files, such as reading, writing, or processing their contents. Overall, glob.globSync provides a flexible and powerful way to search for files and directories that match specific patterns or criteria, making it a useful tool for a wide range of file processing and management tasks.

5
6
7
8
9
10
11
12
13
14
15
const { CasbinAuthorization } = require("../cores/RouterSecurity/authorization.middleware");
const CasbinAdapter = require("../configs/casbin-adapter");


ConfigWrapper.config({
  fileMock: ["../mocks/mocks.json", "../mocks/user.mock.json"],
  fileSchemaValidation: glob.globSync(path.join(__dirname, "../validation/*.schema.js")),
  initSwagger: {
    info: {
      title: "Test",
      description: "TEST DES",
fork icon0
star icon0
watch icon1

+ 5 other calls in file

130
131
132
133
134
135
136
137
138
139
  }],
],

// If you are using Cucumber you need to specify the location of your step definitions.
cucumberOpts: {
  require: globSync('test/e2e/step_definitions/**/*.js'), // <string[]> (file/dir) require files before executing features
  backtrace: false, // <boolean> show full backtrace for errors
  requireModule: ['@babel/register'],
  dryRun: false, // <boolean> invoke formatters without executing steps
  failFast: false, // <boolean> abort the run on first failure
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
const glob = require("glob");

// Search for all .txt files in the current directory
const txtFiles = glob.globSync("*.txt");

// Log the matching file paths to the console
console.log(txtFiles);

In this example, we first import the glob library. We then call glob.globSync('*.txt') to search for all files in the current directory that have a .txt extension. The * character is a wildcard that matches any sequence of characters, and *.txt means "match any file that ends with .txt". glob.globSync returns an array of matching file paths, which we store in the txtFiles variable. Finally, we log the contents of txtFiles to the console, which should output an array of file paths that match the .txt pattern. Note that glob.globSync can also accept other patterns and options to customize the search behavior. For example, glob.globSync('**/*.js') would match all JavaScript files in the current directory and all its subdirectories, and glob.globSync('*.txt', { cwd: '/path/to/dir' }) would match all .txt files in the /path/to/dir directory instead of the current working directory.

20
21
22
23
24
25
26
27
28
29
  }
}

const res = await axios.get(themeUrl);
try {
  const existing = globSync("src/site/styles/_theme.*.css");
  existing.forEach((file) => {
    fs.rmSync(file);
  });
} catch {}
fork icon0
star icon0
watch icon0

6
7
8
9
10
11
12
13
14
15
module.exports = async () => {
  let baseUrl = process.env.SITE_BASE_URL || "";
  if (baseUrl && !baseUrl.startsWith("http")) {
    baseUrl = "https://" + baseUrl;
  }
  let themeStyle = globSync("src/site/styles/_theme.*.css")[0] || "";
  if (themeStyle) {
    themeStyle = themeStyle.split("site")[1];
  }
  let bodyClasses = [];
fork icon0
star icon0
watch icon0

2
3
4
5
6
7
8
9
10
11
12
13
14
const theme = require('./docs/utils/_styleguidist.theme.js')


const srcPath = path.resolve(__dirname, 'src')
const docsPath = path.resolve(__dirname, 'docs')


const getHooksDocFiles = () => globSync(path.join(__dirname, 'docs', '[use]*.md')).map((filePath) => {
  const [filename] = filePath.match(/use[a-zA-Z]*/, 'gm')


  return ({
    name: filename, content: `./docs/${filename}.md`
fork icon0
star icon0
watch icon0