31200609b6
* refactor(config.ts): improve code readability by formatting array elements and conditions * fix(migrations): handle undefined values correctly when setting default config values * fix(migrations): ensure process exits with error code on migration failure * fix(commit.ts): update error handling to provide clearer feedback when commit message generation fails * feat(config.ts): add cleanUndefinedValues function to sanitize config values by converting 'undefined' and 'null' strings to actual values * refactor(config.ts): return cleaned config from getConfig function to ensure consistent data types * chore(migrations): log entriesToSet in migration to assist with debugging and tracking changes
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import fs from 'fs';
|
|
import { homedir } from 'os';
|
|
import { join as pathJoin } from 'path';
|
|
import { migrations } from './_migrations';
|
|
import { outro } from '@clack/prompts';
|
|
import chalk from 'chalk';
|
|
import {
|
|
getConfig,
|
|
getIsGlobalConfigFileExist,
|
|
OCO_AI_PROVIDER_ENUM
|
|
} from '../commands/config';
|
|
|
|
const migrationsFile = pathJoin(homedir(), '.opencommit_migrations');
|
|
|
|
const getCompletedMigrations = (): string[] => {
|
|
if (!fs.existsSync(migrationsFile)) {
|
|
return [];
|
|
}
|
|
const data = fs.readFileSync(migrationsFile, 'utf-8');
|
|
return data ? JSON.parse(data) : [];
|
|
};
|
|
|
|
const saveCompletedMigration = (migrationName: string) => {
|
|
const completedMigrations = getCompletedMigrations();
|
|
completedMigrations.push(migrationName);
|
|
fs.writeFileSync(
|
|
migrationsFile,
|
|
JSON.stringify(completedMigrations, null, 2)
|
|
);
|
|
};
|
|
|
|
export const runMigrations = async () => {
|
|
// if no config file, we assume it's a new installation and no migrations are needed
|
|
if (!getIsGlobalConfigFileExist()) return;
|
|
|
|
const config = getConfig();
|
|
if (config.OCO_AI_PROVIDER === OCO_AI_PROVIDER_ENUM.TEST) return;
|
|
|
|
const completedMigrations = getCompletedMigrations();
|
|
|
|
let isMigrated = false;
|
|
|
|
for (const migration of migrations) {
|
|
if (!completedMigrations.includes(migration.name)) {
|
|
try {
|
|
console.log('Applying migration', migration.name);
|
|
migration.run();
|
|
console.log('Migration applied successfully', migration.name);
|
|
saveCompletedMigration(migration.name);
|
|
} catch (error) {
|
|
outro(
|
|
`${chalk.red('Failed to apply migration')} ${
|
|
migration.name
|
|
}: ${error}`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
isMigrated = true;
|
|
}
|
|
}
|
|
|
|
if (isMigrated) {
|
|
outro(
|
|
`${chalk.green(
|
|
'✔'
|
|
)} Migrations to your config were applied successfully. Please rerun.`
|
|
);
|
|
process.exit(0);
|
|
}
|
|
};
|