UNPKG

1.95 kBJavaScriptView Raw
1'use strict';
2
3var _stringify = require('babel-runtime/core-js/json/stringify');
4
5var _stringify2 = _interopRequireDefault(_stringify);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9var extend = require('xtend');
10var init = require('./init');
11var debug = require('./debug');
12
13var defaultOptions = {
14 update: false,
15 updateAll: false,
16 global: false,
17 cwd: process.cwd(),
18 skipUnused: false,
19
20 ignoreDev: false,
21 devOnly: false,
22 forceColor: false,
23 saveExact: false,
24 specials: '',
25 debug: false,
26 emoji: true,
27 spinner: false,
28 installer: 'npm',
29 ignore: [],
30
31 globalPackages: {},
32 cwdPackageJson: { devDependencies: {}, dependencies: {} },
33
34 packages: false,
35 unusedDependencies: false,
36 missingFromPackageJson: {}
37};
38
39function state(userOptions) {
40 var currentStateObject = extend(defaultOptions);
41
42 function get(key) {
43 if (!currentStateObject.hasOwnProperty(key)) {
44 throw new Error('Can\'t get unknown option "' + key + '".');
45 }
46 return currentStateObject[key];
47 }
48
49 function set(key, value) {
50 if (get('debug')) {
51 debug('set key', key, 'to value', value);
52 }
53
54 if (currentStateObject.hasOwnProperty(key)) {
55 currentStateObject[key] = value;
56 } else {
57 throw new Error('unknown option "' + key + '" setting to "' + (0, _stringify2.default)(value, false, 4) + '".');
58 }
59 }
60
61 function inspectIfDebugMode() {
62 if (get('debug')) {
63 inspect();
64 }
65 }
66
67 function inspect() {
68 debug('current state', all());
69 }
70
71 function all() {
72 return currentStateObject;
73 }
74
75 var currentState = {
76 get: get,
77 set: set,
78 all: all,
79 inspectIfDebugMode: inspectIfDebugMode
80 };
81
82 return init(currentState, userOptions);
83}
84module.exports = state;
\No newline at end of file