-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy path_utils.js
309 lines (276 loc) · 6.21 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const chalk = require('chalk');
const fs = require('fs');
const ora = require('ora');
const YAML = require('json2yaml');
const { js_beautify: beautify } = require('js-beautify');
require('require-yaml');
/**
* Sort an object by its keys
*
* @since 0.8.0
* @public
*
* @param {Object} object
* @return {Object}
*/
function sortObject(object) {
return Object.keys(object)
.sort()
.reduce((result, key) => {
result[key] = object[key];
return result;
}, {});
}
/**
* Print a task name in a custom format
*
* @since 0.5.0
* @public
*
* @param {string} name The name of the task
*/// istanbul ignore next
function printTask(isQuiet, name) {
if (isQuiet) {
return;
}
process.stdout.write(chalk.blue(`\n🤖 - ${name}:\n===================================\n`));
}
/**
* Outputs the task status
*
* @since 0.5.0
* @public
*
* @param {string} taskName The task name
*
* @return {Function} The function to be fired when is loaded
*/// istanbul ignore next
function task(gren, taskName) {
if (gren.options.quiet) {
gren.tasks[taskName] = {};
return noop;
}
const spinner = ora(taskName);
gren.tasks[taskName] = spinner;
spinner.start();
return message => {
spinner.succeed(message);
};
}
/**
* Clears all the tasks that are still running
*
* @since 0.6.0
* @public
*
* @param {GithubReleaseNotes} gren
*/// istanbul ignore next
function clearTasks(gren) {
if (!Object.keys(gren.tasks.length)) {
return;
}
Object.keys(gren.tasks).forEach((taskName) => {
gren.tasks[taskName].stop();
});
process.stdout.write(chalk.red('\nTask(s) stopped because of the following error:\n'));
gren.tasks = [];
}
/**
* Check if e value is between a min and a max
*
* @since 0.5.0
* @public
*
* @param {number} value
* @param {number} min
* @param {number} max
*
* @return {Boolean}
*/
function isInRange(value, min, max) {
return !Math.floor((value - min) / (max - min));
}
/**
* Transforms a dasherize string into a camel case one.
*
* @since 0.3.2
* @public
*
* @param {string} value The dasherize string
*
* @return {string} The camel case string
*/
function dashToCamelCase(value) {
return value
.toLowerCase()
.replace(/-([a-z])/g, (match) => match[1].toUpperCase());
}
/**
* Converts an array like string to an actual Array,
* converting also underscores to spaces
*
* @since 0.6.0
* @public
*
* @param {string} arrayLike The string of items
* e.g.
* "wont_fix, duplicate, bug"
*
* @return {Array} The items with spaces instead of underscores.
*/
function convertStringToArray(arrayLike) {
if (!arrayLike) {
return [];
}
if (typeof arrayLike === 'object') {
return Object.keys(arrayLike).map((itemKey) => arrayLike[itemKey]);
}
return arrayLike
.replace(/\s/g, '')
.split(',')
.map((itemName) => itemName.replace(/_/g, ' ', itemName));
}
/**
* Format a date into a string
*
* @since 0.5.0
* @public
*
* @param {Date} date
* @return {string}
*/
function formatDate(date) {
return ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear();
}
/**
* Gets the content from a filepath a returns an object
*
* @since 0.6.0
* @public
*
* @param {string} filepath
* @return {Object|boolean}
*/
function requireConfig(filepath) {
if (!fs.existsSync(filepath)) {
return false;
}
if (getFileNameFromPath(filepath).match(/\./g).length === 1) {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
}
return require(filepath);
}
/**
* Get configuration from the one of the config files
*
* @since 0.6.0
* @public
*
* @param {string} path Path where to look for config files
* @return {Object} The configuration from the first found file or empty object
*/
function getConfigFromFile(path) {
return getFileTypes()
.reduce((carry, filename) => carry || requireConfig(path + '/' + filename), false) || {};
}
/**
* Return the extension of a filename
*
* @param {string} filename
*
* @return {string}
*/
function getFileExtension(filename) {
return filename.slice((Math.max(0, filename.lastIndexOf('.')) || Infinity) + 1);
}
/**
* Create the content for a configuratio file, based on extension and data
*
* @param {string} path
* @param {Object} data
*
* @return {string} File content
*/// istanbul ignore next
function writeConfigToFile(path, data) {
const extension = getFileExtension(getFileNameFromPath(path));
const dataType = {
yml: content => YAML.stringify(content),
yaml: content => YAML.stringify(content),
json: content => beautify(JSON.stringify(content)),
none: content => beautify(JSON.stringify(content)),
js: content => beautify(`module.exports = ${JSON.stringify(content)}`)
};
return dataType[extension || 'none'](data);
}
/**
* Get the filename from a path
*
* @since 0.10.0
* @private
*
* @param {string} path
*
* @return {string}
*/
function getFileNameFromPath(path = '') {
return path.replace(/^.*[\\/]/, '');
}
/**
* Get the file types for the configuration
*
* @since 0.13.0
*
* @return {Array}
*/
function getFileTypes() {
return [
'.grenrc.yml',
'.grenrc.json',
'.grenrc.yaml',
'.grenrc.js',
'.grenrc'
];
}
/**
* Remove all the configuration files
*
* @since 0.13.0
*
* @param {Boolean} confirm Necessary to force the function.
*/
function cleanConfig(confirm, path = process.cwd()) {
if (confirm !== true) {
return;
}
getFileTypes().forEach(fileName => {
const file = `${path}/${fileName}`;
if (!fs.existsSync(file)) {
return false;
}
fs.unlinkSync(file);
return file;
});
}
/**
* Just a noop function
*/
function noop() {}
// Allow nodeunit to work. Has to be fixed.
module.exports = {
cleanConfig,
clearTasks,
convertStringToArray,
dashToCamelCase,
formatDate,
getConfigFromFile,
getFileExtension,
getFileNameFromPath,
getFileTypes,
isInRange,
noop,
printTask,
requireConfig,
sortObject,
task,
writeConfigToFile
};