UNPKG

johnny-cli

Version:

CLI for Johnny Deps

59 lines (48 loc) 1.19 kB
// @flow import fs from 'fs'; import settings from 'settings'; const _fsErrorMessage = 'Required config param is missed or JSON file is corrupted'; export default (johnnyPath: string = settings.DEFAULT_CONFIG_PATH): { create: Function, get: Function, remove: Function, set: Function, } => ({ create: function(params: {| name: string, path?: string, token: string, user: string, |}) { if(fs.existsSync(johnnyPath)) throw 'Config file johnny.json exists, use --download option if you want to setup existing dll'; fs.writeFileSync( johnnyPath, // Type specfies exact object, no non-relevant params can get into config JSON.stringify(params), err => { if(err) throw err; } ); return this; }, get: (name?: string) => { try { const jsonContent = JSON.parse(fs.readFileSync(johnnyPath, 'utf8')); return name ? jsonContent[name] : jsonContent; } catch(error) { throw _fsErrorMessage; } }, remove: () => fs.unlinkSync(johnnyPath), set: function(values: {[string]: mixed}) { try { fs.writeFileSync(johnnyPath, JSON.stringify({...this.get(), ...values})); } catch(error) { throw _fsErrorMessage; } } });