UNPKG

2.81 kBJavaScriptView Raw
1'use strict';
2
3var utils = require('lazy-cache')(require);
4var fn = require;
5require = utils;
6
7/**
8 * Lazily required module dependencies
9 */
10
11require('arr-flatten', 'flatten');
12require('array-unique', 'unique');
13require('define-property', 'define');
14require('extend-shallow', 'extend');
15require('fs-exists-sync', 'exists');
16require('falsey');
17require('has-glob');
18require('has-value');
19require('kind-of', 'typeOf');
20require('map-schema', 'Schema');
21require('mixin-deep', 'merge');
22require('resolve');
23require('tableize-object', 'tableize');
24require = fn;
25
26/**
27 * Cast `val` to an array.
28 */
29
30utils.arrayify = function(val) {
31 if (utils.isString(val)) {
32 val = val.split(',');
33 }
34 if (Array.isArray(val)) {
35 return val.filter(Boolean);
36 }
37 return [];
38};
39
40/**
41 * Strip the quotes from a string
42 */
43
44utils.stripQuotes = function(str) {
45 return str.replace(/^['"]|['"]$/g, '');
46};
47
48/**
49 * Return true if value is false, undefined, null, an empty array
50 * or empty object.
51 */
52
53utils.isEmpty = function(val) {
54 if (typeof val === 'function') {
55 return false;
56 }
57 return !utils.hasValue(val);
58};
59
60/**
61 * Return true if value is an object
62 */
63
64utils.isObject = function(val) {
65 return utils.typeOf(val) === 'object';
66};
67
68/**
69 * Return true if value is a string with non-zero length
70 */
71
72utils.isString = function(val) {
73 return val && typeof val === 'string';
74};
75
76/**
77 * Arrayify, flatten and uniquify `arr`
78 */
79
80utils.unify = function(arr) {
81 return utils.flatten(utils.arrayify(arr));
82};
83
84/**
85 * Stringify `obj` by tableizing keys and key-value pairs.
86 */
87
88utils.stringify = function(obj) {
89 obj = utils.tableize(obj);
90 var res = '';
91 for (var prop in obj) {
92 res += prop + '.' + obj[prop];
93 }
94 return res;
95};
96
97/**
98 * Invert the keys and values in `obj`.
99 */
100
101utils.invert = function invert(obj) {
102 var res = {};
103 for (var key in obj) res[obj[key]] = key;
104 return res;
105};
106
107/**
108 * File-related properties. Passed to [expand-args]
109 * to ensure that no undesired escaping or processing
110 * is done on filepath arguments.
111 */
112
113utils.fileKeys = [
114 'base', 'basename', 'cwd', 'dir',
115 'dirname', 'ext', 'extname', 'f',
116 'file', 'filename', 'path', 'root',
117 'stem'
118];
119
120/**
121 * Whitelisted flags: these flags can be passed along with task
122 * arguments. To run tasks with any flags, pass `--run`
123 */
124
125utils.whitelist = [
126 'ask',
127 'data',
128 'emit',
129 'force',
130 'init',
131 'layout',
132 'option',
133 'options',
134 'readme',
135 'set',
136 'toc',
137 'verbose'
138].concat(utils.fileKeys);
139
140/**
141 * Aliases to pass to minimist. Defined here
142 * so that it's available to any method.
143 */
144
145utils.aliases = {
146 config: 'c',
147 dirname: 'dir',
148 extname: 'ext',
149 file: 'f',
150 filename: 'stem',
151 global: 'g',
152 save: 's',
153 verbose: 'v',
154 version: 'V'
155};
156
157/**
158 * Expose `utils`
159 */
160
161module.exports = utils;
162