Skip to content

Commit

Permalink
feat(cli): extract compression service (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jul 14, 2023
1 parent b11c5dd commit 3fc9c29
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
32 changes: 32 additions & 0 deletions src/cli/services/compression.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { execa } from 'execa';
import { injectable } from 'inversify';
import tar from 'tar';

export interface ExtractConfig {
file: string;
cwd: string;
strip?: number;

files?: string[];
}

@injectable()
export class CompressionService {
async extract({ file, cwd, strip, files }: ExtractConfig): Promise<void> {
if (file.endsWith('.zip')) {
await execa('bsdtar', [
'-xf',
file,
'-C',
cwd,
...(strip ? ['--strip', `${strip}`] : []),
...(files ?? []),
]);
return;
}

await pipeline(createReadStream(file), tar.x({ cwd, strip }, files));
}
}
10 changes: 9 additions & 1 deletion src/cli/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { Container } from 'inversify';
import { CompressionService } from './compression.service';
import { EnvService } from './env.service';
import { HttpService } from './http.service';
import { PathService } from './path.service';
import { VersionService } from './version.service';

export { EnvService, PathService, VersionService, HttpService };
export {
EnvService,
PathService,
VersionService,
HttpService,
CompressionService,
};

export const rootContainer = new Container();

rootContainer.bind(EnvService).toSelf();
rootContainer.bind(PathService).toSelf();
rootContainer.bind(VersionService).toSelf();
rootContainer.bind(HttpService).toSelf();
rootContainer.bind(CompressionService).toSelf();
12 changes: 9 additions & 3 deletions src/cli/tools/dart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { inject, injectable } from 'inversify';
import semver from 'semver';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
import { PrepareToolBaseService } from '../../prepare-tool/prepare-tool-base.service';
import { EnvService, HttpService, PathService } from '../../services';
import {
CompressionService,
EnvService,
HttpService,
PathService,
} from '../../services';

// Dart SDK sample urls
// https://storage.googleapis.com/dart-archive/channels/stable/release/1.11.0/sdk/dartsdk-linux-x64-release.zip
Expand Down Expand Up @@ -60,7 +65,8 @@ export class InstallDartService extends InstallToolBaseService {
constructor(
@inject(EnvService) envSvc: EnvService,
@inject(PathService) pathSvc: PathService,
@inject(HttpService) private http: HttpService
@inject(HttpService) private http: HttpService,
@inject(CompressionService) private compress: CompressionService
) {
super(pathSvc, envSvc);
}
Expand Down Expand Up @@ -91,7 +97,7 @@ export class InstallDartService extends InstallToolBaseService {
});

const path = await this.pathSvc.createVersionedToolPath(this.name, version);
await execa('bsdtar', ['-xf', file, '-C', path, '--strip', '1']);
await this.compress.extract({ file, cwd: path, strip: 1 });
}

override async link(version: string): Promise<void> {
Expand Down
23 changes: 14 additions & 9 deletions src/cli/tools/docker/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createReadStream } from 'node:fs';
import fs from 'node:fs/promises';
import { join } from 'node:path';
import { pipeline } from 'node:stream/promises';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import tar from 'tar';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
import { PrepareToolBaseService } from '../../prepare-tool/prepare-tool-base.service';
import { EnvService, HttpService, PathService } from '../../services';
import {
CompressionService,
EnvService,
HttpService,
PathService,
} from '../../services';

@injectable()
export class PrepareDockerService extends PrepareToolBaseService {
Expand Down Expand Up @@ -39,7 +41,8 @@ export class InstallDockerService extends InstallToolBaseService {
constructor(
@inject(EnvService) envSvc: EnvService,
@inject(PathService) pathSvc: PathService,
@inject(HttpService) private http: HttpService
@inject(HttpService) private http: HttpService,
@inject(CompressionService) private compress: CompressionService
) {
super(pathSvc, envSvc);
}
Expand All @@ -53,10 +56,12 @@ export class InstallDockerService extends InstallToolBaseService {
'bin'
);
await fs.mkdir(path);
await pipeline(
createReadStream(file),
tar.x({ cwd: path, strip: 1 }, ['docker/docker'])
);
await this.compress.extract({
file,
cwd: path,
strip: 1,
files: ['docker/docker'],
});
}

override async link(version: string): Promise<void> {
Expand Down

0 comments on commit 3fc9c29

Please sign in to comment.