generated from Ariadnemar06/npm-check-updates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.ts
181 lines (168 loc) · 6.03 KB
/
logging.ts
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
/**
* Loggin functions.
*/
import Table from 'cli-table'
import Chalk from 'chalk'
import { colorizeDiff, isGithubUrl, getGithubUrlTag, isNpmAlias, parseNpmAlias } from './version-util'
import getRepoUrl from './lib/getRepoUrl'
import { IgnoredUpgrade, Index, Options, Version, VersionSpec } from './types'
// maps string levels to numeric levels
const logLevels = {
silent: 0,
error: 1,
minimal: 2,
warn: 3,
info: 4,
verbose: 5,
silly: 6
}
/**
* Prints a message if it is included within options.loglevel.
*
* @param options Command line options. These will be compared to the loglevel parameter to determine if the message gets printed.
* @param message The message to print
* @param loglevel silent|error|warn|info|verbose|silly
* @param method The console method to call. Default: 'log'.
*/
export function print(options: Options, message: any, loglevel: 'silent' | 'error' | 'warn' | 'info' | 'verbose' | 'silly' | null = null, method: 'log' | 'warn' | 'info' | 'error' = 'log') {
// not in json mode
// not silent
// not at a loglevel under minimum specified
if (!options.json && options.loglevel !== 'silent' && (loglevel == null || logLevels[options.loglevel as unknown as keyof typeof logLevels] >= logLevels[loglevel])) {
console[method](message)
}
}
/** Pretty print a JSON object. */
export function printJson(options: Options, object: any) {
if (options.loglevel !== 'silent') {
console.log(JSON.stringify(object, null, 2))
}
}
/** Create a table with the appropriate columns and alignment to render dependency upgrades. */
function createDependencyTable() {
return new Table({
colAligns: ['left', 'right', 'right', 'right', 'left', 'left'],
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ''
}
})
}
/**
* Extract just the version number from a package.json dep
*
* @param dep Raw dependency, could be version / npm: string / Git url
*/
function getVersion(dep: string): string {
return isGithubUrl(dep) ? getGithubUrlTag(dep)!
: isNpmAlias(dep) ? parseNpmAlias(dep)![1]
: dep
}
/**
* @param args
* @param args.from
* @param args.to
* @param args.ownersChangedDeps
* @param args.format Array of strings from the --format CLI arg
*/
function toDependencyTable({ from: fromDeps, to: toDeps, ownersChangedDeps, format }: {
from: Index<VersionSpec>,
to: Index<VersionSpec>,
ownersChangedDeps?: Index<boolean>,
format?: string[],
}) {
const table = createDependencyTable()
const rows = Object.keys(toDeps).map(dep => {
const from = fromDeps[dep] || ''
const toRaw = toDeps[dep] || ''
const to = getVersion(toRaw)
const ownerChanged = ownersChangedDeps
? dep in ownersChangedDeps
? ownersChangedDeps[dep] ? '*owner changed*' : ''
: '*unknown*'
: ''
const toColorized = colorizeDiff(getVersion(from), to)
const repoUrl = format?.includes('repo')
? getRepoUrl(dep) || ''
: ''
return [dep, from, '→', toColorized, ownerChanged, repoUrl]
})
rows.forEach(row => table.push(row)) // eslint-disable-line fp/no-mutating-methods
return table
}
/**
* @param options - Options from the configuration
* @param args - The arguments passed to the function.
* @param args.current - The current packages.
* @param args.upgraded - The packages that should be upgraded.
* @param args.numUpgraded - The number of upgraded packages
* @param args.total - The total number of all possible upgrades
* @param args.ownersChangedDeps - Boolean flag per dependency which announces if package owner changed.
*/
export function printUpgrades(options: Options, { current, latest, upgraded, numUpgraded, total, ownersChangedDeps }: {
current: Index<VersionSpec>,
latest?: Index<Version>,
upgraded: Index<VersionSpec>,
numUpgraded: number,
total: number,
ownersChangedDeps?: Index<boolean>,
}) {
const chalk = options.color ? new Chalk.Instance({ level: 1 }) : Chalk
print(options, '')
// print everything is up-to-date
const smiley = chalk.green.bold(':)')
const target = typeof options.target === 'string' ? options.target : 'target'
if (numUpgraded === 0 && total === 0) {
if (Object.keys(current).length === 0) {
print(options, 'No dependencies.')
}
else if (latest && Object.keys(latest).length === 0) {
print(options, `No package versions were returned. This is likely a problem with your installed ${options.packageManager}, the npm registry, or your Internet connection. Make sure ${chalk.cyan('npx pacote packument ncu-test-v2')} is working before reporting an issue.`)
}
else if (options.global) {
print(options, `All global packages are up-to-date ${smiley}`)
}
else {
print(options, `All dependencies match the ${target} package versions ${smiley}`)
}
}
else if (numUpgraded === 0 && total > 0) {
print(options, `All dependencies match the desired package versions ${smiley}`)
}
// print table
if (numUpgraded > 0) {
const table = toDependencyTable({
from: current,
to: upgraded,
ownersChangedDeps,
format: options.format,
})
print(options, table.toString())
}
}
/** Print updates that were ignored due to incompatible peer dependencies. */
export function printIgnoredUpdates(options: Options, ignoredUpdates: Index<IgnoredUpgrade>) {
print(options, `\nIgnored incompatible updates (peer dependencies):\n`)
const table = createDependencyTable()
const rows = Object.entries(ignoredUpdates).map(([pkgName, { from, to, reason }]) => {
const strReason = 'reason: ' + Object.entries(reason)
.map(([pkgReason, requirement]) => pkgReason + ' requires ' + requirement)
.join(', ')
return [pkgName, from, '→', colorizeDiff(from, to), strReason]
})
rows.forEach(row => table.push(row)) // eslint-disable-line fp/no-mutating-methods
print(options, table.toString())
}