Skip to content

Commit 68b5d10

Browse files
authored
Merge pull request #295 from hiddewie/concurrent-download
feat: allow concurrent downloads by downloading file and moving it
2 parents 5c6c7a7 + e97bc23 commit 68b5d10

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

apps/generator-cli/src/app/services/version-manager.service.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ describe('VersionManagerService', () => {
377377

378378
beforeEach(async () => {
379379
data.pipe.mockReset();
380+
fs.mkdtempSync.mockReset().mockReturnValue('/tmp/generator-cli-abcDEF');
380381
fs.ensureDirSync.mockReset();
381382
fs.createWriteStream.mockReset().mockReturnValue(file);
382383

@@ -439,8 +440,17 @@ describe('VersionManagerService', () => {
439440
expect(fs.ensureDirSync).toHaveBeenNthCalledWith(1, fixture.storage);
440441
});
441442

443+
444+
it('creates a temporary directory', () => {
445+
expect(fs.mkdtempSync).toHaveBeenNthCalledWith(1, '/tmp/generator-cli-');
446+
});
447+
442448
it('creates the correct write stream', () => {
443-
expect(fs.createWriteStream).toHaveBeenNthCalledWith(1, `${fixture.storage}/4.2.0.jar`);
449+
expect(fs.createWriteStream).toHaveBeenNthCalledWith(1, '/tmp/generator-cli-abcDEF/4.2.0');
450+
});
451+
452+
it('moves the file to the target location', () => {
453+
expect(fs.moveSync).toHaveBeenNthCalledWith(1, '/tmp/generator-cli-abcDEF/4.2.0', `${fixture.storage}/4.2.0.jar`);
444454
});
445455

446456
it('receives the data piped', () => {

apps/generator-cli/src/app/services/version-manager.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,14 @@ export class VersionManagerService {
111111
.get<Stream>(downloadLink, { responseType: 'stream' })
112112
.pipe(switchMap(res => new Promise(resolve => {
113113
fs.ensureDirSync(this.storage);
114-
const file = fs.createWriteStream(filePath);
114+
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'generator-cli-'));
115+
const temporaryFilePath = path.join(temporaryDirectory, versionName);
116+
const file = fs.createWriteStream(temporaryFilePath);
115117
res.data.pipe(file);
116-
file.on('finish', resolve);
118+
file.on('finish', content => {
119+
fs.moveSync(temporaryFilePath, filePath);
120+
resolve(content);
121+
});
117122
})
118123
)).toPromise();
119124

0 commit comments

Comments
 (0)