feat(commit): Add commit configuration and automatic pre-commit tests
This commit is contained in:
104
ts/mod_config/classes.commitconfig.ts
Normal file
104
ts/mod_config/classes.commitconfig.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import * as plugins from './mod.plugins.js';
|
||||
|
||||
export interface ICommitConfig {
|
||||
alwaysTest: boolean;
|
||||
alwaysBuild: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages commit configuration stored in npmextra.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 npmextra.json
|
||||
*/
|
||||
public async load(): Promise<void> {
|
||||
const npmextraInstance = new plugins.npmextra.Npmextra(this.cwd);
|
||||
const gitzoneConfig = npmextraInstance.dataFor<any>('@git.zone/cli', {});
|
||||
|
||||
this.config = {
|
||||
alwaysTest: gitzoneConfig?.commit?.alwaysTest ?? false,
|
||||
alwaysBuild: gitzoneConfig?.commit?.alwaysBuild ?? false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Save configuration to npmextra.json
|
||||
*/
|
||||
public async save(): Promise<void> {
|
||||
const npmextraPath = plugins.path.join(this.cwd, 'npmextra.json');
|
||||
let npmextraData: any = {};
|
||||
|
||||
// Read existing npmextra.json
|
||||
if (await plugins.smartfs.file(npmextraPath).exists()) {
|
||||
const content = await plugins.smartfs.file(npmextraPath).encoding('utf8').read();
|
||||
npmextraData = JSON.parse(content as string);
|
||||
}
|
||||
|
||||
// Ensure @git.zone/cli namespace exists
|
||||
if (!npmextraData['@git.zone/cli']) {
|
||||
npmextraData['@git.zone/cli'] = {};
|
||||
}
|
||||
|
||||
// Ensure commit object exists
|
||||
if (!npmextraData['@git.zone/cli'].commit) {
|
||||
npmextraData['@git.zone/cli'].commit = {};
|
||||
}
|
||||
|
||||
// Update commit settings
|
||||
npmextraData['@git.zone/cli'].commit.alwaysTest = this.config.alwaysTest;
|
||||
npmextraData['@git.zone/cli'].commit.alwaysBuild = this.config.alwaysBuild;
|
||||
|
||||
// Write back to file
|
||||
await plugins.smartfs
|
||||
.file(npmextraPath)
|
||||
.encoding('utf8')
|
||||
.write(JSON.stringify(npmextraData, 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user