/
github-token.ts
40 lines (31 loc) · 1.13 KB
/
github-token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import fs from 'fs';
import path from 'path';
import registryUrl from 'registry-url';
import { promisify } from 'util';
import settingsUrl from './settings-url';
const readFile = promisify(fs.readFile);
const registry = registryUrl();
// tslint:disable-next-line
const normalizedRegistry = registry.replace('http:', '').replace('https:', '');
export default async function getGitHubToken(
apiUrl = 'https://api.github.com'
): Promise<string> {
if (process.env.GH_TOKEN) {
return process.env.GH_TOKEN;
}
const helpText = `Try setting an access token up ${settingsUrl(apiUrl)}`;
if (!process.env.HOME) {
throw new Error(`Can't find the GH_TOKEN and no HOME defined. ${helpText}`);
}
const rcLocation = path.resolve(process.env.HOME, '.npmrc');
if (fs.existsSync(rcLocation)) {
const rc = await readFile(rcLocation, 'utf8');
const regex = new RegExp(`${normalizedRegistry}:_authToken=\(\\w+\)`);
const token = rc.match(regex);
if (token && token[1]) {
return token[1];
}
throw new Error(`No token in the .npmrc. ${helpText}`);
}
throw new Error(`Can't find a GitHub token to use. ${helpText}`);
}