Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose standalone download function #73

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,23 @@ async function link ({ depBin, version }) {
return localBin
}

/**
* @param {object} options
* @param {string} options.version
* @param {string} options.platform
* @param {string} options.arch
* @param {string} options.installPath
* @param {string} options.distUrl
*/
module.exports.download = download

/**
* @param {string} [version]
* @param {string} [platform]
* @param {string} [arch]
* @param {string} [installPath]
*/
module.exports = async (version, platform, arch, installPath) => {
module.exports.downloadAndUpdateBin = async (version, platform, arch, installPath) => {
Copy link
Member Author

@lidel lidel May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ This funciton did download+link. Renamed to better describe what it really does.

const args = cleanArguments(version, platform, arch, installPath)

return link({
Expand Down
25 changes: 12 additions & 13 deletions src/go-platform.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Had to do this to pass lint

Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

function getGoOs () {
switch (process.platform) {
case "sunos":
return "solaris"
case "win32":
return "windows"
case 'sunos':
return 'solaris'
case 'win32':
return 'windows'
}

return process.platform
}

function getGoArch () {
switch (process.arch) {
case "ia32":
return "386"
case "x64":
return "amd64"
case "arm":
return "arm"
case "arm64":
return "arm64"
case 'ia32':
return '386'
case 'x64':
return 'amd64'
case 'arm':
return 'arm'
case 'arm64':
return 'arm64'
}

return process.arch
}


module.exports = {
GOOS: getGoOs(),
GOARCH: getGoArch()
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const fs = require('fs')
const path = require('path')
const { download } = require('./download')

module.exports.download = download
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to expose the downloadAndUpdateBin as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no, it is only meaningful in NPM's postinstall hook, there is no use case where end use would want to use it.


module.exports.path = function () {
if (process.env.KUBO_BINARY) {
Expand Down
4 changes: 2 additions & 2 deletions src/post-install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const download = require('./download')
const { downloadAndUpdateBin } = require('./download')

download()
downloadAndUpdateBin()
.catch(err => {
console.error(err)
process.exit(1)
Expand Down
35 changes: 29 additions & 6 deletions test/download.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Added test for download function.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

const test = require('tape-promise').default(require('tape'))
const fs = require('fs-extra')
const download = require('../src/download')
const os = require('os')
const path= require('path')
const { download, downloadAndUpdateBin } = require('../src/download')
const { path: detectLocation } = require('../')
const clean = require('./fixtures/clean')

test('Ensure ipfs gets downloaded (current version and platform)', async (t) => {
await clean()

const installPath = await download()
const installPath = await downloadAndUpdateBin()
const stats = await fs.stat(installPath)

t.ok(stats, 'kubo was downloaded')
Expand All @@ -21,7 +23,7 @@ test('Ensure ipfs gets downloaded (current version and platform)', async (t) =>
test('Returns an error when version unsupported', async (t) => {
await clean()

await t.rejects(download('bogusversion', 'linux'), /Error: Version 'bogusversion' not available/)
await t.rejects(downloadAndUpdateBin('bogusversion', 'linux'), /Error: Version 'bogusversion' not available/)

t.end()
})
Expand All @@ -31,7 +33,7 @@ test('Returns an error when KUBO_DIST_URL is 404', async (t) => {

process.env.KUBO_DIST_URL = 'https://dist.ipfs.tech/notfound'

await t.rejects(download(), /404/)
await t.rejects(downloadAndUpdateBin(), /404/)

delete process.env.KUBO_DIST_URL

Expand All @@ -43,18 +45,39 @@ test('Returns an error when legacy GO_IPFS_DIST_URL is 404', async (t) => {

process.env.GO_IPFS_DIST_URL = 'https://dist.ipfs.tech/notfound'

await t.rejects(download(), /404/)
await t.rejects(downloadAndUpdateBin(), /404/)

delete process.env.GO_IPFS_DIST_URL

t.end()
})


test('Path returns undefined when no binary has been downloaded', async (t) => {
await clean()

t.throws(detectLocation, /not found/, 'Path throws if binary is not installed')

t.end()
})

test('Ensure calling download function manually with static values works', async (t) => {
await clean()

const { version } = require('../package.json')
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-dir-'))

console.log(tempDir)
const kuboPath = await download({
version: `v${version}`,
platform: 'darwin',
arch: 'arm64',
distUrl: 'https://dist.ipfs.tech',
installPath: tempDir,
})
console.log(kuboPath)
const stats = await fs.stat(kuboPath)

t.ok(stats, 'kubo was downloaded to installPath')

t.end()
})
Loading