Skip to content

Commit

Permalink
options.format: lines (output packages line by line) (#1283)
Browse files Browse the repository at this point in the history
Co-authored-by: Raine Revere <raine@cybersemics.org>
  • Loading branch information
vanodevium and raineorshine committed Mar 22, 2023
1 parent d3db96f commit 4d825c5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ ncu "/^(?!react-).*$/" # windows
predicate function.
--format <value> Modify the output formatting or show additional
information. Specify one or more comma-delimited
values: group, ownerChanged, repo. Run "ncu --help
--format" for details. (default: [])
values: group, ownerChanged, repo, time, lines.
Run "ncu --help --format" for details. (default:
[])
-g, --global Check global packages instead of in the current
project.
--groupFunction <fn> Customize how packages are divided into groups
Expand Down Expand Up @@ -337,6 +338,8 @@ Modify the output formatting or show additional information. Specify one or more
│ │ installed. │
├──────────────┼────────────────────────────────────────────────────────────────────────────────────────────┤
│ time │ Shows the publish time of each upgrade. │
├──────────────┼────────────────────────────────────────────────────────────────────────────────────────────┤
│ lines │ Prints name@version on separate lines. Useful for piping to npm install. │
└──────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘

## groupFunction
Expand Down
5 changes: 3 additions & 2 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const extendedHelpFormat = (): string => {
['ownerChanged', `Shows if the package owner has changed.`],
['repo', `Infers and displays links to the package's source code repository. Requires packages to be installed.`],
['time', 'Shows the publish time of each upgrade.'],
['lines', 'Prints name@version on separate lines. Useful for piping to npm install.'],
]),
)

Expand Down Expand Up @@ -434,11 +435,11 @@ const cliOptions: CLIOption[] = [
long: 'format',
arg: 'value',
description:
'Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo.',
'Modify the output formatting or show additional information. Specify one or more comma-delimited values: group, ownerChanged, repo, time, lines.',
parse: value => (typeof value === 'string' ? value.split(',') : value),
default: [],
type: 'string[]',
choices: ['group', 'ownerChanged', 'repo', 'time'],
choices: ['group', 'ownerChanged', 'repo', 'time', 'lines'],
help: extendedHelpFormat,
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =
}

// consolidate loglevel
const loglevel = options.silent ? 'silent' : options.verbose ? 'verbose' : options.loglevel
const loglevel =
options.silent || options.format?.includes('lines') ? 'silent' : options.verbose ? 'verbose' : options.loglevel

const json = Object.keys(options)
.filter(option => option.startsWith('json'))
Expand Down
33 changes: 23 additions & 10 deletions src/lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export function printJson(options: Options, object: any) {
}
}

/** Print JSON object keys as string joined by character. */
export function printSimpleJoinedString(object: any, join: string) {
console.log(
Object.keys(object)
.map(pkg => pkg + '@' + object[pkg])
.join(join),
)
}

/** Create a table with the appropriate columns and alignment to render dependency upgrades. */
function renderDependencyTable(rows: string[][]) {
const table = new Table({
Expand Down Expand Up @@ -207,16 +216,20 @@ export async function printUpgradesTable(
)
}
} else {
print(
options,
await toDependencyTable({
from: current,
to: upgraded,
ownersChangedDeps,
time,
format: options.format,
}),
)
if (options.format?.includes('lines')) {
printSimpleJoinedString(upgraded, '\n')
} else {
print(
options,
await toDependencyTable({
from: current,
to: upgraded,
ownersChangedDeps,
time,
format: options.format,
}),
)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface RunOptions {
/** Filter on package version using comma-or-space-delimited list, /regex/, or predicate function. */
filterVersion?: string | string[] | RegExp | RegExp[] | FilterFunction

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

/** Check global packages instead of in the current project. */
Expand Down
30 changes: 30 additions & 0 deletions test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,34 @@ describe('format', () => {
await fs.rm(tempDir, { recursive: true, force: true })
}
})

it('--format lines', async () => {
const stub = stubNpmView(
{
'ncu-test-v2': '2.0.0',
'ncu-test-tag': '1.1.0',
},
{ spawn: true },
)
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'package.json')
await fs.writeFile(
pkgFile,
JSON.stringify({
dependencies: {
'ncu-test-v2': '^1.0.0',
'ncu-test-tag': '^1.0.0',
},
}),
'utf-8',
)
try {
await spawn('npm', ['install'], { cwd: tempDir })
const output = await spawn('node', [bin, '--format', 'lines'], { cwd: tempDir })
output.should.equals('ncu-test-v2@^2.0.0\nncu-test-tag@^1.1.0\n')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
stub.restore()
}
})
})

0 comments on commit 4d825c5

Please sign in to comment.