@rowanmanning/package-json-github 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -0
- package/index.d.ts +30 -0
- package/index.js +4 -0
- package/lib/github.js +57 -0
- package/lib/package-json.js +16 -0
- package/lib/package-lock-json.js +16 -0
- package/package.json +23 -0
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
|
|
2
|
+
# @rowanmanning/package-json-github
|
|
3
|
+
|
|
4
|
+
Load and parse `package.json` and `package-lock.json` files from GitHub repositories.
|
|
5
|
+
|
|
6
|
+
* [Requirements](#requirements)
|
|
7
|
+
* [Usage](#usage)
|
|
8
|
+
* [`packageJson.fromGitHub()`](#packagejsonfromgithub)
|
|
9
|
+
* [`packageLock.fromGitHub()`](#packagelockfromgithub)
|
|
10
|
+
* [Options](#options)
|
|
11
|
+
* [Contributing](#contributing)
|
|
12
|
+
* [License](#license)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
This library requires the following to run:
|
|
18
|
+
|
|
19
|
+
* [Node.js](https://nodejs.org/) 18+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Install with [npm](https://www.npmjs.com/):
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @rowanmanning/package-json-github
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Load into your code with `import` or `require`:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import { packageJson, packageLock } from '@rowanmanning/package-json-github';
|
|
34
|
+
// or
|
|
35
|
+
const { packageJson, packageLock } = require('@rowanmanning/package-json-github');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The following exports are available.
|
|
39
|
+
|
|
40
|
+
### `packageJson.fromGitHub()`
|
|
41
|
+
|
|
42
|
+
Load the `package.json` file present in a GitHub repo.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
(options: GitHubOptions) => Promise<PackageJson>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The `options` argument must be an object. [The available options are documented here](#options).
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const pkg = await packageJson.fromGitHub({
|
|
52
|
+
auth: process.env.GITHUB_AUTH_TOKEN,
|
|
53
|
+
owner: 'rowanmanning',
|
|
54
|
+
repo: 'yeehaw'
|
|
55
|
+
});
|
|
56
|
+
// { name: '@rowanmanning/yeehaw', version: '2.0.0', ... }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `packageLock.fromGitHub()`
|
|
60
|
+
|
|
61
|
+
Load the `package-lock.json` file present in a GitHub repo.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
(options: GitHubOptions) => Promise<PackageLock>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The `options` argument must be an object. [The available options are documented here](#options).
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
const pkg = await packageLock.fromGitHub({
|
|
71
|
+
auth: process.env.GITHUB_AUTH_TOKEN,
|
|
72
|
+
owner: 'rowanmanning',
|
|
73
|
+
repo: 'yeehaw'
|
|
74
|
+
});
|
|
75
|
+
// { lockfileVersion: 2, name: '@rowanmanning/yeehaw', version: '2.0.0', ... }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Options
|
|
79
|
+
|
|
80
|
+
The `options` argument must be an object. It's required and has the following properties. These are passed directly to the GitHub REST API, please [consult the API documentation for more information](https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#get-repository-content).
|
|
81
|
+
|
|
82
|
+
* `auth` (`string`, required). A [GitHub token](https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api) with read access to the repo you specify
|
|
83
|
+
|
|
84
|
+
* `owner` (`string`, required). The name of the GitHub user or organisation that the repo belongs to
|
|
85
|
+
|
|
86
|
+
* `repo` (`string`, required). The name of the GitHub repo
|
|
87
|
+
|
|
88
|
+
* `path` (`string`, optional). The path to the file you'd like to read. Defaults to `package.json` or `package-lock.json` depending on the method you're using
|
|
89
|
+
|
|
90
|
+
* `ref` (`string`, optional). The name of the commit, branch, or tag to fetch the file from. Defaults to the repo's default branch
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
## Contributing
|
|
94
|
+
|
|
95
|
+
[See the central README for a contribution guide and code of conduct](https://github.com/rowanmanning/repo-tools#contributing).
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
Licensed under the [MIT](https://github.com/rowanmanning/repo-tools/blob/main/LICENSE) license.<br/>
|
|
101
|
+
Copyright © 2024, Rowan Manning
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { AnyPackageLock, PackageJson } from '@rowanmanning/package-json';
|
|
2
|
+
|
|
3
|
+
declare module '@rowanmanning/package-json-github' {
|
|
4
|
+
export type {
|
|
5
|
+
PackageJson,
|
|
6
|
+
PackageLockV1,
|
|
7
|
+
PackageLockV2,
|
|
8
|
+
PackageLockV3,
|
|
9
|
+
AnyPackageLock
|
|
10
|
+
} from '@rowanmanning/package-json';
|
|
11
|
+
|
|
12
|
+
export interface GitHubOptions {
|
|
13
|
+
auth: string;
|
|
14
|
+
owner: string;
|
|
15
|
+
repo: string;
|
|
16
|
+
path?: string | undefined;
|
|
17
|
+
ref?: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PackageJsonMethods {
|
|
21
|
+
fromGitHubRepo(options: GitHubOptions): Promise<PackageJson>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface PackageLockMethods {
|
|
25
|
+
fromGitHubRepo(options: GitHubOptions): Promise<AnyPackageLock>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const packageJson: PackageJsonMethods;
|
|
29
|
+
export const packageLock: PackageLockMethods;
|
|
30
|
+
}
|
package/index.js
ADDED
package/lib/github.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { name, version, homepage } = require('../package.json');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { GitHubOptions } from '@rowanmanning/package-json-github'
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const userAgent = `${name}/${version} (${homepage})`;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {GitHubOptions & { path: string }} options
|
|
13
|
+
* @returns {Promise<string>}
|
|
14
|
+
*/
|
|
15
|
+
exports.getRepoContent = async function getRepoContent(options) {
|
|
16
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
17
|
+
throw new TypeError('Invalid argument: options must be an object');
|
|
18
|
+
}
|
|
19
|
+
const { auth, owner, repo, path, ref } = options;
|
|
20
|
+
if (typeof auth !== 'string') {
|
|
21
|
+
throw new TypeError('Invalid argument: options.auth must be a string');
|
|
22
|
+
}
|
|
23
|
+
if (typeof owner !== 'string') {
|
|
24
|
+
throw new TypeError('Invalid argument: options.owner must be a string');
|
|
25
|
+
}
|
|
26
|
+
if (typeof repo !== 'string') {
|
|
27
|
+
throw new TypeError('Invalid argument: options.repo must be a string');
|
|
28
|
+
}
|
|
29
|
+
if (typeof path !== 'string') {
|
|
30
|
+
throw new TypeError('Invalid argument: options.path must be a string');
|
|
31
|
+
}
|
|
32
|
+
if (ref !== undefined && typeof ref !== 'string') {
|
|
33
|
+
throw new TypeError('Invalid argument: options.ref must be a string or undefined');
|
|
34
|
+
}
|
|
35
|
+
const endpoint = new URL(`https://api.github.com/repos/${owner}/${repo}/contents/${path}`);
|
|
36
|
+
if (ref) {
|
|
37
|
+
endpoint.searchParams.set('ref', ref);
|
|
38
|
+
}
|
|
39
|
+
const headers = {
|
|
40
|
+
accept: 'application/vnd.github.raw+json',
|
|
41
|
+
authorization: `Bearer ${auth}`,
|
|
42
|
+
'x-github-api-version': '2022-11-28',
|
|
43
|
+
'user-agent': userAgent
|
|
44
|
+
};
|
|
45
|
+
const response = await fetch(endpoint.toString(), { headers });
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw Object.assign(
|
|
48
|
+
new Error(`GitHub API responded with a ${response.status} status code`),
|
|
49
|
+
{
|
|
50
|
+
code: 'GITHUB_API_ERROR',
|
|
51
|
+
status: response.status,
|
|
52
|
+
statusCode: response.status
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return await response.text();
|
|
57
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { getRepoContent } = require('./github');
|
|
4
|
+
const {
|
|
5
|
+
packageJson: { fromString }
|
|
6
|
+
} = require('@rowanmanning/package-json');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { packageJson } from '@rowanmanning/package-json-github'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** @type {packageJson['fromGitHubRepo']} */
|
|
13
|
+
exports.fromGitHubRepo = async function fromGitHubRepo(options) {
|
|
14
|
+
const optionsWithPath = Object.assign({ path: 'package.json' }, options);
|
|
15
|
+
return fromString(await getRepoContent(optionsWithPath));
|
|
16
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { getRepoContent } = require('./github');
|
|
4
|
+
const {
|
|
5
|
+
packageLock: { fromString }
|
|
6
|
+
} = require('@rowanmanning/package-json');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { packageLock } from '@rowanmanning/package-json-github'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** @type {packageLock['fromGitHubRepo']} */
|
|
13
|
+
exports.fromGitHubRepo = async function fromGitHubRepo(options) {
|
|
14
|
+
const optionsWithPath = Object.assign({ path: 'package-lock.json' }, options);
|
|
15
|
+
return fromString(await getRepoContent(optionsWithPath));
|
|
16
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rowanmanning/package-json-github",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Load and parse package.json and package-lock.json files from GitHub repositories",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "Rowan Manning (https://rowanmanning.com/)",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/rowanmanning/repo-tools.git",
|
|
10
|
+
"directory": "packages/package-json-github"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/rowanmanning/repo-tools/tree/main/packages/package-json-github#readme",
|
|
13
|
+
"bugs": "https://github.com/rowanmanning/repo-tools/issues",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": "18.x || 20.x || 22.x"
|
|
17
|
+
},
|
|
18
|
+
"main": "index.js",
|
|
19
|
+
"types": "index.d.ts",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rowanmanning/package-json": "^1.0.0"
|
|
22
|
+
}
|
|
23
|
+
}
|