Skip to content

Commit

Permalink
Merge pull request #373 from G-Rath/fix-372
Browse files Browse the repository at this point in the history
fix: 🐛 accept `null` as value in `fromJSON` functions
  • Loading branch information
streamich authored Jun 1, 2019
2 parents 446b817 + 07d8ea1 commit 674a1e7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,13 @@ function validateGid(gid: number) {

let promisesWarn = !process.env.MEMFS_DONT_WARN;

export type DirectoryJSON = Record<string, string | null>;

/**
* `Volume` represents a file system.
*/
export class Volume {
static fromJSON(json: { [filename: string]: string }, cwd?: string): Volume {
static fromJSON(json: DirectoryJSON, cwd?: string): Volume {
const vol = new Volume();
vol.fromJSON(json, cwd);
return vol;
Expand Down Expand Up @@ -806,7 +808,7 @@ export class Volume {
});
}

private _toJSON(link = this.root, json = {}, path?: string) {
private _toJSON(link = this.root, json = {}, path?: string): DirectoryJSON {
let isEmpty = true;

for (const name in link.children) {
Expand Down Expand Up @@ -834,7 +836,7 @@ export class Volume {
return json;
}

toJSON(paths?: TFilePath | TFilePath[], json = {}, isRelative = false) {
toJSON(paths?: TFilePath | TFilePath[], json = {}, isRelative = false): DirectoryJSON {
const links: Link[] = [];

if (paths) {
Expand All @@ -855,7 +857,7 @@ export class Volume {
}

// fromJSON(json: {[filename: string]: string}, cwd: string = '/') {
fromJSON(json: { [filename: string]: string }, cwd: string = process.cwd()) {
fromJSON(json: DirectoryJSON, cwd: string = process.cwd()) {
for (let filename in json) {
const data = json[filename];

Expand Down Expand Up @@ -886,7 +888,7 @@ export class Volume {
}

// Legacy interface
mountSync(mountpoint: string, json: { [filename: string]: string }) {
mountSync(mountpoint: string, json: DirectoryJSON) {
this.fromJSON(json, mountpoint);
}

Expand Down

0 comments on commit 674a1e7

Please sign in to comment.