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

fix: always inject dependencies from other subspaces #33

Merged
merged 4 commits into from
May 20, 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
2 changes: 1 addition & 1 deletion packages/pnpm-sync-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pnpm-sync-lib",
"version": "0.2.5",
"version": "0.2.6",
"description": "API library for integrating \"pnpm-sync\" with your toolchain",
"repository": {
"type": "git",
Expand Down
58 changes: 22 additions & 36 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
}

// find injected dependency and all its available versions
const injectedDependencyToVersion: Map<string, Set<string>> = getInjectedDependencyToVersion(pnpmLockfile);
const injectedDependencyToVersion: Map<string, Set<string>> = new Map();
for (const importerItem of Object.values(pnpmLockfile?.importers || {})) {
// based on https://pnpm.io/package_json#dependenciesmeta
// the injected dependencies could available inside dependencies, optionalDependencies, and devDependencies.
getInjectedDependencyToVersion(importerItem?.dependencies, injectedDependencyToVersion);
getInjectedDependencyToVersion(importerItem?.devDependencies, injectedDependencyToVersion);
getInjectedDependencyToVersion(importerItem?.optionalDependencies, injectedDependencyToVersion);
}

// check and process transitive injected dependency
processTransitiveInjectedDependency(pnpmLockfile, injectedDependencyToVersion);
Expand Down Expand Up @@ -260,47 +267,26 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
});
}

// process dependencies and devDependencies to generate injectedDependencyToFilePath
function getInjectedDependencyToVersion(pnpmLockfile: ILockfile | undefined): Map<string, Set<string>> {
const injectedDependencyToVersion: Map<string, Set<string>> = new Map();
for (const importerKey in pnpmLockfile?.importers) {
if (!pnpmLockfile?.importers[importerKey]?.dependenciesMeta) {
continue;
}
const dependenciesMeta = pnpmLockfile?.importers[importerKey]?.dependenciesMeta;

for (const dependency in dependenciesMeta) {
if (dependenciesMeta[dependency]?.injected) {
if (!injectedDependencyToVersion.has(dependency)) {
injectedDependencyToVersion.set(dependency, new Set());
}
}
}

// based on https://pnpm.io/package_json#dependenciesmeta
// the injected dependencies could available inside dependencies, optionalDependencies, and devDependencies.
processDependencies(pnpmLockfile?.importers[importerKey]?.dependencies, injectedDependencyToVersion);
processDependencies(pnpmLockfile?.importers[importerKey]?.devDependencies, injectedDependencyToVersion);
processDependencies(
pnpmLockfile?.importers[importerKey]?.optionalDependencies,
injectedDependencyToVersion
);
}
return injectedDependencyToVersion;
}
function processDependencies(
function getInjectedDependencyToVersion(
dependencies: Record<string, IVersionSpecifier> | undefined,
injectedDependencyToVersion: Map<string, Set<string>>
): void {
if (dependencies) {
for (const [dependency, specifier] of Object.entries(dependencies)) {
if (injectedDependencyToVersion.has(dependency)) {
const specifierToUse: string = typeof specifier === 'string' ? specifier : specifier.version;

// the injected dependency should always start with file protocol
if (specifierToUse.startsWith('file:')) {
injectedDependencyToVersion.get(dependency)?.add(specifierToUse);
const specifierToUse: string = typeof specifier === 'string' ? specifier : specifier.version;
// the injected dependency should always start with file protocol
// and exclude tarball installation
// what is the tarball installation, learn more: https://pnpm.io/cli/add#install-from-local-file-system

const tarballSuffix = ['.tar', '.tar.gz', '.tgz'];
g-chao marked this conversation as resolved.
Show resolved Hide resolved
if (
specifierToUse.startsWith('file:') &&
!tarballSuffix.some((suffix) => specifierToUse.endsWith(suffix))
) {
if (!injectedDependencyToVersion.has(dependency)) {
injectedDependencyToVersion.set(dependency, new Set());
}
injectedDependencyToVersion.get(dependency)?.add(specifierToUse);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pnpm-sync/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pnpm-sync",
"version": "0.2.5",
"version": "0.2.6",
"description": "Recopy injected dependencies whenever a project is rebuilt in your PNPM workspace",
"keywords": [
"rush",
Expand Down
Loading