-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support pkg@range option, tokens in ~/.config/npm-fetch-changel…
…og.json
- Loading branch information
1 parent
58cbe3a
commit 8d42ff0
Showing
6 changed files
with
210 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.