-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add [npm] type definitions badge (#1541)
Let a package show off its type definitions based on devDependency data in the published npm package. Close #1252
- Loading branch information
1 parent
2d65153
commit 6750115
Showing
7 changed files
with
185 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,46 @@ | ||
'use strict'; | ||
|
||
const { rangeStart, minor } = require('./version'); | ||
|
||
const defaultNpmRegistryUri = 'https://registry.npmjs.org'; | ||
|
||
function makePackageDataUrl({ registryUrl, scope, packageName }) { | ||
registryUrl = registryUrl || defaultNpmRegistryUri; | ||
if (scope === undefined) { | ||
// e.g. https://registry.npmjs.org/express/latest | ||
// Use this endpoint as an optimization. It covers the vast majority of | ||
// these badges, and the response is smaller. | ||
return `${registryUrl}/${packageName}/latest`; | ||
} else { | ||
// e.g. https://registry.npmjs.org/@cedx%2Fgulp-david | ||
// because https://registry.npmjs.org/@cedx%2Fgulp-david/latest does not work | ||
const path = encodeURIComponent(`${scope}/${packageName}`); | ||
return `${registryUrl}/@${path}`; | ||
} | ||
} | ||
|
||
function typeDefinitions(packageData) { | ||
const { devDependencies } = packageData; | ||
|
||
const supportedLanguages = [ | ||
{ name: 'TypeScript', range: devDependencies.typescript }, | ||
{ name: 'Flow', range: devDependencies['flow-bin'] }, | ||
] | ||
.filter(lang => lang.range !== undefined) | ||
.map(({ name, range }) => { | ||
const version = minor(rangeStart(range)); | ||
return `${name} v${version}`; | ||
}); | ||
|
||
if (supportedLanguages.length > 0) { | ||
return supportedLanguages.join(' | '); | ||
} else { | ||
return 'none'; | ||
} | ||
} | ||
|
||
module.exports = { | ||
defaultNpmRegistryUri, | ||
makePackageDataUrl, | ||
typeDefinitions, | ||
}; |
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,10 @@ | ||
'use strict'; | ||
|
||
const { test, given } = require('sazerac'); | ||
const { typeDefinitions } = require('./npm-badge-helpers'); | ||
|
||
describe('NPM badge helpers', function () { | ||
test(typeDefinitions, () => { | ||
given({ devDependencies: { typescript: '^2.4.7' } }).expect('TypeScript v2.4'); | ||
}); | ||
}); |
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
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