Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
feat(all): Change how params are handled
Browse files Browse the repository at this point in the history
Make all params but those that are mandatory props of an object
  • Loading branch information
ReidWeb committed Sep 15, 2018
1 parent 94753d6 commit e0e5b71
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 42 deletions.
6 changes: 5 additions & 1 deletion src/cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function invalidateDistribution(distId, files) {
if (err) {
reject(common.handleAwsError(err));
} else {
resolve(`Invalidation with ID ${data.Invalidation.Id} has started for ${files.length} changed files!`);
const res = {
message: `Invalidation with ID ${data.Invalidation.Id} has started for ${files.length} changed files!`,
changedFiles: files,
};
resolve(res);
}
});
});
Expand Down
14 changes: 7 additions & 7 deletions src/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function lookupFileType(filePath) {
});
}

function uploadFiles(pathToUpload, fileList, bucketName, verboseMode, isCli) {
function uploadFiles(pathToUpload, fileList, bucketName, additionalParams) {
const s3 = new AWS.S3();
return new Promise((resolve, reject) => {
const fileListLength = fileList.length;
Expand All @@ -101,7 +101,7 @@ function uploadFiles(pathToUpload, fileList, bucketName, verboseMode, isCli) {
let itemsProcessed = 0;
let bar;

if (isCli) {
if (additionalParams && additionalParams.cli) {
bar = new ProgressBar('Uploading [:bar] :percent', {
total: BAR_SEGMENTS,
clear: true,
Expand All @@ -124,22 +124,22 @@ function uploadFiles(pathToUpload, fileList, bucketName, verboseMode, isCli) {
};
uploadObj(params, s3, 0)
.then((fileName) => {
if (verboseMode) {
if (additionalParams && additionalParams.verbose) {
// eslint-disable-next-line no-console
console.log(chalk.green(`Successfully uploaded ${fileName} to ${bucketName}`));
}
itemsProcessed++;

if (itemsProcessed >= nextIncrement) {
nextIncrement += barIncrement;
if (isCli) {
if (additionalParams && additionalParams.cli) {
bar.tick();
}
}

if (itemsProcessed === fileListLength) {
let i;
if (isCli) {
if (additionalParams && additionalParams.cli) {
for (i = bar.total - bar.curr; i >= 0; i--) {
bar.tick();

Expand All @@ -161,7 +161,7 @@ function uploadFiles(pathToUpload, fileList, bucketName, verboseMode, isCli) {
});
}

function uploadChangedFilesInDir(pathToUpload, bucketName, verboseMode, isCli) {
function uploadChangedFilesInDir(pathToUpload, bucketName, additionalParams) {
return new Promise((resolve, reject) => {
const changedFiles = [];
recursive(pathToUpload, (err, fileList) => {
Expand Down Expand Up @@ -198,7 +198,7 @@ function uploadChangedFilesInDir(pathToUpload, bucketName, verboseMode, isCli) {
if (changedFiles.length > 0) {
// eslint-disable-next-line no-console
console.log(chalk.yellow(`${fileListLength} objects found, ${changedFiles.length} objects require updates...`));
uploadFiles(pathToUpload, changedFiles, bucketName, verboseMode, isCli)
uploadFiles(pathToUpload, changedFiles, bucketName, additionalParams)
.then((msg) => {
resolve({
changedFiles,
Expand Down
54 changes: 40 additions & 14 deletions test/testCloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,52 @@ describe('cloudfront.js [Unit]', () => {

describe('#invalidateDistribution()', () => {
describe('when successful', () => {
it('should resolve with a message containing detail on the invalidation', async () => {
const cloudfront = rewire('../src/cloudfront');
describe('should resolve with an object', () => {
it('with message containing detail on the invalidation', async () => {
const cloudfront = rewire('../src/cloudfront');

const invalidateDistribution = cloudfront.__get__('invalidateDistribution');
const invalidateDistribution = cloudfront.__get__('invalidateDistribution');

class CloudFrontMock {
// eslint-disable-next-line no-empty-function,no-useless-constructor
constructor() {
class CloudFrontMock {
// eslint-disable-next-line no-empty-function,no-useless-constructor
constructor() {
}

// eslint-disable-next-line class-methods-use-this
createInvalidation(params, callback) {
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
}
}
sinon.stub(CloudFrontMock.prototype, 'constructor');

// eslint-disable-next-line class-methods-use-this
createInvalidation(params, callback) {
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
const files = ['/foo.txt,', 'bar.yml'];
const res = await invalidateDistribution('foo', files);
res.message.should.equal('Invalidation with ID INVALIDATION_ID has started for 2 changed files!');
});

it('with key containing files invalidated in the invalidation', async () => {
const cloudfront = rewire('../src/cloudfront');

const invalidateDistribution = cloudfront.__get__('invalidateDistribution');

class CloudFrontMock {
// eslint-disable-next-line no-empty-function,no-useless-constructor
constructor() {
}

// eslint-disable-next-line class-methods-use-this
createInvalidation(params, callback) {
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
}
}
}
sinon.stub(CloudFrontMock.prototype, 'constructor');
sinon.stub(CloudFrontMock.prototype, 'constructor');

cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
const msg = await invalidateDistribution('foo', ['/foo.txt,', 'bar.yml']);
msg.should.equal('Invalidation with ID INVALIDATION_ID has started for 2 changed files!');
cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
const files = ['/foo.txt,', 'bar.yml'];
const res = await invalidateDistribution('foo', files);
res.changedFiles.should.equal(files);
});
});
});

Expand Down
40 changes: 20 additions & 20 deletions test/testS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 2; i++) {
dummyArr[i] = `foo${i}.txt`;
}
await uploadFiles('path/to', dummyArr, 'yourBucket', false, true);
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: true });
constructorCallCount.should.equal(1);
});
});
Expand Down Expand Up @@ -433,7 +433,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 9; i++) {
dummyArr[i] = `foo${i}.txt`;
}
await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: false, verbose: true });
(logSpy.callCount).should.equal(9);
});
});
Expand Down Expand Up @@ -485,7 +485,7 @@ describe('s3.js [Unit]', () => {
};
s3.__set__({ console: consoleMock });

await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
const invocationOne = uploadObjStub.getCall(0);
// eslint-disable-next-line prefer-destructuring
interceptedArgs = invocationOne.args[0];
Expand Down Expand Up @@ -554,7 +554,7 @@ describe('s3.js [Unit]', () => {
};
s3.__set__({ console: consoleMock });

await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
sinon.assert.calledWith(lookupFileTypeStub, 'path/to/foo.txt');
});

Expand Down Expand Up @@ -604,7 +604,7 @@ describe('s3.js [Unit]', () => {
s3.__set__({ console: consoleMock });

try {
await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
'I'.should.equal('Not be called');
} catch (e) {
e.message.should.equal('An error');
Expand Down Expand Up @@ -659,7 +659,7 @@ describe('s3.js [Unit]', () => {
s3.__set__({ console: consoleMock });

try {
await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
'I'.should.not.equal('be called');
} catch (e) {
e.message.should.equal('upload error');
Expand Down Expand Up @@ -720,7 +720,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 100; i++) {
dummyArr[i] = `foo${i}.txt`;
}
await uploadFiles('path/to', dummyArr, 'yourBucket', true, false);
await uploadFiles('path/to', dummyArr, 'yourBucket');
tickCallCount.should.equal(0);
});

Expand Down Expand Up @@ -773,7 +773,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 100; i++) {
dummyArr[i] = `foo${i}.txt`;
}
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket', true, false);
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket');
actual.should.equal('Upload complete!');
});
});
Expand Down Expand Up @@ -830,7 +830,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 100; i++) {
dummyArr[i] = `foo${i}.txt`;
}
await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: true });
tickCallCount.should.equal(61);
});

Expand Down Expand Up @@ -883,7 +883,7 @@ describe('s3.js [Unit]', () => {
for (i = 0; i < 100; i++) {
dummyArr[i] = `foo${i}.txt`;
}
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket');
actual.should.equal('Upload complete!');
});
});
Expand All @@ -910,7 +910,7 @@ describe('s3.js [Unit]', () => {

s3.__set__('hasFileChanged', hasFileChangedStub);

const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
const actual = await uploadChangedFilesInDir('base', bucketName);

actual.message.should.equal('No file updates required, skipping upload...');
});
Expand All @@ -932,7 +932,7 @@ describe('s3.js [Unit]', () => {

s3.__set__('hasFileChanged', hasFileChangedStub);

const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
const actual = await uploadChangedFilesInDir('base', bucketName);

actual.changedFiles.length.should.equal(0);
});
Expand Down Expand Up @@ -967,7 +967,7 @@ describe('s3.js [Unit]', () => {

s3.__set__('uploadFiles', uploadFilesStub);

const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
const actual = await uploadChangedFilesInDir('base', bucketName);
actual.changedFiles.should.contain('path/to/hello.json');
actual.changedFiles.should.contain('foo.txt');
actual.changedFiles.should.not.contain('bar.yml');
Expand Down Expand Up @@ -1000,7 +1000,7 @@ describe('s3.js [Unit]', () => {

s3.__set__('uploadFiles', uploadFilesStub);

const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
const actual = await uploadChangedFilesInDir('base', bucketName);
actual.message.should.equal('Mock Upload complete!');
});
});
Expand All @@ -1023,7 +1023,7 @@ describe('s3.js [Unit]', () => {
s3.__set__('hasFileChanged', hasFileChangedStub);

try {
await uploadChangedFilesInDir('base', bucketName, false, false);
await uploadChangedFilesInDir('base', bucketName);
'i'.should.equal('not invoked');
} catch (e) {
e.message.should.equal('my home made error');
Expand Down Expand Up @@ -1061,7 +1061,7 @@ describe('s3.js [Unit]', () => {
s3.__set__('uploadFiles', uploadFilesStub);

try {
await uploadChangedFilesInDir('base', bucketName, false, false);
await uploadChangedFilesInDir('base', bucketName);
'i'.should.equal('not invoked');
} catch (e) {
e.message.should.equal('mocked error uploading!');
Expand All @@ -1079,7 +1079,7 @@ describe('s3.js [Unit]', () => {
recursiveStub.yields(null, []);
s3.__set__('recursive', recursiveStub);

const actual = await uploadChangedFilesInDir('base', 'myBucket', false, false);
const actual = await uploadChangedFilesInDir('base', 'myBucket');
actual.message.should.equal('No files found at specified path');
});
});
Expand All @@ -1094,7 +1094,7 @@ describe('s3.js [Unit]', () => {
s3.__set__('recursive', recursiveStub);

try {
await uploadChangedFilesInDir('base', 'myBucket', false, false);
await uploadChangedFilesInDir('base', 'myBucket');
'I'.should.equal('not called');
} catch (e) {
e.message.should.equal('Generic error message');
Expand All @@ -1112,7 +1112,7 @@ describe('s3.js [Unit]', () => {
s3.__set__('recursive', recursiveStub);

try {
await uploadChangedFilesInDir('base', 'myBucket', false, false);
await uploadChangedFilesInDir('base', 'myBucket');
'I'.should.equal('not called');
} catch (e) {
e.message.should.equal('Specified path is not a directory, please specify a valid directory path - this module cannot process individual files');
Expand All @@ -1130,7 +1130,7 @@ describe('s3.js [Unit]', () => {
s3.__set__('recursive', recursiveStub);

try {
await uploadChangedFilesInDir('base', 'myBucket', false, false);
await uploadChangedFilesInDir('base', 'myBucket');
'I'.should.equal('not called');
} catch (e) {
e.message.should.equal('Specified path does not exist. Please specify a valid directory path.');
Expand Down

0 comments on commit e0e5b71

Please sign in to comment.