Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 13, 2024
1 parent 15063dd commit 28f60e6
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CheckOptions } from './types'

export { resolvePackage, resolveDependencies, resolveDependency } from './io/resolves'
export { loadPackageJSON as loadPackage, loadPackages, writePackage } from './io/packages'
export { loadPackage, loadPackages, writePackage } from './io/packages'
export { dumpDependencies, parseDependencies } from './io/dependencies'
export { CheckPackages } from './api/check'
export * from './types'
Expand Down
82 changes: 82 additions & 0 deletions src/io/packageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import path from 'node:path'
import type { CommonOptions, PackageMeta, RawDep } from '../types'
import { dumpDependencies, getByPath, parseDependencies, parseDependency, setByPath } from './dependencies'
import { readJSON, writeJSON } from './packages'

const depsFields = [
'dependencies',
'devDependencies',
'optionalDependencies',
'packageManager',
'pnpm.overrides',
'resolutions',
'overrides',
] as const

export async function loadPackageJSON(
relative: string,
options: CommonOptions,
shouldUpdate: (name: string) => boolean,
): Promise<PackageMeta[]> {
const filepath = path.resolve(options.cwd ?? '', relative)
const raw = await readJSON(filepath)
const deps: RawDep[] = []

for (const key of depsFields) {
if (options.depFields?.[key] !== false) {
if (key === 'packageManager') {
if (raw.packageManager) {
const [name, version] = raw.packageManager.split('@')
deps.push(parseDependency(name, `^${version}`, 'packageManager', shouldUpdate))
}
}
else {
deps.push(...parseDependencies(raw, key, shouldUpdate))
}
}
}

return [
{
name: raw.name,
private: !!raw.private,
version: raw.version,
type: 'package.json',
relative,
filepath,
raw,
deps,
resolved: [],
},
]
}

export async function writePackageJSON(
pkg: PackageMeta,
options: CommonOptions,
) {
const { raw, filepath, resolved } = pkg

let changed = false

depsFields.forEach((key) => {
if (options.depFields?.[key] === false)
return
if (key === 'packageManager') {
const value = Object.entries(dumpDependencies(resolved, 'packageManager'))[0]
if (value) {
raw.packageManager = `${value[0]}@${value[1].replace('^', '')}`
changed = true
}
}
else {
if (getByPath(raw, key)) {
setByPath(raw, key, dumpDependencies(resolved, key))
changed = true
}
}
})

if (changed)
await writeJSON(filepath, raw)
}
148 changes: 2 additions & 146 deletions src/io/packages.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'node:path'
import { existsSync, promises as fs } from 'node:fs'
import fg from 'fast-glob'
import YAML from 'js-yaml'
import detectIndent from 'detect-indent'
import type { CommonOptions, PackageMeta, RawDep } from '../types'

Check failure on line 5 in src/io/packages.ts

View workflow job for this annotation

GitHub Actions / lint

'RawDep' is defined but never used
import { createDependenciesFilter } from '../utils/dependenciesFilter'
import { DEFAULT_IGNORE_PATHS } from '../constants'
import { dumpDependencies, getByPath, parseDependencies, parseDependency, setByPath } from './dependencies'
import { loadPackageJSON, writePackageJSON } from './packageJson'
import { loadPnpmWorkspace, writePnpmWorkspace } from './pnpmWorkspaces'

export async function readJSON(filepath: string) {
return JSON.parse(await fs.readFile(filepath, 'utf-8'))

Check failure on line 12 in src/io/packages.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, ubuntu-latest)

test/pnpmCatalog.test.ts > pnpm catalog

Error: ENOENT: no such file or directory, open '/home/runner/work/taze/taze/test/fixtures/pnpm catalog/package.json' ❯ Module.readJSON src/io/packages.ts:12:21 ❯ Module.loadPackageJSON src/io/packageJson.ts:22:15 ❯ Module.loadPackages src/io/packages.ts:67:21 ❯ Module.CheckPackages src/api/check.ts:20:20 ❯ test/pnpmCatalog.test.ts:10:18 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -2, code: 'ENOENT', syscall: 'open', path: '/home/runner/work/taze/taze/test/fixtures/pnpm catalog/package.json' }

Check failure on line 12 in src/io/packages.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, windows-latest)

test/pnpmCatalog.test.ts > pnpm catalog

Error: ENOENT: no such file or directory, open 'D:\a\taze\taze\test\fixtures\pnpm catalog\package.json' ❯ Module.readJSON src/io/packages.ts:12:21 ❯ Module.loadPackageJSON src/io/packageJson.ts:22:15 ❯ Module.loadPackages src/io/packages.ts:67:21 ❯ Module.CheckPackages src/api/check.ts:20:20 ❯ test/pnpmCatalog.test.ts:10:18 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'D:\a\taze\taze\test\fixtures\pnpm catalog\package.json' }

Check failure on line 12 in src/io/packages.ts

View workflow job for this annotation

GitHub Actions / test (lts/*, macos-latest)

test/pnpmCatalog.test.ts > pnpm catalog

Error: ENOENT: no such file or directory, open '/Users/runner/work/taze/taze/test/fixtures/pnpm catalog/package.json' ❯ Module.readJSON src/io/packages.ts:12:21 ❯ Module.loadPackageJSON src/io/packageJson.ts:22:15 ❯ Module.loadPackages src/io/packages.ts:67:21 ❯ Module.CheckPackages src/api/check.ts:20:20 ❯ test/pnpmCatalog.test.ts:10:18 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -2, code: 'ENOENT', syscall: 'open', path: '/Users/runner/work/taze/taze/test/fixtures/pnpm catalog/package.json' }
Expand All @@ -19,16 +19,6 @@ export async function writeJSON(filepath: string, data: any) {
return await fs.writeFile(filepath, `${JSON.stringify(data, null, fileIndent)}\n`, 'utf-8')
}

const depsFields = [
'dependencies',
'devDependencies',
'optionalDependencies',
'packageManager',
'pnpm.overrides',
'resolutions',
'overrides',
] as const

export async function writePackage(
pkg: PackageMeta,
options: CommonOptions,
Expand All @@ -43,102 +33,6 @@ export async function writePackage(
}
}

export async function writePnpmWorkspace(
pkg: PackageMeta,
_options: CommonOptions,
) {
const catalogName = pkg.name.replace('catalog:', '')
const versions = dumpDependencies(pkg.resolved, 'pnpm:catalog')

if (!Object.keys(versions).length)
return

if (catalogName === 'default') {
pkg.raw.catalog = versions
}
else {
pkg.raw.catalogs ??= {}
pkg.raw.catalogs[catalogName] = versions
}

await fs.writeFile(pkg.filepath, YAML.dump(pkg.raw), 'utf-8')
}

export async function writePackageJSON(
pkg: PackageMeta,
options: CommonOptions,
) {
const { raw, filepath, resolved } = pkg

let changed = false

depsFields.forEach((key) => {
if (options.depFields?.[key] === false)
return
if (key === 'packageManager') {
const value = Object.entries(dumpDependencies(resolved, 'packageManager'))[0]
if (value) {
raw.packageManager = `${value[0]}@${value[1].replace('^', '')}`
changed = true
}
}
else {
if (getByPath(raw, key)) {
setByPath(raw, key, dumpDependencies(resolved, key))
changed = true
}
}
})

if (changed)
await writeJSON(filepath, raw)
}

export async function loadPnpmWorkspace(
relative: string,
options: CommonOptions,
shouldUpdate: (name: string) => boolean,
): Promise<PackageMeta[]> {
const filepath = path.resolve(options.cwd ?? '', relative)
const rawText = await fs.readFile(filepath, 'utf-8')
const raw = YAML.load(rawText) as any

const catalogs: PackageMeta[] = []

function createCatalogFromKeyValue(catalogName: string, map: Record<string, string>): PackageMeta {
const deps: RawDep[] = Object.entries(map)
.map(([name, version]) => parseDependency(name, version, 'pnpm:catalog', shouldUpdate))

return {
name: `catalog:${catalogName}`,
private: true,
version: '',
type: 'pnpm-workspace.yaml',
relative,
filepath,
raw,
deps,
resolved: [],
}
}

if (raw.catalog) {
catalogs.push(
createCatalogFromKeyValue('default', raw.catalog),
)
}

if (raw.catalogs) {
for (const key of Object.keys(raw.catalogs)) {
catalogs.push(
createCatalogFromKeyValue(key, raw.catalogs[key]),
)
}
}

return catalogs
}

export async function loadPackage(
relative: string,
options: CommonOptions,
Expand All @@ -149,44 +43,6 @@ export async function loadPackage(
return loadPackageJSON(relative, options, shouldUpdate)
}

export async function loadPackageJSON(
relative: string,
options: CommonOptions,
shouldUpdate: (name: string) => boolean,
): Promise<PackageMeta[]> {
const filepath = path.resolve(options.cwd ?? '', relative)
const raw = await readJSON(filepath)
const deps: RawDep[] = []

for (const key of depsFields) {
if (options.depFields?.[key] !== false) {
if (key === 'packageManager') {
if (raw.packageManager) {
const [name, version] = raw.packageManager.split('@')
deps.push(parseDependency(name, `^${version}`, 'packageManager', shouldUpdate))
}
}
else {
deps.push(...parseDependencies(raw, key, shouldUpdate))
}
}
}

return [
{
name: raw.name,
private: !!raw.private,
version: raw.version,
type: 'package.json',
relative,
filepath,
raw,
deps,
resolved: [],
},
]
}

export async function loadPackages(options: CommonOptions): Promise<PackageMeta[]> {
let packagesNames: string[] = []

Expand Down
71 changes: 71 additions & 0 deletions src/io/pnpmWorkspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import YAML from 'js-yaml'
import type { CommonOptions, PackageMeta, RawDep } from '../types'
import { dumpDependencies, parseDependency } from './dependencies'

export async function loadPnpmWorkspace(
relative: string,
options: CommonOptions,
shouldUpdate: (name: string) => boolean,
): Promise<PackageMeta[]> {
const filepath = path.resolve(options.cwd ?? '', relative)
const rawText = await fs.readFile(filepath, 'utf-8')
const raw = YAML.load(rawText) as any

const catalogs: PackageMeta[] = []

function createCatalogFromKeyValue(catalogName: string, map: Record<string, string>): PackageMeta {
const deps: RawDep[] = Object.entries(map)
.map(([name, version]) => parseDependency(name, version, 'pnpm:catalog', shouldUpdate))

return {
name: `catalog:${catalogName}`,
private: true,
version: '',
type: 'pnpm-workspace.yaml',
relative,
filepath,
raw,
deps,
resolved: [],
}
}

if (raw.catalog) {
catalogs.push(
createCatalogFromKeyValue('default', raw.catalog),
)
}

if (raw.catalogs) {
for (const key of Object.keys(raw.catalogs)) {
catalogs.push(
createCatalogFromKeyValue(key, raw.catalogs[key]),
)
}
}

return catalogs
}

export async function writePnpmWorkspace(
pkg: PackageMeta,
_options: CommonOptions,
) {
const catalogName = pkg.name.replace('catalog:', '')
const versions = dumpDependencies(pkg.resolved, 'pnpm:catalog')

if (!Object.keys(versions).length)
return

if (catalogName === 'default') {
pkg.raw.catalog = versions
}
else {
pkg.raw.catalogs ??= {}
pkg.raw.catalogs[catalogName] = versions
}

await fs.writeFile(pkg.filepath, YAML.dump(pkg.raw), 'utf-8')
}

0 comments on commit 28f60e6

Please sign in to comment.