本文整理汇总了TypeScript中@yarnpkg/lockfile.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: readYarn
/**
* Parse the yarn file at the given path.
*/
function readYarn(basePath: string = '.') {
let file = fs.readFileSync(path.join(basePath, 'yarn.lock'), 'utf8');
let json = lockfile.parse(file);
if (json.type !== 'success') {
throw new Error('Error reading file');
}
return json.object;
}
示例2: readOptions
function readOptions(
logger: logging.LoggerApi,
yarn = false,
showPotentials = false,
): Record<string, string> {
const cwd = process.cwd();
const baseFilename = yarn ? 'yarnrc' : 'npmrc';
const dotFilename = '.' + baseFilename;
let globalPrefix: string;
if (process.env.PREFIX) {
globalPrefix = process.env.PREFIX;
} else {
globalPrefix = path.dirname(process.execPath);
if (process.platform !== 'win32') {
globalPrefix = path.dirname(globalPrefix);
}
}
const defaultConfigLocations = [
path.join(globalPrefix, 'etc', baseFilename),
path.join(homedir(), dotFilename),
];
const projectConfigLocations: string[] = [
path.join(cwd, dotFilename),
];
const root = path.parse(cwd).root;
for (let curDir = path.dirname(cwd); curDir && curDir !== root; curDir = path.dirname(curDir)) {
projectConfigLocations.unshift(path.join(curDir, dotFilename));
}
if (showPotentials) {
logger.info(`Locating potential ${baseFilename} files:`);
}
let options: { [key: string]: string } = {};
for (const location of [...defaultConfigLocations, ...projectConfigLocations]) {
if (existsSync(location)) {
if (showPotentials) {
logger.info(`Trying '${location}'...found.`);
}
const data = readFileSync(location, 'utf8');
options = {
...options,
...(yarn ? lockfile.parse(data) : ini.parse(data)),
};
if (options.cafile) {
const cafile = path.resolve(path.dirname(location), options.cafile);
delete options.cafile;
try {
options.ca = readFileSync(cafile, 'utf8').replace(/\r?\n/, '\\n');
} catch { }
}
} else if (showPotentials) {
logger.info(`Trying '${location}'...not found.`);
}
}
// Substitute any environment variable references
for (const key in options) {
if (typeof options[key] === 'string') {
options[key] = options[key].replace(/\$\{([^\}]+)\}/, (_, name) => process.env[name] || '');
}
}
return options;
}