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 constants_1 = __webpack_require__(1211);
function doDownload(s3, s3Bucket, fileKey, localKey) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(function (resolve, reject) {
const localKeyDir = path_1.default.dirname(localKey);
if (!fs.existsSync(localKeyDir)) {
core.debug(`Creating directory (${localKeyDir}) since it did not exist`);
fs.mkdirSync(localKeyDir, { recursive: true });
}
const getObjectParams = { Bucket: s3Bucket, Key: fileKey };
const writeStream = fs.createWriteStream(localKey);
core.info(`Started download: ${localKey}`);
core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`);
const readStream = s3.getObject(getObjectParams).createReadStream();
readStream.pipe(writeStream);
readStream.on('close', resolve);
readStream.on('error', reject);
return new Promise(function (resolve, reject) {
const localKeyDir = path_1.default.dirname(localKey);
if (!fs.existsSync(localKeyDir)) {
core.debug(`Creating directory (${localKeyDir}) since it did not exist`);
fs.mkdirSync(localKeyDir, { recursive: true });
}
const getObjectParams = { Bucket: s3Bucket, Key: fileKey };
const writeStream = fs.createWriteStream(localKey);
core.info(`Started download: ${localKey}`);
core.debug(`S3 download uri: s3://${s3Bucket}/${fileKey}`);
const readStream = s3.getObject(getObjectParams).createReadStream();
readStream.pipe(writeStream);
readStream.on('close', () => {
core.info(`Finished download: ${localKey}`);
resolve;
});
readStream.on('error', reject);
});
}
function run() {
@ -31211,23 +31212,17 @@ function run() {
Prefix: s3Prefix
};
core.debug(JSON.stringify(s3Params));
s3.listObjects(s3Params, function (err, data) {
return __awaiter(this, void 0, void 0, function* () {
if (err) {
throw err;
}
if (!data.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`);
}
for (const fileObject of data.Contents) {
if (!fileObject.Key) {
continue;
}
const localKey = path_1.default.join(resolvedPath, fileObject.Key.replace(s3Prefix, ''));
yield doDownload(s3, s3Bucket, fileObject.Key, localKey);
}
});
});
const objects = yield s3.listObjects(s3Params).promise();
if (!objects.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`);
}
for (const fileObject of objects.Contents) {
if (!fileObject.Key) {
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
// if no path is provided, an empty string resolves to the current working directory
core.setOutput(constants_1.Outputs.DownloadPath, resolvedPath);

View file

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