UNPKG

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