Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a keyPrefix option #76

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ class Client {
}

_removeDeployedResources() {
let bucketName, manageResources;

let bucketName, manageResources, keyPrefix;

return this._validateConfig()
.then(() => {
bucketName = this.options.bucketName;
keyPrefix = this.options.keyPrefix;
manageResources = this.options.manageResources;
return this.cliOptions.confirm === false
? true
Expand All @@ -76,24 +78,32 @@ class Client {
if (exists) {
this.serverless.cli.log(`Deleting all objects from bucket...`);
return bucketUtils
.emptyBucket(this.aws, bucketName)
.emptyBucket(this.aws, bucketName, keyPrefix)
.then(() => {
if (manageResources === false) {
this.serverless.cli.log(
'manageResources has been set to "false". Bucket will not be deleted'
);
if (keyPrefix) {
this.serverless.cli.log(`Removed only the files under the prefix ${keyPrefix}`);
return true;
} else {
this.serverless.cli.log(`Removing bucket...`);
return bucketUtils.deleteBucket(this.aws, bucketName);
if (manageResources === false) {
this.serverless.cli.log(
'manageResources has been set to "false". Bucket will not be deleted'
);
} else {
this.serverless.cli.log(`Removing bucket...`);
return bucketUtils.deleteBucket(this.aws, bucketName);
}
}
})
.then(() => {

if (manageResources === false) {
this.serverless.cli.log(`Success! Your files have been removed`);
} else {
this.serverless.cli.log(
`Success! Your files have been removed and your bucket has been deleted`
);
if (!keyPrefix) {
this.serverless.cli.log(
`Success! Your files have been removed and your bucket has been deleted`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a logging statement here for if(keyPrefix)

Let's give the user clarification that files from the keyPrefix are deleted.

);
}
}
});
} else {
Expand All @@ -119,6 +129,7 @@ class Client {
indexDoc,
errorDoc,
redirectAllRequestsTo,
keyPrefix,
routingRules,
manageResources;

Expand All @@ -141,6 +152,7 @@ class Client {
distributionFolder = this.options.distributionFolder || path.join('client/dist');
clientPath = path.join(this.serverless.config.servicePath, distributionFolder);
bucketName = this.options.bucketName;
keyPrefix = this.options.keyPrefix;
manageResources = this.options.manageResources;
headerSpec = this.options.objectHeaders;
orderSpec = this.options.uploadOrder;
Expand Down Expand Up @@ -187,7 +199,7 @@ class Client {
}

this.serverless.cli.log(`Deleting all objects from bucket...`);
return bucketUtils.emptyBucket(this.aws, bucketName);
return bucketUtils.emptyBucket(this.aws, bucketName, keyPrefix);
} else {
if (manageResources === false) {
return BbPromise.reject(
Expand Down Expand Up @@ -234,7 +246,14 @@ class Client {
})
.then(() => {
this.serverless.cli.log(`Uploading client files to bucket...`);
return uploadDirectory(this.aws, bucketName, clientPath, headerSpec, orderSpec);
return uploadDirectory(
this.aws,
bucketName,
clientPath,
headerSpec,
orderSpec,
keyPrefix
);
})
.then(() => {
this.serverless.cli.log(
Expand Down
12 changes: 9 additions & 3 deletions lib/bucketUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ function deleteBucket(aws, bucketName) {
* @param {Object} aws - AWS class
* @param {string} bucketName - Name of bucket to be deleted
*/
function emptyBucket(aws, bucketName) {
function emptyBucket(aws, bucketName, keyPrefix) {
return listObjectsInBucket(aws, bucketName).then(resp => {
const contents = resp.Contents;

let testPrefix = false,
prefixRegexp;
if (!contents[0]) {
return Promise.resolve();
} else {

if (keyPrefix) {
testPrefix = true;
prefixRegexp = new RegExp('^' + keyPrefix);
}
const objects = contents.map(function(content) {
return {Key: content.Key};
});
}).filter(content => !testPrefix || prefixRegexp.test(content.Key));

const params = {
Bucket: bucketName,
Expand Down
20 changes: 10 additions & 10 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ const minimatch = require('minimatch');
* @param {string} clientRoot - Full path to the root directory of client files
* @param {Object[]} headerSpec - Array of header values to add to files
* @param {string[]} orderSpec - Array of regex's to order upload by
* @param {string} keyPrefix - A prefix for all files uploaded
*/
function uploadDirectory(aws, bucketName, clientRoot, headerSpec, orderSpec) {
function uploadDirectory(aws, bucketName, clientRoot, headerSpec, orderSpec, keyPrefix) {
const allFiles = getFileList(clientRoot);

const filesGroupedByOrder = groupFilesByOrder(allFiles, orderSpec);

return filesGroupedByOrder.reduce((existingUploads, files) => {
return existingUploads.then(existingResults => {
const uploadList = buildUploadList(files, clientRoot, headerSpec);
const uploadList = buildUploadList(files, clientRoot, headerSpec, keyPrefix);

return Promise.all(
uploadList.map(u => uploadFile(aws, bucketName, u.filePath, u.fileKey, u.headers))
Expand Down Expand Up @@ -75,23 +76,22 @@ function uploadFile(aws, bucketName, filePath, fileKey, headers) {
return aws.request('S3', 'putObject', params);
}

function buildUploadList(files, clientRoot, headerSpec) {
function buildUploadList(files, clientRoot, headerSpec, keyPrefix) {
clientRoot = path.normalize(clientRoot);
if (!clientRoot.endsWith(path.sep)) {
clientRoot += path.sep;
}

const prefix = keyPrefix.split(path.sep);
const uploadList = files.map(f => {
const filePath = path.normalize(f);
const fileRelPath = filePath.replace(clientRoot, '');
const fileKey = path
.normalize(fileRelPath)
.split(path.sep)
.join('/');

const fileKey = path.normalize(fileRelPath).split(path.sep);
if (prefix.length) {
fileKey.unshift(prefix);
}
const upload = {
filePath: filePath,
fileKey: fileKey,
fileKey: fileKey.join('/'),
headers: {}
};

Expand Down