diff --git a/src/cli/index.ts b/src/cli/index.ts index d924134..329d935 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -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(options: T & Partial): 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(options: T & Partial): 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() { @@ -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() } } diff --git a/src/index.ts b/src/index.ts index 0df866a..54624f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/pack.ts b/src/pack.ts new file mode 100644 index 0000000..8943ce2 --- /dev/null +++ b/src/pack.ts @@ -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') +} diff --git a/src/web.ts b/src/web.ts index af5fc7d..2089e11 100644 --- a/src/web.ts +++ b/src/web.ts @@ -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 { @@ -17,12 +18,18 @@ export interface WebOptions { export const defaultWebDir = 'web' export const defaultApiUrl = 'https://packs.macarena.ceo/api' +function validateRelease(options: T & Partial): 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) { + constructor(private readonly options: Readonly) { if (!options.webToken) throw new Error('Web Token missing') this.dir = options.webDir ?? defaultWebDir @@ -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(), @@ -139,14 +156,3 @@ export function readPackData(dir: string): Partial | null { if (!existsSync(file)) return null return yaml.parse(readFileSync(file).toString()) } - -export async function getPackName(options: Partial) { - 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 - } -}