Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): simplified reading and writing files in CrewMembers using…
… the FileSystem being able to directly read files from the file system in crew members should enable services like the ConsoleReporter to display code snippets and will enable refactoring the artifact handling mechanism in the SerenityBDDReporter Related tickets: re #2244
- Loading branch information
Showing
8 changed files
with
281 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,12 @@ | ||
import fs = require('fs'); | ||
import path = require('upath'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { createFsFromVolume, Volume } = require('memfs'); // Typings incorrectly assume the presence of "dom" lib | ||
import type * as fs from 'node:fs'; | ||
|
||
export interface DirectoryStructure { | ||
[ key: string ]: DirectoryStructure | string; | ||
} | ||
import type { NestedDirectoryJSON} from 'memfs'; | ||
import { createFsFromVolume, Volume } from 'memfs'; | ||
|
||
export class FakeFS { | ||
public static readonly Empty_Directory = {}; | ||
|
||
static with(tree: DirectoryStructure, cwd = process.cwd()): typeof fs { | ||
|
||
function flatten( | ||
currentStructure: DirectoryStructure | string, | ||
currentPath = '', | ||
): { [key: string]: string } { | ||
|
||
if (typeof currentStructure === 'string') { | ||
return ({ | ||
[ currentPath ]: currentStructure, | ||
}); | ||
} | ||
|
||
const entries = Object.keys(currentStructure); | ||
|
||
if (entries.length === 0) { | ||
return ({ | ||
[ currentPath ]: void 0, | ||
}); | ||
} | ||
|
||
return Object.keys(currentStructure).reduce((acc, key) => { | ||
return ({ | ||
...acc, | ||
...flatten(currentStructure[key], path.join(currentPath, key)), | ||
}); | ||
}, {}); | ||
} | ||
|
||
return createFsFromVolume(Volume.fromJSON(flatten(tree), cwd)) as typeof fs; | ||
static with(tree: NestedDirectoryJSON, cwd = process.cwd()): typeof fs { | ||
return createFsFromVolume(Volume.fromNestedJSON(tree, cwd)) as unknown as typeof fs; | ||
} | ||
} |
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
Oops, something went wrong.