@smartimpact-it/json-merge-shopify
Version:
185 lines (184 loc) • 6.26 kB
TypeScript
import { DefaultLogFields, ListLogLine, SimpleGit } from 'simple-git';
export type ThemeCheckResultOffense = {
check: string;
severity: number;
start_row: number;
start_column: number;
end_row: number;
end_column: number;
message: string;
};
export type ThemeCheckResult = {
path: string;
offenses: ThemeCheckResultOffense[];
errorCount: number;
suggestionCount: number;
styleCount: number;
};
export type AncestorIdentifier = (file: string, instance: GitMerger, { existsInMain, existsInLiveMirror, }: {
existsInMain: boolean;
existsInLiveMirror: boolean;
}) => Promise<string | null>;
export type Logger = (message: string | Error, type: 'log' | 'warn' | 'error' | 'success') => unknown;
export interface GitMergerOptions {
jsonPaths?: string[];
gitRoot?: string | null;
createCommit?: boolean;
mainBranch?: string;
liveMirrorBranch?: string;
productionBranch?: string | null;
runLocallyOnly?: boolean;
exitIfNoExistingDeployment?: boolean;
preferred?: 'ours' | 'theirs' | null;
checkJsonValidity?: boolean;
failIfThemeCheckMissing?: boolean;
commitMessage?: string;
createNewFiles?: boolean;
ancestorIdentifier?: AncestorIdentifier | null;
formatter?: ((json: string, path: string) => Promise<string>) | null;
logger?: Logger | null;
verbose?: boolean;
}
export interface GitMergerResult {
mergedFiles?: string[];
hasConflict?: boolean;
hasErrors?: boolean;
hasCommitted?: boolean;
error?: any;
}
export declare const defaultGitMergerOptions: Required<GitMergerOptions>;
export declare const possibleConfigFiles: string[];
export declare class GitMerger {
chalkInstance: any | null;
gitRoot: string;
jsonPaths: string[];
git: SimpleGit;
createCommit: boolean;
mainBranch: string;
productionBranch: string | null;
liveMirrorBranch: string;
checkJsonValidity: boolean;
failIfThemeCheckMissing: boolean;
formatter: (json: string, path: string) => Promise<string>;
commitMessage: string;
preferred: 'ours' | 'theirs';
exitIfNoExistingDeployment: boolean;
runLocallyOnly: boolean;
ancestorIdentifier: AncestorIdentifier | null;
logger: Logger | null;
verbose: boolean;
createNewFiles: boolean;
/**
* Create a new GitMerger instance.
*
* @param options - The options for the merger. If it's a string, it's the path to a config file (JSON or JS)
*/
constructor(options: GitMergerOptions | string | null, extraOptions?: Partial<GitMergerOptions>);
getDefaultOptions(): Required<GitMergerOptions>;
/**
* Get the options from a config file.
*/
getOptionsFromConfigFile(filename?: string | null, extraOptions?: Partial<GitMergerOptions>): GitMergerOptions | null;
run(): Promise<GitMergerResult>;
/**
* Check what is the current branch.
*/
checkCurrentBranch(): Promise<string>;
/**
* Pull the latest changes from the remote "live-mirror" branch
*/
pullLiveMirrorBranch(): Promise<void>;
/**
* Use the `origin/` prefix if we are not running strictly locally.
*/
private maybeGetOriginPrefix;
/**
* Get all jsons matching the glob patterns, both from the "main" branch and from the "live-mirror" branch, except the ones that are .gitignored.
*/
getAllJsons(): Promise<{
valid: string[];
ignored: string[];
}>;
/**
* Remove the git root prefix from the file path.
*
* @param file
* @returns
*/
private removeGitRootPrefix;
/**
* Get the file content from a specific commit.
*/
getFileContentFromCommit(file: string, commitOrBranch: string, displayWarning?: boolean): Promise<{
exists: boolean;
content: any | null;
}>;
/**
* Check if a file exists in a specific commit.
*/
fileExistsInCommit(file: string, commitOrBranch: string): Promise<boolean>;
/**
* Save and commit a JSON file.
* @param file
* @param content
*/
saveAndCommitJsonFile(file: string, content: any): Promise<void>;
/**
* Merge the JSON files
* Take the last file content from main and live-mirror branches
*/
mergeJsonFiles(allJsons: string[]): Promise<{
hasConflict: boolean;
mergedFiles: string[];
}>;
/**
* Merge a JSON file
* @param file
*/
mergeJsonFile(file: string): Promise<{
hasConflict: boolean;
isMerged: boolean;
}>;
/**
* Get the commit hash to use as a base for the merge.
* @param file
*/
getAncestorCommit(file: string, { existsInMain, existsInLiveMirror, }?: {
existsInMain?: boolean | null;
existsInLiveMirror?: boolean | null;
}): Promise<string | null>;
/**
* Get the last merge commit for a file.
*/
getLastMergeCommit(file: string): Promise<(DefaultLogFields & ListLogLine) | null>;
/**
* Get the last production deploy commit for a file.
*/
getLastProductionDeployCommit(file: string): Promise<(DefaultLogFields & ListLogLine) | null>;
/**
* Get the most recent common commit between "main" and "live-mirror" branches.
*/
getMostRecentCommonCommit(file: string): Promise<(DefaultLogFields & ListLogLine) | null>;
/**
* Get the commit to use as a base for the merge.
*/
getDeployCommit(file: string): Promise<(DefaultLogFields & ListLogLine) | null>;
sortCommitsByDate(...commits: Array<(DefaultLogFields & ListLogLine) | null>): (DefaultLogFields & ListLogLine) | null;
/**
* Check the JSON validity using theme check. Merges can sometimes create invalid JSON files.
*/
validateJson(paths: string[]): Promise<boolean>;
/**
* Create a commit with the changes
*/
maybeCreateCommit(mergedFiles?: string[]): Promise<boolean>;
/**
* Remove the local "live-mirror" branch
*/
removeLiveMirrorBranch(): Promise<void>;
logInfo(message: string): Promise<void>;
logWarning(message: string): Promise<void>;
logError(message: string | Error): Promise<void>;
logSuccess(message: string): Promise<void>;
}
export default GitMerger;