-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.js
98 lines (82 loc) · 2.46 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
var fs = require('fs');
var path = require('path');
var merge = require('merge-deep');
var debug = require('../debug');
var utils = require('../utils');
var isProcessed;
/**
* Persist a value to a namespaced config object in package.json. For
* example, if you're using `verb`, the value would be saved to the
* `verb` object.
*
* ```sh
* # display the config
* $ app --config
* # set a boolean for the current project
* $ app --config=toc
* # save the cwd to use for the current project
* $ app --config=cwd:foo
* # save the tasks to run for the current project
* $ app --config=tasks:readme
* ```
*
* @name config
* @param {Object} app
* @api public
* @cli public
*/
module.exports = function(app, base, options) {
var ran = false;
return function(val, key, config, next) {
if (ran === true) {
next();
return;
}
ran = true;
debug.field(key, val);
// get the keys of properties defined by an `--init` prompt
var keys = app.get('cache.initKeys') || [];
var name = app._name.toLowerCase();
if (utils.show(val)) {
var pkgConfig = {};
pkgConfig[name] = app.pkg.get(name) || {};
console.error(utils.formatValue(pkgConfig));
next();
return;
}
var pkgPath = path.resolve(app.cwd, 'package.json');
var pkg = {};
if (utils.exists(pkgPath)) {
pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
}
pkg = merge({}, app.pkg.data, pkg);
app.pkg.del(name);
var orig = pkg[name] || {};
// normalize both the old and new values before merging, using
// a schema that is specifically used for normalizing values to
// be written back to package.json
var tmp = app.cli.schema.normalize({config: {}}) || {};
orig = tmp.config;
// merge the normalized values
var merged = val;
if (utils.isObject(val) && utils.isObject(orig)) {
merged = merge({}, orig, val);
}
// show the new value in the console
var show = utils.pick(merged, keys);
app.pkg.logInfo('updated package.json config with', show);
// update options and `cache.config`
app.set('cache.config', merged);
app.emit('config', merged);
// update the config property
config[key] = merged;
if (app.pkg.queued === true) {
app.pkg.data = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
}
// re-set updated config object in `package.json`
app.pkg.set(name, merged);
app.pkg.save();
app.cli.process(merged, next);
};
};