Skip to content

Commit

Permalink
Merge pull request #11 from tiktok/chao/refactor-readWantedLockfile
Browse files Browse the repository at this point in the history
refactor:  readWantedLockfile to readPnpmLockfile
  • Loading branch information
g-chao authored Feb 15, 2024
2 parents e644c89 + b4912a2 commit 1936727
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export interface IPnpmSyncPrepareOptions {
// (undocumented)
lockfilePath: string;
// (undocumented)
readWantedLockfile: (lockfilePath: string, options: {
readPnpmLockfile: (lockfilePath: string, options: {
ignoreIncompatible: boolean;
}) => Promise<ILockfile | null>;
}) => Promise<ILockfile | undefined>;
// (undocumented)
storePath: string;
}
Expand All @@ -63,6 +63,6 @@ export type IVersionSpecifier = string | {
export function pnpmSyncCopyAsync({ pnpmSyncJsonPath, getPackageIncludedFiles, forEachAsyncWithConcurrency, ensureFolder, }: IPnpmSyncCopyOptions): Promise<void>;

// @beta
export function pnpmSyncPrepareAsync({ lockfilePath, storePath, readWantedLockfile, }: IPnpmSyncPrepareOptions): Promise<void>;
export function pnpmSyncPrepareAsync({ lockfilePath, storePath, readPnpmLockfile, }: IPnpmSyncPrepareOptions): Promise<void>;

```
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.1.2",
"version": "0.1.3",
"description": "API library for integrating \"pnpm-sync\" with your toolchain",
"repository": {
"type": "git",
Expand Down
22 changes: 11 additions & 11 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import type { ILockfile, IPnpmSyncJson, IVersionSpecifier } from "./interfaces";
export interface IPnpmSyncPrepareOptions {
lockfilePath: string;
storePath: string;
readWantedLockfile: (
readPnpmLockfile: (
lockfilePath: string,
options: { ignoreIncompatible: boolean }
// eslint-disable-next-line @rushstack/no-new-null
) => Promise<ILockfile | null>;
) => Promise<ILockfile | undefined>;
}

/**
Expand All @@ -31,7 +30,7 @@ export interface IPnpmSyncPrepareOptions {
export async function pnpmSyncPrepareAsync({
lockfilePath,
storePath,
readWantedLockfile,
readPnpmLockfile,
}: IPnpmSyncPrepareOptions): Promise<void> {
console.log("Generate pnpm-sync.json ...");

Expand All @@ -49,11 +48,7 @@ export async function pnpmSyncPrepareAsync({
console.time(`pnpm-sync prepare`);

// read the pnpm-lock.yaml
const pnpmLockFolder = lockfilePath.slice(
0,
lockfilePath.length - "pnpm-lock.yaml".length
);
const pnpmLockfile = await readWantedLockfile(pnpmLockFolder, {
const pnpmLockfile = await readPnpmLockfile(lockfilePath, {
ignoreIncompatible: true,
});

Expand All @@ -63,6 +58,12 @@ export async function pnpmSyncPrepareAsync({
Set<string>
> = getInjectedDependencyToVersion(pnpmLockfile);

// get pnpm-lock.yaml folder path
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 [
Expand Down Expand Up @@ -177,8 +178,7 @@ function transferFilePathToPnpmStorePath(

// process dependencies and devDependencies to generate injectedDependencyToFilePath
function getInjectedDependencyToVersion(
// eslint-disable-next-line @rushstack/no-new-null
pnpmLockfile: ILockfile | null
pnpmLockfile: ILockfile | undefined
): Map<string, Set<string>> {
const injectedDependencyToVersion: Map<string, Set<string>> = new Map();
for (const importerKey in pnpmLockfile?.importers) {
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.1.2",
"version": "0.1.3",
"description": "Recopy injected dependencies whenever a project is rebuilt in your PNPM workspace",
"keywords": [
"rush",
Expand Down
20 changes: 18 additions & 2 deletions packages/pnpm-sync/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command } from "commander";
import { pnpmSyncCopyAsync, pnpmSyncPrepareAsync } from "pnpm-sync-lib";
import { FileSystem, Async } from "@rushstack/node-core-library";
import { PackageExtractor } from "@rushstack/package-extractor";
import { readWantedLockfile } from "@pnpm/lockfile-file";
import { readWantedLockfile, Lockfile } from "@pnpm/lockfile-file";

const program: Command = new Command();

Expand Down Expand Up @@ -35,7 +35,23 @@ program
await pnpmSyncPrepareAsync({
lockfilePath: lockfile,
storePath: store,
readWantedLockfile,
readPnpmLockfile: async (lockfilePath: string, options: {
ignoreIncompatible: boolean
}) => {

const pnpmLockFolder = lockfilePath.slice(
0,
lockfilePath.length - "pnpm-lock.yaml".length
);

const lockfile: Lockfile | null = await readWantedLockfile(pnpmLockFolder, options);

if(lockfile === null){
return undefined;
} else {
return lockfile
}
},
});
} catch (error) {
console.log(error);
Expand Down

0 comments on commit 1936727

Please sign in to comment.