lerna
Version:
Lerna is a fast, modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository
73 lines (70 loc) • 2.21 kB
JavaScript
import {
exec,
npmlog_default,
slash
} from "./chunk-WC2B4V4E.js";
// libs/commands/version/src/lib/git-add.ts
import { readJsonFile, workspaceRoot } from "@nx/devkit";
import fs from "fs";
import { createRequire } from "node:module";
import path from "path";
var require2 = createRequire(import.meta.url);
var resolvedPrettier;
function resolvePrettier() {
if (!resolvedPrettier) {
try {
const packageJson = readJsonFile(path.join(workspaceRoot, "package.json"));
const hasPrettier = packageJson.devDependencies?.prettier || packageJson.dependencies?.prettier;
if (!hasPrettier) {
return;
}
resolvedPrettier = require2("prettier");
} catch {
return;
}
}
return resolvedPrettier;
}
async function maybeFormatFile(filePath) {
const prettier = resolvePrettier();
if (!prettier) {
return;
}
const config = await resolvedPrettier.resolveConfig(filePath);
const ignorePath = path.join(workspaceRoot, ".prettierignore");
const fullFilePath = path.join(workspaceRoot, filePath);
const fileInfo = await resolvedPrettier.getFileInfo(fullFilePath, { ignorePath, resolveConfig: false });
if (fileInfo.ignored) {
npmlog_default.silly("version", `Skipped applying prettier to ignored file: ${filePath}`);
return;
}
try {
const input = fs.readFileSync(fullFilePath, "utf8");
fs.writeFileSync(
fullFilePath,
await resolvedPrettier.format(input, { ...config, filepath: fullFilePath }),
"utf8"
);
npmlog_default.silly("version", `Successfully applied prettier to updated file: ${filePath}`);
} catch {
npmlog_default.silly("version", `Failed to apply prettier to updated file: ${filePath}`);
}
}
async function gitAdd(changedFiles, gitOpts, execOpts) {
let files = [];
for (const file of changedFiles) {
const filePath = slash(path.relative(execOpts.cwd, path.resolve(execOpts.cwd, file)));
await maybeFormatFile(filePath);
if (gitOpts.granularPathspec) {
files.push(filePath);
}
}
if (!gitOpts.granularPathspec) {
files = ".";
}
npmlog_default.silly("gitAdd", files);
return exec("git", ["add", "--", ...files], execOpts);
}
export {
gitAdd
};