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:
committed by
Facebook GitHub Bot
parent
4d3e631ce6
commit
e7c5a5cc93
@@ -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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,60 +12,126 @@ 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');
|
const rootDir = path.resolve(__dirname, '..');
|
||||||
|
const fbPluginsDir = path.join(__dirname, 'fb');
|
||||||
|
|
||||||
|
async function postinstall(): Promise<number> {
|
||||||
|
const [publicPackages, fbPackages, pluginsPackageJson] = await Promise.all([
|
||||||
|
fs.readdir(publicPluginsDir),
|
||||||
|
fs.readdir(fbPluginsDir).catch(() => [] as string[]),
|
||||||
|
fs.readJson(path.join(__dirname, 'package.json')),
|
||||||
|
]);
|
||||||
|
const peerDependencies = pluginsPackageJson.peerDependencies ?? {};
|
||||||
|
const packages = [
|
||||||
|
...publicPackages.map((p) => path.join(publicPluginsDir, p)),
|
||||||
|
...fbPackages.map((p) => path.join(fbPluginsDir, p)),
|
||||||
|
];
|
||||||
|
const errors: string[] = [];
|
||||||
|
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'),
|
||||||
|
);
|
||||||
|
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 (
|
||||||
|
packageJson.keywords &&
|
||||||
|
packageJson.keywords.includes('flipper-plugin')
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const destPath = path.join(modulesDir, packageJson.name);
|
||||||
|
if (await fs.pathExists(destPath)) {
|
||||||
|
await fs.remove(destPath);
|
||||||
|
} else {
|
||||||
|
await fs.ensureDir(path.dirname(destPath));
|
||||||
|
}
|
||||||
|
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', {
|
execSync('yarn install --mutex network:30330', {
|
||||||
cwd: publicPluginsDir,
|
cwd: publicPluginsDir,
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
});
|
});
|
||||||
const fbPluginsDir = path.join(__dirname, 'fb');
|
|
||||||
if (await fs.pathExists(fbPluginsDir)) {
|
if (await fs.pathExists(fbPluginsDir)) {
|
||||||
execSync('yarn install --mutex network:30330', {
|
execSync('yarn install --mutex network:30330', {
|
||||||
cwd: fbPluginsDir,
|
cwd: fbPluginsDir,
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const [publicPackages, fbPackages] = await Promise.all([
|
const peerDependenciesArray = Object.keys(peerDependencies);
|
||||||
fs.readdir(publicPluginsDir),
|
await Promise.all([
|
||||||
fs.readdir(fbPluginsDir).catch(() => [] as string[]),
|
removeInstalledModules(modulesDir, peerDependenciesArray),
|
||||||
|
removeInstalledModules(
|
||||||
|
path.join(publicPluginsDir, 'node_modules'),
|
||||||
|
peerDependenciesArray,
|
||||||
|
),
|
||||||
|
removeInstalledModules(
|
||||||
|
path.join(fbPluginsDir, 'node_modules'),
|
||||||
|
peerDependenciesArray,
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
const packages = [
|
await pmap(
|
||||||
...publicPackages.map((p) => path.join(publicPluginsDir, p)),
|
packages,
|
||||||
...fbPackages.map((p) => path.join(fbPluginsDir, p)),
|
async (packageDir) => {
|
||||||
];
|
await removeInstalledModules(
|
||||||
const modulesDir = path.join(__dirname, 'node_modules');
|
path.join(packageDir, 'node_modules'),
|
||||||
await pmap(packages, async (packageDir) => {
|
peerDependenciesArray,
|
||||||
const packageJsonPath = path.join(packageDir, 'package.json');
|
);
|
||||||
if (!(await fs.pathExists(packageJsonPath))) {
|
},
|
||||||
return;
|
{concurrency: 4},
|
||||||
}
|
);
|
||||||
const packageJson = await fs.readJson(
|
|
||||||
path.join(packageDir, 'package.json'),
|
return 0;
|
||||||
);
|
}
|
||||||
if (
|
|
||||||
packageJson.keywords &&
|
async function removeInstalledModules(dir: string, modules: string[]) {
|
||||||
packageJson.keywords.includes('flipper-plugin')
|
await pmap(
|
||||||
) {
|
modules,
|
||||||
return;
|
async (d) => {
|
||||||
}
|
const fullPath = path.join(dir, d);
|
||||||
const destPath = path.join(modulesDir, packageJson.name);
|
if (await fs.pathExists(fullPath)) {
|
||||||
console.log(
|
await fs.remove(path.join(dir, d));
|
||||||
`linking ${path.relative(__dirname, destPath)} to ${path.relative(
|
}
|
||||||
__dirname,
|
},
|
||||||
packageDir,
|
{concurrency: 1},
|
||||||
)}`,
|
);
|
||||||
);
|
|
||||||
if (await fs.pathExists(destPath)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await fs.ensureDir(path.dirname(destPath));
|
|
||||||
await fs.symlink(packageDir, destPath, 'junction');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
postinstall()
|
postinstall()
|
||||||
.then(() => {
|
.then((code) => {
|
||||||
process.exit(0);
|
process.exit(code);
|
||||||
})
|
})
|
||||||
.catch((err: any) => {
|
.catch((err: any) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
@@ -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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user