Skip to content

Commit

Permalink
Merge pull request #67 from HubSpot/archive-cleanup
Browse files Browse the repository at this point in the history
Minor fixes to archive
  • Loading branch information
camden11 authored Nov 30, 2023
2 parents 308313a + 790b793 commit 19eb324
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions lib/archive.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import fs from 'fs-extra';
import { join } from 'path';
import { tmpdir } from 'os';
import { promisify } from 'util';
import __extract from 'extract-zip';
import extract from 'extract-zip';

import { throwFileSystemError } from '../errors/fileSystemErrors';
import { throwErrorWithMessage } from '../errors/standardErrors';
import { debug } from '../utils/logger';
import { debug, makeTypedLogger } from '../utils/logger';
import { BaseError } from '../types/Error';

const extract = promisify(__extract);
import { LogCallbacksArg } from '../types/LogCallbacks';

const i18nKey = 'lib.archive';

Expand All @@ -18,11 +16,18 @@ type ZipData = {
tmpDir: string;
};

async function extractZip(name: string, zip: Buffer): Promise<ZipData> {
const archiveCallbackKeys = ['init', 'copy'];

async function extractZip(
name: string,
zip: Buffer,
logCallbacks?: LogCallbacksArg<typeof archiveCallbackKeys>
): Promise<ZipData> {
const logger = makeTypedLogger<typeof archiveCallbackKeys>(logCallbacks);
const result: ZipData = { extractDir: '', tmpDir: '' };

const TMP_FOLDER_PREFIX = `hubspot-temp-${name}-`;
debug(`${i18nKey}.extractZip.init`);
logger('init', `${i18nKey}.extractZip.init`);

// Write zip to disk
let tmpZipPath = '';
Expand Down Expand Up @@ -72,10 +77,12 @@ type CopySourceToDestOptions = {
async function copySourceToDest(
src: string,
dest: string,
{ sourceDir, includesRootDir = true }: CopySourceToDestOptions = {}
{ sourceDir, includesRootDir = true }: CopySourceToDestOptions = {},
logCallbacks?: LogCallbacksArg<typeof archiveCallbackKeys>
): Promise<boolean> {
try {
debug(`${i18nKey}.copySourceToDest.init`);
const logger = makeTypedLogger<typeof archiveCallbackKeys>(logCallbacks);
logger('copy', `${i18nKey}.copySourceToDest.init`);
const srcDirPath = [src];

if (includesRootDir) {
Expand Down Expand Up @@ -123,18 +130,24 @@ export async function extractZipArchive(
zip: Buffer,
name: string,
dest: string,
{ sourceDir, includesRootDir }: CopySourceToDestOptions = {}
{ sourceDir, includesRootDir }: CopySourceToDestOptions = {},
logCallbacks?: LogCallbacksArg<typeof archiveCallbackKeys>
): Promise<boolean> {
let success = false;

if (zip) {
const { extractDir, tmpDir } = await extractZip(name, zip);
const { extractDir, tmpDir } = await extractZip(name, zip, logCallbacks);

if (extractDir !== null) {
success = await copySourceToDest(extractDir, dest, {
sourceDir,
includesRootDir,
});
success = await copySourceToDest(
extractDir,
dest,
{
sourceDir,
includesRootDir,
},
logCallbacks
);
}

cleanupTempDir(tmpDir);
Expand Down

0 comments on commit 19eb324

Please sign in to comment.