Files
renovate/lib/modules/manager/npm/utils.ts
Sebastian Poxhofer 73b90e958b refactor: remove string concetation for production code (#43442)
refactor: prefer templates rather than `+`
2026-05-19 13:31:27 +00:00

96 lines
2.8 KiB
TypeScript

import detectIndent from 'detect-indent';
import upath from 'upath';
import { logger } from '../../../logger/index.ts';
import {
deleteLocalFile,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import { PackageJson } from './schema.ts';
import type { LockFile, ParseLockFileResult } from './types.ts';
export function parseLockFile(lockFile: string): ParseLockFileResult {
const detectedIndent = detectIndent(lockFile).indent || ' ';
let lockFileParsed: LockFile | undefined;
try {
lockFileParsed = JSON.parse(lockFile);
} catch (err) {
logger.warn({ err }, 'Error parsing npm lock file');
}
return { detectedIndent, lockFileParsed };
}
export function composeLockFile(lockFile: LockFile, indent: string): string {
return `${JSON.stringify(lockFile, null, indent)}\n`;
}
export async function getNpmrcContent(dir: string): Promise<string | null> {
const npmrcFilePath = upath.join(dir, '.npmrc');
let originalNpmrcContent: string | null = null;
try {
originalNpmrcContent = await readLocalFile(npmrcFilePath, 'utf8');
} catch {
/* v8 ignore next -- needs test */
originalNpmrcContent = null;
}
if (originalNpmrcContent) {
logger.debug(`npmrc file ${npmrcFilePath} found in repository`);
}
return originalNpmrcContent;
}
export async function updateNpmrcContent(
dir: string,
originalContent: string | null,
additionalLines: string[],
): Promise<void> {
const npmrcFilePath = upath.join(dir, '.npmrc');
const newNpmrc = originalContent
? [originalContent, ...additionalLines]
: additionalLines;
try {
const newContent = newNpmrc.length ? newNpmrc.join('\n') : null;
if (newContent !== originalContent) {
logger.debug(`Writing updated .npmrc file to ${npmrcFilePath}`);
await writeLocalFile(npmrcFilePath, `${newContent}\n`);
}
} catch /* v8 ignore next -- TODO: add test #40625 */ {
logger.warn('Unable to write custom npmrc file');
}
}
export async function resetNpmrcContent(
dir: string,
originalContent: string | null,
): Promise<void> {
const npmrcFilePath = upath.join(dir, '.npmrc');
if (originalContent) {
try {
await writeLocalFile(npmrcFilePath, originalContent);
} catch /* v8 ignore next -- TODO: add test #40625 */ {
logger.warn('Unable to reset npmrc to original contents');
}
} else {
try {
await deleteLocalFile(npmrcFilePath);
} catch /* v8 ignore next -- TODO: add test #40625 */ {
logger.warn('Unable to delete custom npmrc');
}
}
}
export async function loadPackageJson(parentDir: string): Promise<PackageJson> {
const json = await readLocalFile(
upath.join(parentDir, 'package.json'),
'utf8',
);
const res = PackageJson.safeParse(json);
if (res.success) {
return res.data;
}
return {};
}