diff --git a/packages/cli/generators/relation/index.js b/packages/cli/generators/relation/index.js index 65512c8316c6..25f140721943 100644 --- a/packages/cli/generators/relation/index.js +++ b/packages/cli/generators/relation/index.js @@ -244,7 +244,6 @@ module.exports = class RelationGenerator extends ArtifactGenerator { ); modelList = modelList.concat(subdirectoryModelList); } catch (err) { - // Handle errors for subdirectory model retrieval console.error( `Error retrieving models from subdirectory ${subdirectory}: ${err}`, ); diff --git a/packages/cli/generators/repository/index.js b/packages/cli/generators/repository/index.js index 1ecf64e8f9d7..8ab2ba7a01f3 100644 --- a/packages/cli/generators/repository/index.js +++ b/packages/cli/generators/repository/index.js @@ -101,14 +101,48 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { this.artifactInfo.modelDir, utils.getModelFileName(modelName), ); - try { - fileContent = this.fs.read(modelFile, {}); - } catch (err) { - debug(`${ERROR_READING_FILE} ${modelFile}: ${err.message}`); - return this.exit(err); + + // Check if the model file exists directly in the modelDir + if (fs.existsSync(modelFile)) { + try { + fileContent = this.fs.read(modelFile, {}); + } catch (err) { + debug(`${ERROR_READING_FILE} ${modelFile}: ${err.message}`); + return this.exit(err); + } + + return tsquery.getIdFromModel(fileContent); + } + + // If the model file is not found directly, search in subdirectories + const subdirectories = await utils.getSubdirectories( + this.artifactInfo.modelDir, + ); + + for (const subdirectory of subdirectories) { + const subdirectoryModelFile = path.join( + subdirectory, + utils.getModelFileName(modelName), + ); + + if (fs.existsSync(subdirectoryModelFile)) { + try { + fileContent = this.fs.read(subdirectoryModelFile, {}); + } catch (err) { + debug( + `${ERROR_READING_FILE} ${subdirectoryModelFile}: ${err.message}`, + ); + return this.exit(err); + } + + return tsquery.getIdFromModel(fileContent); + } } - return tsquery.getIdFromModel(fileContent); + // If the model file is not found in any subdirectory, return an error + return this.exit( + new Error(`Model ${modelName} not found in any subdirectory.`), + ); } /** @@ -344,7 +378,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { ); modelList = modelList.concat(subdirectoryModelList); } catch (err) { - // Handle errors for subdirectory model retrieval console.error( `Error retrieving models from subdirectory ${subdirectory}: ${err}`, );