| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import http from "https"
- import { chmodSync, createWriteStream, existsSync, fstat, mkdirSync, readFileSync, unlink } from "fs"
- import { homedir } from "os"
- import path, { dirname } from "path"
- import AdmZip from "adm-zip"
- import { fileURLToPath } from "url"
- function download(url, dest) {
- console.log(`Fetching ${url}`)
- return new Promise((resolve, reject) => {
- var file = createWriteStream(dest);
- try {
- http.get(url, function (response) {
- const { statusCode } = response
- if (statusCode !== 200) {
- throw new Error("Network error downloading " + url)
- }
- response
- .pipe(file)
- .on("error", () => {
- unlink(file)
- throw new Error("Cannot write to file")
- })
- file.on("finish", function () {
- resolve()
- }).on("error", () => {
- unlink(file)
- throw new Error("Cannot write to file")
- })
- });
- } catch (e) {
- reject(e)
- }
- })
- }
- export async function installEditor() {
- const __filename = fileURLToPath(import.meta.url)
- const __dirname = dirname(__filename)
- const config = JSON.parse(readFileSync(path.join(__dirname, "package.json")))
- const ver = config.version
- // compute metadata
- let platform = {
- "darwin": "macos",
- "linux": "linux",
- "win32": "windows"
- }[process.platform]
- if (process.arch === "arm64") {
- platform += "_arm";
- }
- const distName = `PhaserEditor2D-core-${ver}-${platform}`
- const fileName = `${distName}.zip`
- // create install dir
- const home = homedir()
- const installsDir = path.join(home, ".phasereditor2d", "installs")
- const distInstallDir = path.join(installsDir, distName)
- const execFile = path.join(distInstallDir, "PhaserEditor2D",
- `PhaserEditor2D${platform === "windows" ? ".exe" : ""}`)
- if (existsSync(execFile)) {
- return execFile
- }
- mkdirSync(installsDir, { recursive: true })
- // download
- const outputFile = path.join(installsDir, fileName)
- if (!existsSync(outputFile)) {
- const updatesUrl = "https://updates.phasereditor2d.com"
- const fileUrl = `${updatesUrl}/v${ver}/PhaserEditor2D-core-${ver}-${platform}.zip`
- await download(fileUrl, outputFile)
- }
- // unzip
- console.log(`Unzipping ${outputFile}`)
- mkdirSync(distInstallDir, { recursive: true })
- const zip = new AdmZip(outputFile)
- zip.extractAllTo(distInstallDir, true)
- if (platform !== "windows") {
- chmodSync(execFile, "777");
- }
- // TODO check md5sum
- return execFile
- }
|