All files / src/commands touch.js

100% Statements 21/21
100% Branches 6/6
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40        1x 1x 1x 1x 1x   1x   1x   5x 5x   5x 1x     4x   4x 1x     3x   3x 1x         2x        
/**
 * Creates an empty file.
 * Usage: touch new_file.txt
 */
import parseOptions from 'parser/option-parser';
import * as FileOp from 'fs/operations-with-permissions/file-operations';
import * as OutputFactory from 'emulator-output/output-factory';
import * as FileUtil from 'fs/util/file-util';
import { resolvePath } from 'emulator-state/util';
 
const EMPTY_FILE = FileUtil.makeFile();
 
export const optDef = {};
 
export default (state, commandOptions) => {
  const {argv} = parseOptions(commandOptions, optDef);
 
  if (argv.length === 0) {
    return {}; // do nothing if no arguments are given
  }
 
  const filePath = resolvePath(state, argv[0]);
 
  if (state.getFileSystem().has(filePath)) {
    return {}; // do nothing if already has a file at the provided path
  }
 
  const {fs, err} = FileOp.writeFile(state.getFileSystem(), filePath, EMPTY_FILE);
 
  if (err) {
    return {
      output: OutputFactory.makeErrorOutput(err)
    };
  }
 
  return {
    state: state.setFileSystem(fs)
  };
};