Skip to content

Commit

Permalink
quick and dirty support for a deployPrefix
Browse files Browse the repository at this point in the history
Related issue: gatsby-uc#24
  • Loading branch information
darcy authored Jul 11, 2019
1 parent 670fb43 commit 6de9804
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ const getParams = (path: string, params: Params): Partial<S3.Types.PutObjectRequ
return returned;
};

const listAllObjects = async (s3: S3, bucketName: string): Promise<ObjectList> => {
const listAllObjects = async (s3: S3, bucketName: string, deployPrefix: string): Promise<ObjectList> => {
const list: ObjectList = [];

let token: NextToken | undefined;
do {
const response = await s3.listObjectsV2({
Bucket: bucketName,
ContinuationToken: token,
Prefix: deployPrefix
}).promise();

if (response.Contents) {
Expand Down Expand Up @@ -192,7 +193,7 @@ const deploy = async ({ yes, bucket }: { yes: boolean, bucket: string }) => {

spinner.text = 'Listing objects...';
spinner.color = 'green';
const objects = await listAllObjects(s3, config.bucketName);
const objects = await listAllObjects(s3, config.bucketName, config.deployPrefix);

spinner.color = 'cyan';
spinner.text = 'Syncing...';
Expand All @@ -206,6 +207,9 @@ const deploy = async ({ yes, bucket }: { yes: boolean, bucket: string }) => {
}
uploadQueue.push(asyncify(async () => {
const key = createSafeS3Key(relative(publicDir, path));
if (config.deployPrefix) {
key = config.deployPrefix + "/" + key;
}
const readStream = fs.createReadStream(path);
const hashStream = readStream.pipe(createHash('md5').setEncoding('hex'));
const data = await streamToPromise(hashStream);
Expand Down Expand Up @@ -257,6 +261,10 @@ const deploy = async ({ yes, bucket }: { yes: boolean, bucket: string }) => {
key = join(key, 'index.html');
}
key = createSafeS3Key(key);
if (config.deployPrefix) {
key = config.deployPrefix + "/" + key;
}


const tag = `"${createHash('md5').update(redirectLocation).digest('hex')}"`;
const object = objects.find(currObj => currObj.Key === key && currObj.ETag === tag);
Expand Down

0 comments on commit 6de9804

Please sign in to comment.