Files
cli/ts/mod_config/classes.commitconfig.ts
2026-03-24 16:10:51 +00:00

105 lines
2.7 KiB
TypeScript

import * as plugins from './mod.plugins.js';
export interface ICommitConfig {
alwaysTest: boolean;
alwaysBuild: boolean;
}
/**
* Manages commit configuration stored in .smartconfig.json
* under @git.zone/cli.commit namespace
*/
export class CommitConfig {
private cwd: string;
private config: ICommitConfig;
constructor(cwd: string = process.cwd()) {
this.cwd = cwd;
this.config = { alwaysTest: false, alwaysBuild: false };
}
/**
* Create a CommitConfig instance from current working directory
*/
public static async fromCwd(cwd: string = process.cwd()): Promise<CommitConfig> {
const instance = new CommitConfig(cwd);
await instance.load();
return instance;
}
/**
* Load configuration from .smartconfig.json
*/
public async load(): Promise<void> {
const smartconfigInstance = new plugins.smartconfig.Smartconfig(this.cwd);
const gitzoneConfig = smartconfigInstance.dataFor<any>('@git.zone/cli', {});
this.config = {
alwaysTest: gitzoneConfig?.commit?.alwaysTest ?? false,
alwaysBuild: gitzoneConfig?.commit?.alwaysBuild ?? false,
};
}
/**
* Save configuration to .smartconfig.json
*/
public async save(): Promise<void> {
const smartconfigPath = plugins.path.join(this.cwd, '.smartconfig.json');
let smartconfigData: any = {};
// Read existing .smartconfig.json
if (await plugins.smartfs.file(smartconfigPath).exists()) {
const content = await plugins.smartfs.file(smartconfigPath).encoding('utf8').read();
smartconfigData = JSON.parse(content as string);
}
// Ensure @git.zone/cli namespace exists
if (!smartconfigData['@git.zone/cli']) {
smartconfigData['@git.zone/cli'] = {};
}
// Ensure commit object exists
if (!smartconfigData['@git.zone/cli'].commit) {
smartconfigData['@git.zone/cli'].commit = {};
}
// Update commit settings
smartconfigData['@git.zone/cli'].commit.alwaysTest = this.config.alwaysTest;
smartconfigData['@git.zone/cli'].commit.alwaysBuild = this.config.alwaysBuild;
// Write back to file
await plugins.smartfs
.file(smartconfigPath)
.encoding('utf8')
.write(JSON.stringify(smartconfigData, null, 2));
}
/**
* Get alwaysTest setting
*/
public getAlwaysTest(): boolean {
return this.config.alwaysTest;
}
/**
* Set alwaysTest setting
*/
public setAlwaysTest(value: boolean): void {
this.config.alwaysTest = value;
}
/**
* Get alwaysBuild setting
*/
public getAlwaysBuild(): boolean {
return this.config.alwaysBuild;
}
/**
* Set alwaysBuild setting
*/
public setAlwaysBuild(value: boolean): void {
this.config.alwaysBuild = value;
}
}