Skip to content

Commit 9d59d87

Browse files
committed
feat: echo what is being fetched to stderr
1 parent bc85ec5 commit 9d59d87

File tree

5 files changed

+124
-16
lines changed

5 files changed

+124
-16
lines changed

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/npm-fetch-changelog.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import yargs from 'yargs/yargs'
55
import path from 'path'
66
import fs from 'fs-extra'
77
import semver from 'semver'
8-
import { fetchChangelog } from '../index'
8+
import { describeFilter, fetchChangelog } from '../index'
99
import { debug } from '../util/debug'
1010

1111
/* eslint-env node */
@@ -94,13 +94,22 @@ async function go() {
9494
}
9595

9696
try {
97+
const include = {
98+
range,
99+
prerelease: argv.pre,
100+
minor: argv.pre || !argv.major,
101+
patch: argv.pre || (!argv.major && !argv.minor),
102+
}
103+
// eslint-disable-next-line no-console
104+
console.error(
105+
`fetching ${describeFilter({
106+
pkg,
107+
include,
108+
highlight: !process.env.CI && process.stdout.isTTY,
109+
})}...`
110+
)
97111
const changelog = await fetchChangelog(pkg, {
98-
include: {
99-
range,
100-
prerelease: argv.pre,
101-
minor: argv.pre || !argv.major,
102-
patch: argv.pre || (!argv.major && !argv.minor),
103-
},
112+
include,
104113
})
105114
if (json) {
106115
process.stdout.write(JSON.stringify(changelog, null, 2))

src/index.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Octokit } from 'octokit'
77
import memoize from './util/memoize'
88
import once from './util/once'
99
import { debug } from './util/debug'
10+
import chalk from 'chalk'
1011

1112
const getOctokit = once(async (): Promise<Octokit> => {
1213
const { githubToken } = await getConfig()
@@ -91,14 +92,14 @@ function parseRepositoryUrl(url: string): { owner: string; repo: string } {
9192
return { owner, repo: repo.replace(/\.git$/, '') }
9293
}
9394

94-
export type IncludeOption =
95-
| ((version: string) => boolean)
96-
| {
97-
range?: string | null
98-
prerelease?: boolean | null
99-
minor?: boolean | null
100-
patch?: boolean | null
101-
}
95+
export type IncludeOptionObject = {
96+
range?: string | null
97+
prerelease?: boolean | null
98+
minor?: boolean | null
99+
patch?: boolean | null
100+
}
101+
102+
export type IncludeOption = ((version: string) => boolean) | IncludeOptionObject
102103

103104
export function includeFilter(
104105
include?: IncludeOption | null
@@ -115,6 +116,43 @@ export function includeFilter(
115116
}
116117
}
117118

119+
export function describeFilter({
120+
pkg,
121+
include,
122+
highlight,
123+
}: {
124+
pkg?: string
125+
include?: IncludeOption | null
126+
highlight?: boolean
127+
}) {
128+
if (typeof include === 'function') {
129+
return `versions matching custom function`
130+
}
131+
const { range, prerelease, minor, patch } =
132+
include || ({} as IncludeOptionObject)
133+
134+
const types: string[] = [
135+
...(prerelease ? ['prerelease'] : []),
136+
'major',
137+
...(minor === false ? [] : ['minor']),
138+
...(patch === false ? [] : ['patch']),
139+
]
140+
return [
141+
...(range ? [] : [highlight ? chalk.greenBright('all') : 'all']),
142+
types.slice(0, Math.max(1, types.length - 1)).join(', '),
143+
...(types.length > 1 ? [`and ${types[types.length - 1]}`] : []),
144+
'versions',
145+
...(pkg ? [`of ${highlight ? chalk.bold(pkg) : pkg}`] : []),
146+
...(range
147+
? [
148+
/^[<>]/.test(range)
149+
? `${highlight ? chalk.greenBright(range) : range}`
150+
: `satisfying ${highlight ? chalk.greenBright(range) : range}`,
151+
]
152+
: []),
153+
].join(' ')
154+
}
155+
118156
export type Options = {
119157
include?: IncludeOption | null
120158
}

test/describeFilter.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, it } from 'mocha'
2+
import { expect } from 'chai'
3+
import { describeFilter } from '../src'
4+
import chalk from 'chalk'
5+
6+
describe(`describeFilter`, function () {
7+
for (const [input, expected] of [
8+
[{}, 'all major, minor and patch versions'],
9+
[
10+
{ highlight: true },
11+
chalk`{greenBright all} major, minor and patch versions`,
12+
],
13+
[{ pkg: 'foo' }, 'all major, minor and patch versions of foo'],
14+
[
15+
{ pkg: 'foo', highlight: true },
16+
chalk`{greenBright all} major, minor and patch versions of {bold foo}`,
17+
],
18+
[{ include: { range: '>5' } }, 'major, minor and patch versions >5'],
19+
[
20+
{ pkg: 'foo', include: { range: '>5' } },
21+
'major, minor and patch versions of foo >5',
22+
],
23+
[
24+
{ pkg: 'foo', include: { range: '>5' }, highlight: true },
25+
chalk`major, minor and patch versions of {bold foo} {greenBright >5}`,
26+
],
27+
[{ include: { range: '>=5' } }, 'major, minor and patch versions >=5'],
28+
[{ include: { range: '<5' } }, 'major, minor and patch versions <5'],
29+
[{ include: { range: '<=5' } }, 'major, minor and patch versions <=5'],
30+
[
31+
{ include: { range: '^5.3' } },
32+
'major, minor and patch versions satisfying ^5.3',
33+
],
34+
[
35+
{ pkg: 'foo', include: { range: '^5.3' } },
36+
'major, minor and patch versions of foo satisfying ^5.3',
37+
],
38+
[
39+
{ include: { range: '~5.3' } },
40+
'major, minor and patch versions satisfying ~5.3',
41+
],
42+
[{ include: { minor: false } }, 'all major and patch versions'],
43+
[{ include: { patch: false } }, 'all major and minor versions'],
44+
[{ include: { minor: false, patch: false } }, 'all major versions'],
45+
[
46+
{ include: { patch: false, prerelease: true } },
47+
'all prerelease, major and minor versions',
48+
],
49+
] as const) {
50+
it(`${JSON.stringify(input)} -> ${expected}`, function () {
51+
expect(describeFilter(input)).to.equal(expected)
52+
})
53+
}
54+
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "./node_modules/@jcoreio/toolchain-typescript/tsconfig.json",
3-
"include": ["./src"],
3+
"include": ["./src", "./test"],
44
"exclude": ["node_modules"],
55
"compilerOptions": {
66
"lib": ["es2019"]

0 commit comments

Comments
 (0)