Files
flipper/desktop/plugins/postinstall.ts
Anton Nikolaev e5261967cb Link shared libs to allow re-using them in both in public and fb-internal plugins
Summary:
Link plugin shared libraries to the "plugins/node_modules" dir so they can be used as peer dependencies by both public and fb-internal plugins.

```
plugins
- node_modules
-- fb_shared_lib (symlink pointing to "plugins/fb/fb_shared_lib")
-- public_shared_lib (symlink pointing to "plugins/public/public_shared_lib")
- fb
-- fb_shared_lib
-- fb_plugin (can now use both fb_shared_lib and public_shared_lib as peer dependencies)
- public
-- public_shared_lib
-- public_plugin (can now use both public_shared_lib and fb_shared_lib (optionally if it's an fb-internal repo) as peer dependencies)
```

Reviewed By: passy

Differential Revision: D27034936

fbshipit-source-id: 68ee5312060ff57649ae061dd7dd14f8beb6d4a1
2021-04-09 05:22:00 -07:00

74 lines
2.0 KiB
TypeScript

/**
* 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 {execSync} from 'child_process';
import path from 'path';
import fs from 'fs-extra';
import pmap from 'p-map';
async function postinstall() {
const publicPluginsDir = path.join(__dirname, 'public');
execSync('yarn install --mutex network:30330', {
cwd: publicPluginsDir,
stdio: 'inherit',
});
const fbPluginsDir = path.join(__dirname, 'fb');
if (await fs.pathExists(fbPluginsDir)) {
execSync('yarn install --mutex network:30330', {
cwd: fbPluginsDir,
stdio: 'inherit',
});
}
const [publicPackages, fbPackages] = await Promise.all([
fs.readdir(publicPluginsDir),
fs.readdir(fbPluginsDir).catch(() => [] as string[]),
]);
const packages = [
...publicPackages.map((p) => path.join(publicPluginsDir, p)),
...fbPackages.map((p) => path.join(fbPluginsDir, p)),
];
const modulesDir = path.join(__dirname, 'node_modules');
await pmap(packages, async (packageDir) => {
const packageJsonPath = path.join(packageDir, 'package.json');
if (!(await fs.pathExists(packageJsonPath))) {
return;
}
const packageJson = await fs.readJson(
path.join(packageDir, 'package.json'),
);
if (
packageJson.keywords &&
packageJson.keywords.includes('flipper-plugin')
) {
return;
}
const destPath = path.join(modulesDir, packageJson.name);
console.log(
`linking ${path.relative(__dirname, destPath)} to ${path.relative(
__dirname,
packageDir,
)}`,
);
if (await fs.pathExists(destPath)) {
return;
}
await fs.ensureDir(path.dirname(destPath));
await fs.symlink(packageDir, destPath, 'junction');
});
}
postinstall()
.then(() => {
process.exit(0);
})
.catch((err: any) => {
console.error(err);
process.exit(1);
});