Skip to content

Commit d1139dd

Browse files
committed
fix: add validation check for JIT plugin versions
Closes #1286
1 parent fe31ecb commit d1139dd

2 files changed

Lines changed: 79 additions & 16 deletions

File tree

src/plugins.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ function notifyUser(plugin: Config, output: Output): void {
8181
}
8282
}
8383

84+
export function normaliseTag(jitPlugins: Record<string, string>, modifiedPlugins: Plugin[], p: Interfaces.UserPlugin) {
85+
// a not valid tag indicates that it's a dist-tag like 'latest'
86+
if (!valid(p.tag)) return `${p.name}@${p.tag}`
87+
88+
if (p.tag && valid(p.tag) && jitPlugins[p.name] && valid(jitPlugins[p.name]) && gt(p.tag, jitPlugins[p.name])) {
89+
// The user has installed a version of the JIT plugin that is newer than the one
90+
// specified by the root plugin's JIT configuration. In this case, we want to
91+
// keep the version installed by the user.
92+
return `${p.name}@${p.tag}`
93+
}
94+
95+
const tag = jitPlugins[p.name] ?? p.tag
96+
modifiedPlugins.push({...p, tag})
97+
return `${p.name}@${tag}`
98+
}
99+
84100
export default class Plugins {
85101
public config: Interfaces.Config
86102
public readonly npm: NPM
@@ -379,21 +395,7 @@ export default class Plugins {
379395
const modifiedPlugins: Plugin[] = []
380396
if (npmPlugins.length > 0) {
381397
await this.npm.install(
382-
npmPlugins.map((p) => {
383-
// a not valid tag indicates that it's a dist-tag like 'latest'
384-
if (!valid(p.tag)) return `${p.name}@${p.tag}`
385-
386-
if (p.tag && valid(p.tag) && jitPlugins[p.name] && gt(p.tag, jitPlugins[p.name])) {
387-
// The user has installed a version of the JIT plugin that is newer than the one
388-
// specified by the root plugin's JIT configuration. In this case, we want to
389-
// keep the version installed by the user.
390-
return `${p.name}@${p.tag}`
391-
}
392-
393-
const tag = jitPlugins[p.name] ?? p.tag
394-
modifiedPlugins.push({...p, tag})
395-
return `${p.name}@${tag}`
396-
}),
398+
npmPlugins.map((p) => normaliseTag(jitPlugins, modifiedPlugins, p)),
397399
{cwd: this.config.dataDir, logLevel: this.logLevel, prod: true},
398400
)
399401
}

test/plugins.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {expect} from 'chai'
33
import {join} from 'node:path'
44
import {createSandbox, SinonSandbox, SinonSpy} from 'sinon'
55

6-
import Plugins from '../src/plugins.js'
6+
import Plugins, {normaliseTag} from '../src/plugins.js'
77

88
describe('Plugins', () => {
99
let sandbox: SinonSandbox
@@ -189,4 +189,65 @@ describe('Plugins', () => {
189189
expect(saveStub.calledWithExactly(userPJSON)).to.be.true
190190
})
191191
})
192+
193+
describe('normaliseTag', () => {
194+
it('should keep invalid semver tag', async () => {
195+
const cases: {
196+
expected: string
197+
expectedModifiedPluginsLength?: number
198+
jitPlugins: Record<string, string>
199+
modifiedPlugins: (Interfaces.LinkedPlugin | Interfaces.UserPlugin)[]
200+
tag: string
201+
}[] = [
202+
{
203+
expected: 'latest',
204+
jitPlugins: {},
205+
modifiedPlugins: [],
206+
tag: 'latest',
207+
},
208+
{
209+
expected: 'latest',
210+
jitPlugins: {'@oclif/plugin-user': '*'},
211+
modifiedPlugins: [],
212+
tag: 'latest',
213+
},
214+
{
215+
expected: '0.0.2',
216+
jitPlugins: {'@oclif/plugin-user': '0.0.1'},
217+
modifiedPlugins: [],
218+
tag: '0.0.2',
219+
},
220+
{
221+
expected: '0.0.2',
222+
expectedModifiedPluginsLength: 1,
223+
jitPlugins: {'@oclif/plugin-user': '0.0.2'},
224+
modifiedPlugins: [],
225+
tag: '0.0.1',
226+
},
227+
{
228+
expected: '0.0.1',
229+
expectedModifiedPluginsLength: 1,
230+
jitPlugins: {'@oclif/plugin-user': '0.0.1'},
231+
modifiedPlugins: [],
232+
tag: '0.0.1',
233+
},
234+
{
235+
expected: 'latest',
236+
expectedModifiedPluginsLength: 1,
237+
jitPlugins: {'@oclif/plugin-user': 'latest'},
238+
modifiedPlugins: [],
239+
tag: '0.0.1',
240+
},
241+
]
242+
for (const c of cases) {
243+
const actual = normaliseTag(c.jitPlugins, c.modifiedPlugins, {
244+
name: '@oclif/plugin-user',
245+
tag: c.tag,
246+
type: 'user',
247+
})
248+
expect(actual).to.equal(`@oclif/plugin-user@${c.expected}`)
249+
expect(c.modifiedPlugins).to.lengthOf(c.expectedModifiedPluginsLength || 0)
250+
}
251+
})
252+
})
192253
})

0 commit comments

Comments
 (0)