Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "5.19.0",
"version": "5.21.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
20 changes: 16 additions & 4 deletions src/helpers/Exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,30 @@ export class Exec extends Macroable {
}
}

public static download(url: string): Promise<any>
public static download(
url: string,
options?: { path?: string }
): Promise<File>

/**
* Download an archive to a determined path.
* Download the data of an URL.
*/
public static async download(path: string, url: string): Promise<File> {
public static async download(url: string, options?: { path?: string }) {
return new Promise((resolve, reject) => {
const callback = response => {
const data = new Transform()

response.on('data', chunk => data.push(chunk))

response.on('end', function () {
resolve(new File(path, data.read()).loadSync())
response.on('end', () => {
if (options.path) {
resolve(new File(options.path, data.read()).loadSync())

return
}

resolve(data.read())
})

response.on('error', error => reject(error))
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/helpers/ExecTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,9 @@ export default class ExecTest {

@Test()
public async shouldBeAbleToDownloadFiles({ assert }: Context) {
const file = await Exec.download(
Path.storage('downloads/node.pkg'),
'https://nodejs.org/dist/v23.5.0/node-23.5.0.pkg'
)
const file = await Exec.download('https://nodejs.org/dist/v23.5.0/node-23.5.0.pkg', {
path: Path.storage('downloads/node.pkg')
})

assert.equal(file.base, 'node.pkg')
assert.isTrue(await File.exists(file.path))
Expand Down
Loading