2020-05-09 05:55:00 +00:00
|
|
|
import { logger } from '../../../logger';
|
2022-03-14 08:13:21 +00:00
|
|
|
import {
|
|
|
|
resolvePackageUrl,
|
|
|
|
resolveRegistryUrl,
|
|
|
|
} from '../../../modules/datasource/npm/npmrc';
|
2022-04-24 22:48:54 +00:00
|
|
|
import type {
|
|
|
|
NpmResponse,
|
|
|
|
NpmResponseVersion,
|
|
|
|
} from '../../../modules/datasource/npm/types';
|
2021-03-20 08:27:33 +00:00
|
|
|
import { Http } from '../../../util/http';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { Preset, PresetConfig } from '../types';
|
2021-04-19 13:31:38 +00:00
|
|
|
import {
|
|
|
|
PRESET_DEP_NOT_FOUND,
|
|
|
|
PRESET_NOT_FOUND,
|
|
|
|
PRESET_RENOVATE_CONFIG_NOT_FOUND,
|
|
|
|
} from '../util';
|
2019-07-15 09:04:05 +00:00
|
|
|
|
2021-03-20 08:27:33 +00:00
|
|
|
const id = 'npm';
|
|
|
|
|
|
|
|
const http = new Http(id);
|
|
|
|
|
2020-05-09 07:04:39 +00:00
|
|
|
export async function getPreset({
|
2022-02-28 16:39:44 +00:00
|
|
|
repo: pkg,
|
2020-05-09 07:04:39 +00:00
|
|
|
presetName = 'default',
|
2022-04-24 22:48:54 +00:00
|
|
|
}: PresetConfig): Promise<Preset | undefined> {
|
|
|
|
let dep: (NpmResponseVersion & { 'renovate-config'?: any }) | undefined;
|
2021-03-20 08:27:33 +00:00
|
|
|
try {
|
2022-03-14 08:13:21 +00:00
|
|
|
const registryUrl = resolveRegistryUrl(pkg);
|
2023-04-21 06:04:08 +00:00
|
|
|
logger.once.warn(
|
|
|
|
{ registryUrl, pkg },
|
2023-11-07 15:50:29 +00:00
|
|
|
'Using npm packages for Renovate presets is now deprecated. Please migrate to repository-based presets instead.',
|
2023-04-21 06:04:08 +00:00
|
|
|
);
|
2022-03-14 08:13:21 +00:00
|
|
|
const packageUrl = resolvePackageUrl(registryUrl, pkg);
|
2022-02-26 14:59:05 +00:00
|
|
|
const body = (await http.getJson<NpmResponse>(packageUrl)).body;
|
2023-08-15 09:31:15 +00:00
|
|
|
// TODO: check null #22198
|
2022-04-24 22:48:54 +00:00
|
|
|
dep = body.versions![body['dist-tags']!.latest];
|
2024-08-14 10:33:02 +00:00
|
|
|
} catch {
|
2021-04-19 13:31:38 +00:00
|
|
|
throw new Error(PRESET_DEP_NOT_FOUND);
|
2019-01-05 07:00:07 +00:00
|
|
|
}
|
2022-03-28 08:55:14 +00:00
|
|
|
if (!dep?.['renovate-config']) {
|
2021-04-19 13:31:38 +00:00
|
|
|
throw new Error(PRESET_RENOVATE_CONFIG_NOT_FOUND);
|
2019-01-05 07:00:07 +00:00
|
|
|
}
|
|
|
|
const presetConfig = dep['renovate-config'][presetName];
|
|
|
|
if (!presetConfig) {
|
2019-07-11 07:50:08 +00:00
|
|
|
const presetNames = Object.keys(dep['renovate-config']);
|
2020-02-24 07:43:01 +00:00
|
|
|
logger.debug(
|
2019-07-11 07:50:08 +00:00
|
|
|
{ presetNames, presetName },
|
2023-11-07 15:50:29 +00:00
|
|
|
'Preset not found within renovate-config',
|
2019-07-11 07:50:08 +00:00
|
|
|
);
|
2021-04-19 13:31:38 +00:00
|
|
|
throw new Error(PRESET_NOT_FOUND);
|
2019-01-05 07:00:07 +00:00
|
|
|
}
|
|
|
|
return presetConfig;
|
|
|
|
}
|