-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
147 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
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) | ||
} |
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 |
---|---|---|
@@ -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') | ||
} |