Skip to content

Commit

Permalink
write zip file in path (dawidd6#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz authored and gabriel-samfira committed Jul 8, 2022
1 parent 27fee4d commit f0541cd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/download.yml
Expand Up @@ -103,9 +103,11 @@ jobs:
skip_unpack: true
- name: Test
run: |
test -f artifact.zip
! test -d artifact
unzip -l artifact.zip
test -d artifact
test -f artifact/artifact.zip
! test -d artifact/artifact
! test -f artifact.zip
unzip -l artifact/artifact.zip
download-dry-run-exists:
runs-on: ubuntu-latest
needs: wait
Expand Down
50 changes: 43 additions & 7 deletions main.js
@@ -1,9 +1,11 @@
const core = require('@actions/core')
const https = require('https');
const github = require('@actions/github')
const AdmZip = require('adm-zip')
const filesize = require('filesize')
const pathname = require('path')
const fs = require('fs')
const fs = require('fs');
const url = require('url');

function inform(key, val) {
core.info(`==> ${key}: ${val}`)
Expand Down Expand Up @@ -183,33 +185,67 @@ async function main() {

core.info(`==> Downloading: ${artifact.name}.zip (${size})`)

const zip = await client.actions.downloadArtifact({
let saveTo = `${pathname.join(path, artifact.name)}.zip`
fs.mkdirSync(path, { recursive: true })

let request = client.actions.downloadArtifact.endpoint({
owner: owner,
repo: repo,
artifact_id: artifact.id,
archive_format: "zip",
})
});

const options = {
hostname: url.parse(request.url).hostname,
path: url.parse(request.url).pathname,
headers: {
...request.headers,
Authorization: `token ${token}`,
}
}
const file = fs.createWriteStream(saveTo);
https.get(options, (response) => {
core.info("statusCode: ", response.statusCode); // <======= Here's the status code
core.info("headers: ", response.headers);

response.pipe(file);
file.on("finish", () => {
file.close();
console.log("Download Completed");
});
});

// await client.actions.downloadArtifact({
// owner: owner,
// repo: repo,
// artifact_id: artifact.id,
// archive_format: "zip",
// }).then(function (response) {
// response.data.pipe(fs.createWriteStream(saveTo));
// });

// fs.writeFileSync(saveTo, Buffer.from(zip.data), 'binary')

if (skipUnpack) {
fs.writeFileSync(`${artifact.name}.zip`, Buffer.from(zip.data), 'binary')
continue
}

const dir = name ? path : pathname.join(path, artifact.name)

fs.mkdirSync(dir, { recursive: true })

const adm = new AdmZip(Buffer.from(zip.data))
core.info(`==> Extracting: ${saveTo}`)
const adm = new AdmZip(saveTo)

core.startGroup(`==> Extracting: ${artifact.name}.zip`)
adm.getEntries().forEach((entry) => {
const action = entry.isDirectory ? "creating" : "inflating"
const filepath = pathname.join(dir, entry.entryName)

core.info(` ${action}: ${filepath}`)
adm.extractEntryTo(entry.entryName, dir)
})

adm.extractAllTo(dir, true)
// adm.extractAllTo(dir, true)
core.endGroup()
}
} catch (error) {
Expand Down

0 comments on commit f0541cd

Please sign in to comment.