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

Commit e0e5b71

Browse files
committed
feat(all): Change how params are handled
Make all params but those that are mandatory props of an object
1 parent 94753d6 commit e0e5b71

4 files changed

Lines changed: 72 additions & 42 deletions

File tree

src/cloudfront.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ function invalidateDistribution(distId, files) {
3434
if (err) {
3535
reject(common.handleAwsError(err));
3636
} else {
37-
resolve(`Invalidation with ID ${data.Invalidation.Id} has started for ${files.length} changed files!`);
37+
const res = {
38+
message: `Invalidation with ID ${data.Invalidation.Id} has started for ${files.length} changed files!`,
39+
changedFiles: files,
40+
};
41+
resolve(res);
3842
}
3943
});
4044
});

src/s3.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function lookupFileType(filePath) {
9292
});
9393
}
9494

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

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

133133
if (itemsProcessed >= nextIncrement) {
134134
nextIncrement += barIncrement;
135-
if (isCli) {
135+
if (additionalParams && additionalParams.cli) {
136136
bar.tick();
137137
}
138138
}
139139

140140
if (itemsProcessed === fileListLength) {
141141
let i;
142-
if (isCli) {
142+
if (additionalParams && additionalParams.cli) {
143143
for (i = bar.total - bar.curr; i >= 0; i--) {
144144
bar.tick();
145145

@@ -161,7 +161,7 @@ function uploadFiles(pathToUpload, fileList, bucketName, verboseMode, isCli) {
161161
});
162162
}
163163

164-
function uploadChangedFilesInDir(pathToUpload, bucketName, verboseMode, isCli) {
164+
function uploadChangedFilesInDir(pathToUpload, bucketName, additionalParams) {
165165
return new Promise((resolve, reject) => {
166166
const changedFiles = [];
167167
recursive(pathToUpload, (err, fileList) => {
@@ -198,7 +198,7 @@ function uploadChangedFilesInDir(pathToUpload, bucketName, verboseMode, isCli) {
198198
if (changedFiles.length > 0) {
199199
// eslint-disable-next-line no-console
200200
console.log(chalk.yellow(`${fileListLength} objects found, ${changedFiles.length} objects require updates...`));
201-
uploadFiles(pathToUpload, changedFiles, bucketName, verboseMode, isCli)
201+
uploadFiles(pathToUpload, changedFiles, bucketName, additionalParams)
202202
.then((msg) => {
203203
resolve({
204204
changedFiles,

test/testCloudfront.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,52 @@ describe('cloudfront.js [Unit]', () => {
2222

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

28-
const invalidateDistribution = cloudfront.__get__('invalidateDistribution');
29+
const invalidateDistribution = cloudfront.__get__('invalidateDistribution');
2930

30-
class CloudFrontMock {
31-
// eslint-disable-next-line no-empty-function,no-useless-constructor
32-
constructor() {
31+
class CloudFrontMock {
32+
// eslint-disable-next-line no-empty-function,no-useless-constructor
33+
constructor() {
34+
}
35+
36+
// eslint-disable-next-line class-methods-use-this
37+
createInvalidation(params, callback) {
38+
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
39+
}
3340
}
41+
sinon.stub(CloudFrontMock.prototype, 'constructor');
3442

35-
// eslint-disable-next-line class-methods-use-this
36-
createInvalidation(params, callback) {
37-
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
43+
cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
44+
const files = ['/foo.txt,', 'bar.yml'];
45+
const res = await invalidateDistribution('foo', files);
46+
res.message.should.equal('Invalidation with ID INVALIDATION_ID has started for 2 changed files!');
47+
});
48+
49+
it('with key containing files invalidated in the invalidation', async () => {
50+
const cloudfront = rewire('../src/cloudfront');
51+
52+
const invalidateDistribution = cloudfront.__get__('invalidateDistribution');
53+
54+
class CloudFrontMock {
55+
// eslint-disable-next-line no-empty-function,no-useless-constructor
56+
constructor() {
57+
}
58+
59+
// eslint-disable-next-line class-methods-use-this
60+
createInvalidation(params, callback) {
61+
callback(null, { Invalidation: { Id: 'INVALIDATION_ID' } });
62+
}
3863
}
39-
}
40-
sinon.stub(CloudFrontMock.prototype, 'constructor');
64+
sinon.stub(CloudFrontMock.prototype, 'constructor');
4165

42-
cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
43-
const msg = await invalidateDistribution('foo', ['/foo.txt,', 'bar.yml']);
44-
msg.should.equal('Invalidation with ID INVALIDATION_ID has started for 2 changed files!');
66+
cloudfront.__set__({ AWS: { CloudFront: CloudFrontMock } });
67+
const files = ['/foo.txt,', 'bar.yml'];
68+
const res = await invalidateDistribution('foo', files);
69+
res.changedFiles.should.equal(files);
70+
});
4571
});
4672
});
4773

test/testS3.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('s3.js [Unit]', () => {
376376
for (i = 0; i < 2; i++) {
377377
dummyArr[i] = `foo${i}.txt`;
378378
}
379-
await uploadFiles('path/to', dummyArr, 'yourBucket', false, true);
379+
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: true });
380380
constructorCallCount.should.equal(1);
381381
});
382382
});
@@ -433,7 +433,7 @@ describe('s3.js [Unit]', () => {
433433
for (i = 0; i < 9; i++) {
434434
dummyArr[i] = `foo${i}.txt`;
435435
}
436-
await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
436+
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: false, verbose: true });
437437
(logSpy.callCount).should.equal(9);
438438
});
439439
});
@@ -485,7 +485,7 @@ describe('s3.js [Unit]', () => {
485485
};
486486
s3.__set__({ console: consoleMock });
487487

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

557-
await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
557+
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
558558
sinon.assert.calledWith(lookupFileTypeStub, 'path/to/foo.txt');
559559
});
560560

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

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

661661
try {
662-
await uploadFiles('path/to', ['foo.txt'], 'yourBucket', true, true);
662+
await uploadFiles('path/to', ['foo.txt'], 'yourBucket');
663663
'I'.should.not.equal('be called');
664664
} catch (e) {
665665
e.message.should.equal('upload error');
@@ -720,7 +720,7 @@ describe('s3.js [Unit]', () => {
720720
for (i = 0; i < 100; i++) {
721721
dummyArr[i] = `foo${i}.txt`;
722722
}
723-
await uploadFiles('path/to', dummyArr, 'yourBucket', true, false);
723+
await uploadFiles('path/to', dummyArr, 'yourBucket');
724724
tickCallCount.should.equal(0);
725725
});
726726

@@ -773,7 +773,7 @@ describe('s3.js [Unit]', () => {
773773
for (i = 0; i < 100; i++) {
774774
dummyArr[i] = `foo${i}.txt`;
775775
}
776-
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket', true, false);
776+
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket');
777777
actual.should.equal('Upload complete!');
778778
});
779779
});
@@ -830,7 +830,7 @@ describe('s3.js [Unit]', () => {
830830
for (i = 0; i < 100; i++) {
831831
dummyArr[i] = `foo${i}.txt`;
832832
}
833-
await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
833+
await uploadFiles('path/to', dummyArr, 'yourBucket', { cli: true });
834834
tickCallCount.should.equal(61);
835835
});
836836

@@ -883,7 +883,7 @@ describe('s3.js [Unit]', () => {
883883
for (i = 0; i < 100; i++) {
884884
dummyArr[i] = `foo${i}.txt`;
885885
}
886-
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket', true, true);
886+
const actual = await uploadFiles('path/to', dummyArr, 'yourBucket');
887887
actual.should.equal('Upload complete!');
888888
});
889889
});
@@ -910,7 +910,7 @@ describe('s3.js [Unit]', () => {
910910

911911
s3.__set__('hasFileChanged', hasFileChangedStub);
912912

913-
const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
913+
const actual = await uploadChangedFilesInDir('base', bucketName);
914914

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

933933
s3.__set__('hasFileChanged', hasFileChangedStub);
934934

935-
const actual = await uploadChangedFilesInDir('base', bucketName, false, false);
935+
const actual = await uploadChangedFilesInDir('base', bucketName);
936936

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

968968
s3.__set__('uploadFiles', uploadFilesStub);
969969

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

10011001
s3.__set__('uploadFiles', uploadFilesStub);
10021002

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

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

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

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

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

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

11321132
try {
1133-
await uploadChangedFilesInDir('base', 'myBucket', false, false);
1133+
await uploadChangedFilesInDir('base', 'myBucket');
11341134
'I'.should.equal('not called');
11351135
} catch (e) {
11361136
e.message.should.equal('Specified path does not exist. Please specify a valid directory path.');

0 commit comments

Comments
 (0)