-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
157 lines (132 loc) · 3.71 KB
/
utils.ts
File metadata and controls
157 lines (132 loc) · 3.71 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {getInput, InputOptions} from "@actions/core"
import {validate as validUUID} from "uuid"
import {parse} from "semver"
/**
* List of version suffixes, ordered in MOST to LEAST public.
* Demo is accessible to all users.
* Stable is accessible to all purchasers.
* Etc
*/
const VERSIONS = ['demo', 'stable', 'beta', 'alpha', 'private'] as const
type Version = typeof VERSIONS[number]
const VERSION_MAP = new Set<Version>(VERSIONS)
const VERSION_REGEX = /(.*?)-(stable|beta|alpha|private|demo)$/gi
const defaultOptions: InputOptions & ((extra: Partial<InputOptions>) => InputOptions) = function (this: Partial<InputOptions>, extra: Partial<InputOptions>): InputOptions {
return {...this, ...extra}
}
defaultOptions.required = true
defaultOptions.trimWhitespace = true
const optional = defaultOptions({required: false})
export function token(): string {
return getInput("token", defaultOptions)
}
export function product(): string {
let pid = getInput("product", defaultOptions)
if (!validUUID(pid)){
throw "Input 'product' is not a valid UUID."
}
return pid
}
export function version(): string {
return getInput("version", defaultOptions)
}
export function hasType(): boolean {
return getInput("type", optional).toLowerCase() !== ""
}
export function type(): Version {
const type = getInput("type", optional).toLowerCase()
if (type === ""){
return "stable"
} else if (VERSION_MAP.has(<Version>type)) {
return <Version>type
}
throw `Input 'type' must be one of ${[...VERSION_MAP.keys()].join(", ")}, got "${type}"`
}
export function path(): string {
const path = getInput("path", defaultOptions)
if (!path.endsWith(".zip")){
throw "Input path must end in .zip"
}
return path
}
export function changelog(): string {
const log = getInput("changelog", optional)
if (log === ""){
return "No changelog provided."
}
return log
}
export function baseUrl(): URL {
let url = getInput("baseurl", optional)
if (url === ""){
url = "https://api.gmodstore.com/v3/"
}
return new URL(url)
}
export function dry(): boolean {
return getInput("dryrun", optional).toLowerCase() === "true"
}
/**
* Set if we should disable intuiting versions, and instead only use the type input.
*/
export function nointuit(): boolean {
return getInput("nointuit", optional).toLowerCase() === "true"
}
/**
* Get the effective name and version to upload.
* First, attempt to parse as semver.
* If a single, non-numbered, pre-release version is encountered, which is a valid suffix, it is removed and used as the version type.
* Otherwise, use the legacy regex.
* Lastly, fall back to type input.
*/
export function effectiveNameVersion(): string[] {
const intuit = !nointuit()
const raw = version()
if (hasType()){
return [version(), type()]
}
if (intuit){
console.log("Intuiting")
const ver = parse(raw)
console.log(ver)
if (ver !== null){
let last: string | number | null = null
const parsed = new Map<string, number>()
for (const value of ver.prerelease){
if (typeof last === "string"){
if (typeof value === "number"){
parsed.set(last, value)
last = null
} else {
parsed.set(last, 0)
last = null
}
}
if (typeof value === "string"){
last = value
}
}
if (last){
parsed.set(last, 0)
}
console.log(parsed)
for (const suffix of VERSIONS.toReversed()){
if (parsed.has(suffix)){
console.log(`has ${suffix}`)
if (parsed.size !== 1 || parsed.get(suffix) !== 0){
console.log("returning")
return [ver.raw, suffix]
} else {
ver.prerelease = []
return [ver.format(), suffix]
}
}
}
}
const res = VERSION_REGEX.exec(version())
if (res !== null){
return [res[1], res[2]]
}
}
return [version(), type()]
}