Last active
December 21, 2017 20:08
-
-
Save eschwartz/666edf7a8710ad346c9260e74c161754 to your computer and use it in GitHub Desktop.
Assert a file does not exist (node)
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; |
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'); | |
module.exports = function assertFileNotExists(filePath, msg) { | |
try { | |
fs.statSync(filePath); | |
} | |
catch (err) { | |
if (!_.includes(['ENOENT', 'ENOTDIR'], err.code)) { | |
assert.ifError(err); | |
} | |
return; | |
} | |
assert(false, msg); | |
} |
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 assertFileNotExists(filePath:string, msg?:string) { | |
try { | |
fs.statSync(filePath); | |
} | |
catch (err) { | |
if (!_.includes(['ENOENT', 'ENOTDIR'], err.code)) { | |
assert.ifError(err); | |
} | |
return; | |
} | |
assert(false, `${msg}: (file exists)`); | |
} | |
export default assertFileNotExists; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment