Skip to content

Commit 7a889b4

Browse files
committed
--format dep
#166 (comment)
1 parent 8f04424 commit 7a889b4

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Options that take no arguments can be negated by prefixing them with `--no-`, e.
262262
</tr>
263263
<tr>
264264
<td><a href="#format">--format &lt;value&gt;</a></td>
265-
<td>Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines, installedVersion. (default: [])</td>
265+
<td>Modify the output formatting or show additional information. Specify one or more comma-delimited values: dep, group, ownerChanged, repo, time, lines, installedVersion. (default: [])</td>
266266
</tr>
267267
<tr>
268268
<td>-g, --global</td>
@@ -634,12 +634,13 @@ Usage:
634634
Modify the output formatting or show additional information. Specify one or more comma-delimited values.
635635
636636
<table>
637+
<tr><td>dep</td><td>Prints the dependency type (dev, peer, optional) of each package.</td></tr>
637638
<tr><td>group</td><td>Groups packages by major, minor, patch, and major version zero updates.</td></tr>
639+
<tr><td>installedVersion</td><td>Prints the exact current version number instead of a range.</td></tr>
640+
<tr><td>lines</td><td>Prints name@version on separate lines. Useful for piping to npm install.</td></tr>
638641
<tr><td>ownerChanged</td><td>Shows if the package owner has changed.</td></tr>
639642
<tr><td>repo</td><td>Infers and displays links to the package's source code repository. Requires packages to be installed.</td></tr>
640643
<tr><td>time</td><td>Shows the publish time of each upgrade.</td></tr>
641-
<tr><td>lines</td><td>Prints name@version on separate lines. Useful for piping to npm install.</td></tr>
642-
<tr><td>installedVersion</td><td>Prints the exact current version number instead of a range.</td></tr>
643644
</table>
644645
645646
## groupFunction

src/cli-options.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,13 @@ const extendedHelpFormat: ExtendedHelp = ({ markdown }) => {
171171
colAligns: ['right', 'left'],
172172
markdown,
173173
rows: [
174+
['dep', `Prints the dependency type (dev, peer, optional) of each package.`],
174175
['group', `Groups packages by major, minor, patch, and major version zero updates.`],
176+
['installedVersion', 'Prints the exact current version number instead of a range.'],
177+
['lines', 'Prints name@version on separate lines. Useful for piping to npm install.'],
175178
['ownerChanged', `Shows if the package owner has changed.`],
176179
['repo', `Infers and displays links to the package's source code repository. Requires packages to be installed.`],
177180
['time', 'Shows the publish time of each upgrade.'],
178-
['lines', 'Prints name@version on separate lines. Useful for piping to npm install.'],
179-
['installedVersion', 'Prints the exact current version number instead of a range.'],
180181
],
181182
})
182183

@@ -759,11 +760,11 @@ const cliOptions: CLIOption[] = [
759760
long: 'format',
760761
arg: 'value',
761762
description:
762-
'Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines, installedVersion.',
763+
'Modify the output formatting or show additional information. Specify one or more comma-delimited values: dep, group, ownerChanged, repo, time, lines, installedVersion.',
763764
parse: value => (typeof value === 'string' ? value.split(',') : value),
764765
default: [],
765766
type: 'readonly string[]',
766-
choices: ['group', 'ownerChanged', 'repo', 'time', 'lines', 'installedVersion'],
767+
choices: ['dep', 'group', 'ownerChanged', 'repo', 'time', 'lines', 'installedVersion'],
767768
help: extendedHelpFormat,
768769
},
769770
{

src/lib/logging.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Loggin functions.
33
*/
44
import Table from 'cli-table3'
5+
import fs from 'fs/promises'
56
import { IgnoredUpgradeDueToEnginesNode } from '../types/IgnoredUpgradeDueToEnginesNode'
67
import { IgnoredUpgradeDueToPeerDeps } from '../types/IgnoredUpgradeDueToPeerDeps'
78
import { Index } from '../types/IndexType'
@@ -164,6 +165,7 @@ export async function toDependencyTable({
164165
pkgFile?: string
165166
time?: Index<string>
166167
}) {
168+
const pkg = format?.includes('dep') && pkgFile ? JSON.parse(await fs.readFile(pkgFile, 'utf-8')) : null
167169
const table = renderDependencyTable(
168170
await Promise.all(
169171
Object.keys(toDeps)
@@ -173,6 +175,14 @@ export async function toDependencyTable({
173175
(format?.includes('installedVersion')
174176
? await getPackageVersion(dep, undefined, { pkgFile })
175177
: fromDeps[dep]) || ''
178+
const depType =
179+
dep in (pkg?.devDependencies ?? {})
180+
? 'dev'
181+
: dep in (pkg?.peerDependencies ?? {})
182+
? 'peer'
183+
: dep in (pkg?.optionalDependencies ?? {})
184+
? 'optional'
185+
: ''
176186
const toRaw = toDeps[dep] || ''
177187
const to = getVersion(toRaw)
178188
const ownerChanged = ownersChangedDeps
@@ -185,7 +195,15 @@ export async function toDependencyTable({
185195
const toColorized = colorizeDiff(getVersion(from), to)
186196
const repoUrl = format?.includes('repo') ? (await getRepoUrl(dep, undefined, { pkgFile })) || '' : ''
187197
const publishTime = format?.includes('time') && time?.[dep] ? time[dep] : ''
188-
return [dep, from, '→', toColorized, ownerChanged, ...[repoUrl, publishTime].filter(x => x)]
198+
return [
199+
dep,
200+
...(format?.includes('dep') ? [depType ? chalk.gray(depType) : ''] : []),
201+
from,
202+
'→',
203+
toColorized,
204+
ownerChanged,
205+
...[repoUrl, publishTime].filter(x => x),
206+
]
189207
}),
190208
),
191209
)

src/types/RunOptions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
"description": "Filter on package version using comma-or-space-delimited list, /regex/, or predicate function. Run \"ncu --help --filterVersion\" for details."
303303
},
304304
"format": {
305-
"description": "Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines, installedVersion. Run \"ncu --help --format\" for details.",
305+
"description": "Modify the output formatting or show additional information. Specify one or more comma-delimited values: dep, group, ownerChanged, repo, time, lines, installedVersion. Run \"ncu --help --format\" for details.",
306306
"items": {
307307
"type": "string"
308308
},

src/types/RunOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export interface RunOptions {
8989
/** Filter on package version using comma-or-space-delimited list, /regex/, or predicate function. Run "ncu --help --filterVersion" for details. */
9090
filterVersion?: string | RegExp | readonly (string | RegExp)[] | FilterFunction
9191

92-
/** Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines, installedVersion. Run "ncu --help --format" for details. */
92+
/** Modify the output formatting or show additional information. Specify one or more comma-delimited values: dep, group, ownerChanged, repo, time, lines, installedVersion. Run "ncu --help --format" for details. */
9393
format?: readonly string[]
9494

9595
/** Check global packages instead of in the current project. */

test/format.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,54 @@ chaiSetup()
2929
const bin = path.join(__dirname, '../build/cli.js')
3030

3131
describe('format', () => {
32+
it('--format dep', async () => {
33+
const stub = stubVersions(
34+
{
35+
'ncu-test-v2': '2.0.0',
36+
'ncu-test-tag': '2.0.0',
37+
'ncu-test-peer-update': '2.0.0',
38+
'ncu-test-return-version': '2.0.0',
39+
},
40+
{ spawn: true },
41+
)
42+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
43+
const pkgFile = path.join(tempDir, 'package.json')
44+
await fs.writeFile(
45+
pkgFile,
46+
JSON.stringify({
47+
dependencies: {
48+
'ncu-test-v2': '^1.0.0',
49+
},
50+
devDependencies: {
51+
'ncu-test-tag': '^1.0.0',
52+
},
53+
peerDependencies: {
54+
'ncu-test-peer-update': '^1.0.0',
55+
},
56+
optionalDependencies: {
57+
'ncu-test-return-version': '^1.0.0',
58+
},
59+
}),
60+
'utf-8',
61+
)
62+
try {
63+
const { stdout } = await spawn(
64+
'node',
65+
// -u was added to avoid accidentally matching dev, peer, optional from "Run ncu --dep prod,dev,peer,optional --format dep -u to upgrade package.json"
66+
[bin, '--dep', 'prod,dev,peer,optional', '--format', 'dep', '-u'],
67+
{},
68+
{ cwd: tempDir },
69+
)
70+
71+
stdout.should.include('dev')
72+
stdout.should.include('peer')
73+
stdout.should.include('optional')
74+
} finally {
75+
await fs.rm(tempDir, { recursive: true, force: true })
76+
stub.restore()
77+
}
78+
})
79+
3280
// do not stubVersions here, because we need to test if if time is parsed correctly from npm-registry-fetch
3381
it('--format time', async () => {
3482
const timestamp = '2020-04-27T21:48:11.660Z'

0 commit comments

Comments
 (0)