automated-deployment-aws
Version:
Library that automates the deployment of the current project into aws machine
92 lines (77 loc) • 3.06 kB
JavaScript
var archiver = require('archiver');
var fs = require('fs');
var path = require('path');
function createZipFile(dirPath){
return new Promise((resolve,reject)=>{
const directoryPath = path.normalize(dirPath)
const zipFielPAth = dirPath + ".zip"
// this code is used to find list of file which we want to exclude in zip file
var aswIngnoreFilePath = directoryPath + "/.awsignore"
//var aswIngnoreFilePath = "./.awsignore" // this line of code for local testing
var awsIgnoreFileList = []
var normalizedPath = path.normalize(aswIngnoreFilePath)
try {
const data = fs.readFileSync(aswIngnoreFilePath, 'utf8')
awsIgnoreFileList = data.split("\n").filter(i => i.trim() != "").map(ignoredFilePath => {
ignoredFilePath = ignoredFilePath.trim();
let directoryOfIgnoredPath = directoryPath+"/"+ignoredFilePath;
if(fs.existsSync(directoryOfIgnoredPath)) {
var statsOfEachFile = fs.statSync(directoryOfIgnoredPath);
if(statsOfEachFile.isDirectory()){
return ignoredFilePath+"/**";
}
return ignoredFilePath;
} else {
return "";
}
})
console.log(".awsignore files path array = ",awsIgnoreFileList)
} catch (err) {
console.error(".awsignore file does not exist..")
}
// create a file to stream archive data to.
var output = fs.createWriteStream(zipFielPAth);
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function() {
//console.log(archive.pointer() + ' total bytes');
//console.log('archiver has been finalized and the output file descriptor has closed.');
resolve()
});
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function() {
console.log('Data has been drained');
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.log('Warning:',err);
resolve()
// log warning
} else {
// throw error
reject(err)
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
reject(err)
});
archive.pipe(output);
//archive.directory(directoryPath, false);
archive.glob('**/*', {
cwd: directoryPath,
ignore: awsIgnoreFileList,
dot : true
})
archive.finalize()
})
}
module.exports = {
createZipFile : createZipFile
}