Skip to content

Commit

Permalink
feat: support pkg@range option, tokens in ~/.config/npm-fetch-changel…
Browse files Browse the repository at this point in the history
…og.json
  • Loading branch information
jedwards1211 committed Feb 10, 2021
1 parent 58cbe3a commit 8d42ff0
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 149 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ personal access token, `npm-fetch-changelog` will use it when requesting GitHub
the npm token from your `~/.npmrc`, so that it can get information for private
packages you request.

You can also store one or both of these tokesn in `~/.config/npm-fetch-changelog.json`:

```json
{
"githubToken": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"npmToken": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```

# CLI

```
npm i -g npm-fetch-changelog
```

```
npm-fetch-changelog <package name>
npm-fetch-changelog <package name>[@<range>]
```

Prints changelog entries fetched from GitHub for each
Expand All @@ -58,6 +67,12 @@ version released on npm in the given 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)

You can also pass the range together with the package name (you might need to quote it):

```
npm-fetch-changelog 'foo@>=2'
```

### `--json`

output JSON instead of Markdown
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "fetch the changelog for an npm package from GitHub",
"main": "index.js",
"bin": {
"npm-fetch-changelog": "./index.js"
"npm-fetch-changelog": "./cli.js"
},
"engines": {
"node": ">=8.0.0"
Expand Down
104 changes: 104 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env node
// @flow

import chalk from 'chalk'
import yargs from 'yargs'
import path from 'path'
import { fetchChangelog } from './index'
import { type Release } from './parseChangelog'

/* eslint-env node */
const { argv } = yargs
.usage(
`Usage: $0 <package name>[@<range>]
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',
default: false,
})
.default('minor', true)
.boolean('minor')
.hide('minor')
.describe('no-minor', 'exclude minor versions')
.default('patch', undefined)
.boolean('patch')
.hide('patch')
.describe('no-patch', 'exclude patch versions')
.hide('version')

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

if (!pkg) {
yargs.showHelp()
process.exit(1)
}
if (!range) {
const match = /^(@?[^@]+)@(.+)$/.exec(pkg)
if (match) {
pkg = match[1]
range = match[2]
} else {
try {
// $FlowFixMe
const { version } = require(require.resolve(
path.join(pkg, 'package.json'),
{
paths: [process.cwd()],
}
))
range = `>${version}`
} catch (error) {
// ignore
}
}
}

fetchChangelog(pkg, {
include: {
range,
prerelease,
minor,
patch,
},
}).then(
(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)
},
(error: Error) => {
process.stderr.write(error.stack + '\n')
process.exit(1)
}
)
47 changes: 47 additions & 0 deletions src/getConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// @flow

import * as fs from 'fs-extra'
import path from 'path'
import os from 'os'
import once from './util/once'

const configFilePath = path.join(
os.homedir(),
'.config',
'npm-fetch-changelog.json'
)

type Config = {
githubToken?: string,
npmToken?: string,
}

const getConfig = once(async function getConfig(): Promise<Config> {
const result: Config = {}

await Promise.all([
fs
.readJson(configFilePath)
.then((raw: any) => {
if (typeof raw.githubToken === 'string')
result.githubToken = raw.githubToken
if (typeof raw.npmToken === 'string') result.npmToken = raw.npmToken
})
.catch(() => {}),
fs
.readFile(path.join(os.homedir(), '.npmrc'), 'utf8')
.then((npmrc: any) => {
const match = /:_authToken=([a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12})/.exec(
npmrc
)
if (match) result.npmToken = match[1]
})
.catch(() => {}),
])

if (process.env.GH_TOKEN) result.githubToken = process.env.GH_TOKEN
if (process.env.NPM_TOKEN) result.npmToken = process.env.NPM_TOKEN
return result
})

export default getConfig
23 changes: 0 additions & 23 deletions src/getNpmToken.js

This file was deleted.

0 comments on commit 8d42ff0

Please sign in to comment.