-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstallEditor.js
126 lines (78 loc) · 2.78 KB
/
installEditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
}