@jti/task
Version:
This package installs go-task in the `bin` folder.
97 lines (80 loc) • 2.28 kB
JavaScript
// Installs a platform/arch appropriate binary for go-task/task.
const axios = require('axios')
const decompress = require('decompress')
const fs = require('fs')
const path = require('path')
// Debugging helper.
const DEBUG = false
const debug = function (...msgs) {
if (DEBUG) {
for (let m of msgs) {
console.log(m)
}
}
}
const cwd = process.env.INIT_CWD
? process.env.INIT_CWD
: process.cwd()
const { platform, arch } = process
const taskVersion = '3.41.0'
const targetFolder = path.join(__dirname, 'bin')
// Returns the release-specific platform string based on platform
const mapPlatform = (map = {}) => {
if (!(platform in map)) return null
return map[platform]
}
// Returns the release-specific arch string based on arch
const mapArch = (map = {}) => {
if (!(arch in map)) return null
return map[arch]
}
const getRelease = async (url, targetFolder) => {
console.log(`Installing ${url} into ${targetFolder}`)
let res = await axios.get(url, { responseType: 'arraybuffer' })
try {
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder, { recursive: true })
}
debug(`Created target folder!`, targetFolder)
}
catch (err) {
console.log(`Could not create target folder:`, err)
}
try {
let dres = await decompress(res.data, targetFolder)
debug(`Decompressed assets:`, dres)
}
catch (err) {
console.log(`Could not decompress downloaded asset:`, err)
}
}
const task = async () => {
// Set up the download URL for the appropriate platform/arch.
const p = mapPlatform({
'darwin': 'darwin',
'linux': 'linux',
'win32': 'windows'
})
const a = mapArch({
'x86': '386',
'x64': 'amd64',
'arm': 'arm',
'arm64': 'arm64',
})
if (!p || !a) {
console.log(`No task release available for $${platform}/${arch}.`)
return
}
const ext = (p === 'windows') ? 'zip' : 'tar.gz'
if (ext !== 'tar.gz' && ext !== 'zip') {
console.log(`Don't know how to handle a .${ext} file!`)
return
}
const url = `https://github.com/go-task/task/releases/download/`
+ `v${taskVersion}/task_${p}_${a}.${ext}`
await getRelease(url, targetFolder)
}
console.log(`Installing task...`)
task()