Skip to content

Commit

Permalink
refactor(npm): Simplify .yarnrc parsing (#24931)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Oct 1, 2023
1 parent 2611b88 commit 906c982
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions lib/modules/manager/npm/extract/yarnrc.ts
@@ -1,19 +1,22 @@
import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import { z } from 'zod';
import { logger } from '../../../../logger';
import { regEx } from '../../../../util/regex';
import { Result } from '../../../../util/result';
import { Yaml } from '../../../../util/schema-utils';

const YarnrcYmlSchema = z.object({
npmRegistryServer: z.string().optional(),
npmScopes: z
.record(
z.object({
npmRegistryServer: z.string().optional(),
})
)
.optional(),
});
const YarnrcYmlSchema = Yaml.pipe(
z.object({
npmRegistryServer: z.string().optional(),
npmScopes: z
.record(
z.object({
npmRegistryServer: z.string().optional(),
})
)
.optional(),
})
);

export type YarnConfig = z.infer<typeof YarnrcYmlSchema>;

Expand Down Expand Up @@ -43,19 +46,11 @@ export function loadConfigFromLegacyYarnrc(
}

export function loadConfigFromYarnrcYml(yarnrcYml: string): YarnConfig | null {
try {
const obj = load(yarnrcYml, {
json: true,
});
if (!obj) {
// emtpy yaml file
return null;
}
return YarnrcYmlSchema.parse(obj);
} catch (err) {
logger.warn({ yarnrcYml, err }, `Failed to load yarnrc file`);
return null;
}
return Result.parse(yarnrcYml, YarnrcYmlSchema)
.onError((err) => {
logger.warn({ yarnrcYml, err }, `Failed to load yarnrc file`);
})
.unwrapOrNull();
}

export function resolveRegistryUrl(
Expand Down

0 comments on commit 906c982

Please sign in to comment.