s3-ng6-upload
Version:
Allows you to upload angular 6 apps to S3 buckets.
89 lines (74 loc) • 3.06 kB
JavaScript
function TestHook(GlobRunner,RemoteRunner,SyncedFileCollection,S3PromiseWrapper,AWS,fileUtils){
GlobRunner = GlobRunner || require('./GlobRunner.js');
RemoteRunner = RemoteRunner || require('./RemoteRunner.js');
SyncedFileCollection = SyncedFileCollection || require('./SyncedFileCollection.js');
S3PromiseWrapper = S3PromiseWrapper || require('./S3PromiseWrapper.js');
fileUtils = fileUtils || require('./file-utils.js');
AWS = AWS || require('aws-sdk');
var S3 = AWS.S3;
return function ConfigRunner(_pathfrom,_assets){
var config;
this.setConfig = function(conf){
config = conf;
return this;
};
console.log(_pathfrom);
console.log(_assets);
this.run = function(){
config.credentials && AWS.config.loadFromPath(config.credentials);
var s3 = new S3();
var s3Wrapper = new S3PromiseWrapper(s3);
var collection = new SyncedFileCollection();
var globRunner = new GlobRunner(collection);
var remoteRunner = new RemoteRunner(config.bucketName,collection,s3Wrapper);
var patterns = config.patterns;
for(var i = 0; i < patterns.length; i ++){
globRunner.addPattern(patterns[i]);
}
// config.patterns.forEach(globRunner.addPattern);
remoteRunner.run();
globRunner.run();
collection.allDone.then(function(actions){
var deletes = [];
actions.forEach(function(obj){
switch(obj.action){
case 'delete':
var awsPath = obj.path;
if (!_assets && awsPath.startsWith('assets/')){
break;
}
console.log(awsPath + 'added to delete after');
deletes.push(awsPath);
break;
case 'upload':
fileUtils.getContents(obj.path).then(function(contents){
var awsPath = obj.path.substring(6+_pathfrom.length);
if (!_assets && awsPath.startsWith('assets/')){
console.log('ignored: ' + obj.path);
}
else {
console.log('uploading: ' + obj.path + ' to ' + awsPath);
s3Wrapper.putObject(config.bucketName,awsPath,contents).then(function(){
console.log('done uploading: ' + awsPath);
},function(reason){
console.log('error uploading: ' + awsPath);
console.log(reason);
});
}
});
}
});
if(deletes.length !== 0) {
console.log('deleting the following: ');
deletes.forEach(function(path){console.log('\t' + path)});
s3Wrapper.deleteObjects(config.bucketName,deletes).then(
function(){console.log('delete successful')},
function(reason){console.log('delete failed ' + reason); console.log(reason); });
}
});
};
};
}
var ConfigRunner = TestHook();
ConfigRunner.TestHook = TestHook;
module.exports = ConfigRunner;