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

59
dist/index.js vendored
View file

@ -31169,22 +31169,23 @@ 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)) { core.debug(`Creating directory (${localKeyDir}) since it did not exist`);
core.debug(`Creating directory (${localKeyDir}) since it did not exist`); fs.mkdirSync(localKeyDir, { recursive: true });
fs.mkdirSync(localKeyDir, { recursive: true }); }
} const getObjectParams = { Bucket: s3Bucket, Key: fileKey };
const getObjectParams = { Bucket: s3Bucket, Key: fileKey }; const writeStream = fs.createWriteStream(localKey);
const writeStream = fs.createWriteStream(localKey); core.info(`Started download: ${localKey}`);
core.info(`Started download: ${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', () => {
readStream.on('close', resolve); core.info(`Finished download: ${localKey}`);
readStream.on('error', reject); 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 new Error(`Could not find objects with ${s3Prefix}`);
throw err; }
} for (const fileObject of objects.Contents) {
if (!data.Contents) { if (!fileObject.Key) {
throw new Error(`Could not find objects with ${s3Prefix}`); continue;
} }
for (const fileObject of data.Contents) { const localKey = path_1.default.join(resolvedPath, fileObject.Key.replace(s3Prefix, ''));
if (!fileObject.Key) { yield doDownload(s3, s3Bucket, fileObject.Key, localKey);
continue; }
}
const localKey = path_1.default.join(resolvedPath, fileObject.Key.replace(s3Prefix, ''));
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,24 +55,20 @@ 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 throw new Error(`Could not find objects with ${s3Prefix}`)
}
for (const fileObject of objects.Contents) {
if (!fileObject.Key) {
continue
} }
if (!data.Contents) { const localKey = path.join(
throw new Error(`Could not find objects with ${s3Prefix}`) resolvedPath,
} fileObject.Key.replace(s3Prefix, '')
for (const fileObject of data.Contents) { )
if (!fileObject.Key) { await doDownload(s3, s3Bucket, fileObject.Key, localKey)
continue }
}
const localKey = path.join(
resolvedPath,
fileObject.Key.replace(s3Prefix, '')
)
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)