-
Notifications
You must be signed in to change notification settings - Fork 33
/
install.ts
46 lines (39 loc) 路 1.16 KB
/
install.ts
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
/**
* Imports
*/
import { getPlatform, Platform } from '@prisma/get-platform'
import * as https from 'https'
import * as zlib from 'zlib'
import * as fs from 'fs'
/**
* Gets the download URL for a platform
*/
function getFmtDownloadUrl(platform: Platform) {
const extension = platform === 'windows' ? '.exe.gz' : '.gz'
return `https://prisma-builds.s3-eu-west-1.amazonaws.com/master/latest/${platform}/prisma-fmt${extension}`
}
/**
* Install prisma format
*/
export default async function install(fmtPath: string): Promise<string> {
const platform = await getPlatform()
const url = getFmtDownloadUrl(platform)
const file = fs.createWriteStream(fmtPath)
// Fetch fetch fetch.
return new Promise<string>(function(resolve, reject) {
https.get(url, function(response) {
// Did everything go well?
if (response.statusCode !== 200) {
reject(response.statusMessage)
}
// If so, unzip and pipe into our file.
const unzip = zlib.createGunzip()
response.pipe(unzip).pipe(file)
file.on('finish', function() {
fs.chmodSync(fmtPath, '755')
file.close()
resolve(fmtPath)
})
})
})
}