Skip to content

Commit

Permalink
cleanup options
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Nov 24, 2022
1 parent a1ce402 commit 8989956
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
20 changes: 17 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import chalk from 'chalk'
import CurseforgeService from '../curseforge.js'
import WebService from '../web.js'
import CurseforgeService, { CurseforgeOptions } from '../curseforge.js'
import WebService, { WebOptions } from '../web.js'
import parseCliOptions, { Action, ReleaseOptions } from './options.js'

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

function validateWebOptions<T>(options: T & Partial<WebOptions>): asserts options is T & WebOptions {
if (!options.webToken) throw new Error('Web Token missing')
}

function validateCurseforgeOptions<T>(
options: T & Partial<CurseforgeOptions>
): asserts options is T & CurseforgeOptions {
if (!options.curseforgeProject) throw new Error('CurseForge Project-ID missing')
if (!options.curseforgeToken) throw new Error('CurseForge Token missing')
if (!options.paths) throw new Error('Pack path are missing')
}

async function run() {
const { params, ...options } = parseCliOptions()

Expand All @@ -17,6 +29,7 @@ async function run() {
}

if (params.includes(Action.WEB)) {
validateWebOptions(options)
const web = new WebService(options)
await web.updateWeb()

Expand All @@ -27,6 +40,7 @@ async function run() {
}

if (params.includes(Action.CURSEFORGE)) {
validateCurseforgeOptions(options)
const curseforge = new CurseforgeService(options)
validateRelease(options)

Expand Down
8 changes: 4 additions & 4 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import arg from 'arg'
import commandLineUsage, { OptionDefinition, Section } from 'command-line-usage'
import { CurseforgeOptions } from '../curseforge.js'
import { defaultApiUrl, readPackData, WebOptions } from '../web.js'
import { defaultApiUrl, defaultWebDir, readPackData, WebOptions } from '../web.js'

const defaultPaths = ['config', 'mods', 'kubejs', 'defaultconfigs']
export const defaultPaths = ['config', 'mods', 'kubejs', 'defaultconfigs']

const optionDefinitions: OptionDefinition[] = [
{
Expand All @@ -18,7 +18,7 @@ const optionDefinitions: OptionDefinition[] = [
},
{
name: 'web-dir',
defaultValue: './web',
defaultValue: defaultWebDir,
typeLabel: '{underline file}',
description: 'Directory of the web assets',
},
Expand Down Expand Up @@ -117,7 +117,7 @@ export interface ReleaseOptions {
name?: string
}

interface CliOptions extends WebOptions, CurseforgeOptions, Partial<ReleaseOptions> {
interface CliOptions extends Partial<ReleaseOptions & WebOptions & CurseforgeOptions> {
params: string[]
name?: string
}
Expand Down
9 changes: 3 additions & 6 deletions src/curseforge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ import type { MinecraftInstance } from './types'
import { getPackName, WebOptions } from './web.js'

export interface CurseforgeOptions {
curseforgeToken?: string
curseforgeProject?: number
curseforgeToken: string
curseforgeProject: number
paths: string[]
}

export default class CurseforgeService {
private readonly api: AxiosInstance

constructor(private readonly options: Readonly<CurseforgeOptions & WebOptions>) {
if (!options.curseforgeToken) throw new Error('CurseForge token missing')
if (!options.curseforgeProject) throw new Error('CurseForge project ID missing')

constructor(private readonly options: Readonly<CurseforgeOptions & Partial<WebOptions>>) {
this.api = axios.create({
baseURL: 'https://minecraft.curseforge.com/api',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { ReleaseOptions } from './cli/options.js'
export { Action, defaultPaths, ReleaseOptions } from './cli/options.js'
export { CurseforgeOptions, default as CurseforgeService } from './curseforge.js'
export { default as WebService, WebOptions } from './web.js'
20 changes: 12 additions & 8 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import type { MinecraftInstance, PackData, Release, WebData } from './types'

export interface WebOptions {
apiUrl?: string
webToken?: string
webDir: string
webDir?: string
webToken: string
}

export const defaultWebDir = 'web'
export const defaultApiUrl = 'https://pack.macarena.ceo/api'

export default class WebService {
private readonly api: AxiosInstance
private readonly dir: string

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

this.dir = options.webDir ?? defaultWebDir

this.api = axios.create({
baseURL: options.apiUrl ?? defaultApiUrl,
headers: {
Expand All @@ -44,7 +48,7 @@ export default class WebService {
}

async updateData() {
const packData = readPackData(this.options.webDir)
const packData = readPackData(this.dir)

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

async updateAssets() {
const assetsDir = join(this.options.webDir, 'assets')
const assetsDir = join(this.dir, 'assets')

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

updatePages() {
const pageDir = join(this.options.webDir, 'pages')
const pageDir = join(this.dir, 'pages')

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

export async function getPackName(options: WebOptions) {
if (options.webToken && options.apiUrl) {
const service = new WebService(options)
export async function getPackName(options: Partial<WebOptions>) {
if (options.webToken) {
const service = new WebService(options as WebOptions)
const data = await service.getWebData()
return data.name
} else {
Expand Down

0 comments on commit 8989956

Please sign in to comment.