'use strict';

const fs = require('fs');
const log = console.log;

fs.readdir('./stuff/', function(err, files) {
  if (err != null) {
    log('Uh oh!');
    return;
  }

  if (!files.includes('my_file.txt')) {
    log('Uh oh!');
    return;
  }

  fs.readFile('./stuff/my_file.txt', function(err, data) {
    if (err != null) {
      log('Uh oh!');
      return;
    }

    let new_data =  data + "bleep\n";

    fs.writeFile('./stuff/my_file.txt', new_data, function(err) {
      if (err != null) {
        log('Uh oh!');
        return;
      }

      fs.readFile('./stuff/my_file.txt', function(err, data) {
        log("my_file now contains...");
        log(data.toString());
      });
    });
  });
});