Skip to content

Commit

Permalink
feat: #185 - Ability to save all descendant files in a directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 22, 2017
1 parent 18f1e7b commit 334f20b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/TsSimpleAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,14 @@ export class TsSimpleAst {
}

private getUnsavedSourceFiles() {
return this.getSourceFiles().filter(f => !f.isSaved());
return ArrayUtils.from(getUnsavedIterator(this.global.compilerFactory.getSourceFilesByDirectoryDepth()));

function *getUnsavedIterator(sourceFiles: IterableIterator<compiler.SourceFile>) {
for (const sourceFile of sourceFiles) {
if (!sourceFile.isSaved())
yield sourceFile;
}
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ export class Directory {
this._global = undefined;
}

/**
* Saves all the unsaved descendant source files.
*/
saveUnsavedSourceFiles() {
return Promise.all(this._getUnsavedSourceFiles().map(f => f.save()));
}

/**
* Saves all the unsaved descendant source files synchronously.
*
* Remarks: This might be very slow compared to the asynchronous version if there are a lot of files.
*/
saveUnsavedSourceFilesSync() {
for (const file of this._getUnsavedSourceFiles())
file.saveSync();
}

/** @internal */
_addSourceFile(sourceFile: SourceFile) {
const baseName = sourceFile.getBaseName().toUpperCase();
Expand Down Expand Up @@ -357,6 +374,17 @@ export class Directory {
throw new errors.InvalidOperationError("Cannot use a directory that was deleted or removed.");
}

private _getUnsavedSourceFiles() {
return ArrayUtils.from(getUnsavedIterator(this.getDescendantSourceFilesIterator()));

function *getUnsavedIterator(sourceFiles: IterableIterator<SourceFile>) {
for (const sourceFile of sourceFiles) {
if (!sourceFile.isSaved())
yield sourceFile;
}
}
}

/** @internal */
private static isAncestorOfDir(ancestor: Directory, descendant: Directory | SourceFile) {
if (descendant instanceof SourceFile) {
Expand Down
35 changes: 35 additions & 0 deletions src/tests/fileSystem/directoryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,39 @@ describe(nameof(Directory), () => {
expect(otherSourceFile.wasForgotten()).to.be.false;
});
});

describe(nameof<Directory>(dir => dir.saveUnsavedSourceFiles), () => {
it("should save all the unsaved source files asynchronously", async () => {
const fileSystem = getFileSystemHostWithFiles([]);
const ast = new TsSimpleAst(undefined, fileSystem);
const otherFile = ast.createSourceFile("file.ts");
const dir = ast.createDirectory("dir");
dir.createSourceFile("file1.ts", "").saveSync();
dir.createSourceFile("file2.ts", "");
dir.createSourceFile("child/file3.ts", "");
await dir.saveUnsavedSourceFiles();
expect(dir.getDescendantSourceFiles().map(f => f.isSaved())).to.deep.equal([true, true, true]);
expect(otherFile.isSaved()).to.be.false;
expect(fileSystem.getWriteLog().length).to.equal(2); // 2 writes
expect(fileSystem.getSyncWriteLog().length).to.equal(1); // 1 write
});
});

describe(nameof<Directory>(dir => dir.saveUnsavedSourceFilesSync), () => {
it("should save all the unsaved source files synchronously", () => {
const fileSystem = getFileSystemHostWithFiles([]);
const ast = new TsSimpleAst(undefined, fileSystem);
const otherFile = ast.createSourceFile("file.ts");
const dir = ast.createDirectory("dir");
dir.createSourceFile("file1.ts", "").saveSync();
dir.createSourceFile("file2.ts", "");
dir.createSourceFile("child/file3.ts", "");
dir.saveUnsavedSourceFilesSync();

expect(dir.getDescendantSourceFiles().map(f => f.isSaved())).to.deep.equal([true, true, true]);
expect(otherFile.isSaved()).to.be.false;
expect(fileSystem.getWriteLog().length).to.equal(0);
expect(fileSystem.getSyncWriteLog().length).to.equal(3); // 3 writes
});
});
});

0 comments on commit 334f20b

Please sign in to comment.