Skip to content

Commit

Permalink
fix: apply reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
younggglcy committed Jan 31, 2025
1 parent 3ddff82 commit 93e30bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/io/pnpmWorkspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function writePnpmWorkspace(
}

if (changed)
await fs.writeFile(pkg.filepath, stringify(contents), 'utf-8')
await writeYaml(pkg, contents)

// currently only support preserve yaml anchor and alias with single string value
function updateCatalog(catalog: YAMLMap<Scalar.Parsed, Scalar.Parsed>, contents: Record<string, any>) {
Expand Down Expand Up @@ -117,3 +117,7 @@ export async function writePnpmWorkspace(
}
}
}

export function writeYaml(pkg: PnpmWorkspaceMeta, yamlContents: any) {
return fs.writeFile(pkg.filepath, stringify(yamlContents), 'utf-8')
}
36 changes: 24 additions & 12 deletions test/pnpmCatalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { CheckOptions, PnpmWorkspaceMeta } from '../src'
import fs from 'node:fs/promises'
import process from 'node:process'
import { expect, it } from 'vitest'
import { parse, parseDocument } from 'yaml'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { parse, parseDocument, stringify } from 'yaml'
import { CheckPackages } from '../src'
import { writePnpmWorkspace } from '../src/io/pnpmWorkspaces'
import * as pnpmWorkspaces from '../src/io/pnpmWorkspaces'

it('pnpm catalog', async () => {
const options: CheckOptions = {
Expand Down Expand Up @@ -104,7 +103,25 @@ it('pnpm catalog', async () => {
`)
})

it('pnpm catalog updates should preserve yaml anchors and aliases with single string value', async () => {
describe('pnpm catalog updates', async () => {
// stringified yaml output that should be
// written to the pnpm-workspace.yaml file
let output: string | undefined
beforeAll(() => {
// mock fn writeYaml
vi.spyOn(pnpmWorkspaces, 'writeYaml').mockImplementation((_pkg: PnpmWorkspaceMeta, contents: any) => {
return Promise.resolve().then(() => {
output = stringify(contents)
})
})
})

afterAll(() => {
// @ts-expect-error we mocked it in `beforeAll` hook
pnpmWorkspaces.writeYaml.mockRestore()
})

it('pnpm catalog updates should preserve yaml anchors and aliases with single string value, when anchor is defined inline', async () => {
const workspaceYamlContents = `
catalog:
react: &foo ^18.2.0
Expand All @@ -124,16 +141,11 @@ catalog:
filepath: '',
type: 'pnpm-workspace.yaml',
}

let output: string
// @ts-expect-error testing purpose
fs.writeFile = (_path, data, _ops) => output = data

await writePnpmWorkspace(pkg, {})
// @ts-expect-error testing purpose
await pnpmWorkspaces.writePnpmWorkspace(pkg, {})
expect(output).toMatchInlineSnapshot(`
"catalog:
react: &foo ^18.3.1
react-dom: *foo
"`)
})
})

0 comments on commit 93e30bc

Please sign in to comment.