-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
58 lines (48 loc) · 1.18 KB
/
utils.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
'use strict';
var fs = require('fs');
var path = require('path');
var utils = require('lazy-cache')(require);
var fn = require;
/**
* Lazily required module dependencies
*/
require = utils;
require('get-value', 'get');
require('has-value', 'has');
require('set-value', 'set');
require('unset-value', 'unset');
require('rimraf', 'del');
require('project-name', 'project');
require('resolve-dir');
require('write-json');
require = fn;
/**
* Create the key to use for getting and setting values.
* If the key contains a filepath, and the filepath has
* dots in it, we need to escape them to ensure that
* `set-value` doesn't split on those dots.
*/
utils.toKey = function(filepath, key) {
if (typeof filepath !== 'string') {
throw new TypeError('expected filepath to be a string');
}
filepath = filepath.split('.').join('\\.');
return filepath + (key ? ('.' + key) : '');
};
/**
* Read a JSON file.
*
* @param {String} `filepath`
* @return {Object}
*/
utils.readJson = function(filepath) {
try {
var str = fs.readFileSync(path.resolve(filepath), 'utf8');
return JSON.parse(str);
} catch (err) {}
return {};
};
/**
* Expose `utils`
*/
module.exports = utils;