Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active December 18, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eschwartz/d7ac200025023481c43c08d89207b4e0 to your computer and use it in GitHub Desktop.
Save eschwartz/d7ac200025023481c43c08d89207b4e0 to your computer and use it in GitHub Desktop.
Assert that a file exists
const fs = require('fs');
const assert = require('assert');
const _ = require('lodash');
function fileExists(filePath, msg) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
return assert.fail(undefined, filePath, msg || `File does not exist at path ${filePath}`);
}
assert.ifError(err);
}
}
module.exports = fileExists;
import * as fs from 'fs';
import * as assert from 'assert';
import * as _ from 'lodash';
function assertFileExists(filePath:string, msg?:string) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
return assert.fail(undefined, filePath, msg || `File does not exist at path ${filePath}`, 'exists');
}
assert.ifError(err);
}
}
export default assertFileExists;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment