Skip to content

Glob Patterns

Find files using glob patterns.

Method

glob(pattern)

Find files matching a pattern.

javascript
const files = gemshell.glob('**/*.png');
// ['assets/player.png', 'assets/enemy.png', 'ui/button.png']

Returns: string[] - Array of matching file paths

Pattern Syntax

PatternDescription
*Match any characters except /
**Match any characters including /
?Match single character
[abc]Match any character in brackets
[!abc]Match any character not in brackets
{a,b}Match either pattern

Examples

All Files of Type

javascript
// All PNG images
const pngs = gemshell.glob('**/*.png');

// All JavaScript files
const js = gemshell.glob('**/*.js');

// All JSON files
const json = gemshell.glob('**/*.json');

Specific Directories

javascript
// Images in assets folder
const assets = gemshell.glob('assets/**/*.{png,jpg,gif}');

// Data files
const data = gemshell.glob('data/*.json');

// All sounds
const sounds = gemshell.glob('sounds/**/*.{mp3,ogg,wav}');

Exclude Patterns

javascript
// Get all files, then filter
const allJs = gemshell.glob('**/*.js');
const srcOnly = allJs.filter(f => !f.includes('node_modules'));

Multiple Extensions

javascript
// All images
const images = gemshell.glob('**/*.{png,jpg,jpeg,gif,webp}');

// All audio
const audio = gemshell.glob('**/*.{mp3,ogg,wav,m4a}');

Plugin Examples

Count Assets

javascript
module.exports = {
  name: 'Asset Counter',
  version: '1.0.0',
  
  async onPreBuild(context, settings) {
    const images = gemshell.glob('**/*.{png,jpg,gif}');
    const sounds = gemshell.glob('**/*.{mp3,ogg,wav}');
    const scripts = gemshell.glob('**/*.js');
    
    gemshell.log(`Assets: ${images.length} images, ${sounds.length} sounds, ${scripts.length} scripts`);
  }
};

Process All Images

javascript
async onModifyAssets(context, settings) {
  const images = gemshell.glob('**/*.png');
  
  for (const img of images) {
    const buffer = gemshell.fs.readBinary(img);
    const processed = await optimizeImage(buffer);
    gemshell.fs.writeBinary(img, processed);
  }
  
  gemshell.log(`Processed ${images.length} images`);
}

Find Large Files

javascript
async onPreBuild(context, settings) {
  const allFiles = gemshell.glob('**/*');
  const largeFiles = [];
  
  for (const file of allFiles) {
    const stat = gemshell.fs.stat(file);
    if (!stat.isDirectory && stat.size > 1024 * 1024) { // > 1MB
      largeFiles.push({ file, size: stat.size });
    }
  }
  
  if (largeFiles.length > 0) {
    gemshell.warn('Large files found:');
    for (const f of largeFiles) {
      gemshell.warn(`  ${f.file}: ${(f.size / 1024 / 1024).toFixed(2)} MB`);
    }
  }
}

Clean Up Source Files

javascript
async onModifyAssets(context, settings) {
  // Remove development files
  const devFiles = [
    ...gemshell.glob('**/*.ts'),
    ...gemshell.glob('**/*.map'),
    ...gemshell.glob('**/tsconfig.json'),
    ...gemshell.glob('**/.eslintrc*')
  ];
  
  for (const file of devFiles) {
    gemshell.fs.remove(file);
  }
  
  gemshell.log(`Removed ${devFiles.length} development files`);
}