-
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.
Reformat all files using "prettier -w ."
- Loading branch information
Showing
16 changed files
with
92 additions
and
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require("@rushstack/eslint-patch/modern-module-resolution"); | ||
require('@rushstack/eslint-patch/modern-module-resolution'); | ||
module.exports = { | ||
extends: ['@rushstack/eslint-config/profile/node'], | ||
parserOptions: { tsconfigRootDir: __dirname } | ||
}; | ||
}; |
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,8 +1,8 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { cwd } from "process"; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { cwd } from 'process'; | ||
|
||
import type { ILockfile, IPnpmSyncJson, IVersionSpecifier } from "./interfaces"; | ||
import type { ILockfile, IPnpmSyncJson, IVersionSpecifier } from './interfaces'; | ||
|
||
/** | ||
* @beta | ||
|
@@ -30,79 +30,56 @@ export interface IPnpmSyncPrepareOptions { | |
export async function pnpmSyncPrepareAsync({ | ||
lockfilePath, | ||
storePath, | ||
readPnpmLockfile, | ||
readPnpmLockfile | ||
}: IPnpmSyncPrepareOptions): Promise<void> { | ||
console.log("Generate pnpm-sync.json ..."); | ||
console.log('Generate pnpm-sync.json ...'); | ||
|
||
// get the pnpm-lock.yaml path | ||
lockfilePath = path.resolve(cwd(), lockfilePath); | ||
storePath = path.resolve(cwd(), storePath); | ||
|
||
console.log("The pnpm-lock.yaml file path =>", lockfilePath); | ||
console.log("The .pnpm folder path =>", storePath); | ||
console.log('The pnpm-lock.yaml file path =>', lockfilePath); | ||
console.log('The .pnpm folder path =>', storePath); | ||
|
||
if (!fs.existsSync(lockfilePath)) { | ||
throw Error("The input pnpm-lock.yaml path is not correct!"); | ||
throw Error('The input pnpm-lock.yaml path is not correct!'); | ||
} | ||
|
||
console.time(`pnpm-sync prepare`); | ||
|
||
// read the pnpm-lock.yaml | ||
const pnpmLockfile = await readPnpmLockfile(lockfilePath, { | ||
ignoreIncompatible: true, | ||
ignoreIncompatible: true | ||
}); | ||
|
||
// find injected dependency and all its available versions | ||
const injectedDependencyToVersion: Map< | ||
string, | ||
Set<string> | ||
> = getInjectedDependencyToVersion(pnpmLockfile); | ||
const injectedDependencyToVersion: Map<string, Set<string>> = getInjectedDependencyToVersion(pnpmLockfile); | ||
|
||
// get pnpm-lock.yaml folder path | ||
const pnpmLockFolder = lockfilePath.slice( | ||
0, | ||
lockfilePath.length - "pnpm-lock.yaml".length | ||
); | ||
const pnpmLockFolder = lockfilePath.slice(0, lockfilePath.length - 'pnpm-lock.yaml'.length); | ||
|
||
// generate a map, where key is the actual path of the injectedDependency, value is all available paths in .pnpm folder | ||
const injectedDependencyToFilePathSet: Map<string, Set<string>> = new Map(); | ||
for (const [ | ||
injectedDependency, | ||
injectedDependencyVersionSet, | ||
] of injectedDependencyToVersion) { | ||
for (const [injectedDependency, injectedDependencyVersionSet] of injectedDependencyToVersion) { | ||
for (const injectedDependencyVersion of injectedDependencyVersionSet) { | ||
// this logic is heavily depends on pnpm-lock formate | ||
// the current logic is for pnpm v8 | ||
// for example: file:../../libraries/lib1([email protected]) -> ../../libraries/lib1 | ||
let injectedDependencyPath = injectedDependencyVersion | ||
.split("(")[0] | ||
.slice("file:".length); | ||
injectedDependencyPath = path.resolve( | ||
pnpmLockFolder, | ||
injectedDependencyPath | ||
); | ||
let injectedDependencyPath = injectedDependencyVersion.split('(')[0].slice('file:'.length); | ||
injectedDependencyPath = path.resolve(pnpmLockFolder, injectedDependencyPath); | ||
if (!injectedDependencyToFilePathSet.has(injectedDependencyPath)) { | ||
injectedDependencyToFilePathSet.set(injectedDependencyPath, new Set()); | ||
} | ||
|
||
injectedDependencyToFilePathSet | ||
.get(injectedDependencyPath) | ||
?.add( | ||
transferFilePathToPnpmStorePath( | ||
injectedDependencyVersion, | ||
injectedDependency, | ||
storePath | ||
) | ||
); | ||
?.add(transferFilePathToPnpmStorePath(injectedDependencyVersion, injectedDependency, storePath)); | ||
} | ||
} | ||
|
||
// now, we have everything we need to generate the the pnpm-sync.json | ||
// console.log('injectedDependencyToFilePathSet =>', injectedDependencyToFilePathSet); | ||
for (const [ | ||
projectFolder, | ||
targetFolderSet, | ||
] of injectedDependencyToFilePathSet) { | ||
for (const [projectFolder, targetFolderSet] of injectedDependencyToFilePathSet) { | ||
if (targetFolderSet.size === 0) { | ||
continue; | ||
} | ||
|
@@ -111,37 +88,31 @@ export async function pnpmSyncPrepareAsync({ | |
|
||
let pnpmSyncJsonFile: IPnpmSyncJson = { | ||
postbuildInjectedCopy: { | ||
sourceFolder: "../..", | ||
targetFolders: [], | ||
}, | ||
sourceFolder: '../..', | ||
targetFolders: [] | ||
} | ||
}; | ||
|
||
// if .pnpm-sync.json already exists, read it first | ||
if (fs.existsSync(pnpmSyncJsonPath)) { | ||
pnpmSyncJsonFile = JSON.parse( | ||
fs.readFileSync(pnpmSyncJsonPath).toString() | ||
); | ||
pnpmSyncJsonFile = JSON.parse(fs.readFileSync(pnpmSyncJsonPath).toString()); | ||
} | ||
|
||
const existingTargetFolderSet: Set<string> = new Set(); | ||
|
||
for (const targetFolder of pnpmSyncJsonFile.postbuildInjectedCopy | ||
.targetFolders) { | ||
for (const targetFolder of pnpmSyncJsonFile.postbuildInjectedCopy.targetFolders) { | ||
existingTargetFolderSet.add(targetFolder.folderPath); | ||
} | ||
|
||
for (const targetFolder of targetFolderSet) { | ||
const relativePath = path.relative(pnpmSyncJsonPath, targetFolder); | ||
if (!existingTargetFolderSet.has(relativePath)) { | ||
pnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.push({ | ||
folderPath: relativePath, | ||
folderPath: relativePath | ||
}); | ||
} | ||
} | ||
fs.writeFileSync( | ||
pnpmSyncJsonPath, | ||
JSON.stringify(pnpmSyncJsonFile, null, 2) | ||
); | ||
fs.writeFileSync(pnpmSyncJsonPath, JSON.stringify(pnpmSyncJsonFile, null, 2)); | ||
} | ||
console.timeEnd(`pnpm-sync prepare`); | ||
} | ||
|
@@ -157,36 +128,33 @@ function transferFilePathToPnpmStorePath( | |
// an example, file:../../libraries/lib1([email protected]) -> [email protected] | ||
|
||
// 1. replace ':' with '+' | ||
rawFilePath = rawFilePath.replaceAll(":", "+"); | ||
rawFilePath = rawFilePath.replaceAll(':', '+'); | ||
|
||
// 2. replace '/' with '+' | ||
rawFilePath = rawFilePath.replaceAll("/", "+"); | ||
rawFilePath = rawFilePath.replaceAll('/', '+'); | ||
|
||
// 3. replace '(' with '_' | ||
rawFilePath = rawFilePath.replaceAll("(", "_"); | ||
rawFilePath = rawFilePath.replaceAll('(', '_'); | ||
|
||
// 4. remove ')' | ||
rawFilePath = rawFilePath.replaceAll(")", ""); | ||
rawFilePath = rawFilePath.replaceAll(')', ''); | ||
|
||
// 5. add dependencyName | ||
rawFilePath = rawFilePath + `/node_modules/${dependencyName}`; | ||
|
||
rawFilePath = storePath + "/" + rawFilePath; | ||
rawFilePath = storePath + '/' + rawFilePath; | ||
|
||
return rawFilePath; | ||
} | ||
|
||
// process dependencies and devDependencies to generate injectedDependencyToFilePath | ||
function getInjectedDependencyToVersion( | ||
pnpmLockfile: ILockfile | undefined | ||
): Map<string, Set<string>> { | ||
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; | ||
const dependenciesMeta = pnpmLockfile?.importers[importerKey]?.dependenciesMeta; | ||
|
||
for (const dependency in dependenciesMeta) { | ||
if (dependenciesMeta[dependency]?.injected) { | ||
|
@@ -198,14 +166,8 @@ function getInjectedDependencyToVersion( | |
|
||
// 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]?.dependencies, injectedDependencyToVersion); | ||
processDependencies(pnpmLockfile?.importers[importerKey]?.devDependencies, injectedDependencyToVersion); | ||
processDependencies( | ||
pnpmLockfile?.importers[importerKey]?.optionalDependencies, | ||
injectedDependencyToVersion | ||
|
@@ -220,8 +182,7 @@ function processDependencies( | |
if (dependencies) { | ||
for (const [dependency, specifier] of Object.entries(dependencies)) { | ||
if (injectedDependencyToVersion.has(dependency)) { | ||
const specifierToUse: string = | ||
typeof specifier === "string" ? specifier : specifier.version; | ||
const specifierToUse: string = typeof specifier === 'string' ? specifier : specifier.version; | ||
injectedDependencyToVersion.get(dependency)?.add(specifierToUse); | ||
} | ||
} | ||
|
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,5 +1,5 @@ | ||
require("@rushstack/eslint-patch/modern-module-resolution"); | ||
require('@rushstack/eslint-patch/modern-module-resolution'); | ||
module.exports = { | ||
extends: ['@rushstack/eslint-config/profile/node'], | ||
parserOptions: { tsconfigRootDir: __dirname } | ||
}; | ||
}; |
Oops, something went wrong.