Skip to content

Commit

Permalink
Add single-bucket sync to use subcommand to match sls deploy function…
Browse files Browse the repository at this point in the history
… syntax; update metadata sync to support bucketNameKey format
  • Loading branch information
ziktar committed Mar 15, 2020
1 parent b155d16 commit 1893405
Showing 1 changed file with 63 additions and 29 deletions.
92 changes: 63 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,32 @@ class ServerlessS3Sync {
lifecycleEvents: [
'sync',
'metadata'
]
],
commands: {
bucket: {
options: {
bucket: {
usage: 'Specify the bucket you want to deploy (e.g. "-b myBucket1")',
required: true,
shortcut: 'b'
}
},
lifecycleEvents: [
'sync',
'metadata'
]
}
}
}
};

this.hooks = {
'after:deploy:deploy': () => options.nos3sync ? undefined : BbPromise.bind(this).then(this.sync).then(this.syncMetadata),
'before:remove:remove': () => BbPromise.bind(this).then(this.clear),
's3sync:sync': () => BbPromise.bind(this).then(this.sync),
's3sync:metadata': () => BbPromise.bind(this).then(this.syncMetadata)
's3sync:metadata': () => BbPromise.bind(this).then(this.syncMetadata),
's3sync:bucket:sync': () => BbPromise.bind(this).then(this.sync),
's3sync:bucket:metadata': () => BbPromise.bind(this).then(this.syncMetadata)
};
}

Expand Down Expand Up @@ -66,7 +83,11 @@ class ServerlessS3Sync {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
return Promise.resolve();
}
cli.consoleLog(`${messagePrefix}${chalk.yellow('Syncing directories and S3 prefixes...')}`);
if (this.options.bucket) {
cli.consoleLog(`${messagePrefix}${chalk.yellow(`Syncing directory attached to S3 bucket ${this.options.bucket}...`)}`);
} else {
cli.consoleLog(`${messagePrefix}${chalk.yellow('Syncing directories and S3 prefixes...')}`);
}
const servicePath = this.servicePath;
const promises = s3Sync.map((s) => {
let bucketPrefix = '';
Expand Down Expand Up @@ -95,6 +116,11 @@ class ServerlessS3Sync {

return this.getBucketName(s)
.then(bucketName => {
if (this.options.bucket && bucketName != this.options.bucket) {
// if the bucket option is given, that means we're in the subcommand where we're
// only syncing one bucket, so only continue if this bucket name matches
return null;
}
return new Promise((resolve) => {
const localDir = [servicePath, s.localDir].join('/');

Expand Down Expand Up @@ -235,35 +261,43 @@ class ServerlessS3Sync {
});
});
}
return filesToSync.forEach((file) => {
return new Promise((resolve) => {
let contentTypeObject = {};
let detectedContentType = mime.getType(file.name)
if (detectedContentType !== null || s.hasOwnProperty('defaultContentType')) {
contentTypeObject.ContentType = detectedContentType ? detectedContentType : s.defaultContentType;
return this.getBucketName(s)
.then(bucketName => {
if (this.options && this.options.bucket && bucketName != this.options.bucket) {
// if the bucket option is given, that means we're in the subcommand where we're
// only syncing one bucket, so only continue if this bucket name matches
return null;
}
let params = {
...contentTypeObject,
...file.params,
...{
CopySource: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${s.bucketName}${bucketPrefix == '' ? '' : bucketPrefix}/`)),
Key: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, '')),
Bucket: s.bucketName,
ACL: acl,
MetadataDirective: 'REPLACE'
}
};
const uploader = this.client().copyObject(params);
uploader.on('error', (err) => {
throw err;
});
uploader.on('end', () => {
resolve('done');
});

return Promise.all(filesToSync.map((file) => {
return new Promise((resolve) => {
let contentTypeObject = {};
let detectedContentType = mime.getType(file.name)
if (detectedContentType !== null || s.hasOwnProperty('defaultContentType')) {
contentTypeObject.ContentType = detectedContentType ? detectedContentType : s.defaultContentType;
}
let params = {
...contentTypeObject,
...file.params,
...{
CopySource: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, `${bucketName}${bucketPrefix == '' ? '' : bucketPrefix}/`)),
Key: toS3Path(file.name.replace(path.resolve(localDir) + path.sep, '')),
Bucket: bucketName,
ACL: acl,
MetadataDirective: 'REPLACE'
}
};
const uploader = this.client().copyObject(params);
uploader.on('error', (err) => {
throw err;
});
uploader.on('end', () => {
resolve('done');
});
});
}));
});
});
});
cli.consoleLog(`${JSON.stringify(promises)}`);
return Promise.all((promises))
.then(() => {
cli.printDot();
Expand Down

0 comments on commit 1893405

Please sign in to comment.