Skip to content

Commit

Permalink
Merge pull request #34 from tiktok/chao/add-identifier
Browse files Browse the repository at this point in the history
feat: add an identifier for pnpmSyncPrepare
  • Loading branch information
g-chao authored Jun 6, 2024
2 parents 7e8b03d + 8f5ca35 commit b01b345
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface IPnpmSyncCopyOptions {
export interface IPnpmSyncPrepareOptions {
dotPnpmFolder: string;
ensureFolderAsync: (folderPath: string) => Promise<void>;
lockfileId?: string;
lockfilePath: string;
logMessageCallback: (options: ILogMessageCallbackOptions) => void;
readPnpmLockfile: (lockfilePath: string, options: {
Expand Down
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.6",
"version": "0.2.7",
"description": "API library for integrating \"pnpm-sync\" with your toolchain",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/pnpm-sync-lib/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IPnpmSyncJson {

export interface ITargetFolder {
folderPath: string;
lockfileId?: string;
}

export interface ISyncItem {
Expand Down
29 changes: 25 additions & 4 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
LogMessageIdentifier,
IPnpmSyncJson,
IVersionSpecifier,
ILockfilePackage
ILockfilePackage,
ITargetFolder
} from './interfaces';
import { pnpmSyncGetJsonVersion } from './utilities';

Expand All @@ -29,6 +30,11 @@ export interface IPnpmSyncPrepareOptions {
*/
dotPnpmFolder: string;

/**
* A lockfileId that can be used to recognize the `pnpm-lock.yaml`
*/
lockfileId?: string;

/**
* Environment-provided API to avoid an NPM dependency.
* The "pnpm-sync" NPM package provides a reference implementation.
Expand Down Expand Up @@ -63,7 +69,7 @@ export interface IPnpmSyncPrepareOptions {
* @beta
*/
export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Promise<void> {
const { ensureFolderAsync, readPnpmLockfile, logMessageCallback } = options;
const { lockfileId, ensureFolderAsync, readPnpmLockfile, logMessageCallback } = options;
let { lockfilePath, dotPnpmFolder } = options;

// get the pnpm-lock.yaml path
Expand Down Expand Up @@ -214,6 +220,15 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr

const actualPnpmSyncJsonVersion: string = existingPnpmSyncJsonFile?.version;
if (actualPnpmSyncJsonVersion === expectedPnpmSyncJsonVersion) {
// If a lockfileId is provided
// then all entries with this lockfileId should be deleted
// they will be regenerated later
if (lockfileId) {
const filteredTargetFolders = existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.filter(
(targetFolder) => targetFolder?.lockfileId !== lockfileId
);
existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders = filteredTargetFolders;
}
pnpmSyncJsonFile = existingPnpmSyncJsonFile;
} else {
logMessageCallback({
Expand Down Expand Up @@ -243,9 +258,15 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
relativePath = relativePath.split(path.sep).join(path.posix.sep);

if (!existingTargetFolderSet.has(relativePath)) {
pnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.push({
const targetFolderItem: ITargetFolder = {
folderPath: relativePath
});
};

if (lockfileId) {
targetFolderItem.lockfileId = lockfileId;
}

pnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.push(targetFolderItem);
}
}

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.6",
"version": "0.2.7",
"description": "Recopy injected dependencies whenever a project is rebuilt in your PNPM workspace",
"keywords": [
"rush",
Expand Down
5 changes: 4 additions & 1 deletion tests/pnpm-sync-api-test/config/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"coverageReporters": ["cobertura", "html"],

// Use v8 coverage provider to avoid Babel
"coverageProvider": "v8"
"coverageProvider": "v8",

// Let jest test files run one by one
"maxWorkers": 1
}
21 changes: 15 additions & 6 deletions tests/pnpm-sync-api-test/src/test/pnpmSyncCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ describe('pnpm-sync-api copy test', () => {
const lockfilePath = '../../pnpm-lock.yaml';
const dotPnpmFolder = '../../node_modules/.pnpm';

const pnpmSyncJsonFolder1 = `../test-fixtures/sample-lib1/node_modules`;
const pnpmSyncJsonFolder2 = `../test-fixtures/sample-lib2/node_modules`;

const pnpmSyncJsonPath1 = `${pnpmSyncJsonFolder1}/.pnpm-sync.json`;
const pnpmSyncJsonPath2 = `${pnpmSyncJsonFolder2}/.pnpm-sync.json`;

// if .pnpm-sync.json already exists, delete it first
if (fs.existsSync(pnpmSyncJsonPath1)) {
fs.unlinkSync(pnpmSyncJsonPath1);
}

if (fs.existsSync(pnpmSyncJsonPath2)) {
fs.unlinkSync(pnpmSyncJsonPath2);
}

// generate .pnpm-sync.json file first.
await pnpmSyncPrepareAsync({
lockfilePath: lockfilePath,
Expand All @@ -26,12 +41,6 @@ describe('pnpm-sync-api copy test', () => {
logMessageCallback: (): void => {}
});

const pnpmSyncJsonFolder1 = `../test-fixtures/sample-lib1/node_modules`;
const pnpmSyncJsonFolder2 = `../test-fixtures/sample-lib2/node_modules`;

const pnpmSyncJsonPath1 = `${pnpmSyncJsonFolder1}/.pnpm-sync.json`;
const pnpmSyncJsonPath2 = `${pnpmSyncJsonFolder2}/.pnpm-sync.json`;

const targetFolderPath1 =
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib1';
const targetFolderPath2 =
Expand Down
73 changes: 73 additions & 0 deletions tests/pnpm-sync-api-test/src/test/pnpmSyncPrepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,77 @@ describe('pnpm-sync-api prepare test', () => {
}
});
});

it('pnpmSyncPrepareAsync should handle identifier (if provided)', async () => {
const lockfilePath = '../../pnpm-lock.yaml';
const dotPnpmFolder = '../../node_modules/.pnpm';

const pnpmSyncJsonFolder = `../test-fixtures/sample-lib1/node_modules`;
const pnpmSyncJsonPath = `${pnpmSyncJsonFolder}/.pnpm-sync.json`;

if (!fs.existsSync(pnpmSyncJsonFolder)) {
await FileSystem.ensureFolderAsync(pnpmSyncJsonFolder);
}

// create an .pnpm-sync.json with some identifiers
const pnpmSyncJsonFile = {
version: pnpmSyncLibVersion,
postbuildInjectedCopy: {
sourceFolder: '..',
targetFolders: [
{
folderPath:
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib1',
lockfileId: 'identifier1'
},
{
folderPath:
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib2',
lockfileId: 'identifier1'
},
{
folderPath:
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib3',
lockfileId: 'identifier2'
}
]
}
};

// write .pnpm-sync.json
await fs.promises.writeFile(pnpmSyncJsonPath, JSON.stringify(pnpmSyncJsonFile, null, 2));

await pnpmSyncPrepareAsync({
lockfilePath: lockfilePath,
dotPnpmFolder: dotPnpmFolder,
lockfileId: 'identifier1',
ensureFolderAsync: FileSystem.ensureFolderAsync,
readPnpmLockfile,
logMessageCallback: (): void => {}
});

// now, read .pnpm-sync.json and check the fields
expect(fs.existsSync(pnpmSyncJsonPath)).toBe(true);

// the folderPath with identifier1 should be regenerated
// the folderPath with identifier2 should keep as it is
expect(JSON.parse(fs.readFileSync(pnpmSyncJsonPath).toString())).toEqual({
version: pnpmSyncLibVersion,
postbuildInjectedCopy: {
sourceFolder: '..',
targetFolders: [
{
folderPath:
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib3',
lockfileId: 'identifier2'
},
{
folderPath:
'../../../../node_modules/.pnpm/[email protected]/node_modules/api-demo-sample-lib1',
lockfileId: 'identifier1'
}
]
}
});
});
});

0 comments on commit b01b345

Please sign in to comment.