-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathhelpers.js
More file actions
122 lines (110 loc) · 3.71 KB
/
helpers.js
File metadata and controls
122 lines (110 loc) · 3.71 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
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
/*
* Copyright 2020 Adobe Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const fs = require('fs')
const inquirer = require('inquirer')
require('./types.jsdoc') // get types
/**
* Sort array values according to the sort order and/or sort-field.
*
* Note that this will use the Javascript sort() function, thus the values will
* be sorted in-place.
*
* @param {Array<object>} values array of objects (with fields to sort by)
* @param {object} [options] sort options to pass
* @param {boolean} [options.descending] true by default, sort order
* @param {string} [options.field] 'date' by default, sort field ('name', 'date' options)
* @returns {Array<object>} the sorted values array (input values array sorted in place)
*/
function sortValues (values, { descending = true, field = 'date' } = {}) {
const supportedFields = ['name', 'date']
if (!supportedFields.includes(field)) { // unknown field, we just return the array
console.log('unknown field ... ', field)
return values
}
values.sort((left, right) => {
const d1 = left[field]
const d2 = right[field]
if (descending) {
return (d1 > d2) ? -1 : (d1 < d2) ? 1 : 0
} else {
return (d1 > d2) ? 1 : (d1 < d2) ? -1 : 0
}
})
return values
}
/**
* Gets the latest version of a plugin from npm.
*
* @param {string} npmPackageName the npm package name of the plugin
* @returns {string} the latest version of the plugin from the npm registry
*/
async function getNpmLatestVersion (npmPackageName) {
const res = await fetch(`https://registry.npmjs.com/${npmPackageName}`)
const { 'dist-tags': distTags } = await res.json()
return distTags && distTags.latest
}
/**
* Gets the npm package version of an npm package installed in the cli.
*
* @param {string} cliRoot the root path of the cli
* @param {string} npmPackageName the npm package name
* @returns {string} the version of the package from the cli node_modules
*/
async function getNpmLocalVersion (cliRoot, npmPackageName) {
const pjsonPath = `${cliRoot}/node_modules/${npmPackageName}/package.json`
const pjson = JSON.parse(fs.readFileSync(pjsonPath))
return pjson.version
}
/**
* Prompt for confirmation.
*
* @param {string} [message] the message to show
* @param {boolean} [defaultValue] the default value if the user presses 'Enter'
* @returns {boolean} true or false chosen for the confirmation
*/
async function prompt (message = 'Confirm?', defaultValue = false) {
return inquirer.prompt({
name: 'confirm',
type: 'confirm',
message,
default: defaultValue
}).then(function (answers) {
return answers.confirm
})
}
/**
* Hide NPM Warnings by intercepting process.stderr.write stream
*
*/
function hideNPMWarnings () {
const fn = process.stderr.write
/**
* Function to override the process.stderr.write and hide npm warnings
*
* @private
*/
function write () {
const msg = Buffer.isBuffer(arguments[0]) ? arguments[0].toString() : arguments[0]
if (!msg.startsWith('warning')) {
fn.apply(process.stderr, arguments)
}
return true
}
process.stderr.write = write
}
module.exports = {
prompt,
sortValues,
getNpmLatestVersion,
getNpmLocalVersion,
hideNPMWarnings
}