@better-builds/lets-version
Version:
A package that reads your conventional commits and git history and recommends (or applies) a SemVer version bump for you
32 lines (31 loc) • 1.3 kB
JavaScript
import appRootPath from 'app-root-path';
import fs from 'fs-extra';
import path from 'path';
import yaml from 'yaml';
import { fixCWD } from './cwd.js';
import { getPackageManager } from './getPackageManager.js';
/**
* Attempts to detect whether this repository is a multi-package monorepo.
*/
export async function detectIfMonorepo(cwd = appRootPath.toString()) {
const fixedCWD = fixCWD(cwd);
const pm = await getPackageManager(fixedCWD);
const rootPjsonPath = path.join(fixedCWD, 'package.json');
const pjson = JSON.parse(await fs.readFile(rootPjsonPath, 'utf8'));
// short circuit if the package.json tells us it's a monorepo
if (Array.isArray(pjson.workspaces) && pjson.workspaces.length)
return true;
if (pm === 'pnpm') {
const pnpmWorkspaceFilePath = path.join(fixedCWD, 'pnpm-workspace.yaml');
try {
const workspaceFileContents = await fs.readFile(pnpmWorkspaceFilePath, 'utf-8');
const parsedYaml = yaml.parse(workspaceFileContents) ?? {};
return Boolean(parsedYaml.packages?.length);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (err) {
/* file doesn't exist or the input is malformed */
return false;
}
}
}