BREAKING CHANGE(core): Migrate filesystem to smartfs (async) and add Elasticsearch service support; refactor format/commit/meta modules

This commit is contained in:
2025-11-27 21:32:34 +00:00
parent 2f3d67f9e3
commit e1d28bc10a
30 changed files with 2217 additions and 995 deletions

View File

@@ -19,6 +19,11 @@ export interface IServiceConfig {
S3_BUCKET: string;
S3_ENDPOINT: string;
S3_USESSL: boolean;
ELASTICSEARCH_HOST: string;
ELASTICSEARCH_PORT: string;
ELASTICSEARCH_USER: string;
ELASTICSEARCH_PASS: string;
ELASTICSEARCH_URL: string;
}
export class ServiceConfiguration {
@@ -61,10 +66,10 @@ export class ServiceConfiguration {
* Save the configuration to file
*/
public async saveConfig(): Promise<void> {
await plugins.smartfile.memory.toFs(
JSON.stringify(this.config, null, 2),
this.configPath
);
await plugins.smartfs
.file(this.configPath)
.encoding('utf8')
.write(JSON.stringify(this.config, null, 2));
}
/**
@@ -72,21 +77,24 @@ export class ServiceConfiguration {
*/
private async ensureNogitDirectory(): Promise<void> {
const nogitPath = plugins.path.join(process.cwd(), '.nogit');
await plugins.smartfile.fs.ensureDir(nogitPath);
await plugins.smartfs.directory(nogitPath).recursive().create();
}
/**
* Check if configuration file exists
*/
private async configExists(): Promise<boolean> {
return plugins.smartfile.fs.fileExists(this.configPath);
return plugins.smartfs.file(this.configPath).exists();
}
/**
* Load configuration from file
*/
private async loadConfig(): Promise<void> {
const configContent = plugins.smartfile.fs.toStringSync(this.configPath);
const configContent = (await plugins.smartfs
.file(this.configPath)
.encoding('utf8')
.read()) as string;
this.config = JSON.parse(configContent);
}
@@ -94,16 +102,16 @@ export class ServiceConfiguration {
* Create default configuration
*/
private async createDefaultConfig(): Promise<void> {
const projectName = helpers.getProjectName();
const projectName = await helpers.getProjectName();
const mongoPort = await helpers.getRandomAvailablePort();
const s3Port = await helpers.getRandomAvailablePort();
let s3ConsolePort = s3Port + 1;
// Ensure console port is also available
while (!(await helpers.isPortAvailable(s3ConsolePort))) {
s3ConsolePort++;
}
const mongoUser = 'defaultadmin';
const mongoPass = 'defaultpass';
const mongoHost = 'localhost';
@@ -111,7 +119,11 @@ export class ServiceConfiguration {
const mongoPortStr = mongoPort.toString();
const s3Host = 'localhost';
const s3PortStr = s3Port.toString();
const esHost = 'localhost';
const esPort = '9200';
const esUser = 'elastic';
const esPass = 'elastic';
this.config = {
PROJECT_NAME: projectName,
MONGODB_HOST: mongoHost,
@@ -127,22 +139,28 @@ export class ServiceConfiguration {
S3_SECRETKEY: 'defaultpass',
S3_BUCKET: `${projectName}-documents`,
S3_ENDPOINT: s3Host,
S3_USESSL: false
S3_USESSL: false,
ELASTICSEARCH_HOST: esHost,
ELASTICSEARCH_PORT: esPort,
ELASTICSEARCH_USER: esUser,
ELASTICSEARCH_PASS: esPass,
ELASTICSEARCH_URL: `http://${esUser}:${esPass}@${esHost}:${esPort}`
};
await this.saveConfig();
logger.log('ok', '✅ Created .nogit/env.json with project defaults');
logger.log('info', `📍 MongoDB port: ${mongoPort}`);
logger.log('info', `📍 S3 API port: ${s3Port}`);
logger.log('info', `📍 S3 Console port: ${s3ConsolePort}`);
logger.log('info', `📍 Elasticsearch port: ${esPort}`);
}
/**
* Update missing fields in existing configuration
*/
private async updateMissingFields(): Promise<void> {
const projectName = helpers.getProjectName();
const projectName = await helpers.getProjectName();
let updated = false;
const fieldsAdded: string[] = [];
@@ -249,7 +267,39 @@ export class ServiceConfiguration {
fieldsAdded.push('S3_ENDPOINT');
updated = true;
}
if (!this.config.ELASTICSEARCH_HOST) {
this.config.ELASTICSEARCH_HOST = 'localhost';
fieldsAdded.push('ELASTICSEARCH_HOST');
updated = true;
}
if (!this.config.ELASTICSEARCH_PORT) {
this.config.ELASTICSEARCH_PORT = '9200';
fieldsAdded.push('ELASTICSEARCH_PORT');
updated = true;
}
if (!this.config.ELASTICSEARCH_USER) {
this.config.ELASTICSEARCH_USER = 'elastic';
fieldsAdded.push('ELASTICSEARCH_USER');
updated = true;
}
if (!this.config.ELASTICSEARCH_PASS) {
this.config.ELASTICSEARCH_PASS = 'elastic';
fieldsAdded.push('ELASTICSEARCH_PASS');
updated = true;
}
// Always update ELASTICSEARCH_URL based on current settings
const oldEsUrl = this.config.ELASTICSEARCH_URL;
this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
if (oldEsUrl !== this.config.ELASTICSEARCH_URL) {
fieldsAdded.push('ELASTICSEARCH_URL');
updated = true;
}
if (updated) {
await this.saveConfig();
logger.log('ok', `✅ Added missing fields: ${fieldsAdded.join(', ')}`);
@@ -272,17 +322,19 @@ export class ServiceConfiguration {
public getContainerNames() {
return {
mongo: `${this.config.PROJECT_NAME}-mongodb`,
minio: `${this.config.PROJECT_NAME}-minio`
minio: `${this.config.PROJECT_NAME}-minio`,
elasticsearch: `${this.config.PROJECT_NAME}-elasticsearch`
};
}
/**
* Get data directories
*/
public getDataDirectories() {
return {
mongo: plugins.path.join(process.cwd(), '.nogit', 'mongodata'),
minio: plugins.path.join(process.cwd(), '.nogit', 'miniodata')
minio: plugins.path.join(process.cwd(), '.nogit', 'miniodata'),
elasticsearch: plugins.path.join(process.cwd(), '.nogit', 'esdata')
};
}
@@ -330,12 +382,27 @@ export class ServiceConfiguration {
}
}
}
// Check Elasticsearch container
const esStatus = await this.docker.getStatus(containers.elasticsearch);
if (esStatus !== 'not_exists') {
const portMappings = await this.docker.getPortMappings(containers.elasticsearch);
if (portMappings && portMappings['9200']) {
const dockerPort = portMappings['9200'];
if (this.config.ELASTICSEARCH_PORT !== dockerPort) {
logger.log('note', `📍 Syncing Elasticsearch port from Docker: ${dockerPort}`);
this.config.ELASTICSEARCH_PORT = dockerPort;
updated = true;
}
}
}
if (updated) {
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
this.config.S3_ENDPOINT = this.config.S3_HOST;
this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
await this.saveConfig();
logger.log('ok', '✅ Configuration synced with Docker containers');
}
@@ -347,11 +414,12 @@ export class ServiceConfiguration {
public async validateAndUpdatePorts(): Promise<boolean> {
let updated = false;
const containers = this.getContainerNames();
// Check if containers exist - if they do, ports are fine
const mongoExists = await this.docker.exists(containers.mongo);
const minioExists = await this.docker.exists(containers.minio);
const esExists = await this.docker.exists(containers.elasticsearch);
// Only check port availability if containers don't exist
if (!mongoExists) {
const mongoPort = parseInt(this.config.MONGODB_PORT);
@@ -363,11 +431,11 @@ export class ServiceConfiguration {
updated = true;
}
}
if (!minioExists) {
const s3Port = parseInt(this.config.S3_PORT);
const s3ConsolePort = parseInt(this.config.S3_CONSOLE_PORT);
if (!(await helpers.isPortAvailable(s3Port))) {
logger.log('note', `⚠️ S3 API port ${s3Port} is in use, finding new port...`);
const newPort = await helpers.getRandomAvailablePort();
@@ -375,7 +443,7 @@ export class ServiceConfiguration {
logger.log('ok', `✅ New S3 API port: ${newPort}`);
updated = true;
}
if (!(await helpers.isPortAvailable(s3ConsolePort))) {
logger.log('note', `⚠️ S3 Console port ${s3ConsolePort} is in use, finding new port...`);
let newPort = parseInt(this.config.S3_PORT) + 1;
@@ -387,15 +455,27 @@ export class ServiceConfiguration {
updated = true;
}
}
if (!esExists) {
const esPort = parseInt(this.config.ELASTICSEARCH_PORT);
if (!(await helpers.isPortAvailable(esPort))) {
logger.log('note', `⚠️ Elasticsearch port ${esPort} is in use, finding new port...`);
const newPort = await helpers.getRandomAvailablePort();
this.config.ELASTICSEARCH_PORT = newPort.toString();
logger.log('ok', `✅ New Elasticsearch port: ${newPort}`);
updated = true;
}
}
if (updated) {
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
this.config.S3_ENDPOINT = this.config.S3_HOST;
this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
await this.saveConfig();
}
return updated;
}
@@ -404,29 +484,35 @@ export class ServiceConfiguration {
*/
public async reconfigurePorts(): Promise<void> {
logger.log('note', '🔄 Finding new available ports...');
const mongoPort = await helpers.getRandomAvailablePort();
const s3Port = await helpers.getRandomAvailablePort();
let s3ConsolePort = s3Port + 1;
// Ensure console port is also available
while (!(await helpers.isPortAvailable(s3ConsolePort))) {
s3ConsolePort++;
}
// Elasticsearch uses standard port 9200
const esPort = '9200';
this.config.MONGODB_PORT = mongoPort.toString();
this.config.S3_PORT = s3Port.toString();
this.config.S3_CONSOLE_PORT = s3ConsolePort.toString();
this.config.ELASTICSEARCH_PORT = esPort;
// Update derived fields
this.config.MONGODB_URL = `mongodb://${this.config.MONGODB_USER}:${this.config.MONGODB_PASS}@${this.config.MONGODB_HOST}:${this.config.MONGODB_PORT}/${this.config.MONGODB_NAME}?authSource=admin`;
this.config.S3_ENDPOINT = this.config.S3_HOST;
this.config.ELASTICSEARCH_URL = `http://${this.config.ELASTICSEARCH_USER}:${this.config.ELASTICSEARCH_PASS}@${this.config.ELASTICSEARCH_HOST}:${this.config.ELASTICSEARCH_PORT}`;
await this.saveConfig();
logger.log('ok', '✅ New port configuration:');
logger.log('info', ` 📍 MongoDB: ${mongoPort}`);
logger.log('info', ` 📍 S3 API: ${s3Port}`);
logger.log('info', ` 📍 S3 Console: ${s3ConsolePort}`);
logger.log('info', ` 📍 Elasticsearch: ${esPort}`);
}
}