diff --git a/src/packager.ts b/src/packager.ts index ecad3b0..46ce880 100644 --- a/src/packager.ts +++ b/src/packager.ts @@ -2,7 +2,7 @@ import path, { join } from 'node:path'; import { spawnSync } from 'node:child_process'; import { existsSync, mkdirSync, rmSync, unlinkSync } from 'node:fs'; import { readdir, mkdir } from 'node:fs/promises'; -import { toUrl, toUrlOrUndefined } from './util'; +import { createS3cmdArgs, toUrl, toUrlOrUndefined } from './util'; import mv from 'mv'; const DEFAULT_STAGING_DIR = '/tmp/data'; @@ -99,13 +99,11 @@ export async function download( if (source.protocol === 's3:') { const sourceFile = new URL(join(source.pathname, input.filename), source); const localFilename = join(stagingDir, input.filename); - const { status, stderr } = spawnSync('aws', [ - 's3', - endpointUrl ? `--endpoint-url=${endpointUrl}` : '', - 'cp', - sourceFile.toString(), - localFilename - ]); + const args = createS3cmdArgs( + ['cp', sourceFile.toString(), localFilename], + endpointUrl + ); + const { status, stderr } = spawnSync('aws', args); if (status !== 0) { if (stderr) { console.log(stderr.toString()); @@ -181,14 +179,11 @@ export async function uploadPackage( } if (dest.protocol === 's3:') { console.log(`Uploading package to ${dest.toString()}`); - const { status, stderr, error } = spawnSync('aws', [ - 's3', - s3EndpointUrl ? `--endpoint-url=${s3EndpointUrl}` : '', - 'cp', - '--recursive', - stagingDir, - dest.toString() - ]); + const args = createS3cmdArgs( + ['cp', '--recursive', stagingDir, dest.toString()], + s3EndpointUrl + ); + const { status, stderr, error } = spawnSync('aws', args); if (status !== 0) { if (error) { console.error(`Upload failed: ${error.message}`); diff --git a/src/util.test.ts b/src/util.test.ts new file mode 100644 index 0000000..b62dcfa --- /dev/null +++ b/src/util.test.ts @@ -0,0 +1,13 @@ +import { createS3cmdArgs } from './util'; + +describe('cmd args helper', () => { + test('can construct s3cmd args without s3 endpoint url', () => { + const args = createS3cmdArgs(['ls']); + expect(args).toEqual(['s3', 'ls']); + }); + + test('can construct s3cmd args s3 endpoint url', () => { + const args = createS3cmdArgs(['ls'], 'https://my.api.url.com'); + expect(args).toEqual(['s3', '--endpoint-url=https://my.api.url.com', 'ls']); + }); +}); diff --git a/src/util.ts b/src/util.ts index 6934d04..8c655e3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -12,3 +12,14 @@ export function toUrl(url: string) { ? new URL(url) : new URL(`file://${path.resolve(url)}`); } + +export function createS3cmdArgs( + cmdArgs: string[], + s3EndpointUrl?: string +): string[] { + const args = ['s3']; + if (s3EndpointUrl) { + args.push(`--endpoint-url=${s3EndpointUrl}`); + } + return args.concat(cmdArgs); +}