Created
November 7, 2019 06:46
-
-
Save john-osullivan/95c5b422f43fe8d25fc090ace8d404ef to your computer and use it in GitHub Desktop.
Dev Diary #3 - CLI File Loader
This file contains 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
/** | |
* Middlware: Listen for any options whose name ends in "Path", and if found, | |
* read the file's contents as a string and add it as an argument | |
* whose name ends in "File". For instance, "authPath" will yield | |
* an "authFile" string, which can be JSON.parse()d to get the | |
* actual authData. | |
* @param args | |
*/ | |
export function loadFileFromPath(args:ArgShape): ArgShape { | |
const pathKeys = Object.keys(args).filter(key => key.indexOf('Path') > -1); | |
pathKeys.forEach(pathKey => { | |
const fullPath = path.resolve(process.cwd(), args[pathKey]); | |
if (!fs.existsSync(fullPath)) throw new Error(`The specified path (${pathKey}, ${args[pathKey]}) does not have a file!`); | |
const fileKey = `${pathKey.slice(0, pathKey.indexOf('Path'))}File`; | |
args[fileKey] = fs.readFileSync(fullPath).toString(); | |
}) | |
return args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment