Skip to content

Commit

Permalink
Merge pull request #273 from pantheon-systems/ag/add-support-for-link…
Browse files Browse the repository at this point in the history
…ing-local-packages

Force link local packages
  • Loading branch information
a11rew authored Jun 6, 2024
2 parents f9e4eee + b73ed1e commit c820b10
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 149 deletions.
3 changes: 2 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
shamefully-hoist=true
save-workspace-protocol=false
prefer-workspace-packages=true
prefer-workspace-packages=true
link-workspace-packages=true
68 changes: 68 additions & 0 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const fs = require("fs");
const path = require("path");

/**
* This pnpm install hook links all packages in the monorepo to their local versions.
* This allows us to use published version numbers in package.json files, while still
* depending on the local version of the package when inside the monorepo.
*/
const monorepoRoot = findMonorepoRoot(process.cwd());
const workspacePackageDirectories = [
"packages",
"starters",
"configs",
"local_testing",
];
const workspacePackages = new Map();

workspacePackageDirectories.forEach((workspacePackageDir) => {
const allDirs = fs.readdirSync(path.join(monorepoRoot, workspacePackageDir));
allDirs
.filter((dir) => {
return fs
.statSync(path.join(monorepoRoot, workspacePackageDir, dir))
.isDirectory();
})
.forEach((dir) => {
const pkgPath = path.join(monorepoRoot, workspacePackageDir, dir);
const pkgName = require(path.join(pkgPath, "package.json")).name;
workspacePackages.set(pkgName, pkgPath);
});
});

module.exports = {
hooks: {
// This runs after package.json is parsed but before versions are resolved
// We just set the dependencies to the local version if they are in the monorepo
// These changes to the package.json are not saved to disk, but the resolved versions
// are, which is fine for us because users cloning the starterkits will not use the
// monorepo's lockfile
readPackage(pkg) {
for (const dep in pkg.dependencies) {
if (workspacePackages.has(dep)) {
console.log(`INFO: Linking ${pkg.name} to local version of ${dep}`);
pkg.dependencies[dep] = `workspace:*`;
}
}
for (const dep in pkg.devDependencies) {
if (workspacePackages.has(dep)) {
console.log(`INFO: Linking ${pkg.name} to local version of ${dep}`);
pkg.devDependencies[dep] = `workspace:*`;
}
}

return pkg;
},
},
};

function findMonorepoRoot(currentDir) {
let dir = currentDir;
while (dir !== path.parse(dir).root) {
if (fs.existsSync(path.join(dir, "pnpm-workspace.yaml"))) {
return dir;
}
dir = path.dirname(dir);
}
return null;
}
162 changes: 14 additions & 148 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c820b10

Please sign in to comment.