Skip to content

Commit

Permalink
fix: return {[version: string]: Release} POJO instead of array
Browse files Browse the repository at this point in the history
BREAKING CHANGE: return an object of version-release pairs instead of an array
  • Loading branch information
jedwards1211 committed May 7, 2019
1 parent 7885998 commit 3af66aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,29 @@ npm i -g what-broke
```

```
what-broke <package> [--full] [<from verison> [<to version>]]
what-broke <package name>
```

Will print out the changelog contents for all major and prerelease versions in
the given range. (If `--full` is given, it will also include minor and patch
versions.)
Prints changelog entries fetched from GitHub for each
version released on npm in the given range.

If `package` is installed in the current working directory, `<from version>`
will default to the installed version.
## Options:

### `-r`, `--range`

semver version range to get changelog entries for, e.g. `^7.0.0` (defaults to `>` the version installed in the working directory, if it exists)

### `--prereleases`

include prerelease versions

### `--no-minor`

exclude minor versions

### `--no-patch`

exclude patch versions (defaults to `--no-minor`)

# Node.js API

Expand All @@ -68,9 +82,12 @@ import whatBroke from 'what-broke'
async function whatBroke(
package: string,
options?: {
fromVersion?: ?string,
toVersion?: ?string,
full?: ?boolean,
include?: ?(((version: string) => boolean) | {
range?: ?string,
prerelease?: ?boolean,
minor?: ?boolean,
patch?: ?boolean,
}),
}
): Promise<Array<{version: string, body: string}>>
```
57 changes: 34 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const octokitOptions: Object = {
if (GH_TOKEN) octokitOptions.auth = `token ${GH_TOKEN}`
const octokit = new Octokit(octokitOptions)

export const getChangelog = memoize(
export const getChangelogFromFile = memoize(
async (owner: string, repo: string): Promise<{ [string]: Release }> => {
let changelog
let lastError: ?Error
Expand Down Expand Up @@ -106,22 +106,22 @@ export type Options = {
export async function fetchChangelog(
pkg: string,
{ include }: Options = {}
): Promise<Object> {
): Promise<{ [version: string]: Release }> {
const npmInfo = await npmRegistryFetch.json(pkg, {
token: await getNpmToken(),
})

const versions = Object.keys(npmInfo.versions).filter(includeFilter(include))

const releases = []
const releases = {}

for (let version of versions) {
const release: Release = {
version,
header: `# ${version}`,
date: new Date(npmInfo.time[version]),
}
releases.push(release)
releases[version] = release

const { url } =
npmInfo.versions[version].repository || npmInfo.repository || {}
Expand Down Expand Up @@ -159,7 +159,7 @@ export async function fetchChangelog(
}
}
} catch (error) {
const changelog = await getChangelog(owner, repo)
const changelog = await getChangelogFromFile(owner, repo)
const { header, body } = changelog[version] || {}
if (header) release.header = header
release.body = body
Expand All @@ -178,26 +178,23 @@ export async function fetchChangelog(
}

if (!module.parent) {
let {
argv: {
_: [pkg],
range,
includePrereleases: prereleases,
minor,
patch,
},
} = require('yargs')
/* eslint-env node */
const { argv } = require('yargs')
.usage(
`Usage: $0 <package name>
Fetches changelog entries for an npm package from GitHub.
Prints changelog entries for an npm package from GitHub.
(Other repository hosts aren't currently supported.)`
)
.option('r', {
alias: 'range',
describe: 'semver version range to get changelog entries for',
type: 'string',
})
.option('json', {
describe: 'output json',
type: 'boolean',
})
.option('prereleases', {
describe: 'include prerelease versions',
type: 'boolean',
Expand All @@ -213,6 +210,15 @@ Fetches changelog entries for an npm package from GitHub.
.describe('no-patch', 'exclude patch versions')
.hide('version')

let {
_: [pkg],
range,
includePrereleases: prerelease,
minor,
patch,
json,
} = argv

if (!pkg) {
require('yargs').showHelp()
process.exit(1)
Expand All @@ -232,21 +238,26 @@ Fetches changelog entries for an npm package from GitHub.
}
}

/* eslint-env node */
fetchChangelog(pkg, {
include: {
range,
prereleases,
prerelease,
minor,
patch,
},
}).then(
(changelog: Array<Release>) => {
for (const { header, body, error } of changelog) {
process.stdout.write(chalk.bold(header) + '\n\n')
if (body) process.stdout.write(body + '\n\n')
if (error) {
process.stdout.write(`Failed to get changelog: ${error.stack}\n\n`)
(changelog: { [version: string]: Release }) => {
if (json) {
process.stdout.write(JSON.stringify(changelog, null, 2))
} else {
for (const version in changelog) {
if (!changelog.hasOwnProperty(version)) continue
const { header, body, error } = changelog[version]
process.stdout.write(chalk.bold(header) + '\n\n')
if (body) process.stdout.write(body + '\n\n')
if (error) {
process.stdout.write(`Failed to get changelog: ${error.stack}\n\n`)
}
}
}
process.exit(0)
Expand Down

0 comments on commit 3af66aa

Please sign in to comment.