Skip to content

Commit 8989956

Browse files
committed
cleanup options
1 parent a1ce402 commit 8989956

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

src/cli/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import chalk from 'chalk'
2-
import CurseforgeService from '../curseforge.js'
3-
import WebService from '../web.js'
2+
import CurseforgeService, { CurseforgeOptions } from '../curseforge.js'
3+
import WebService, { WebOptions } from '../web.js'
44
import parseCliOptions, { Action, ReleaseOptions } from './options.js'
55

6-
function validateRelease(options: Partial<ReleaseOptions>): asserts options is ReleaseOptions {
6+
function validateRelease<T>(options: T & Partial<ReleaseOptions>): asserts options is T & ReleaseOptions {
77
if (!options.version) throw new Error('Version missing')
88
if (!options.changelog) throw new Error('Changelog missing')
99
if (!options.releaseType) throw new Error('Release-Type missing')
1010
}
1111

12+
function validateWebOptions<T>(options: T & Partial<WebOptions>): asserts options is T & WebOptions {
13+
if (!options.webToken) throw new Error('Web Token missing')
14+
}
15+
16+
function validateCurseforgeOptions<T>(
17+
options: T & Partial<CurseforgeOptions>
18+
): asserts options is T & CurseforgeOptions {
19+
if (!options.curseforgeProject) throw new Error('CurseForge Project-ID missing')
20+
if (!options.curseforgeToken) throw new Error('CurseForge Token missing')
21+
if (!options.paths) throw new Error('Pack path are missing')
22+
}
23+
1224
async function run() {
1325
const { params, ...options } = parseCliOptions()
1426

@@ -17,6 +29,7 @@ async function run() {
1729
}
1830

1931
if (params.includes(Action.WEB)) {
32+
validateWebOptions(options)
2033
const web = new WebService(options)
2134
await web.updateWeb()
2235

@@ -27,6 +40,7 @@ async function run() {
2740
}
2841

2942
if (params.includes(Action.CURSEFORGE)) {
43+
validateCurseforgeOptions(options)
3044
const curseforge = new CurseforgeService(options)
3145
validateRelease(options)
3246

src/cli/options.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import arg from 'arg'
22
import commandLineUsage, { OptionDefinition, Section } from 'command-line-usage'
33
import { CurseforgeOptions } from '../curseforge.js'
4-
import { defaultApiUrl, readPackData, WebOptions } from '../web.js'
4+
import { defaultApiUrl, defaultWebDir, readPackData, WebOptions } from '../web.js'
55

6-
const defaultPaths = ['config', 'mods', 'kubejs', 'defaultconfigs']
6+
export const defaultPaths = ['config', 'mods', 'kubejs', 'defaultconfigs']
77

88
const optionDefinitions: OptionDefinition[] = [
99
{
@@ -18,7 +18,7 @@ const optionDefinitions: OptionDefinition[] = [
1818
},
1919
{
2020
name: 'web-dir',
21-
defaultValue: './web',
21+
defaultValue: defaultWebDir,
2222
typeLabel: '{underline file}',
2323
description: 'Directory of the web assets',
2424
},
@@ -117,7 +117,7 @@ export interface ReleaseOptions {
117117
name?: string
118118
}
119119

120-
interface CliOptions extends WebOptions, CurseforgeOptions, Partial<ReleaseOptions> {
120+
interface CliOptions extends Partial<ReleaseOptions & WebOptions & CurseforgeOptions> {
121121
params: string[]
122122
name?: string
123123
}

src/curseforge.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ import type { MinecraftInstance } from './types'
1111
import { getPackName, WebOptions } from './web.js'
1212

1313
export interface CurseforgeOptions {
14-
curseforgeToken?: string
15-
curseforgeProject?: number
14+
curseforgeToken: string
15+
curseforgeProject: number
1616
paths: string[]
1717
}
1818

1919
export default class CurseforgeService {
2020
private readonly api: AxiosInstance
2121

22-
constructor(private readonly options: Readonly<CurseforgeOptions & WebOptions>) {
23-
if (!options.curseforgeToken) throw new Error('CurseForge token missing')
24-
if (!options.curseforgeProject) throw new Error('CurseForge project ID missing')
25-
22+
constructor(private readonly options: Readonly<CurseforgeOptions & Partial<WebOptions>>) {
2623
this.api = axios.create({
2724
baseURL: 'https://minecraft.curseforge.com/api',
2825
headers: {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { ReleaseOptions } from './cli/options.js'
1+
export { Action, defaultPaths, ReleaseOptions } from './cli/options.js'
22
export { CurseforgeOptions, default as CurseforgeService } from './curseforge.js'
33
export { default as WebService, WebOptions } from './web.js'

src/web.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import type { MinecraftInstance, PackData, Release, WebData } from './types'
99

1010
export interface WebOptions {
1111
apiUrl?: string
12-
webToken?: string
13-
webDir: string
12+
webDir?: string
13+
webToken: string
1414
}
1515

16+
export const defaultWebDir = 'web'
1617
export const defaultApiUrl = 'https://pack.macarena.ceo/api'
1718

1819
export default class WebService {
1920
private readonly api: AxiosInstance
21+
private readonly dir: string
2022

2123
constructor(private readonly options: Readonly<WebOptions>) {
2224
if (!options.webToken) throw new Error('Web Token missing')
2325

26+
this.dir = options.webDir ?? defaultWebDir
27+
2428
this.api = axios.create({
2529
baseURL: options.apiUrl ?? defaultApiUrl,
2630
headers: {
@@ -44,7 +48,7 @@ export default class WebService {
4448
}
4549

4650
async updateData() {
47-
const packData = readPackData(this.options.webDir)
51+
const packData = readPackData(this.dir)
4852

4953
if (!packData) {
5054
console.warn('Skip updating pack data')
@@ -56,7 +60,7 @@ export default class WebService {
5660
}
5761

5862
async updateAssets() {
59-
const assetsDir = join(this.options.webDir, 'assets')
63+
const assetsDir = join(this.dir, 'assets')
6064

6165
if (!existsSync(assetsDir)) {
6266
console.warn('No assets defined')
@@ -76,7 +80,7 @@ export default class WebService {
7680
}
7781

7882
updatePages() {
79-
const pageDir = join(this.options.webDir, 'pages')
83+
const pageDir = join(this.dir, 'pages')
8084

8185
if (!existsSync(pageDir)) {
8286
console.warn('No pages defined')
@@ -137,9 +141,9 @@ export function readPackData(dir: string): Partial<PackData> | null {
137141
return yaml.parse(readFileSync(file).toString())
138142
}
139143

140-
export async function getPackName(options: WebOptions) {
141-
if (options.webToken && options.apiUrl) {
142-
const service = new WebService(options)
144+
export async function getPackName(options: Partial<WebOptions>) {
145+
if (options.webToken) {
146+
const service = new WebService(options as WebOptions)
143147
const data = await service.getWebData()
144148
return data.name
145149
} else {

0 commit comments

Comments
 (0)