@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
50 lines • 2.07 kB
JavaScript
import { execFileSync } from 'child_process';
import { logger } from './logger.js';
const RELEASE_BRANCH_NAMES = ['master', 'main'];
const RELEASE_BRANCH_PATTERNS = [/^\d+\.\d+\.x$/];
const formatGitCommand = args => ['git', ...args].join(' ');
const createGitError = (args, cwd, error) => {
const workingDirectory = cwd ?? process.cwd();
return new Error(`${formatGitCommand(args)} failed in ${workingDirectory}. ${error.message}`);
};
const runGitCommand = (args, options = {}, dependencies = {}) => {
const {
cwd,
stdio = 'pipe'
} = options;
const {
execFileSyncImpl = execFileSync
} = dependencies;
try {
return execFileSyncImpl('git', args, {
cwd,
stdio
});
} catch (error) {
throw createGitError(args, cwd, error);
}
};
export const hasLocalChanges = (options = {}, dependencies = {}) => {
const changes = runGitCommand(['status', '--porcelain'], options, dependencies).toString();
return changes.length > 0;
};
export const diff = (v1, v2, options = {}, dependencies = {}) => {
const args = ['diff', v1, v2, '--word-diff=porcelain'];
if (options.path) {
args.push('--', options.path);
}
return runGitCommand(args, options, dependencies).toString();
};
export const getCurrentBranch = (options = {}, dependencies = {}) => runGitCommand(['branch', '--show-current'], options, dependencies).toString().trim();
export const isOnBranch = (...branches) => branches.includes(getCurrentBranch());
export const isReleaseBranchName = branchName => RELEASE_BRANCH_NAMES.includes(branchName) || RELEASE_BRANCH_PATTERNS.some(pattern => pattern.test(branchName));
export const isReleaseBranch = (options = {}, dependencies = {}) => isReleaseBranchName(getCurrentBranch(options, dependencies));
export const warnIfNotReleaseBranch = (dependencies = {}) => {
const {
isReleaseBranchImpl = isReleaseBranch,
loggerImpl = logger
} = dependencies;
if (!isReleaseBranchImpl()) {
loggerImpl.warning('You are not on a release branch (main, master or x.y.x). Be careful selecting the project!');
}
};