(returnPathsRelativeTo: string = '')
| 115 | } |
| 116 | |
| 117 | export function createMemoryEnvironment(returnPathsRelativeTo: string = '') { |
| 118 | const environment = createDefaultEnvironment() |
| 119 | |
| 120 | const output: MemoryEnvironmentOutput = { |
| 121 | files: {}, |
| 122 | commands: [], |
| 123 | deletedFiles: [], |
| 124 | } |
| 125 | |
| 126 | const { fs, vol } = memfs({}) |
| 127 | |
| 128 | environment.appendFile = async (path: string, contents: string) => { |
| 129 | fs.mkdirSync(dirname(path), { recursive: true }) |
| 130 | await fs.appendFileSync(path, contents) |
| 131 | } |
| 132 | environment.copyFile = async (from: string, to: string) => { |
| 133 | fs.mkdirSync(dirname(to), { recursive: true }) |
| 134 | fs.copyFileSync(from, to) |
| 135 | return Promise.resolve() |
| 136 | } |
| 137 | environment.execute = async (command: string, args: Array<string>) => { |
| 138 | output.commands.push({ |
| 139 | command, |
| 140 | args, |
| 141 | }) |
| 142 | return Promise.resolve({ stdout: '' }) |
| 143 | } |
| 144 | environment.readFile = async (path: string) => { |
| 145 | return Promise.resolve(fs.readFileSync(path, 'utf-8').toString()) |
| 146 | } |
| 147 | environment.writeFile = async (path: string, contents: string) => { |
| 148 | fs.mkdirSync(dirname(path), { recursive: true }) |
| 149 | await fs.writeFileSync(path, contents) |
| 150 | } |
| 151 | environment.writeFileBase64 = async (path: string, contents: string) => { |
| 152 | // For the in-memory file system, we are not converting the base64 to binary |
| 153 | // because it's not needed. |
| 154 | fs.mkdirSync(dirname(path), { recursive: true }) |
| 155 | await fs.writeFileSync(path, contents) |
| 156 | } |
| 157 | environment.deleteFile = async (path: string) => { |
| 158 | output.deletedFiles.push(path) |
| 159 | if (fs.existsSync(path)) { |
| 160 | await fs.unlinkSync(path) |
| 161 | } |
| 162 | } |
| 163 | environment.finishRun = () => { |
| 164 | output.files = vol.toJSON() as Record<string, string> |
| 165 | for (const file of Object.keys(output.files)) { |
| 166 | if (fs.statSync(file).isDirectory()) { |
| 167 | delete output.files[file] |
| 168 | } |
| 169 | } |
| 170 | if (returnPathsRelativeTo.length) { |
| 171 | output.files = cleanUpFiles(output.files, returnPathsRelativeTo) |
| 172 | output.deletedFiles = cleanUpFileArray( |
| 173 | output.deletedFiles, |
| 174 | returnPathsRelativeTo, |
no test coverage detected