-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* keep file history * Update write.ts
- Loading branch information
Showing
8 changed files
with
96 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
import assert from 'assert'; | ||
import { resolve } from 'path'; | ||
|
||
import { isFileStateModified, setModifiedFileState } from '../state.js'; | ||
import type { MemFsEditor } from '../index.js'; | ||
import type { MemFsEditor, MemFsEditorFile } from '../index.js'; | ||
import File from 'vinyl'; | ||
|
||
type CompareFile = { contents: null | Buffer; stat?: { mode?: number } | null }; | ||
|
||
export const isMemFsEditorFileEqual = (a: CompareFile, b: CompareFile) => { | ||
if (a.stat?.mode !== b.stat?.mode) { | ||
return false; | ||
} | ||
return a.contents === b.contents || a.contents?.equals(b.contents!); | ||
}; | ||
|
||
export function _write<EditorFile extends MemFsEditorFile>(this: MemFsEditor<EditorFile>, file: EditorFile) { | ||
if (this.store.existsInMemory(file.path)) { | ||
// Backward compatibility, keep behavior for existing files, custom properties may have been added | ||
const existingFile = this.store.get(file.path); | ||
if (!isFileStateModified(existingFile) || !isMemFsEditorFileEqual(existingFile, file)) { | ||
const { contents, stat } = file; | ||
setModifiedFileState(existingFile); | ||
Object.assign(existingFile, { contents, stat: stat ?? existingFile.stat }); | ||
this.store.add(existingFile); | ||
} | ||
} else { | ||
setModifiedFileState(file); | ||
this.store.add(file); | ||
} | ||
} | ||
|
||
export default function write( | ||
this: MemFsEditor, | ||
filepath: string, | ||
contents: string | Buffer, | ||
stat?: { mode?: number } | null, | ||
stat: { mode?: number } | null = null, | ||
) { | ||
assert(typeof contents === 'string' || Buffer.isBuffer(contents), 'Expected `contents` to be a String or a Buffer'); | ||
|
||
const file = this.store.get(filepath); | ||
const newContents = Buffer.isBuffer(contents) ? contents : Buffer.from(contents); | ||
if ( | ||
!isFileStateModified(file) || | ||
!Buffer.isBuffer(file.contents) || | ||
!newContents.equals(file.contents) || | ||
(stat && file.stat !== stat) | ||
) { | ||
setModifiedFileState(file); | ||
file.contents = newContents; | ||
file.stat = stat ?? null; | ||
this.store.add(file); | ||
} | ||
|
||
return file.contents.toString(); | ||
this._write( | ||
new File({ | ||
path: resolve(filepath), | ||
contents: newContents, | ||
stat: stat as any, | ||
}), | ||
); | ||
return contents.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters