File system writes

Jobs that need to write to the file system must do so in the /tmp directory. Writing to a location outside of this directory is not permitted and will result in an error being thrown.

Below is a sample job which writes to the file system:

const fs = require('fs');
const fsp = require('fs/promises');
const path = require('path');

const WRITE_DIR = `/tmp`; (1)
const getPath = (relPath) => path.resolve(WRITE_DIR, relPath);

module.exports = async function (input, info) {
  const newDir = getPath('output');
  if (!fs.existsSync(newDir)) {
    fs.mkdirSync(newDir);
  }

  const filename = 'filename.txt';
  const filenamePath = path.resolve(newDir, filename);

  console.log(`Writing to file: ${filenamePath}`);
  await fsp.writeFile(filenamePath, 'The quick brown fox jumps over the lazy dog');

  const content = await fsp.readFile(filenamePath, 'utf-8');
  console.log(`File contains: "${content}"`);

  console.log(`Job executed successfully!`);
};
1 Explicitly setting the write directory.