I've been doing some reading and tutorials. As such I've contrived a node based project to apply what I've learned. I thought this was a good example as it had some conditional async calls and error handling.
Any and all criticism of not following current JS best practices welcome.
/**
* mkLocalCfg function,
* makes a local config directory structure and empty config file.
* in the form "~/.config/nobjs/nobjs_config.json"
*
* @param {mkLocalCfgCallback} cb - The callback that handles the response.
*
*/
function mkLocalCfg(cb) {
//validate root folder exists, if not make it.
if (!admin.hasHomeConfigDir()) {
fs.mkdir(os.homedir() + "/.config", "775", function(err) {
//stop here if you can't make it.
if (err) {cb(err); return;}
});
}
fs.mkdir(admin.getConfigLocation(false), "775", function(err) {
//can't make config folder end it here
if (err) {cb(err); return;}
//write config file, return null or err on error
fs.writeFile(admin.getConfigLocation(true), JSON.stringify({blogs: {}}), function(err) {
if (err) {cb(err); return;}
cb(null);
});
});
}