Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit bb3e9e2

Browse files
committed
Add code
1 parent 53b8c50 commit bb3e9e2

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
# typescript-project-template
2-
Template repo for new TypeScript projects.
1+
# @pythoncoderas/get-github-token
32

4-
This repo assumes the following:
3+
Get a github token from multiple locations.
54

6-
* Source code lives in the `src` directory.
7-
* The main export is located in `src/index.ts`.
8-
* Tests live in the `test` directory.
9-
* Each test is name `<name>.test.ts`.
10-
* The `npm_token`, `app_id`, and `app_private_key` secrets are present in the repo.
11-
* The code will be a CommonJS module.
5+
## API
6+
7+
### `getToken(tokenInput?: string | null, environmentVariableName = "GITHUB_TOKEN"): string | null`
8+
9+
Gets a token from the environment or a command line argument.
10+
11+
**Parameters**
12+
13+
* `tokenInput?: string | null` - The token input from the command line.
14+
* `environmentVariableName: string"` - The environment variable name to check for a token. Defaults to `GITHUB_TOKEN`.
15+
16+
**Returns**: `string | null` - The token if found, otherwise returns `null`.

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
"name": "<package-name>",
2+
"name": "@pythoncoderas/get-github-token",
3+
"private": false,
34
"version": "1.0.0",
4-
"description": "<package-description>",
5+
"description": "Get a github token from multiple locations.",
56
"main": "dist/index.js",
67
"type": "commonjs",
78
"types": "dist/index.d.ts",
@@ -13,16 +14,17 @@
1314
},
1415
"repository": {
1516
"type": "git",
16-
"url": "git+https://github.com/PythonCoderAS/<package-name>.git"
17+
"url": "git+https://github.com/PythonCoderAS/get-github-token.git"
1718
},
1819
"keywords": [],
1920
"author": "PythonCoderAS",
2021
"license": "MIT",
2122
"bugs": {
22-
"url": "https://github.com/PythonCoderAS/<package-name>/issues"
23+
"url": "https://github.com/PythonCoderAS/get-github-token/issues"
2324
},
24-
"homepage": "https://github.com/PythonCoderAS/<package-name>#readme",
25+
"homepage": "https://github.com/PythonCoderAS/get-github-token#readme",
2526
"dependencies": {
27+
"dotenv": "^16.0.1"
2628
},
2729
"devDependencies": {
2830
"@types/chai": "^4.3.1",

src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "dotenv/config";
2+
3+
/**
4+
* Gets a token from the environment or a command line argument.
5+
* @param tokenInput The value of the command line argument, if any.
6+
* @param environmentVariableName The name of the environment variable to check.
7+
* @returns The token.
8+
*/
9+
export default function getToken(tokenInput?: string | null, environmentVariableName = "GITHUB_TOKEN"): string | null {
10+
if (tokenInput) {
11+
return tokenInput;
12+
}
13+
14+
if (process.env[environmentVariableName]) {
15+
return process.env[environmentVariableName] || null;
16+
}
17+
18+
return null;
19+
}

0 commit comments

Comments
 (0)