Created
September 5, 2019 07:22
-
-
Save marcel-ploch/a598ce86073189fa310eb320a97648a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const mime = require('mime'); | |
const packageJSON = require('./../package.json'); | |
const crypto = require('crypto'); | |
const rqhttp = require('request-promise'); | |
console.log('\x1b[0m'); | |
const ENVIRONMENT = process.env.ENVIRONMENT; | |
if (!ENVIRONMENT) { | |
console.log('Die Angabe des Environment (z.B. ENV=DEV) fehlt.'); | |
process.exit(1); | |
} | |
const versionNumberRandom = crypto.createHash('sha256').update(Math.random().toString()).digest('base64').substring(0, 4); | |
const config = { | |
s3BucketName: undefined, | |
folderPath: 'dist', | |
key: 'dev' | |
}; | |
let environment = 'SPRINT'; | |
const versionName = packageJSON.version + '+' + (!process.env.BUILD_NUMBER ? versionNumberRandom : process.env.BUILD_NUMBER); | |
//Hier liegt die AWS Config | |
AWS.config.loadFromPath(path.join(__dirname, 'config.json')); | |
config.s3BucketName = ''; | |
if (ENVIRONMENT === 'STAGE') { | |
config.s3BucketName = ''; | |
environment = 'STAGING'; | |
} else if (ENVIRONMENT === 'LIVE') { | |
environment = 'LIVE'; | |
config.s3BucketName = ''; | |
} | |
// initialise S3 client | |
const awsS3Client = new AWS.S3({signatureVersion: 'v4', region: 'eu-central-1'}); | |
// resolve full folder path | |
const distFolderPath = path.join(__dirname, '../', config.folderPath); | |
const promises = []; | |
// Read the Files from dist Folder and Upload them to the desired S3 bucket | |
readFiles(distFolderPath, versionName + '/'); | |
Promise.all(promises) | |
.then((data) => { | |
// Set new Version | |
var options = { | |
method: 'POST', | |
body: { | |
'appName': 'COMPARABLES', | |
'environment': environment, | |
'bucket': packageJSON.s3 + '/' + config.s3BucketName, | |
'version': versionName | |
}, | |
uri: '', | |
headers: { | |
'X-ApiKey': '', | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
}, | |
json: true | |
}; | |
options.uri = ''; | |
// options.headers['HOST'] = ''; | |
_setVersionNumber(options, true); | |
}) | |
.catch((err) => { | |
console.error(err); | |
deleteFiles(versionName).then(() => { | |
process.exit(1); | |
}); | |
}); | |
/** | |
* Readll all files and Upload them to S3 | |
*/ | |
function readFiles(distFolderPath, key) { | |
key = key || ''; | |
const files = fs.readdirSync(distFolderPath); | |
files.forEach((file) => { | |
console.log('****** Reading File: ' + file + ' ******'); | |
if (fs.statSync(path.join(distFolderPath, file)).isFile()) { | |
const body = fs.readFileSync(path.join(distFolderPath, file)); | |
const mimeType = mime.lookup(path.join(distFolderPath, file)); | |
if (file !== '.DS_Store') { | |
const promise = new Promise((resolve, reject) => { | |
let path = key + file; | |
if (path.indexOf('assets') !== -1) { | |
path = path.replace(versionName + '/', ''); | |
} | |
console.log(path, config.s3BucketName); | |
awsS3Client.putObject( | |
{Bucket: config.s3BucketName, Body: body, ACL: 'public-read', Key: path, ContentType: mimeType}, (err, data) => { | |
if (err) { | |
reject(err); | |
} | |
console.log(file, data); | |
return resolve(file); | |
}); | |
}); | |
promises.push(promise); | |
} | |
} else { | |
readFiles(path.join(distFolderPath, file), key + file + '/'); | |
} | |
}); | |
} | |
/** | |
* Function to delete all uploaded files for the current version and bucket type | |
*/ | |
function deleteFiles(key) { | |
console.log('\x1b[0m', '****** Listing all objects in current Bucket with key: ' + key + ' ******'); | |
const promise = new Promise((resolve, reject) => { | |
const params = {Bucket: config.s3BucketName, Prefix: key}; | |
awsS3Client.listObjects(params, function(err, data) { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(data); | |
}); | |
}); | |
return promise.then((data) => { | |
return new Promise((resolve, rejecet) => { | |
const keys = []; | |
if (data.Contents.length > 0) { | |
data.Contents.forEach((data) => { | |
keys.push({Key: data.Key}); | |
}); | |
const params = {Bucket: config.s3BucketName, Delete: {Objects: keys, Quiet: false}}; | |
awsS3Client.deleteObjects(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
return reject(err); | |
} | |
console.log('****** DELETED SUCCESSFULLY ******'); // successful response | |
return resolve(data); | |
}); | |
} | |
}); | |
return new Promise((resolve, reject) => { | |
resolve(); | |
}) | |
}); | |
} | |
function _setVersionNumber(options, deleteFilesIfDamaged) { | |
rqhttp(options) | |
.then((data) => { | |
console.log( | |
'\n' + | |
'\n'); | |
console.log( | |
'\x1b[32m', | |
'****** Upload to current Bucket with key: ' + versionName + ' was successfull. ******' + | |
'\n'); | |
console.log('\x1b[32m', '****** Version was set to ' + versionName + '****** \n'); | |
console.log('\x1b[0m'); | |
process.exit(0); | |
}) | |
.catch((err) => { | |
console.log(err); | |
console.log('\x1b[31m', err.message); | |
if (deleteFilesIfDamaged) { | |
deleteFiles(versionName) | |
.then(() => { | |
process.exit(1); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment