Skip to content

Commit

Permalink
Merge pull request #66 from peacegiverman/master
Browse files Browse the repository at this point in the history
Don't reject whole batch of files because some of them failed to load…
  • Loading branch information
mt40 committed Jul 15, 2017
2 parents db7bbf6 + 25b0c44 commit e850476
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/classes/FileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class FileReader {
return [];
}

// TODO: Try using workspace.findFiles(...) instead of node fs methods
let files = fs.readdirSync(root);
let names = [];
for (let i = 0; i < files.length; i++) {
Expand All @@ -125,7 +126,12 @@ export class FileReader {
private static readFileFromNames(uris_or_strings): Promise<FileType[]> {
return new Promise(function (resolve, reject) {
let docs: FileType[] = [];
let count = 0;
// Count of successfully opened files
let openedCount = 0;
// Count of files which failed to load
let failedCount = 0;

function totalCount() { return openedCount + failedCount; }

for (let uri of uris_or_strings) {
let docPrm = workspace.openTextDocument(uri);
Expand All @@ -134,14 +140,23 @@ export class FileReader {
if(doc) // File maybe corrupted
docs.push(new FileType(doc));

count++;
openedCount++;
// Detect and end the function early
if (count == uris_or_strings.length) {
if (totalCount() == uris_or_strings.length) {
resolve(docs);
}
},
function (reason) {
reject(reason);
// Keep going, try other files.
failedCount++;
// Detect and end the function early
if (failedCount == uris_or_strings.length) {
// All files failed to open, so we reject
reject("No file has been read successfully.");
}
else if (totalCount() == uris_or_strings.length) {
resolve(docs);
}
});
}
if (uris_or_strings.length == 0)
Expand Down
6 changes: 4 additions & 2 deletions src/types/CommandType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ export class ParseAllFilesCommand implements CommandType {
let results = [], errors = [];
FileReader.readProjectFiles(
(files: FileType[], progress, error) => {
if (!error) {
if (files.length > 0) {
files = FileFilter.filter(files);
let todos = Parser.parse(files);
results = results.concat(todos);
OutputWriter.writeTodo(todos);
StatusBarManager.getInstance().setWorking(`${WORKING_ICON} ${progress}%`, "Click to cancel");
totalFiles += todos.length;
}
else {
// We could have files available, but still have an error
// because some of them failed to load.
if (error) {
errors.push(error);
}
},
Expand Down

0 comments on commit e850476

Please sign in to comment.