-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
63 lines (51 loc) · 1.3 KB
/
utils.js
File metadata and controls
63 lines (51 loc) · 1.3 KB
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
'use strict';
var path = require('path');
var homedir = require('homedir-polyfill');
var utils = module.exports;
/**
* Return true if `str` is a string with length greater than zero.
*/
utils.isString = function(str) {
return str && typeof str === 'string';
};
/**
* Logging utils
*/
utils.log = require('log-utils');
utils.color = utils.log.colors;
utils.timestamp = function() {
var args = [].slice.call(arguments);
args.unshift(utils.log.timestamp);
console.error.apply(console, args);
};
/**
* Get a home-relative filepath
*/
utils.homeRelative = function(filepath) {
if (!utils.isString(filepath)) {
throw new TypeError('expected filepath to be a string');
}
filepath = path.relative(homedir(), path.resolve(filepath));
if (filepath.charAt(0) === '/') {
filepath = filepath.slice(1);
}
return filepath;
};
/**
* Return a formatted, home-relative config-file path.
*
* @param {String} `configName` Config file name
* @param {String} `filepath`
* @return {String}
*/
utils.configPath = function(msg, filepath) {
var configPath = path.dirname(filepath) === process.cwd()
? path.basename(filepath)
: utils.homeRelative(filepath);
var fp = utils.color.green('~/' + configPath);
utils.timestamp(msg + ' ' + fp);
};
/**
* Expose `utils`
*/
module.exports = utils;