Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active October 16, 2018 19:11
Show Gist options
  • Save TravisMullen/9e6e48e38a6cfa74218e97202524b2b4 to your computer and use it in GitHub Desktop.
Save TravisMullen/9e6e48e38a6cfa74218e97202524b2b4 to your computer and use it in GitHub Desktop.
Check file and delete file.
import { statSync } from 'fs'
import { spawnSync } from 'child_process'
/**
* Does file exist.
*
* @public
* @param {string} filePath Path to file.
* @return {boolean} File is found. <true>
*/
export const checkFile = filePath => {
let stats
try {
stats = statSync(filePath)
} catch (err) {
stats = err
}
return !(stats instanceof Error)
}
/**
* Delete file if exists.
*
* @public
* @param {string} filePath Path to file.
*/
export const purgeFile = filePath => {
if (checkFile(filePath)) {
// purge before we begin
spawnSync('rm', [filePath])
}
}
{
"name": "check-file",
"main": "check-file.js",
"engines": {
"node": ">= 8.6"
},
"author": "Travis Mullen <[email protected]> (http://travismullen.com/)",
"files": [
"check-file.js"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment