Skip to content

Commit

Permalink
fix: cleanup downloaded source files before syncing to S3 (#7)
Browse files Browse the repository at this point in the history
* fix: do not upload source files to S3

* fix: cleanup jobdir after upload

* fix: force removal of staging dir
  • Loading branch information
birme authored Nov 9, 2024
1 parent 61ee675 commit a985b79
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/packager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path, { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync } from 'node:fs';
import { existsSync, mkdirSync, rmSync, unlinkSync } from 'node:fs';
import { readdir, mkdir } from 'node:fs/promises';
import { toUrl, toUrlOrUndefined } from './util';
import mv from 'mv';
Expand Down Expand Up @@ -54,7 +54,12 @@ export async function doPackage(opts: PackageOptions) {
validateOptios(opts);
const stagingDir = await prepare(opts.stagingDir);
await createPackage({ ...opts, stagingDir });
if (toUrl(opts.dest).protocol === 's3:') {
// We don't want to upload source files to S3
await removeDownloadedFiles(opts.inputs, stagingDir);
}
await uploadPackage(toUrl(opts.dest), stagingDir);
await cleanup(stagingDir);
}

export async function prepare(
Expand All @@ -68,6 +73,11 @@ export async function prepare(
return jobDir;
}

export async function cleanup(stagingDir: string) {
console.log(`Cleaning up staging directory: ${stagingDir}`);
await rmSync(stagingDir, { recursive: true, force: true });
}

export async function download(
input: Input,
source?: URL,
Expand Down Expand Up @@ -108,19 +118,21 @@ export async function download(
auth.push('-H');
auth.push(`x-jwt: Bearer ${serviceAccessToken}`);
}
const { status, stdout, stderr } = spawnSync(
const { status, stderr, error } = spawnSync(
'curl',
auth.concat([
'-v',
'-o',
localFilename,
source.href.replace(/\/$/, '') + input.filename
])
);
if (stderr) {
console.log(stderr.toString());
}
if (status !== 0) {
if (error) {
console.error(`Download failed: ${error.message}`);
} else {
console.error(`Download failed with exit code ${status}`);
console.log(stderr.toString());
}
throw new Error('Download failed');
}
console.log(`Downloaded ${input.filename} to ${localFilename}`);
Expand All @@ -130,6 +142,19 @@ export async function download(
}
}

async function removeDownloadedFiles(inputs: Input[], stagingDir: string) {
console.log(`Removing downloaded files from ${stagingDir}`);
for (const input of inputs) {
const localFilename = join(stagingDir, path.basename(input.filename));
console.log(`Removing ${localFilename}`);
if (existsSync(localFilename)) {
unlinkSync(localFilename);
} else {
console.log(`File not found: ${localFilename}`);
}
}
}

async function moveFile(src: string, dest: string) {
return new Promise((resolve, reject) => {
mv(src, dest, (err) => (err ? reject(err) : resolve(dest)));
Expand All @@ -148,16 +173,20 @@ export async function uploadPackage(dest: URL, stagingDir: string) {
return;
}
if (dest.protocol === 's3:') {
const { status, stderr } = spawnSync('aws', [
console.log(`Uploading package to ${dest.toString()}`);
const { status, stderr, error } = spawnSync('aws', [
's3',
'cp',
'--recursive',
stagingDir,
dest.toString()
]);
if (status !== 0) {
if (stderr) {
console.log(stderr.toString());
if (error) {
console.error(`Upload failed: ${error.message}`);
} else {
console.error(`Upload failed with exit code ${status}`);
console.error(stderr.toString());
}
throw new Error('Upload failed');
}
Expand Down

0 comments on commit a985b79

Please sign in to comment.