mirror of
https://github.com/expo/react-native-community-cli.git
synced 2026-11-15 09:18:35 -10:00
Summary: --------- Historically, Android and iOS platform were hardcoded inside CLI and provided by default. Then, other packages could export additional platforms on top of it. React Native as a platform was hardcoded in Metro configuration (default platforms, default extensions) and inside link. That means all new features were always developed with these platforms in mind, leaving others behind. We often used private APIs, which made things more complicated for 3rd party platform developers. From this PR on, there's no longer concept of an out-of-tree platform inside of the CLI. In a follow up PR to React Native, I am going to configure iOS and Android platforms inside React Native, just like React Native Windows exposes Windows platform. This will open room for improvements and should make it even easier for the CLI to welcome other platforms.
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import path from 'path';
|
|
import MemoryFS from 'metro-memory-fs';
|
|
import util from 'util';
|
|
|
|
let fs;
|
|
|
|
function setMockFilesystem(object, platform) {
|
|
reset(platform);
|
|
const root = platform === 'win32' ? 'c:\\' : '/';
|
|
mockDir(root, {...object});
|
|
return root;
|
|
}
|
|
|
|
function mockDir(dirPath, desc) {
|
|
for (const entName of Object.keys(desc)) {
|
|
const ent = desc[entName];
|
|
const entPath = path.join(dirPath, entName);
|
|
if (typeof ent === 'string' || ent instanceof Buffer) {
|
|
fs.writeFileSync(entPath, ent);
|
|
continue;
|
|
}
|
|
if (typeof ent !== 'object') {
|
|
throw new Error(util.format('invalid entity:', ent));
|
|
}
|
|
if (ent.SYMLINK != null) {
|
|
fs.symlinkSync(ent.SYMLINK, entPath);
|
|
continue;
|
|
}
|
|
fs.mkdirSync(entPath);
|
|
mockDir(entPath, ent);
|
|
}
|
|
}
|
|
|
|
function reset(platform) {
|
|
if (path.mock == null) {
|
|
throw new Error(
|
|
'to use this "fs" module mock, you must also mock the "path" module',
|
|
);
|
|
}
|
|
path.mock.reset(platform);
|
|
const cwd = () => (platform === 'win32' ? 'c:\\' : '/');
|
|
fs = new MemoryFS({platform, cwd});
|
|
Object.assign(mockFs, fs);
|
|
}
|
|
|
|
const mockFs = {};
|
|
mockFs.__setMockFilesystem = setMockFilesystem;
|
|
mockFs.mock = {clear: reset};
|
|
|
|
reset('posix');
|
|
|
|
module.exports = mockFs;
|