Fix symlinks resolution for plugin dependencies

Summary: Moved getWatchFolders script to flipper-pkg and used it from other packages. Script helps to resolve all the folders with package dependencies including symlinked folders in case if plugin is a part of yarn workspaces.

Reviewed By: mweststrate

Differential Revision: D21068373

fbshipit-source-id: 8691837fdb1aef333dab4c13d8758262838d36ee
This commit is contained in:
Anton Nikolaev
2020-04-17 05:15:55 -07:00
committed by Facebook GitHub Bot
parent a5e5ed5b7b
commit ebfd045328
12 changed files with 51 additions and 40 deletions

View File

@@ -10,3 +10,4 @@
export {run} from '@oclif/command';
export const PKG = 'flipper-pkg';
export {default as runBuild} from './utils/runBuild';
export {default as getWatchFolders} from './utils/getWatchFolders';

View File

@@ -0,0 +1,42 @@
/**
* 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.
*
* @format
*/
import fs from 'fs-extra';
import path from 'path';
export default async (packageDir: string): Promise<string[]> => {
if (!(await fs.pathExists(packageDir))) {
return [];
}
const watchDirs: string[] = [packageDir];
const pkg = await fs.readJson(path.join(packageDir, 'package.json'));
while (true) {
const nodeModulesDir = path.join(packageDir, 'node_modules');
if (await fs.pathExists(nodeModulesDir)) {
watchDirs.push(nodeModulesDir);
const modules = await fs.readdir(nodeModulesDir);
for (const moduleName of modules) {
if (pkg.dependencies && pkg.dependencies[moduleName]) {
const fullPath = path.join(nodeModulesDir, moduleName);
const stat = await fs.lstat(fullPath);
if (stat.isSymbolicLink()) {
const target = await fs.readlink(fullPath);
watchDirs.push(path.resolve(nodeModulesDir, target));
}
}
}
}
const nextDir = path.dirname(packageDir);
if (!nextDir || nextDir === '/' || nextDir === packageDir) {
break;
}
packageDir = nextDir;
}
return watchDirs;
};

View File

@@ -8,7 +8,7 @@
*/
import Metro from 'metro';
import * as path from 'path';
import getWatchFolders from './getWatchFolders';
function hash(string: string) {
let hash = 0;
@@ -41,27 +41,28 @@ export default async function runBuild(
entry: string,
out: string,
) {
await Metro.runBuild(
{
reporter: {update: () => {}},
projectRoot: inputDirectory,
watchFolders: [inputDirectory, path.resolve(__dirname, '..', '..')],
serializer: {
getRunModuleStatement: (moduleID: string) =>
`module.exports = global.__r(${moduleID}).default;`,
createModuleIdFactory,
},
transformer: {
babelTransformerPath: require.resolve('flipper-babel-transformer'),
},
const baseConfig = await Metro.loadConfig();
const config = Object.assign({}, baseConfig, {
reporter: {update: () => {}},
projectRoot: inputDirectory,
watchFolders: [inputDirectory, ...(await getWatchFolders(inputDirectory))],
serializer: {
...baseConfig.serializer,
getRunModuleStatement: (moduleID: string) =>
`module.exports = global.__r(${moduleID}).default;`,
createModuleIdFactory,
},
{
dev: false,
minify: false,
resetCache: true,
sourceMap: true,
entry,
out,
transformer: {
...baseConfig.transformer,
babelTransformerPath: require.resolve('flipper-babel-transformer'),
},
);
});
await Metro.runBuild(config, {
dev: false,
minify: false,
resetCache: false,
sourceMap: true,
entry,
out,
});
}