Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active December 21, 2017 20:08
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/666edf7a8710ad346c9260e74c161754 to your computer and use it in GitHub Desktop.
Save eschwartz/666edf7a8710ad346c9260e74c161754 to your computer and use it in GitHub Desktop.
Assert a file does not exist (node)
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;
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);
}
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