UNPKG

osx-defaults

Version:

Access to OS X defaults program via simple API.

276 lines (261 loc) 10.2 kB
'use strict'; const shjs = require('shelljs'); var defaults = Object.create(null); // Constants Object.defineProperties(defaults, { /** @name defaults.STRING */ STRING: { enumerable: true, configurable: false, writable: false, value: 'string' }, /** @name defaults.INT */ INT: { enumerable: true, configurable: false, writable: false, value: 'int' }, /** @name defaults.INTEGER */ INTEGER: { enumerable: true, configurable: false, writable: false, value: 'integer' }, /** @name defaults.FLOAT */ FLOAT: { enumerable: true, configurable: false, writable: false, value: 'float' }, /** @name defaults.BOOL */ BOOL: { enumerable: true, configurable: false, writable: false, value: 'bool' }, /** @name defaults.BOOLEAN */ BOOLEAN: { enumerable: true, configurable: false, writable: false, value: 'boolean' }, /** @name defaults.ARRAY */ ARRAY: { enumerable: true, configurable: false, writable: false, value: 'array' }, /** @name defaults.DICT */ DICT: { enumerable: true, configurable: false, writable: false, value: 'dict' }, /** @name defaults.DICTIONARY */ DICTIONARY: { enumerable: true, configurable: false, writable: false, value: 'dict' } }); function opt(obj, key, def) { return obj.hasOwnProperty(key) ? obj[key] : (def || null); } function setup(obj) { const defaults = [ 'hostname', 'isCurrentHost', 'domain', 'isGlobalDomain', 'key', 'valueType', 'value', 'new_key' ]; var settings = {}, _value = null; defaults.forEach(function(key) { if (['isCurrentHost', 'isGlobalDomain'].indexOf(key) > -1) { _value = false; } settings[key] = (obj[key] || _value); }); return settings; } function command(name, settings) { return [ 'defaults', settings.hostname !== null && settings.isCurrentHost === false ? `-host ${settings.hostname}` : (settings.isCurrentHost === true ? '-currentHost' : ''), name, settings.domain !== null && settings.isGlobalDomain === false ? settings.domain : (settings.isGlobalDomain === true ? '-g' : ''), settings.key !== null ? settings.key : '', name === 'write' && settings.valueType !== null ? `-${settings.valueType}` : '', name === 'write' && settings.value !== null ? settings.value : '', name === 'rename' && settings.new_key !== null ? settings.new_key : '' ].join(' '); } function defaultCallback(error) { if (error !== null) throw error; } defaults.read = function(options, next) { var settings = setup((function(settings) { settings.hostname = opt(options, 'hostname', null); settings.isCurrentHost = settings.hostname !== null ? false : opt(options, 'isCurrentHost', false); settings.domain = opt(options, 'domain', null); settings.isGlobalDomain = settings.domain !== null ? false : opt(options, 'isGlobalDomain', false); settings.key = opt(options, 'key', null); return settings; })({})); let result; if (typeof next !== 'function') { try { if (settings.key === null && settings.domain === null) { throw new Error('Invalid arguments'); } result = shjs.exec(command('read', settings), { async: false, silent: true }); if (result.code !== 0) { if (result.output.indexOf('does not exist') > -1) { return null; } throw new Error(`Exit code: ${result.code}\nResult: ${result.output}`); } return result.output.replace(/\n$/, ''); } catch (error) { return defaultCallback(error); } } else { if (settings.key === null && settings.domain === null) { return next(new Error('Invalid arguments'), null); } return shjs.exec(command('read', settings), { async: true, silent: true }, function(code, output) { if (code !== 0) { if (output.indexOf('does not exist') > -1) { return next(null, null); } return next( new Error(`Exit code: ${code}\nResult: ${output}`), output.replace(/\n$/, '') ); } return next(null, output.replace(/\n$/, '')); }); } }; defaults.write = function(options, next) { var settings = setup((function(settings) { settings.hostname = opt(options, 'hostname', null); settings.isCurrentHost = settings.hostname !== null ? false : opt(options, 'isCurrentHost', false); settings.domain = opt(options, 'domain', null); settings.isGlobalDomain = settings.domain !== null ? false : opt(options, 'isGlobalDomain', false); settings.key = opt(options, 'key', null); settings.valueType = opt(options, 'valueType', 'string'); settings.value = opt(options, 'value', null); return settings; })({})); let result; if (typeof next !== 'function') { try { if (settings.key === null && settings.value === null) { throw new Error('Invalid arguments'); } result = shjs.exec(command('write', settings), { async: false, silent: true }); if (result.code !== 0) { throw new Error(`Exit code: ${result.code}\nResult: ${result.output}`); } return result.output.replace(/\n$/, ''); } catch (error) { return defaultCallback(error); } } else { if (settings.key === null && settings.value === null) { return next(new Error('Invalid arguments'), null); } return shjs.exec(command('write', settings), { async: true, silent: true }, function(code, output) { if (code !== 0) { return next( new Error(`Exit code: ${code}\nResult: ${output}`), output.replace(/\n$/, '') ); } return next(null, output.replace(/\n$/, '')); }); } }; defaults.rename = function(options, next) { var settings = setup((function(settings) { settings.hostname = opt(options, 'hostname', null); settings.isCurrentHost = settings.hostname !== null ? false : opt(options, 'isCurrentHost', false); settings.domain = opt(options, 'domain', null); settings.isGlobalDomain = settings.domain !== null ? false : opt(options, 'isGlobalDomain', false); settings.key = opt(options, 'key', null); settings.new_key = opt(options, 'new_key', null); return settings; })({})); let result; if (typeof next !== 'function') { try { if (settings.key === null && settings.new_key === null) { throw new Error('Invalid arguments'); } result = shjs.exec(command('rename', settings), { async: false, silent: true }); if (result.code !== 0) { if (result.output.indexOf('does not exist') > -1) { return false; } throw new Error(`Exit code: ${result.code}\nResult: ${result.output}`); } return true; } catch (error) { return defaultCallback(error); } } else { if (settings.key === null && settings.new_key === null) { return next(new Error('Invalid arguments'), true); } return shjs.exec(command('rename', settings), { async: true, silent: true }, function(code, output) { if (code !== 0) { if (result.output.indexOf('does not exist') > -1) { return next(null, false); } return next( new Error(`Exit code: ${code}\nResult: ${output}`), false ); } return next(null, true); }); } }; defaults.delete = function(options, next) { var settings = setup((function(settings) { settings.hostname = opt(options, 'hostname', null); settings.isCurrentHost = settings.hostname !== null ? false : opt(options, 'isCurrentHost', false); settings.domain = opt(options, 'domain', null); settings.isGlobalDomain = settings.domain !== null ? false : opt(options, 'isGlobalDomain', false); settings.key = opt(options, 'key', null); return settings; })({})); let result; if (typeof next !== 'function') { try { if (settings.key === null && settings.domain === null) { throw new Error('Invalid arguments'); } result = shjs.exec(command('delete', settings), { async: false, silent: true }); if (result.code !== 0) { if (result.output.indexOf('does not exist') > -1) { return false; } throw new Error(`Exit code: ${result.code}\nResult: ${result.output}`); } return true; } catch (error) { return defaultCallback(error); } } else { if (settings.key === null && settings.domain === null) { return next(new Error('Invalid arguments'), false); } return shjs.exec(command('delete', settings), { async: true, silent: true }, function(code, output) { if (code !== 0) { if (result.output.indexOf('does not exist') > -1) { return next(null, false); } return next( new Error(`Exit code: ${code}\nResult: ${output}`), false ); } return next(null, true); }); } }; module.exports = defaults;