Last active
December 18, 2017 18:30
-
-
Save eschwartz/d7ac200025023481c43c08d89207b4e0 to your computer and use it in GitHub Desktop.
Assert that a file exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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