Check that all dependencies provided by Flipper core are specified as peers in plugins

Summary: This is a guard to ensure all Flipper built-in packages are declared as peer dependencies. It also removes all of them from nested node_modules after installation to be 100% sure they always loaded from the root folder, because e.g. react can be still installed as a transitive dependency even it is not declared as dependency directly.

Reviewed By: passy

Differential Revision: D27040267

fbshipit-source-id: 1e315a6b280b36ab20778ee261aa386b51d9f964
This commit is contained in:
Anton Nikolaev
2021-04-09 05:15:14 -07:00
committed by Facebook GitHub Bot
parent 4d3e631ce6
commit e7c5a5cc93
3 changed files with 118 additions and 43 deletions

View File

@@ -10,6 +10,18 @@
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"p-map": "^4.0.0" "p-map": "^4.0.0"
}, },
"peerDependencies": {
"flipper": "*",
"flipper-plugin": "*",
"antd": "*",
"react": "*",
"react-dom": "*",
"@emotion/styled": "*",
"@ant-design/icons": "*",
"@types/react": "*",
"@types/react-dom": "*",
"@types/node": "*"
},
"scripts": { "scripts": {
"postinstall": "../ts-node ./postinstall.ts" "postinstall": "../ts-node ./postinstall.ts"
} }

View File

@@ -12,29 +12,26 @@ import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import pmap from 'p-map'; import pmap from 'p-map';
async function postinstall() {
const publicPluginsDir = path.join(__dirname, 'public'); const publicPluginsDir = path.join(__dirname, 'public');
execSync('yarn install --mutex network:30330', { const rootDir = path.resolve(__dirname, '..');
cwd: publicPluginsDir,
stdio: 'inherit',
});
const fbPluginsDir = path.join(__dirname, 'fb'); const fbPluginsDir = path.join(__dirname, 'fb');
if (await fs.pathExists(fbPluginsDir)) {
execSync('yarn install --mutex network:30330', { async function postinstall(): Promise<number> {
cwd: fbPluginsDir, const [publicPackages, fbPackages, pluginsPackageJson] = await Promise.all([
stdio: 'inherit',
});
}
const [publicPackages, fbPackages] = await Promise.all([
fs.readdir(publicPluginsDir), fs.readdir(publicPluginsDir),
fs.readdir(fbPluginsDir).catch(() => [] as string[]), fs.readdir(fbPluginsDir).catch(() => [] as string[]),
fs.readJson(path.join(__dirname, 'package.json')),
]); ]);
const peerDependencies = pluginsPackageJson.peerDependencies ?? {};
const packages = [ const packages = [
...publicPackages.map((p) => path.join(publicPluginsDir, p)), ...publicPackages.map((p) => path.join(publicPluginsDir, p)),
...fbPackages.map((p) => path.join(fbPluginsDir, p)), ...fbPackages.map((p) => path.join(fbPluginsDir, p)),
]; ];
const errors: string[] = [];
const modulesDir = path.join(__dirname, 'node_modules'); const modulesDir = path.join(__dirname, 'node_modules');
await pmap(packages, async (packageDir) => { await pmap(
packages,
async (packageDir) => {
const packageJsonPath = path.join(packageDir, 'package.json'); const packageJsonPath = path.join(packageDir, 'package.json');
if (!(await fs.pathExists(packageJsonPath))) { if (!(await fs.pathExists(packageJsonPath))) {
return; return;
@@ -42,6 +39,22 @@ async function postinstall() {
const packageJson = await fs.readJson( const packageJson = await fs.readJson(
path.join(packageDir, 'package.json'), path.join(packageDir, 'package.json'),
); );
const allDependencies = Object.assign(
{},
packageJson.optionalDependencies ?? {},
packageJson.devDependencies ?? {},
packageJson.dependencies ?? {},
);
for (const dependency of Object.keys(allDependencies)) {
if (peerDependencies[dependency]) {
errors.push(
`[ERROR] Dependency "${dependency}" in plugin package "${path.relative(
rootDir,
packageDir,
)}" must be specified as peer dependency, because it is provided by Flipper.`,
);
}
}
if ( if (
packageJson.keywords && packageJson.keywords &&
packageJson.keywords.includes('flipper-plugin') packageJson.keywords.includes('flipper-plugin')
@@ -49,23 +62,76 @@ async function postinstall() {
return; return;
} }
const destPath = path.join(modulesDir, packageJson.name); const destPath = path.join(modulesDir, packageJson.name);
console.log(
`linking ${path.relative(__dirname, destPath)} to ${path.relative(
__dirname,
packageDir,
)}`,
);
if (await fs.pathExists(destPath)) { if (await fs.pathExists(destPath)) {
return; await fs.remove(destPath);
} } else {
await fs.ensureDir(path.dirname(destPath)); await fs.ensureDir(path.dirname(destPath));
}
await fs.symlink(packageDir, destPath, 'junction'); await fs.symlink(packageDir, destPath, 'junction');
},
{
concurrency: 4,
},
);
if (errors.length) {
console.error('');
for (const err of errors) {
console.error(err);
}
return 1;
}
execSync('yarn install --mutex network:30330', {
cwd: publicPluginsDir,
stdio: 'inherit',
}); });
if (await fs.pathExists(fbPluginsDir)) {
execSync('yarn install --mutex network:30330', {
cwd: fbPluginsDir,
stdio: 'inherit',
});
}
const peerDependenciesArray = Object.keys(peerDependencies);
await Promise.all([
removeInstalledModules(modulesDir, peerDependenciesArray),
removeInstalledModules(
path.join(publicPluginsDir, 'node_modules'),
peerDependenciesArray,
),
removeInstalledModules(
path.join(fbPluginsDir, 'node_modules'),
peerDependenciesArray,
),
]);
await pmap(
packages,
async (packageDir) => {
await removeInstalledModules(
path.join(packageDir, 'node_modules'),
peerDependenciesArray,
);
},
{concurrency: 4},
);
return 0;
}
async function removeInstalledModules(dir: string, modules: string[]) {
await pmap(
modules,
async (d) => {
const fullPath = path.join(dir, d);
if (await fs.pathExists(fullPath)) {
await fs.remove(path.join(dir, d));
}
},
{concurrency: 1},
);
} }
postinstall() postinstall()
.then(() => { .then((code) => {
process.exit(0); process.exit(code);
}) })
.catch((err: any) => { .catch((err: any) => {
console.error(err); console.error(err);

View File

@@ -21,8 +21,5 @@
"devDependencies": { "devDependencies": {
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"patch-package": "^6.2.0" "patch-package": "^6.2.0"
},
"scripts": {
"postinstall": "rimraf ./node_modules/react ./node_modules/react-dom ./node_modules/@types/react ./node_modules/@types/react-dom"
} }
} }