Skip to content

Commit

Permalink
Exclude workspace symlinks from build.zip
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Feb 1, 2024
1 parent f9687d7 commit dacbf6d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"lodash": "4.17.21",
"marked": "4.2.12",
"marked-terminal": "5.2.0",
"minimatch": "9.0.3",
"node-fetch": "2.6.7",
"ora": "5.4.0",
"parse-gitignore": "0.5.1",
Expand Down
46 changes: 42 additions & 4 deletions packages/cli/src/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const colors = require('colors/safe');
const ignore = require('ignore');
const gitIgnore = require('parse-gitignore');
const semver = require('semver');
const { minimatch } = require('minimatch');

const {
constants: { Z_BEST_COMPRESSION },
Expand Down Expand Up @@ -318,6 +319,24 @@ const maybeRunBuildScript = async (options = {}) => {
}
};

const listWorkspaces = (workspaceRoot) => {
const packageJsonPath = path.join(workspaceRoot, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
return [];
}

let packageJson;
try {
packageJson = require(packageJsonPath);
} catch (err) {
return [];
}

return (packageJson.workspaces || []).map((relpath) =>
path.join(workspaceRoot, relpath)
);
};

const _buildFunc = async ({
skipNpmInstall = false,
disableDependencyDetection = false,
Expand Down Expand Up @@ -351,19 +370,38 @@ const _buildFunc = async ({
}

const copyFilter = skipNpmInstall
? (src) => !src.includes('.zip')
? (src) => !src.endsWith('.zip')
: undefined;

await copyDir(wdir, tmpDir, { filter: copyFilter });

if (skipNpmInstall) {
const corePackageDir = findCorePackageDir();
const nodeModulesDir = path.dirname(corePackageDir);
const workspaceDir = path.dirname(nodeModulesDir);
if (wdir !== workspaceDir) {
const workspaceRoot = path.dirname(nodeModulesDir);
if (wdir !== workspaceRoot) {
// If we're in here, it means the user is using npm/yarn workspaces
const workspaces = listWorkspaces(workspaceRoot);

await copyDir(nodeModulesDir, path.join(tmpDir, 'node_modules'), {
filter: copyFilter,
filter: (src) => {
if (src.endsWith('.zip')) {
return false;
}
const stat = fse.lstatSync(src);
if (stat.isSymbolicLink()) {
const realPath = path.resolve(
path.dirname(src),
fse.readlinkSync(src)
);
for (const workspace of workspaces) {
if (minimatch(realPath, workspace)) {
return false;
}
}
}
return true;
},
onDirExists: (dir) => {
// Don't overwrite existing sub-directories in node_modules
return false;
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8129,6 +8129,13 @@ minimatch@5.0.1:
dependencies:
brace-expansion "^2.0.1"

minimatch@9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
dependencies:
brace-expansion "^2.0.1"

minimatch@^5.0.1:
version "5.1.6"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96"
Expand Down

0 comments on commit dacbf6d

Please sign in to comment.