action-zip/index.js

42 lines
961 B
JavaScript
Raw Normal View History

2020-03-21 04:41:59 -07:00
const fs = require("fs");
const path = require("path");
const AdmZip = require("adm-zip");
const core = require("@actions/core");
2020-03-21 05:08:23 -07:00
const files = core
.getInput("files", { required: true })
2020-03-21 04:46:09 -07:00
.split(" ")
.map(file => path.join(process.env.GITHUB_WORKSPACE, file));
2020-03-21 04:41:59 -07:00
2020-03-21 05:08:23 -07:00
const output = path.join(process.env.GITHUB_WORKSPACE, core.getInput("output"));
2020-03-21 04:41:59 -07:00
2020-03-21 05:08:23 -07:00
console.log(`Ready to zip "${files}" into ${output}`);
2020-03-21 04:41:59 -07:00
2020-03-21 05:08:23 -07:00
const zip = new AdmZip();
2020-03-21 04:46:09 -07:00
2020-03-21 04:41:59 -07:00
files.forEach(file => {
2020-03-21 05:11:41 -07:00
const filename = file.replace(`${process.env.GITHUB_WORKSPACE}/`, "");
2020-03-21 05:08:23 -07:00
2020-03-21 05:11:41 -07:00
if (!fs.existsSync(file)) {
2020-03-21 05:08:23 -07:00
console.log(` - ${filename} (Not Found)`);
2020-03-21 04:41:59 -07:00
return;
}
2020-03-21 05:08:23 -07:00
const dir = path.dirname(filename);
2020-03-21 05:11:41 -07:00
const stats = fs.lstatSync(file);
2020-03-21 04:41:59 -07:00
if (stats.isDirectory()) {
2020-03-21 05:11:41 -07:00
zip.addLocalFolder(file, dir);
2020-03-21 04:41:59 -07:00
} else {
2020-03-21 05:11:41 -07:00
zip.addLocalFile(file, dir === "." ? "" : dir);
2020-03-21 04:41:59 -07:00
}
2020-03-21 05:08:23 -07:00
console.log(` - ${filename}`);
2020-03-21 04:41:59 -07:00
});
2020-03-21 05:08:23 -07:00
zip.writeZip(output);
2020-03-21 04:41:59 -07:00
2020-03-21 05:08:23 -07:00
console.log(`\nZipped file ${output}`);
2020-03-21 04:41:59 -07:00
2020-03-21 05:08:23 -07:00
core.setOutput(output);