nx
Version:
61 lines (60 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPnpmConfigDir = getPnpmConfigDir;
exports.readPnpmYamlConfig = readPnpmYamlConfig;
const os_1 = require("os");
const path_1 = require("path");
const fileutils_1 = require("../fileutils");
/**
* Mirrors pnpm's getConfigDir. Hosts pnpm's global config.yaml and (v11+)
* auth.ini.
* See https://github.com/pnpm/pnpm/blob/b7195db5c8469c80908d625c648302b26c2f9977/config/reader/src/dirs.ts#L73-L92
*/
function getPnpmConfigDir(env) {
if (env.XDG_CONFIG_HOME) {
return (0, path_1.join)(env.XDG_CONFIG_HOME, 'pnpm');
}
if (process.platform === 'darwin') {
return (0, path_1.join)((0, os_1.homedir)(), 'Library/Preferences/pnpm');
}
if (process.platform !== 'win32') {
return (0, path_1.join)((0, os_1.homedir)(), '.config/pnpm');
}
if (env.LOCALAPPDATA) {
return (0, path_1.join)(env.LOCALAPPDATA, 'pnpm/config');
}
return (0, path_1.join)((0, os_1.homedir)(), '.config/pnpm');
}
/**
* Reads a pnpm YAML config file (pnpm-workspace.yaml or the global
* config.yaml). An absent file returns null so callers can fall through to
* lower surfaces; everything else returns 'unusable', which every caller turns
* into a throw. pnpm's own reader tolerates ENOENT alone and rethrows the rest,
* and it dies the same way on a document it cannot parse or that is not a
* mapping, so the two failures need no separate states. Requiring an object
* also keeps the sentinel out of the success domain: a returned string can only
* ever mean unusable.
*/
function readPnpmYamlConfig(path) {
let doc;
try {
doc = (0, fileutils_1.readYamlFile)(path);
}
catch (e) {
// Only ENOENT resolves on, so a path through a non-directory or a file that
// will not open is fatal here. Callers whose pnpm counterpart looks the file
// up before reading it widen "absent" themselves.
if (e?.code === 'ENOENT') {
return null;
}
return 'unusable';
}
// An empty file declares nothing; pnpm accepts it.
if (doc === null || doc === undefined) {
return {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
return 'unusable';
}
return doc;
}