mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
added wrapper
package
This commit is contained in:
parent
6388a99f1b
commit
fcded2db92
20
packages/wrapper/package.json
Normal file
20
packages/wrapper/package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "wrapper",
|
||||
"version": "1.0.0",
|
||||
"main": "./src/index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"dev": "corenode-node ./src/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"7zip-min": "^1.4.3",
|
||||
"@octokit/rest": "^19.0.4",
|
||||
"cors": "2.8.5",
|
||||
"axios": "^0.27.2",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"corenode": "^0.28.26"
|
||||
}
|
||||
}
|
62
packages/wrapper/src/index.js
Normal file
62
packages/wrapper/src/index.js
Normal file
@ -0,0 +1,62 @@
|
||||
require("dotenv").config()
|
||||
|
||||
const packagejson = require("../package.json")
|
||||
|
||||
import fs from "fs"
|
||||
import path from "path"
|
||||
import express from "express"
|
||||
import cors from "cors"
|
||||
import setupLatestRelease from "./lib/setupDist"
|
||||
|
||||
global.remoteRepo = "ragestudio/comty"
|
||||
global.cachePath = path.join(process.cwd(), "cache")
|
||||
global.distPath = path.join(process.cwd(), "dist")
|
||||
|
||||
async function checkDistIntegrity() {
|
||||
// check if dist folder exists
|
||||
if (!fs.existsSync(global.distPath)) {
|
||||
return false
|
||||
}
|
||||
|
||||
// TODO: check the dist checksum with oficial server checksum
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async function runServer() {
|
||||
const app = express()
|
||||
|
||||
const portFromArgs = process.argv[2]
|
||||
let portListen = portFromArgs ? portFromArgs : 9000
|
||||
|
||||
app.use(cors({
|
||||
origin: "*",
|
||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
||||
preflightContinue: true,
|
||||
optionsSuccessStatus: 204
|
||||
}))
|
||||
|
||||
app.use(express.static(global.distPath))
|
||||
|
||||
app.get("*", function (req, res) {
|
||||
res.sendFile(path.join(global.distPath, "index.html"))
|
||||
})
|
||||
|
||||
app.listen(portListen)
|
||||
|
||||
console.log(`🌐 Listening app in port [${portListen}]`)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// check if dist is valid
|
||||
if (!checkDistIntegrity()) {
|
||||
await setupLatestRelease()
|
||||
}
|
||||
|
||||
// start app
|
||||
await runServer()
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(`[FATAL ERROR] >`, err)
|
||||
})
|
87
packages/wrapper/src/lib/setupDist.js
Normal file
87
packages/wrapper/src/lib/setupDist.js
Normal file
@ -0,0 +1,87 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const axios = require("axios")
|
||||
const _7z = require("7zip-min")
|
||||
const { Octokit } = require("@octokit/rest")
|
||||
|
||||
const octokit = new Octokit({
|
||||
// auth: process.env.GITHUB_TOKEN
|
||||
})
|
||||
|
||||
export async function getLatestReleaseBundleFromGithub() {
|
||||
console.log("Getting latest release bundle from github...")
|
||||
|
||||
const release = await octokit.repos.getLatestRelease({
|
||||
owner: global.remoteRepo.split("/")[0],
|
||||
repo: global.remoteRepo.split("/")[1]
|
||||
})
|
||||
|
||||
const bundle = release.data.assets.find(asset => asset.name === "app_dist.7z")
|
||||
|
||||
return bundle
|
||||
}
|
||||
|
||||
export async function downloadBundle(bundle) {
|
||||
// check if bundle exists
|
||||
if (fs.existsSync(path.join(global.cachePath, "app_dist.7z"))) {
|
||||
fs.unlinkSync(path.join(global.cachePath, "app_dist.7z"))
|
||||
}
|
||||
|
||||
console.log("Downloading bundle...")
|
||||
|
||||
const response = await axios.get(bundle.browser_download_url, {
|
||||
responseType: "stream"
|
||||
})
|
||||
|
||||
const writer = fs.createWriteStream(path.join(global.cachePath, "app_dist.7z"))
|
||||
|
||||
response.data.pipe(writer)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
writer.on("finish", resolve)
|
||||
writer.on("error", reject)
|
||||
})
|
||||
}
|
||||
|
||||
export async function extractBundle() {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log("Extracting bundle...")
|
||||
|
||||
_7z.unpack(path.join(global.cachePath, "app_dist.7z"), path.resolve(global.distPath, ".."), (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve(global.distPath)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function setupLatestRelease() {
|
||||
// create cache folder
|
||||
if (!fs.existsSync(global.cachePath)) {
|
||||
fs.mkdirSync(global.cachePath)
|
||||
}
|
||||
|
||||
// create dist folder
|
||||
if (!fs.existsSync(global.distPath)) {
|
||||
fs.mkdirSync(global.distPath)
|
||||
}
|
||||
|
||||
const bundle = await getLatestReleaseBundleFromGithub()
|
||||
|
||||
await downloadBundle(bundle)
|
||||
|
||||
const bundlePath = await extractBundle()
|
||||
|
||||
console.log(`Bundle extracted to ${bundlePath}`)
|
||||
|
||||
console.log("Cleaning up...")
|
||||
|
||||
// delete cache folder
|
||||
if (fs.existsSync(global.cachePath)) {
|
||||
fs.rmdirSync(global.cachePath, { recursive: true })
|
||||
}
|
||||
}
|
||||
|
||||
export default setupLatestRelease
|
Loading…
x
Reference in New Issue
Block a user