action-zip/index.js

42 lines
900 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");
const inputFiles = core.getInput("files", { required: true });
const filename = core.getInput("filename");
2020-03-21 04:46:09 -07:00
const files = inputFiles
.split(" ")
.map(file => path.join(process.env.GITHUB_WORKSPACE, file));
2020-03-21 04:41:59 -07:00
console.log(`Ready to zip "${inputFiles}" into ${filename}`);
const zip = new AdmZip();
2020-03-21 04:46:09 -07:00
console.log(files);
2020-03-21 04:41:59 -07:00
files.forEach(file => {
if (!fs.existsSync(file)) {
console.log(` - ${file} (Not Found)`);
return;
}
const dir = path.dirname(file);
const stats = fs.lstatSync(file);
if (stats.isDirectory()) {
zip.addLocalFolder(file, dir);
} else {
zip.addLocalFile(file, dir === "." ? "" : dir);
}
console.log(` - ${file}`);
});
zip.writeZip(filename);
console.log(`\nZipped file ${filename}`);
core.setOutput(filename);