From 380149da272220a5850e9bc0141171779e3ca2cd Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 24 Jan 2023 02:08:34 +0100 Subject: [PATCH] buildx: reduce calls to getVersion Signed-off-by: CrazyMax --- __tests__/buildx.test.ts | 3 +-- src/buildx.ts | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/__tests__/buildx.test.ts b/__tests__/buildx.test.ts index 0db98af..8b282f8 100644 --- a/__tests__/buildx.test.ts +++ b/__tests__/buildx.test.ts @@ -169,8 +169,7 @@ describe('printVersion', () => { describe('getVersion', () => { it('valid', async () => { const buildx = new Buildx(); - const version = await buildx.getVersion(); - expect(semver.valid(version)).not.toBeNull(); + expect(semver.valid(await buildx.version())).not.toBeNull(); }); }); diff --git a/src/buildx.ts b/src/buildx.ts index 5056598..922e8b2 100644 --- a/src/buildx.ts +++ b/src/buildx.ts @@ -13,17 +13,17 @@ export interface BuildxOpts { } export class Buildx { - private standalone: boolean; - private version: Promise; - private tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep); + private _standalone: boolean; + private _version: Promise; + private _tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep); constructor(opts?: BuildxOpts) { - this.standalone = opts?.standalone ?? !Docker.isAvailable(); - this.version = this.getVersion(); + this._standalone = opts?.standalone ?? !Docker.isAvailable(); + this._version = this.getVersion(); } public tmpDir() { - return this.tmpdir; + return this._tmpdir; } public tmpName(options?: tmp.TmpNameOptions): string { @@ -32,8 +32,8 @@ export class Buildx { public getCommand(args: Array) { return { - command: this.standalone ? 'buildx' : 'docker', - args: this.standalone ? args : ['buildx', ...args] + command: this._standalone ? 'buildx' : 'docker', + args: this._standalone ? args : ['buildx', ...args] }; } @@ -56,7 +56,7 @@ export class Buildx { }); } - public async getVersion(): Promise { + private async getVersion(): Promise { const cmd = this.getCommand(['version']); return await exec .getExecOutput(cmd.command, cmd.args, { @@ -71,6 +71,10 @@ export class Buildx { }); } + public async version(): Promise { + return this._version; + } + public async printVersion() { const cmd = this.getCommand(['version']); await exec.exec(cmd.command, cmd.args, { @@ -87,7 +91,7 @@ export class Buildx { } public async versionSatisfies(range: string, version?: string): Promise { - const ver = version ?? (await this.getVersion()); + const ver = version ?? (await this.version()); return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null; }