-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manager/npm): read registry URLs from .yarnrc.yml (#19864)
Co-authored-by: Valentin Agachi <github-com@agachi.name> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Closes #16353
- Loading branch information
1 parent
458dfe0
commit 2143c97
Showing
6 changed files
with
183 additions
and
5 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
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
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,100 @@ | ||
import { codeBlock } from 'common-tags'; | ||
import { loadConfigFromYarnrcYml, resolveRegistryUrl } from './yarnrc'; | ||
|
||
describe('modules/manager/npm/extract/yarnrc', () => { | ||
describe('resolveRegistryUrl()', () => { | ||
it('considers default registry', () => { | ||
const registryUrl = resolveRegistryUrl('a-package', { | ||
npmRegistryServer: 'https://private.example.com/npm', | ||
}); | ||
expect(registryUrl).toBe('https://private.example.com/npm'); | ||
}); | ||
|
||
it('chooses matching scoped registry over default registry', () => { | ||
const registryUrl = resolveRegistryUrl('@scope/a-package', { | ||
npmRegistryServer: 'https://private.example.com/npm', | ||
npmScopes: { | ||
scope: { | ||
npmRegistryServer: 'https://scope.example.com/npm', | ||
}, | ||
}, | ||
}); | ||
expect(registryUrl).toBe('https://scope.example.com/npm'); | ||
}); | ||
|
||
it('ignores non matching scoped registry', () => { | ||
const registryUrl = resolveRegistryUrl('@scope/a-package', { | ||
npmScopes: { | ||
'other-scope': { | ||
npmRegistryServer: 'https://other-scope.example.com/npm', | ||
}, | ||
}, | ||
}); | ||
expect(registryUrl).toBeNull(); | ||
}); | ||
|
||
it('ignores partial scope match', () => { | ||
const registryUrl = resolveRegistryUrl('@scope-2/a-package', { | ||
npmScopes: { | ||
scope: { | ||
npmRegistryServer: 'https://scope.example.com/npm', | ||
}, | ||
}, | ||
}); | ||
expect(registryUrl).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('loadConfigFromYarnrcYml()', () => { | ||
it.each([ | ||
[ | ||
'npmRegistryServer: https://npm.example.com', | ||
{ npmRegistryServer: 'https://npm.example.com' }, | ||
], | ||
[ | ||
codeBlock` | ||
npmRegistryServer: https://npm.example.com | ||
npmScopes: | ||
foo: | ||
npmRegistryServer: https://npm-foo.example.com | ||
`, | ||
{ | ||
npmRegistryServer: 'https://npm.example.com', | ||
npmScopes: { | ||
foo: { | ||
npmRegistryServer: 'https://npm-foo.example.com', | ||
}, | ||
}, | ||
}, | ||
], | ||
[ | ||
codeBlock` | ||
npmRegistryServer: https://npm.example.com | ||
nodeLinker: pnp | ||
`, | ||
{ npmRegistryServer: 'https://npm.example.com' }, | ||
], | ||
['npmRegistryServer: 42', null], | ||
['npmScopes: 42', null], | ||
[ | ||
codeBlock` | ||
npmScopes: | ||
foo: 42 | ||
`, | ||
null, | ||
], | ||
[ | ||
codeBlock` | ||
npmScopes: | ||
foo: | ||
npmRegistryServer: 42 | ||
`, | ||
null, | ||
], | ||
])('produces expected config (%s)', (yarnrcYml, expectedConfig) => { | ||
const config = loadConfigFromYarnrcYml(yarnrcYml); | ||
|
||
expect(config).toEqual(expectedConfig); | ||
}); | ||
}); | ||
}); |
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,46 @@ | ||
import { load } from 'js-yaml'; | ||
import { z } from 'zod'; | ||
import { logger } from '../../../../logger'; | ||
|
||
const YarnrcYmlSchema = z.object({ | ||
npmRegistryServer: z.string().optional(), | ||
npmScopes: z | ||
.record( | ||
z.object({ | ||
npmRegistryServer: z.string().optional(), | ||
}) | ||
) | ||
.optional(), | ||
}); | ||
|
||
export type YarnConfig = z.infer<typeof YarnrcYmlSchema>; | ||
|
||
export function loadConfigFromYarnrcYml(yarnrcYml: string): YarnConfig | null { | ||
try { | ||
return YarnrcYmlSchema.parse( | ||
load(yarnrcYml, { | ||
json: true, | ||
}) | ||
); | ||
} catch (err) { | ||
logger.warn({ yarnrcYml, err }, `Failed to load yarnrc file`); | ||
return null; | ||
} | ||
} | ||
|
||
export function resolveRegistryUrl( | ||
packageName: string, | ||
yarnConfig: YarnConfig | ||
): string | null { | ||
if (yarnConfig.npmScopes) { | ||
for (const scope in yarnConfig.npmScopes) { | ||
if (packageName.startsWith(`@${scope}/`)) { | ||
return yarnConfig.npmScopes[scope].npmRegistryServer ?? null; | ||
} | ||
} | ||
} | ||
if (yarnConfig.npmRegistryServer) { | ||
return yarnConfig.npmRegistryServer; | ||
} | ||
return null; | ||
} |