Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force link local packages #273

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading