Skip to content

File System

Read, write, and manipulate files during build.

All paths are relative to the game's source folder unless absolute.

Methods

read(path)

Read file as string:

javascript
const content = gemshell.fs.read('data/levels.json');

readJson(path)

Read and parse JSON:

javascript
const config = gemshell.fs.readJson('package.json');
console.log(config.version);

readBinary(path)

Read file as Buffer:

javascript
const buffer = gemshell.fs.readBinary('assets/image.png');

write(path, content)

Write string to file:

javascript
gemshell.fs.write('build-info.txt', 'Built: ' + new Date().toISOString());

writeJson(path, data)

Write JSON to file:

javascript
gemshell.fs.writeJson('config.json', { version: '1.0.0', debug: false });

writeBinary(path, buffer)

Write Buffer to file:

javascript
const processed = processImage(buffer);
gemshell.fs.writeBinary('assets/image.png', processed);

exists(path)

Check if file exists:

javascript
if (gemshell.fs.exists('config.json')) {
  // ...
}

stat(path)

Get file info:

javascript
const info = gemshell.fs.stat('assets/sprite.png');
// { size: 12345, mtime: 1706000000000, isDirectory: false }

mkdir(path)

Create directory (recursive):

javascript
gemshell.fs.mkdir('assets/processed');

copy(src, dest)

Copy file or directory:

javascript
gemshell.fs.copy('assets/original.png', 'assets/backup.png');

remove(path)

Delete file or directory:

javascript
gemshell.fs.remove('temp');

list(path)

List directory contents:

javascript
const files = gemshell.fs.list('assets');
// ['sprite.png', 'sound.mp3', 'data/']

Example: Add Build Info

javascript
module.exports = {
  name: 'Build Info',
  version: '1.0.0',
  
  async onPostBuild(context, settings) {
    const info = {
      version: context.config.version,
      buildTime: new Date().toISOString(),
      platform: context.platform,
      arch: context.arch
    };
    
    gemshell.fs.writeJson(
      `${context.outputPath}/build-info.json`,
      info
    );
    
    gemshell.log('Build info written');
  }
};

Example: Process All Images

javascript
module.exports = {
  name: 'Image Processor',
  version: '1.0.0',
  
  async onModifyAssets(context, settings) {
    const images = gemshell.glob('**/*.png');
    
    for (const img of images) {
      const buffer = gemshell.fs.readBinary(img);
      const processed = await processImage(buffer);
      gemshell.fs.writeBinary(img, processed);
      gemshell.log(`Processed: ${img}`);
    }
  }
};