-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
github.ts
69 lines (60 loc) · 2.02 KB
/
github.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import path from 'node:path';
import { OctokitOptions } from '@octokit/core/dist-types/types.d';
import { retry } from '@octokit/plugin-retry';
import { Octokit } from '@octokit/rest';
import debug from 'debug';
const logInfo = debug('electron-forge:publisher:github:info');
const logDebug = debug('electron-forge:publisher:github:debug');
export default class GitHub {
private options: OctokitOptions;
token?: string;
constructor(authToken: string | undefined = undefined, requireAuth = false, options: OctokitOptions = {}) {
const noOp = () => {
/* Intentionally does nothing */
};
this.options = {
...options,
log: {
debug: logDebug.enabled ? logDebug : noOp,
error: console.error,
info: logInfo.enabled ? logInfo : noOp,
warn: console.warn,
},
userAgent: 'Electron Forge',
};
if (authToken) {
this.token = authToken;
} else if (process.env.GITHUB_TOKEN) {
this.token = process.env.GITHUB_TOKEN;
} else if (requireAuth) {
throw new Error('Please set GITHUB_TOKEN in your environment to access these features');
}
}
getGitHub(): Octokit {
const options: OctokitOptions = { ...this.options };
if (this.token) {
options.auth = this.token;
}
const RetryableOctokit = Octokit.plugin(retry);
const github = new RetryableOctokit(options);
return github;
}
// Based on https://github.com/cli/cli/blob/b07f955c23fb54c400b169d39255569e240b324e/pkg/cmd/release/upload/upload.go#L131-L153
static sanitizeName(name: string): string {
return (
path
.basename(name)
// Remove diacritics (e.g. é -> e)
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
// Replace special characters with dot
.replace(/[^\w_.@+-]+/g, '.')
// Replace multiple dots with a single dot
.replace(/\.+/g, '.')
// Remove leading dot if present
.replace(/^\./g, '')
// Remove trailing dot if present
.replace(/\.$/g, '')
);
}
}