fix promise resolution

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

30
dist/index.js vendored
View file

@ -31168,21 +31168,15 @@ const os = __importStar(__webpack_require__(2087));
const fs = __importStar(__webpack_require__(5747)); 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, writeStream) {
return new Promise(function (resolve, 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 getObjectParams = { Bucket: s3Bucket, Key: fileKey };
const writeStream = fs.createWriteStream(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(); s3.getObject(getObjectParams)
readStream.pipe(writeStream); .createReadStream()
readStream.on('close', resolve); .on('end', () => resolve())
readStream.on('error', reject); .on('error', error => reject(error))
.pipe(writeStream);
}); });
} }
function run() { function run() {
@ -31213,14 +31207,20 @@ function run() {
if (!objects.Contents) { if (!objects.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`); throw new Error(`Could not find objects with ${s3Prefix}`);
} }
core.info(`Found ${objects.Contents.length} objects with prefix ${s3Prefix}`);
for (const fileObject of objects.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).then(() => { const localKeyDir = path_1.default.dirname(localKey);
core.info(`Finished download: ${localKey}`); if (!fs.existsSync(localKeyDir)) {
}); core.debug(`Creating directory (${localKeyDir}) since it did not exist`);
fs.mkdirSync(localKeyDir, { recursive: true });
}
core.info(`Starting download (): ${localKey}`);
yield doDownload(s3, s3Bucket, fileObject.Key, fs.createWriteStream(localKey));
core.info(`Finished download: ${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

View file

@ -10,22 +10,16 @@ function doDownload(
s3: AWS.S3, s3: AWS.S3,
s3Bucket: string, s3Bucket: string,
fileKey: string, fileKey: string,
localKey: string writeStream: fs.WriteStream
): Promise<void> { ): Promise<void> {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
const localKeyDir = path.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 getObjectParams = {Bucket: s3Bucket, Key: fileKey}
const writeStream = fs.createWriteStream(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() s3.getObject(getObjectParams)
readStream.pipe(writeStream) .createReadStream()
readStream.on('close', resolve) .on('end', () => resolve())
readStream.on('error', reject) .on('error', error => reject(error))
.pipe(writeStream)
}) })
} }
@ -56,6 +50,7 @@ async function run(): Promise<void> {
if (!objects.Contents) { if (!objects.Contents) {
throw new Error(`Could not find objects with ${s3Prefix}`) throw new Error(`Could not find objects with ${s3Prefix}`)
} }
core.info(`Found ${objects.Contents.length} objects with prefix ${s3Prefix}`)
for (const fileObject of objects.Contents) { for (const fileObject of objects.Contents) {
if (!fileObject.Key) { if (!fileObject.Key) {
continue continue
@ -64,9 +59,19 @@ async function run(): Promise<void> {
resolvedPath, resolvedPath,
fileObject.Key.replace(s3Prefix, '') fileObject.Key.replace(s3Prefix, '')
) )
await doDownload(s3, s3Bucket, fileObject.Key, localKey).then(() => { const localKeyDir = path.dirname(localKey)
core.info(`Finished download: ${localKey}`) if (!fs.existsSync(localKeyDir)) {
}) core.debug(`Creating directory (${localKeyDir}) since it did not exist`)
fs.mkdirSync(localKeyDir, {recursive: true})
}
core.info(`Starting download (): ${localKey}`)
await doDownload(
s3,
s3Bucket,
fileObject.Key,
fs.createWriteStream(localKey)
)
core.info(`Finished download: ${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