Skip to content

Instantly share code, notes, and snippets.

@john-osullivan
Created November 7, 2019 06:46
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 john-osullivan/95c5b422f43fe8d25fc090ace8d404ef to your computer and use it in GitHub Desktop.
Save john-osullivan/95c5b422f43fe8d25fc090ace8d404ef to your computer and use it in GitHub Desktop.
Dev Diary #3 - CLI File Loader
/**
* 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