Jean Lauliac c6b96c0df7 react-native-github: remove old fs mock implementation
Reviewed By: rafeca

Differential Revision: D7652914

fbshipit-source-id: 5494305750e7616b5120169266c519f460ae7e6d
2018-04-25 07:37:10 -07:00

44 lines
1.0 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const fs = new (require('metro-memory-fs'))();
function setMockFilesystem(object) {
fs.reset();
const root = process.platform === 'win32' ? 'c:\\' : '/';
mockDir(root, {...object});
}
function mockDir(dirPath, desc) {
for (const entName in desc) {
const ent = desc[entName];
const entPath = require('path').join(dirPath, entName);
if (typeof ent === 'string' || ent instanceof Buffer) {
fs.writeFileSync(entPath, ent);
continue;
}
if (typeof ent !== 'object') {
throw new Error(require('util').format('invalid entity:', ent));
}
if (ent.SYMLINK != null) {
fs.symlinkSync(ent.SYMLINK, entPath);
continue;
}
fs.mkdirSync(entPath);
mockDir(entPath, ent);
}
}
fs.__setMockFilesystem = setMockFilesystem;
fs.mock = {clear: () => fs.reset()};
module.exports = fs;