try to do it without a weird callback

Signed-off-by: Eli Uriegas <eliuriegas@fb.com>
This commit is contained in:
Eli Uriegas 2021-05-19 15:16:40 -07:00
parent 35ec0229c4
commit fdf1695da1
No known key found for this signature in database
GPG key ID: EE2C3B4893010973
2 changed files with 45 additions and 51 deletions

19
dist/index.js vendored
View file

@ -31169,7 +31169,6 @@ const fs = __importStar(__webpack_require__(5747));
const path_1 = __importDefault(__webpack_require__(5622)); const path_1 = __importDefault(__webpack_require__(5622));
const constants_1 = __webpack_require__(1211); const constants_1 = __webpack_require__(1211);
function doDownload(s3, s3Bucket, fileKey, localKey) { function doDownload(s3, s3Bucket, fileKey, localKey) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
const localKeyDir = path_1.default.dirname(localKey); const localKeyDir = path_1.default.dirname(localKey);
if (!fs.existsSync(localKeyDir)) { if (!fs.existsSync(localKeyDir)) {
@ -31182,9 +31181,11 @@ function doDownload(s3, s3Bucket, fileKey, localKey) {
core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`); core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`);
const readStream = s3.getObject(getObjectParams).createReadStream(); const readStream = s3.getObject(getObjectParams).createReadStream();
readStream.pipe(writeStream); readStream.pipe(writeStream);
readStream.on('close', resolve); readStream.on('close', () => {
readStream.on('error', reject); core.info(`Finished download: ${localKey}`);
resolve;
}); });
readStream.on('error', reject);
}); });
} }
function run() { function run() {
@ -31211,23 +31212,17 @@ function run() {
Prefix: s3Prefix Prefix: s3Prefix
}; };
core.debug(JSON.stringify(s3Params)); core.debug(JSON.stringify(s3Params));
s3.listObjects(s3Params, function (err, data) { const objects = yield s3.listObjects(s3Params).promise();
return __awaiter(this, void 0, void 0, function* () { if (!objects.Contents) {
if (err) {
throw err;
}
if (!data.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`); throw new Error(`Could not find objects with ${s3Prefix}`);
} }
for (const fileObject of data.Contents) { for (const fileObject of objects.Contents) {
if (!fileObject.Key) { if (!fileObject.Key) {
continue; continue;
} }
const localKey = path_1.default.join(resolvedPath, fileObject.Key.replace(s3Prefix, '')); const localKey = path_1.default.join(resolvedPath, fileObject.Key.replace(s3Prefix, ''));
yield doDownload(s3, s3Bucket, fileObject.Key, localKey); yield doDownload(s3, s3Bucket, fileObject.Key, localKey);
} }
});
});
// output the directory that the artifact(s) was/were downloaded to // output the directory that the artifact(s) was/were downloaded to
// if no path is provided, an empty string resolves to the current working directory // if no path is provided, an empty string resolves to the current working directory
core.setOutput(constants_1.Outputs.DownloadPath, resolvedPath); core.setOutput(constants_1.Outputs.DownloadPath, resolvedPath);

View file

@ -6,7 +6,7 @@ import * as fs from 'fs'
import path from 'path' import path from 'path'
import {Inputs, Outputs} from './constants' import {Inputs, Outputs} from './constants'
async function doDownload( function doDownload(
s3: AWS.S3, s3: AWS.S3,
s3Bucket: string, s3Bucket: string,
fileKey: string, fileKey: string,
@ -24,7 +24,10 @@ async function doDownload(
core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`) core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`)
const readStream = s3.getObject(getObjectParams).createReadStream() const readStream = s3.getObject(getObjectParams).createReadStream()
readStream.pipe(writeStream) readStream.pipe(writeStream)
readStream.on('close', resolve) readStream.on('close', () => {
core.info(`Finished download: ${localKey}`)
resolve
})
readStream.on('error', reject) readStream.on('error', reject)
}) })
} }
@ -52,14 +55,11 @@ async function run(): Promise<void> {
Prefix: s3Prefix Prefix: s3Prefix
} }
core.debug(JSON.stringify(s3Params)) core.debug(JSON.stringify(s3Params))
s3.listObjects(s3Params, async function (err, data) { const objects = await s3.listObjects(s3Params).promise()
if (err) { if (!objects.Contents) {
throw err
}
if (!data.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`) throw new Error(`Could not find objects with ${s3Prefix}`)
} }
for (const fileObject of data.Contents) { for (const fileObject of objects.Contents) {
if (!fileObject.Key) { if (!fileObject.Key) {
continue continue
} }
@ -69,7 +69,6 @@ async function run(): Promise<void> {
) )
await doDownload(s3, s3Bucket, fileObject.Key, localKey) await doDownload(s3, s3Bucket, fileObject.Key, localKey)
} }
})
// output the directory that the artifact(s) was/were downloaded to // output the directory that the artifact(s) was/were downloaded to
// if no path is provided, an empty string resolves to the current working directory // if no path is provided, an empty string resolves to the current working directory
core.setOutput(Outputs.DownloadPath, resolvedPath) core.setOutput(Outputs.DownloadPath, resolvedPath)