Skip to content

Commit 0339d49

Browse files
yyz945947732antfu
andauthored
fix: url version should be exclude from upgrading (#177)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 5c0ceca commit 0339d49

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

src/io/dependencies.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ export function setByPath(obj: any, path: string, value: any) {
2727
target[lastKey] = value
2828
}
2929

30-
export function parseDependencies(pkg: any, type: DepType, shouldUpdate: (name: string) => boolean): RawDep[] {
31-
return Object.entries(getByPath(pkg, type) || {}).map(([name, { version, parents }]) => parseDependency(name, version, type, shouldUpdate, parents))
30+
export function parseDependencies(
31+
pkg: any,
32+
type: DepType,
33+
shouldUpdate: (name: string) => boolean,
34+
): RawDep[] {
35+
return Object.entries(getByPath(pkg, type) || {})
36+
.map(([name, { version, parents }]) => parseDependency(name, version, type, shouldUpdate, parents))
3237
}
3338

3439
export function parseDependency(name: string, version: string, type: DepType, shouldUpdate: (name: string) => boolean, parents?: string[]): RawDep {

src/io/resolves.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export async function resolveDependency(
181181
? optionMode
182182
: optionMode === 'default' ? configMode : 'ignore'
183183
: optionMode
184-
if (isLocalPackage(raw.currentVersion) || !raw.update || !await Promise.resolve(filter(raw)) || mergeMode === 'ignore') {
184+
if (isLocalPackage(raw.currentVersion) || isUrlPackage(raw.currentVersion) || !raw.update || !await Promise.resolve(filter(raw)) || mergeMode === 'ignore') {
185185
return {
186186
...raw,
187187
diff: null,
@@ -292,17 +292,15 @@ export async function resolvePackage(pkg: PackageMeta, options: CheckOptions, fi
292292
return pkg
293293
}
294294

295-
function isLocalPackage(currentVersion: string) {
296-
const localPackagePrefix = [
297-
'link:',
298-
'file:',
299-
'workspace:',
300-
'catalog:',
301-
]
302-
return localPackagePrefix.some(prefix => currentVersion.startsWith(prefix))
295+
export function isUrlPackage(currentVersion: string) {
296+
return /^(?:https?:|git\+|github:)/.test(currentVersion)
303297
}
304298

305-
function isAliasedPackage(currentVersion: string) {
299+
export function isLocalPackage(currentVersion: string) {
300+
return /^(?:link|file|workspace|catalog):/.test(currentVersion)
301+
}
302+
303+
export function isAliasedPackage(currentVersion: string) {
306304
return currentVersion.startsWith('npm:')
307305
}
308306

test/parseDependencies.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { parseDependencies } from '../src/io/dependencies'
3+
import { isLocalPackage, isUrlPackage } from '../src/io/resolves'
34

45
describe('parseDependencies', () => {
56
it('parse package `dependencies`', () => {
@@ -62,6 +63,90 @@ describe('parseDependencies', () => {
6263
`)
6364
})
6465

66+
it('parse package `dependencies` should exclude URL version', () => {
67+
const myPackage = {
68+
name: '@taze/package1',
69+
private: true,
70+
dependencies: {
71+
'@taze/not-exists': '^4.13.19',
72+
'@typescript/lib-dom': 'npm:@types/web@^0.0.80',
73+
'my-lib1': 'https://example.com/packages/my-lib1-1.0.0.tgz',
74+
'my-lib2': 'git+https://github.com/user/my-lib2.git',
75+
'my-lib3': 'github:user/my-lib3#v1.2.3',
76+
'my-lib4': 'file:../my-lib4',
77+
},
78+
}
79+
const result = parseDependencies(myPackage, 'dependencies', () => true)
80+
expect(result).toMatchInlineSnapshot(`
81+
[
82+
{
83+
"currentVersion": "^4.13.19",
84+
"name": "@taze/not-exists",
85+
"parents": [],
86+
"source": "dependencies",
87+
"update": true,
88+
},
89+
{
90+
"currentVersion": "npm:@types/web@^0.0.80",
91+
"name": "@typescript/lib-dom",
92+
"parents": [],
93+
"source": "dependencies",
94+
"update": true,
95+
},
96+
{
97+
"currentVersion": "https://example.com/packages/my-lib1-1.0.0.tgz",
98+
"name": "my-lib1",
99+
"parents": [],
100+
"source": "dependencies",
101+
"update": true,
102+
},
103+
{
104+
"currentVersion": "git+https://github.com/user/my-lib2.git",
105+
"name": "my-lib2",
106+
"parents": [],
107+
"source": "dependencies",
108+
"update": true,
109+
},
110+
{
111+
"currentVersion": "github:user/my-lib3#v1.2.3",
112+
"name": "my-lib3",
113+
"parents": [],
114+
"source": "dependencies",
115+
"update": true,
116+
},
117+
{
118+
"currentVersion": "file:../my-lib4",
119+
"name": "my-lib4",
120+
"parents": [],
121+
"source": "dependencies",
122+
"update": true,
123+
},
124+
]
125+
`)
126+
127+
expect(
128+
result
129+
.filter(i => !isUrlPackage(i.currentVersion) && !isLocalPackage(i.currentVersion)),
130+
).toMatchInlineSnapshot(`
131+
[
132+
{
133+
"currentVersion": "^4.13.19",
134+
"name": "@taze/not-exists",
135+
"parents": [],
136+
"source": "dependencies",
137+
"update": true,
138+
},
139+
{
140+
"currentVersion": "npm:@types/web@^0.0.80",
141+
"name": "@typescript/lib-dom",
142+
"parents": [],
143+
"source": "dependencies",
144+
"update": true,
145+
},
146+
]
147+
`)
148+
})
149+
65150
it('parse package `pnpm.overrides`', () => {
66151
const myPackage = {
67152
name: '@taze/package1',

0 commit comments

Comments
 (0)