Files
flipper/desktop/scripts/copy-package-with-dependencies.ts
Anton Nikolaev c1bb656a0d Re-use babel transformations
Summary:
SORRY FOR BIG DIFF, but it's really hard to split it as all these changes are cross-dependent and should be made at once:
1. Moved transformations to separate package "flipper-babel-transformer" and linked it using yarn workspaces to "static" and "pkg" packages where they are re-used. Removed double copies of transformations we had before int these two packages.
2. Converted transformations to typescript
3. Refactored transformations to avoid relying on file system paths for customisation (FB stubs and Electron stubs for headless build)
4. As babel transformations must be built before other builds - enabled incremental build for them and changed scripts to invoke the transformations build before other build scripts
5. As we need to deploy all the dependencies including the fresh "flipper-babel-transformer" as a part of "static" - implemented script which copies package with all the dependencies taking in account yarn workspaces (hoisting and symlinks)

Reviewed By: passy, mweststrate

Differential Revision: D20690662

fbshipit-source-id: 38a275b60d3c91e01ec21d1dbd72d03c05cfac0b
2020-03-27 03:26:51 -07:00

111 lines
3.5 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 fs from 'fs-extra';
import path from 'path';
/**
* This function copies package into the specified target dir with all its dependencies:
* 1) Both direct and transitive dependencies are copied.
* 2) Symlinks are dereferenced and copied to the target dir as normal dirs.
* 3) Hoisting is supported, so the function scans node_modules up the file tree until dependency is resolved.
* 4) All the dependencies keep their scopes, e.g. dependency from <packageDir>/node_modules/package1/node_modules/package2
* is copied to <targetDir>/node_modules/package1/node_modules/package2.
* 5) Prints informative error and fails fast if a dependency is not resolved.
*/
export default async function copyPackageWithDependencies(
packageDir: string,
targetDir: string,
) {
await fs.remove(targetDir);
await copyPackageWithDependenciesRecursive(packageDir, targetDir, targetDir);
}
async function copyPackageWithDependenciesRecursive(
packageDir: string,
targetDir: string,
rootTargetDir: string,
) {
if (await fs.pathExists(targetDir)) {
return;
}
await fs.mkdirp(targetDir);
if ((await fs.stat(packageDir)).isSymbolicLink()) {
packageDir = await fs.readlink(packageDir);
}
await fs.copy(packageDir, targetDir, {
dereference: true,
recursive: true,
filter: (src) => !src.startsWith(path.join(packageDir, 'node_modules')),
});
const pkg = await fs.readJson(path.join(packageDir, 'package.json'));
const dependencies = (pkg.dependencies ?? {}) as {[key: string]: string};
let unresolvedCount = Object.keys(dependencies).length;
let curPackageDir = packageDir;
let curTargetDir = targetDir;
while (unresolvedCount > 0) {
const curPackageModulesDir = path.join(curPackageDir, 'node_modules');
if (await fs.pathExists(curPackageModulesDir)) {
for (const moduleName of Object.keys(dependencies)) {
const curModulePath = path.join(
curPackageModulesDir,
...moduleName.split('/'),
);
const targetModulePath = path.join(
curTargetDir,
'node_modules',
...moduleName.split('/'),
);
if (await fs.pathExists(curModulePath)) {
await copyPackageWithDependenciesRecursive(
curModulePath,
targetModulePath,
rootTargetDir,
);
delete dependencies[moduleName];
unresolvedCount--;
}
}
}
const parentPackageDir = getParentPackageDir(curPackageDir);
if (
!parentPackageDir ||
parentPackageDir === '' ||
parentPackageDir === curPackageDir
) {
break;
}
curPackageDir = parentPackageDir;
curTargetDir = getParentPackageDir(curTargetDir);
if (!curTargetDir || curTargetDir.length < rootTargetDir.length) {
curTargetDir = rootTargetDir;
}
}
if (unresolvedCount > 0) {
for (const unresolvedDependency of Object.keys(dependencies)) {
console.error(`Cannot resolve ${unresolvedDependency} in ${packageDir}`);
}
process.exit(1);
}
}
function getParentPackageDir(packageDir: string) {
packageDir = path.dirname(packageDir);
while (
path.basename(packageDir) === 'node_modules' ||
path.basename(packageDir).startsWith('@')
) {
packageDir = path.dirname(packageDir);
}
return packageDir;
}