-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from tiktok/chao/depPathToFilename
feat: use PNPM logic to process depPathToFilename
- Loading branch information
Showing
7 changed files
with
60 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters