'use strict';

var fs = require('fs').promises;
var path = require('path');
var {clearFileState, isFileStateModified, isFileStateDeleted, setCommittedFile} = require('../state');

async function write(file) {
  var dir = path.dirname(file.path);
  try {
    if (!(await fs.stat(dir)).isDirectory()) {
      throw new Error(`${file.path} is not a directory`);
    }
  } catch (error) {
    if (error.code === 'ENOENT') {
      await fs.mkdir(dir, {recursive: true});
    } else {
      throw error;
    }
  }

  await fs.writeFile(file.path, file.contents, {
    mode: file.stat ? file.stat.mode : null
  });
}

async function remove(file) {
  await fs.rmdir(file.path, {recursive: true});
}

module.exports = async function (file) {
  this.store.add(file);
  if (isFileStateModified(file)) {
    setCommittedFile(file);
    await write(file);
  } else if (isFileStateDeleted(file)) {
    setCommittedFile(file);
    await remove(file);
  }

  clearFileState(file);
};

@@ -640,22 +640,54 @@
 %7B%0A  
-await fs.rmdir
+let remove = fs.rm %7C%7C fs.rmdir;%0A  await remove
 (fil

Replace current removal by dynamic method (#179)