Skip to content

Commit 2957b83

Browse files
committed
Add almost vlt support
1 parent 26699ba commit 2957b83

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

src/commands/optimize.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ const getOverridesDataByAgent: Record<Agent, GetOverrides> = {
6969
const overrides = (pkgJson as any)?.pnpm?.overrides ?? {}
7070
return { type: 'pnpm', overrides }
7171
},
72+
vlt(pkgJson: PackageJsonContent) {
73+
const overrides = (pkgJson as any)?.overrides ?? {}
74+
return { type: 'vlt', overrides }
75+
},
7276
// Yarn resolutions documentation:
7377
// https://yarnpkg.com/configuration/manifest#resolutions
7478
'yarn/berry'(pkgJson: PackageJsonContent) {
@@ -117,6 +121,11 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
117121
'm'
118122
).test(lockSrc)
119123
},
124+
vlt(lockSrc: string, name: string) {
125+
// Detects the package name in the following cases:
126+
// "name"
127+
return lockSrc.includes(`"${name}"`)
128+
},
120129
'yarn/berry': yarn,
121130
'yarn/classic': yarn
122131
}
@@ -127,36 +136,36 @@ type AgentModifyManifestFn = (
127136
overrides: Overrides
128137
) => void
129138

130-
const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = {
131-
bun(pkgJson: EditablePackageJson, overrides: Overrides) {
132-
pkgJson.update({
133-
[RESOLUTIONS_FIELD_NAME]: <PnpmOrYarnOverrides>overrides
134-
})
135-
},
136-
npm(pkgJson: EditablePackageJson, overrides: Overrides) {
139+
const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
140+
function updateOverrides(pkgJson: EditablePackageJson, overrides: Overrides) {
137141
pkgJson.update({
138142
[OVERRIDES_FIELD_NAME]: overrides
139143
})
140-
},
141-
pnpm(pkgJson: EditablePackageJson, overrides: Overrides) {
142-
pkgJson.update({
143-
pnpm: {
144-
...(<object>pkgJson.content['pnpm']),
145-
[OVERRIDES_FIELD_NAME]: overrides
146-
}
147-
})
148-
},
149-
'yarn/berry'(pkgJson: EditablePackageJson, overrides: Overrides) {
150-
pkgJson.update({
151-
[RESOLUTIONS_FIELD_NAME]: <PnpmOrYarnOverrides>overrides
152-
})
153-
},
154-
'yarn/classic'(pkgJson: EditablePackageJson, overrides: Overrides) {
144+
}
145+
function updateResolutions(
146+
pkgJson: EditablePackageJson,
147+
overrides: Overrides
148+
) {
155149
pkgJson.update({
156150
[RESOLUTIONS_FIELD_NAME]: <PnpmOrYarnOverrides>overrides
157151
})
158152
}
159-
}
153+
return {
154+
bun: updateResolutions,
155+
npm: updateOverrides,
156+
pnpm(pkgJson: EditablePackageJson, overrides: Overrides) {
157+
pkgJson.update({
158+
pnpm: {
159+
...(<object>pkgJson.content['pnpm']),
160+
[OVERRIDES_FIELD_NAME]: overrides
161+
}
162+
})
163+
},
164+
vlt: updateOverrides,
165+
'yarn/berry': updateResolutions,
166+
'yarn/classic': updateResolutions
167+
}
168+
})()
160169

161170
type AgentListDepsOptions = {
162171
npmExecPath?: string
@@ -251,6 +260,16 @@ const lsByAgent = (() => {
251260
}
252261
return stdout
253262
},
263+
async vlt(agentExecPath: string, cwd: string) {
264+
try {
265+
return (
266+
await spawn(agentExecPath!, ['ls', '--view', 'human', ':not(.dev)'], {
267+
cwd
268+
})
269+
).stdout
270+
} catch {}
271+
return ''
272+
},
254273
async 'yarn/berry'(agentExecPath: string, cwd: string) {
255274
try {
256275
return (
@@ -286,6 +305,7 @@ const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = {
286305
bun: (stdout: string, name: string) => stdout.includes(` ${name}@`),
287306
npm: (stdout: string, name: string) => stdout.includes(`/${name}\n`),
288307
pnpm: (stdout: string, name: string) => stdout.includes(`/${name}\n`),
308+
vlt: (stdout: string, name: string) => stdout.includes(` ${name}@`),
289309
'yarn/berry': (stdout: string, name: string) => stdout.includes(` ${name}@`),
290310
'yarn/classic': (stdout: string, name: string) => stdout.includes(` ${name}@`)
291311
}
@@ -663,6 +683,12 @@ export const optimize: CliSubcommand = {
663683
)
664684
return
665685
}
686+
if (agent === 'vlt') {
687+
console.log(
688+
`✘ ${COMMAND_TITLE}: ${agent} does not support overrides. Soon, though ⚡`
689+
)
690+
return
691+
}
666692
const lockName = lockPath ? path.basename(lockPath) : 'lock file'
667693
if (lockSrc === undefined) {
668694
console.log(`✘ ${COMMAND_TITLE}: No ${lockName} found`)

src/utils/package-manager-detector.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const AGENTS = [
1919
'npm',
2020
'pnpm',
2121
'yarn/berry',
22-
'yarn/classic'
22+
'yarn/classic',
23+
'vlt'
2324
] as const
2425
export type Agent = (typeof AGENTS)[number]
2526
export type StringKeyValueObject = { [key: string]: string }
@@ -90,15 +91,16 @@ const maintainedNodeVersions = (() => {
9091

9192
const LOCKS: Record<string, Agent> = {
9293
'bun.lockb': 'bun',
93-
'pnpm-lock.yaml': 'pnpm',
94-
'pnpm-lock.yml': 'pnpm',
95-
'yarn.lock': 'yarn/classic',
9694
// If both package-lock.json and npm-shrinkwrap.json are present in the root
9795
// of a project, npm-shrinkwrap.json will take precedence and package-lock.json
9896
// will be ignored.
9997
// https://docs.npmjs.com/cli/v10/configuring-npm/package-lock-json#package-lockjson-vs-npm-shrinkwrapjson
10098
'npm-shrinkwrap.json': 'npm',
10199
'package-lock.json': 'npm',
100+
'pnpm-lock.yaml': 'pnpm',
101+
'pnpm-lock.yml': 'pnpm',
102+
'yarn.lock': 'yarn/classic',
103+
'vlt-lock.json': 'vlt',
102104
// Look for a hidden lock file if .npmrc has package-lock=false:
103105
// https://docs.npmjs.com/cli/v10/configuring-npm/package-lock-json#hidden-lockfiles
104106
//
@@ -126,6 +128,9 @@ const readLockFileByAgent: Record<Agent, ReadLockFile> = (() => {
126128
return undefined
127129
}
128130
}
131+
const defaultReader = wrapReader(
132+
async (lockPath: string) => await readFileUtf8(lockPath)
133+
)
129134
return {
130135
bun: wrapReader(async (lockPath: string, agentExecPath: string) => {
131136
let lockBuffer: Buffer | undefined
@@ -137,18 +142,16 @@ const readLockFileByAgent: Record<Agent, ReadLockFile> = (() => {
137142
try {
138143
return <string>parseBunLockb(lockBuffer)
139144
} catch {}
140-
// To print a Yarn lockfile to your console without writing it to disk use `bun bun.lockb`.
145+
// To print a Yarn lockfile to your console without writing it to disk
146+
// use `bun bun.lockb`.
141147
// https://bun.sh/guides/install/yarnlock
142148
return (await spawn(agentExecPath, [lockPath])).stdout.trim()
143149
}),
144-
npm: wrapReader(async (lockPath: string) => await readFileUtf8(lockPath)),
145-
pnpm: wrapReader(async (lockPath: string) => await readFileUtf8(lockPath)),
146-
'yarn/berry': wrapReader(
147-
async (lockPath: string) => await readFileUtf8(lockPath)
148-
),
149-
'yarn/classic': wrapReader(
150-
async (lockPath: string) => await readFileUtf8(lockPath)
151-
)
150+
npm: defaultReader,
151+
pnpm: defaultReader,
152+
vlt: defaultReader,
153+
'yarn/berry': defaultReader,
154+
'yarn/classic': defaultReader
152155
}
153156
})()
154157

0 commit comments

Comments
 (0)