From 29f7388d53eda9267be2a59204928ff3db17b628 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Thu, 14 Nov 2019 16:45:30 -0500 Subject: [PATCH] Clean up args and arrange imports --- src/cacheHttpClient.ts | 6 ++++-- src/restore.ts | 22 ++++++++++++---------- src/save.ts | 25 +++++++++++++------------ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/cacheHttpClient.ts b/src/cacheHttpClient.ts index b3885e4..e448157 100644 --- a/src/cacheHttpClient.ts +++ b/src/cacheHttpClient.ts @@ -93,9 +93,11 @@ export async function downloadCache( } export async function saveCache( - stream: NodeJS.ReadableStream, - key: string + key: string, + archivePath: string ): Promise { + const stream = fs.createReadStream(archivePath); + const cacheUrl = getCacheUrl(); const token = process.env["ACTIONS_RUNTIME_TOKEN"] || ""; const bearerCredentialHandler = new BearerCredentialHandler(token); diff --git a/src/restore.ts b/src/restore.ts index 6936652..5d9da4a 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -19,7 +19,7 @@ async function run(): Promise { ); } - let cachePath = utils.resolvePath( + const cachePath = utils.resolvePath( core.getInput(Inputs.Path, { required: true }) ); core.debug(`Cache Path: ${cachePath}`); @@ -67,7 +67,7 @@ async function run(): Promise { return; } - let archivePath = path.join( + const archivePath = path.join( await utils.createTempDirectory(), "cache.tgz" ); @@ -90,15 +90,17 @@ async function run(): Promise { // http://man7.org/linux/man-pages/man1/tar.1.html // tar [-options] [files or directories which to add into archive] - const args = ["-xz"]; - const IS_WINDOWS = process.platform === "win32"; - if (IS_WINDOWS) { - args.push("--force-local"); - archivePath = archivePath.replace(/\\/g, "/"); - cachePath = cachePath.replace(/\\/g, "/"); - } - args.push(...["-f", archivePath, "-C", cachePath]); + const args = IS_WINDOWS + ? [ + "-xz", + "--force-local", + "-f", + archivePath.replace(/\\/g, "/"), + "-C", + cachePath.replace(/\\/g, "/") + ] + : ["-xz", "-f", archivePath, "-C", cachePath]; const tarPath = await io.which("tar", true); core.debug(`Tar Path: ${tarPath}`); diff --git a/src/save.ts b/src/save.ts index c4d5d61..52b03fd 100644 --- a/src/save.ts +++ b/src/save.ts @@ -1,7 +1,6 @@ import * as core from "@actions/core"; import { exec } from "@actions/exec"; import * as io from "@actions/io"; -import * as fs from "fs"; import * as path from "path"; import * as cacheHttpClient from "./cacheHttpClient"; import { Inputs, State } from "./constants"; @@ -38,22 +37,25 @@ async function run(): Promise { // http://man7.org/linux/man-pages/man1/tar.1.html // tar [-options] [files or directories which to add into archive] - const args = ["-cz"]; const IS_WINDOWS = process.platform === "win32"; - if (IS_WINDOWS) { - args.push("--force-local"); - archivePath = archivePath.replace(/\\/g, "/"); - cachePath = cachePath.replace(/\\/g, "/"); - } - - args.push(...["-f", archivePath, "-C", cachePath, "."]); + const args = IS_WINDOWS + ? [ + "-cz", + "--force-local", + "-f", + archivePath.replace(/\\/g, "/"), + "-C", + cachePath.replace(/\\/g, "/"), + "." + ] + : ["-cz", "-f", archivePath, "-C", cachePath, "."]; const tarPath = await io.which("tar", true); core.debug(`Tar Path: ${tarPath}`); await exec(`"${tarPath}"`, args); const fileSizeLimit = 400 * 1024 * 1024; // 400MB - const archiveFileSize = fs.statSync(archivePath).size; + const archiveFileSize = utils.getArchiveFileSize(archivePath); core.debug(`File Size: ${archiveFileSize}`); if (archiveFileSize > fileSizeLimit) { core.warning( @@ -64,8 +66,7 @@ async function run(): Promise { return; } - const stream = fs.createReadStream(archivePath); - await cacheHttpClient.saveCache(stream, primaryKey); + await cacheHttpClient.saveCache(primaryKey, archivePath); } catch (error) { core.warning(error.message); }