diff --git a/cli/src/commands/create.ts b/cli/src/commands/create.ts index 2175468..78a06ab 100644 --- a/cli/src/commands/create.ts +++ b/cli/src/commands/create.ts @@ -1,31 +1,11 @@ -import type { CommandModule, Argv, ArgumentsCamelCase } from 'yargs' import c from 'chalk' import { input, select } from '@inquirer/prompts' import clone from 'git-clone' import path from 'node:path' -import { checkForUpdate } from '../utils/update' - -function builder(yargs: Argv) { - return yargs - .option('projectName', { - alias: 'p', - description: 'Name of the folder to scaffold a project to', - type: 'string', - demandOption: false, - }) - .option('template', { - alias: 't', - description: 'Frontend framework to use for CEF', - type: 'string', - demandOption: false, - }) - .middleware(async () => await checkForUpdate()) -} - -async function handler(args: ArgumentsCamelCase) { - let folder = (args.projectName as string) ?? args.p - let framework = (args.template as string) ?? args.t +export async function initProject() { + let folder + let framework if (!folder) { folder = await input({ @@ -84,13 +64,3 @@ async function handler(args: ArgumentsCamelCase) { }, ) } - -const init: CommandModule = { - command: 'create [folderName] [template]', - aliases: 'c', - describe: 'Scaffold a template project using RageFW', - builder, - handler, -} - -export default init diff --git a/cli/src/commands/download-updater.ts b/cli/src/commands/download-updater.ts new file mode 100644 index 0000000..4ce885f --- /dev/null +++ b/cli/src/commands/download-updater.ts @@ -0,0 +1,37 @@ +import axios from 'axios' +import * as fs from 'node:fs' + +const latestReleases = + 'https://git.entityseven.com/api/v1/repos/entityseven/rage-server-downloader/releases?page=1&limit=1' + +type Release = { + id: number +} + +type Asset = { + browser_download_url: string +} + +export async function downloadUpdater(): Promise { + const id = await getLatestReleaseID() + + const latestAssets = `https://git.entityseven.com/api/v1/repos/entityseven/rage-server-downloader/releases/${id}/assets?page=1&limit=1` + + axios.get(latestAssets).then(async ({ data }) => { + const downloadURL = data[0].browser_download_url + + const file = await axios.get(data[0].browser_download_url, { + responseType: 'arraybuffer', + }) + const fileData = Buffer.from(file.data, 'binary') + + const fileSplit = downloadURL.split('/') + const fileName = fileSplit[fileSplit.length - 1] + + fs.writeFileSync(`./${fileName}`, fileData) + }) +} + +async function getLatestReleaseID() { + return axios.get(latestReleases).then(({ data }) => data[0].id) +} diff --git a/cli/src/index.ts b/cli/src/index.ts index a2efb24..912bc83 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -1,11 +1,40 @@ -import yargs from 'yargs' +import c from 'chalk' +import { select } from '@inquirer/prompts' -import create from './commands/create' +import { checkForUpdate } from './utils/update' +import { initProject } from './commands/create' +import { downloadUpdater } from './commands/download-updater' -yargs - .usage(' [args]') - // .scriptName('rage-fw') - // .usage('$0 [args]') - // @ts-ignore - .command(create) - .help().argv +enum Actions { + INIT_PROJECT = 'INIT_PROJECT', + UPDATER = 'UPDATER', +} + +;(async () => { + await checkForUpdate() + + console.log( + c.blueBright('Rage FW CLI | Powered by Entity Seven Group ️ <3'), + ) + + const action = await select({ + message: c.gray('Select action:'), + choices: [ + { + name: 'Initialize new project', + value: Actions.INIT_PROJECT, + description: 'Initialize new project and start develop', + }, + { + name: 'Install RAGE:MP updater', + value: Actions.UPDATER, + description: + 'Use our custom updater to download and update RAGE:MP server files.', + }, + ], + loop: true, + }) + + if (action === Actions.INIT_PROJECT) await initProject() + if (action === Actions.UPDATER) await downloadUpdater() +})() diff --git a/cli/src/utils/update.ts b/cli/src/utils/update.ts index d4f124e..f7b54b0 100644 --- a/cli/src/utils/update.ts +++ b/cli/src/utils/update.ts @@ -18,7 +18,7 @@ export async function checkForUpdate(): Promise { .then(({ data }) => { const latestVersion = data[0].name - if (!(latestVersion === version)) + if (latestVersion !== `v${version}`) notifyUserAboutUpdate(latestVersion) }) .then(() => res()),