Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
feat(s3): Add better error handling to invocation of recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidWeb committed Sep 14, 2018
1 parent c1647b1 commit 2c4c64d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (program.path && program.bucket) {
deploy(program.path, program.bucket, program.distributuon, program.profile, program.verbose, true).then((msg) => {
console.log(chalk.greenBright(msg));
}).catch((e) => {
console.log(chalk.bold.red(e));
console.log(chalk.bold.red(`ERROR: ${e.message}`));
});
} else {
program.outputHelp();
Expand Down
87 changes: 52 additions & 35 deletions src/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,45 +159,62 @@ function uploadChangedFilesInDir(pathToUpload, bucketName, verboseMode, isCli) {
return new Promise((resolve, reject) => {
const changedFiles = [];
recursive(pathToUpload, (err, fileList) => {
const s3 = new AWS.S3();

let testedFiles = 0;
const fileListLength = fileList.length;

fileList.forEach((fileName) => {
const bucketPath = fileName.substring(pathToUpload.length + 1);
hasFileChanged(pathToUpload, bucketPath, bucketName, s3, getFileLastModifiedDate)
.then((hasChanged) => {
if (hasChanged) {
changedFiles.push(bucketPath);
}
testedFiles++;

if (testedFiles === fileListLength) {
if (changedFiles.length > 0) {
// eslint-disable-next-line no-console
console.log(chalk.yellow(`${fileListLength} objects found, ${changedFiles.length} objects require updates...`));
uploadFiles(pathToUpload, changedFiles, bucketName, verboseMode, isCli)
.then((msg) => {
if (err) {
if (err.code === 'ENOTDIR') {
reject(new Error('Specified path is not a directory, please specify a valid directory path - this module cannot process individual files'));
} else if (err.code === 'ENOENT') {
reject(new Error('Specified path does not exist. Please specify a valid directory path.'));
} else {
reject(err);
}
} else {
const s3 = new AWS.S3();

let testedFiles = 0;
const fileListLength = fileList.length;

if (fileListLength === 0) {
resolve({
changedFiles: [],
message: 'No files found at specified path',
});
} else {
fileList.forEach((fileName) => {
const bucketPath = fileName.substring(pathToUpload.length + 1);
hasFileChanged(pathToUpload, bucketPath, bucketName, s3, getFileLastModifiedDate)
.then((hasChanged) => {
if (hasChanged) {
changedFiles.push(bucketPath);
}
testedFiles++;

if (testedFiles === fileListLength) {
if (changedFiles.length > 0) {
// eslint-disable-next-line no-console
console.log(chalk.yellow(`${fileListLength} objects found, ${changedFiles.length} objects require updates...`));
uploadFiles(pathToUpload, changedFiles, bucketName, verboseMode, isCli)
.then((msg) => {
resolve({
changedFiles,
message: msg,
});
})
.catch(e => reject(e));
} else {
resolve({
changedFiles,
message: msg,
changedFiles: [],
message: 'No file updates required, skipping upload...',
});
})
.catch(e => reject(e));
} else {
resolve({
changedFiles: [],
message: 'No file updates required, skipping upload...',
});
}
}
})
.catch(e => reject(e));
});
}
}
})
.catch(e => reject(e));
});
}
}
});
});
}


module.exports.uploadChangedFilesInDir = uploadChangedFilesInDir;
module.exports.uploadChangedFilesInDir = uploadChangedFilesInDir;

0 comments on commit 2c4c64d

Please sign in to comment.