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

feat: use PNPM logic to process depPathToFilename #24

Merged
merged 5 commits into from
Apr 10, 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
6 changes: 6 additions & 0 deletions packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export type LogMessageDetails = {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_FORMAT;
lockfilePath: string;
lockfileVersion: string | undefined;
} | {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION;
lockfilePath: string;
pnpmVersion: string | undefined;
} | {
messageIdentifier: LogMessageIdentifier.PREPARE_PROCESSING;
lockfilePath: string;
Expand Down Expand Up @@ -134,6 +138,8 @@ export enum LogMessageIdentifier {
// (undocumented)
PREPARE_ERROR_UNSUPPORTED_FORMAT = "prepare-error-unsupported-format",
// (undocumented)
PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION = "prepare-error-unsupported-pnpm-version",
// (undocumented)
PREPARE_FINISHING = "prepare-finishing",
// (undocumented)
PREPARE_PROCESSING = "prepare-processing",
Expand Down
6 changes: 5 additions & 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.1",
"version": "0.2.2",
"description": "API library for integrating \"pnpm-sync\" with your toolchain",
"repository": {
"type": "git",
Expand All @@ -16,6 +16,10 @@
"lint": "eslint src/**",
"prepublishOnly": "heft build --clean"
},
"dependencies": {
"@pnpm/dependency-path": "2.1.8",
"yaml": "2.4.1"
},
"devDependencies": {
"@rushstack/eslint-config": "^3.6.2",
"@rushstack/eslint-patch": "^1.7.2",
Expand Down
6 changes: 6 additions & 0 deletions packages/pnpm-sync-lib/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum LogMessageIdentifier {
// pnpmSyncPrepareAsync() messages
PREPARE_STARTING = 'prepare-starting',
PREPARE_ERROR_UNSUPPORTED_FORMAT = 'prepare-error-unsupported-format',
PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION = 'prepare-error-unsupported-pnpm-version',
PREPARE_PROCESSING = 'prepare-processing',
PREPARE_REPLACING_FILE = 'prepare-replacing-file',
PREPARE_WRITING_FILE = 'prepare-writing-file',
Expand All @@ -59,6 +60,11 @@ export type LogMessageDetails =
lockfilePath: string;
lockfileVersion: string | undefined;
}
| {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION;
lockfilePath: string;
pnpmVersion: string | undefined;
}
| {
messageIdentifier: LogMessageIdentifier.PREPARE_PROCESSING;
lockfilePath: string;
Expand Down
61 changes: 28 additions & 33 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import fs from 'fs';
import YAML from 'yaml';
import process from 'node:process';
import { depPathToFilename } from '@pnpm/dependency-path';

import {
ILockfile,
Expand Down Expand Up @@ -87,6 +89,24 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr

const startTime = process.hrtime.bigint();

const pnpmModulesYamlPath: string = path.resolve(dotPnpmFolder, '..');
const pnpmModulesYaml = YAML.parse(fs.readFileSync(`${pnpmModulesYamlPath}/.modules.yaml`, 'utf8'));
const pnpmVersion: string | undefined = pnpmModulesYaml?.packageManager?.split('@')[1];

// currently, only support pnpm v8
if (!pnpmVersion || !pnpmVersion.startsWith('8')) {
logMessageCallback({
message: `The pnpm version is not supported; pnpm-sync requires pnpm version 8.x`,
messageKind: LogMessageKind.ERROR,
details: {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION,
lockfilePath,
pnpmVersion: pnpmVersion
}
});
return;
}

// read the pnpm-lock.yaml
const pnpmLockfile: ILockfile | undefined = await readPnpmLockfile(lockfilePath, {
ignoreIncompatible: true
Expand Down Expand Up @@ -129,9 +149,14 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
injectedDependencyToFilePathSet.set(injectedDependencyPath, new Set());
}

injectedDependencyToFilePathSet
.get(injectedDependencyPath)
?.add(transferFilePathToDotPnpmFolder(injectedDependencyVersion, injectedDependency, dotPnpmFolder));
const fullPackagePath = path.join(
dotPnpmFolder,
depPathToFilename(injectedDependencyVersion),
'node_modules',
injectedDependency
);

injectedDependencyToFilePathSet.get(injectedDependencyPath)?.add(fullPackagePath);
}
}

Expand Down Expand Up @@ -235,36 +260,6 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
});
}

function transferFilePathToDotPnpmFolder(
rawFilePath: string,
dependencyName: string,
dotPnpmFolder: string
): string {
// this logic is heavily depends on pnpm-lock format
// the current logic is for pnpm v8

// an example, file:../../libraries/lib1([email protected]) -> [email protected]

// 1. replace ':' with '+'
rawFilePath = rawFilePath.replaceAll(':', '+');

// 2. replace '/' with '+'
rawFilePath = rawFilePath.replaceAll('/', '+');

// 3. replace '(' with '_'
rawFilePath = rawFilePath.replaceAll('(', '_');

// 4. remove ')'
rawFilePath = rawFilePath.replaceAll(')', '');

// 5. add dependencyName
rawFilePath = rawFilePath + `/node_modules/${dependencyName}`;

rawFilePath = dotPnpmFolder + '/' + rawFilePath;

return rawFilePath;
}

// process dependencies and devDependencies to generate injectedDependencyToFilePath
function getInjectedDependencyToVersion(pnpmLockfile: ILockfile | undefined): Map<string, Set<string>> {
const injectedDependencyToVersion: Map<string, Set<string>> = new Map();
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.1",
"version": "0.2.2",
"description": "Recopy injected dependencies whenever a project is rebuilt in your PNPM workspace",
"keywords": [
"rush",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion tests/pnpm-sync-api-test/src/test/pnpmSyncPrepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('pnpm-sync-api prepare test', () => {
Object {
"details": Object {
"actualVersion": "incompatible-version",
"expectedVersion": "0.2.1",
"expectedVersion": "${pnpmSyncLibVersion}",
"messageIdentifier": "prepare-replacing-file",
"pnpmSyncJsonPath": "<root>/pnpm-sync/tests/test-fixtures/sample-lib1/node_modules/.pnpm-sync.json",
"projectFolder": "<root>/pnpm-sync/tests/test-fixtures/sample-lib1",
Expand Down
Loading