Skip to content

Commit

Permalink
move pack detection to seperate exported function
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Jan 29, 2024
1 parent 7e9593e commit b8891d3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 59 deletions.
48 changes: 2 additions & 46 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,12 @@
import { isAxiosError } from 'axios'
import chalk from 'chalk'
import { existsSync, readFileSync } from 'fs'
import CurseforgeService, { CurseforgeOptions, validateCurseforgeOptions } from '../curseforge.js'
import PackwizService, { PackwizOptions, validatePackwizOptions } from '../packwiz.js'
import WebService, { WebOptions } from '../web.js'
import parseCliOptions, { CliOptions, ReleaseOptions } from './options.js'

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')
}
import parseCliOptions from './options.js'

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

function fromMinecraftInstance(options: CurseforgeOptions) {
const file = options.curseforgePackFile ?? 'minecraftinstance.json'
if (!existsSync(file)) throw new Error(`curseforge manifest file '${file}' does not exist`)

const curseforge = new CurseforgeService(options)

const parsed = JSON.parse(readFileSync(file).toString())

return curseforge.importCurseforgePack(parsed)
}

function fromPackwiz(options: CliOptions & PackwizOptions) {
const service = new PackwizService(options)
return service.importPackwizPack()
}

async function parsePack(options: CliOptions) {
if (options.curseforgePackFile || existsSync('minecraftinstance.json')) {
validateCurseforgeOptions(options)
return fromMinecraftInstance(options)
}

if (options.packwizFile || existsSync('pack.toml')) {
validatePackwizOptions(options)
return fromPackwiz(options)
}

throw new Error('No pack metadata file auto-detected')
}

const options = parseCliOptions()

async function run() {
Expand All @@ -57,12 +18,7 @@ async function run() {
}

if (options.params.includes('release')) {
const { mods, version } = await parsePack(options)

if (!options.version) options.version = version

validateRelease(options)
await web.createRelease(mods, options)
await web.parseAndCreateRelease()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { ReleaseOptions } from './cli/options.js'
export { CurseforgeOptions, default as CurseforgeService } from './curseforge.js'
export { parsePack } from './pack.js'
export { WebOptions, default as WebService } from './web.js'
34 changes: 34 additions & 0 deletions src/pack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { existsSync, readFileSync } from 'fs'
import { CliOptions } from './cli/options.js'
import CurseforgeService, { CurseforgeOptions, validateCurseforgeOptions } from './curseforge.js'
import PackwizService, { PackwizOptions, validatePackwizOptions } from './packwiz.js'

function fromMinecraftInstance(options: CurseforgeOptions) {
const file = options.curseforgePackFile ?? 'minecraftinstance.json'
if (!existsSync(file)) throw new Error(`curseforge manifest file '${file}' does not exist`)

const curseforge = new CurseforgeService(options)

const parsed = JSON.parse(readFileSync(file).toString())

return curseforge.importCurseforgePack(parsed)
}

function fromPackwiz(options: CliOptions & PackwizOptions) {
const service = new PackwizService(options)
return service.importPackwizPack()
}

export async function parsePack(options: CliOptions) {
if (options.curseforgePackFile || existsSync('minecraftinstance.json')) {
validateCurseforgeOptions(options)
return fromMinecraftInstance(options)
}

if (options.packwizFile || existsSync('pack.toml')) {
validatePackwizOptions(options)
return fromPackwiz(options)
}

throw new Error('No pack metadata file auto-detected')
}
32 changes: 19 additions & 13 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import FormData from 'form-data'
import { createReadStream, existsSync, readdirSync, readFileSync } from 'fs'
import { basename, extname, join } from 'path'
import yaml from 'yaml'
import { ReleaseOptions } from './cli/options.js'
import { CliOptions, ReleaseOptions } from './cli/options.js'
import { IMod } from './models/index.js'
import { parsePack } from './pack.js'
import type { PackData, Release, WebData } from './types.js'

export interface WebOptions {
Expand All @@ -17,12 +18,18 @@ export interface WebOptions {
export const defaultWebDir = 'web'
export const defaultApiUrl = 'https://packs.macarena.ceo/api'

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')
}

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

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

this.dir = options.webDir ?? defaultWebDir
Expand Down Expand Up @@ -116,6 +123,16 @@ export default class WebService {
})
}

async parseAndCreateRelease() {
const options = { ...this.options }
const { mods, version } = await parsePack(options)

if (!options.version) options.version = version
validateRelease(options)

await this.createRelease(mods, options)
}

async createRelease(mods: IMod[], release: ReleaseOptions) {
const releaseData: Release = {
date: new Date().toISOString(),
Expand All @@ -139,14 +156,3 @@ export function readPackData(dir: string): Partial<PackData> | null {
if (!existsSync(file)) return null
return yaml.parse(readFileSync(file).toString())
}

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 {
const packData = readPackData(options.webDir ?? 'web')
return packData?.name
}
}

0 comments on commit b8891d3

Please sign in to comment.