Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "",
"type": "none",
"packageName": "@microsoft/rush"
}
],
"packageName": "@microsoft/rush",
"email": "iclanton@users.noreply.github.com"
}
25 changes: 13 additions & 12 deletions libraries/rush-lib/src/logic/base/BaseWorkspaceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ export abstract class BaseWorkspaceFile {
/**
* Serializes and saves the workspace file to specified location
*/
public save(filePath: string, options: IWorkspaceFileSaveOptions): void {
public async saveAsync(filePath: string, options: IWorkspaceFileSaveOptions): Promise<void> {
const { onlyIfChanged, ensureFolderExists } = options;

// Do we need to read the previous file contents?
let oldBuffer: Buffer | undefined = undefined;
if (options.onlyIfChanged && FileSystem.exists(filePath)) {
let oldBuffer: Buffer | undefined;
if (onlyIfChanged) {
try {
oldBuffer = FileSystem.readFileToBuffer(filePath);
oldBuffer = await FileSystem.readFileToBufferAsync(filePath);
} catch (error) {
// Ignore this error, and try writing a new file. If that fails, then we should report that
// error instead.
}
}

const newYaml: string = this.serialize();

const newBuffer: Buffer = Buffer.from(newYaml); // utf8 encoding happens here
const newContent: string = await this.serializeAsync();
const newBuffer: Buffer = Buffer.from(newContent); // utf8 encoding happens here

if (options.onlyIfChanged) {
if (oldBuffer) {
// Has the file changed?
if (oldBuffer && Buffer.compare(newBuffer, oldBuffer) === 0) {
if (Buffer.compare(newBuffer, oldBuffer) === 0) {
// Nothing has changed, so don't touch the file
return;
}
}

FileSystem.writeFile(filePath, newBuffer.toString(), {
ensureFolderExists: options.ensureFolderExists
await FileSystem.writeFileAsync(filePath, newBuffer.toString(), {
ensureFolderExists
});
}

Expand All @@ -63,5 +64,5 @@ export abstract class BaseWorkspaceFile {
public abstract addPackage(packagePath: string): void;

/** @virtual */
protected abstract serialize(): string;
protected abstract serializeAsync(): Promise<string>;
}
Loading