├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 21 15 | - 20 16 | - 18 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: actions/setup-node@v4 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - run: npm install 23 | - run: npm test 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | A subset of fields from a `package.json` object needed to extract a repo URL. 3 | 4 | For a more fully-featured type, see [`type-fest`](https://github.com/sindresorhus/type-fest/blob/main/source/package-json.d.ts). 5 | */ 6 | export type PackageJson = { 7 | /** 8 | The name of the package. 9 | */ 10 | name: string; 11 | 12 | /** 13 | The location of the source code repository. 14 | */ 15 | repository?: string | { 16 | url: string; 17 | }; 18 | }; 19 | 20 | /** 21 | Extracts the repo URL from a `package.json` object. 22 | 23 | @example 24 | ``` 25 | import repoUrlFromPackage from 'repo-url-from-package'; 26 | import packageJson from './package.json' with {type: 'json'}; 27 | 28 | const {url} = repoUrlFromPackage(packageJson); 29 | 30 | console.log(url); 31 | //=> 'https://github.com/sindresorhus/repo-url-from-package' 32 | ``` 33 | */ 34 | export default function repoUrlFromPackage(packageJson: PackageJson): { 35 | url?: string; 36 | warnings: string[]; 37 | }; 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import githubUrlFromGit from 'github-url-from-git'; 2 | import isUrl from 'is-url-superb'; 3 | 4 | // Look for GitHub shorthand inputs such as "user/repo#branch". 5 | const githubShorthandRegex = /^(\w(?:-?\w){0,38}\/[\w.-]{1,100})(#\S*)?$/; 6 | 7 | export default function repoUrlFromPackage(packageJson) { 8 | if (!packageJson.repository) { 9 | return {warnings: ['No `repository` field found in package.json.']}; 10 | } 11 | 12 | const warnings = []; 13 | let repoUrl = typeof packageJson.repository === 'string' ? packageJson.repository : packageJson.repository.url; 14 | 15 | if (repoUrl.startsWith('github:')) { 16 | repoUrl = repoUrl.slice(7); 17 | } 18 | 19 | let branch; 20 | const match = repoUrl.match(githubShorthandRegex); 21 | if (match) { 22 | repoUrl = `https://github.com/${match[1]}`; 23 | branch = match[2] ? match[2].slice(1) : undefined; 24 | } 25 | 26 | let url = githubUrlFromGit(repoUrl); 27 | 28 | if (!url) { 29 | url = repoUrl; 30 | 31 | if (isUrl(url) && /^https?:\/\//.test(url)) { 32 | warnings.push(`The \`repository\` field in package.json should point to a Git repo and not a website. Please open an issue or pull request on \`${packageJson.name}\`.`); 33 | } else { 34 | warnings.push(`The \`repository\` field in package.json is invalid. Please open an issue or pull request on \`${packageJson.name}\`.`); 35 | return {warnings}; 36 | } 37 | } 38 | 39 | if (branch) { 40 | url += `/tree/${branch}`; 41 | } 42 | 43 | return {url, warnings}; 44 | } 45 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectError, expectType} from 'tsd'; 2 | import repoUrlFromPackage, {type PackageJson} from './index.js'; 3 | 4 | declare const packageJson: PackageJson; 5 | 6 | expectType(packageJson.name); 7 | expectType(packageJson.repository); 8 | 9 | const {url, warnings} = repoUrlFromPackage(packageJson); 10 | 11 | expectType(url); 12 | expectType(warnings); 13 | 14 | repoUrlFromPackage({ 15 | name: 'my-package', 16 | repository: 'https://github.com/user/repo.git', 17 | }); 18 | 19 | repoUrlFromPackage({ 20 | name: 'my-package', 21 | repository: { 22 | url: 'https://github.com/user/repo.git', 23 | }, 24 | }); 25 | 26 | repoUrlFromPackage({ 27 | name: 'my-package', 28 | repository: { 29 | url: 'https://example.com', 30 | }, 31 | }); 32 | 33 | repoUrlFromPackage({ 34 | name: 'my-package', 35 | repository: { 36 | url: 'invalid-url', 37 | }, 38 | }); 39 | 40 | repoUrlFromPackage({ 41 | name: 'my-package', 42 | }); 43 | 44 | expectError(repoUrlFromPackage({})); 45 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "repo-url-from-package", 3 | "version": "0.2.0", 4 | "description": "Extracts the repo URL from a package.json object", 5 | "license": "MIT", 6 | "repository": "sindresorhus/repo-url-from-package", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "repo", 31 | "repository", 32 | "url", 33 | "package", 34 | "json", 35 | "parse" 36 | ], 37 | "dependencies": { 38 | "github-url-from-git": "^1.5.0", 39 | "is-url-superb": "^6.1.0" 40 | }, 41 | "devDependencies": { 42 | "ava": "^6.1.3", 43 | "tsd": "^0.31.1", 44 | "xo": "^0.59.2" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # repo-url-from-package 2 | 3 | > Extracts the repo URL from a package.json object 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install repo-url-from-package 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import repoUrlFromPackage from 'repo-url-from-package'; 15 | import packageJson from './package.json' with {type: 'json'}; 16 | 17 | const {url} = repoUrlFromPackage(packageJson); 18 | 19 | console.log(url); 20 | //=> 'https://github.com/sindresorhus/repo-url-from-package' 21 | ``` 22 | 23 | ## API 24 | 25 | ### repoUrlFromPackage(packageJson) 26 | 27 | Returns an object with the possible parsed `url` and an array of any `warnings`. 28 | 29 | #### packageJson 30 | 31 | Type: `object` 32 | 33 | A `package.json` object. 34 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import repoUrlFromPackage from './index.js'; 3 | 4 | const verifyResult = test.macro(async (t, {packageJson, expectations}) => { 5 | const result = repoUrlFromPackage(packageJson); 6 | 7 | const assertions = await t.try(tt => { 8 | tt.log('expectations:', expectations); 9 | tt.like(result, expectations); 10 | }); 11 | 12 | assertions.commit({retainLogs: !assertions.passed}); 13 | }); 14 | 15 | test('should return repo URL from repository field', verifyResult, { 16 | packageJson: { 17 | name: 'my-package', 18 | repository: 'https://github.com/user/repo.git', 19 | }, 20 | expectations: { 21 | url: 'https://github.com/user/repo', 22 | warnings: [], 23 | }, 24 | }); 25 | 26 | test('should return repo URL from repository.url field', verifyResult, { 27 | packageJson: { 28 | name: 'my-package', 29 | repository: { 30 | url: 'https://github.com/user/repo.git', 31 | }, 32 | }, 33 | expectations: { 34 | url: 'https://github.com/user/repo', 35 | warnings: [], 36 | }, 37 | }); 38 | 39 | test('should return warning if repository field is a website', verifyResult, { 40 | packageJson: { 41 | name: 'my-package', 42 | repository: { 43 | url: 'https://example.com', 44 | }, 45 | }, 46 | expectations: { 47 | url: 'https://example.com', 48 | warnings: [ 49 | 'The `repository` field in package.json should point to a Git repo and not a website. Please open an issue or pull request on `my-package`.', 50 | ], 51 | }, 52 | }); 53 | 54 | test('should return warning if repository field is invalid', verifyResult, { 55 | packageJson: { 56 | name: 'my-package', 57 | repository: { 58 | url: 'invalid-url', 59 | }, 60 | homepage: 'https://example.com', 61 | }, 62 | expectations: { 63 | url: undefined, 64 | warnings: [ 65 | 'The `repository` field in package.json is invalid. Please open an issue or pull request on `my-package`.', 66 | ], 67 | }, 68 | }); 69 | 70 | test('should return warning if repository field is missing', verifyResult, { 71 | packageJson: { 72 | name: 'my-package', 73 | }, 74 | expectations: { 75 | url: undefined, 76 | warnings: [ 77 | 'No `repository` field found in package.json.', 78 | ], 79 | }, 80 | }); 81 | 82 | test('should support user/repo format', verifyResult, { 83 | packageJson: { 84 | name: 'my-package', 85 | repository: 'user/repo', 86 | }, 87 | expectations: { 88 | url: 'https://github.com/user/repo', 89 | warnings: [], 90 | }, 91 | }); 92 | 93 | test('should support github:user/repo format', verifyResult, { 94 | packageJson: { 95 | name: 'my-package', 96 | repository: 'github:user/repo', 97 | }, 98 | expectations: { 99 | url: 'https://github.com/user/repo', 100 | warnings: [], 101 | }, 102 | }); 103 | 104 | test('should support user/repo#branch format', verifyResult, { 105 | packageJson: { 106 | name: 'my-package', 107 | repository: 'user/repo#branch', 108 | }, 109 | expectations: { 110 | url: 'https://github.com/user/repo/tree/branch', 111 | warnings: [], 112 | }, 113 | }); 114 | --------------------------------------------------------------------------------