2021-09-29 19:58:42 +00:00
|
|
|
import os from 'os';
|
|
|
|
import is from '@sindresorhus/is';
|
2021-12-22 08:37:47 +00:00
|
|
|
import upath from 'upath';
|
2022-03-03 09:35:26 +00:00
|
|
|
import { logger } from '../../../logger';
|
2022-07-01 12:57:30 +00:00
|
|
|
import { readSystemFile } from '../../../util/fs';
|
2022-02-07 06:37:17 +00:00
|
|
|
import type { GlobalManagerConfig } from '../types';
|
2021-09-29 19:58:42 +00:00
|
|
|
|
|
|
|
export async function detectGlobalConfig(): Promise<GlobalManagerConfig> {
|
|
|
|
const res: GlobalManagerConfig = {};
|
|
|
|
const homedir = os.homedir();
|
2021-12-22 08:37:47 +00:00
|
|
|
const npmrcFileName = upath.join(homedir, '.npmrc');
|
2021-09-29 19:58:42 +00:00
|
|
|
try {
|
2022-07-01 12:57:30 +00:00
|
|
|
const npmrc = await readSystemFile(npmrcFileName, 'utf8');
|
2021-09-29 19:58:42 +00:00
|
|
|
if (is.nonEmptyString(npmrc)) {
|
|
|
|
res.npmrc = npmrc;
|
|
|
|
res.npmrcMerge = true;
|
|
|
|
logger.debug(`Detected ${npmrcFileName} and adding it to global config`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ npmrcFileName }, 'Error reading .npmrc file');
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|