Refactor (from the review) credential file handling to use fs module for directory creation and file writing