From c0542e9a0c17a06ad37ad1bbe898d0caa2100c63 Mon Sep 17 00:00:00 2001 From: warisniz02 Date: Tue, 10 Sep 2024 17:04:27 +0500 Subject: [PATCH] feat: discover models from subdirectories Signed-off-by: warisniz02 Signed-off-by: warisniz02 --- packages/cli/generators/relation/index.js | 21 ++++++++++++++++++++ packages/cli/generators/repository/index.js | 22 +++++++++++++++++++++ packages/cli/lib/utils.js | 16 +++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/packages/cli/generators/relation/index.js b/packages/cli/generators/relation/index.js index 0c7cb5703e50..65512c8316c6 100644 --- a/packages/cli/generators/relation/index.js +++ b/packages/cli/generators/relation/index.js @@ -230,6 +230,27 @@ module.exports = class RelationGenerator extends ArtifactGenerator { /* istanbul ignore next */ return this.exit(err); } + // Check if modelDir contains subdirectories + const subdirectories = await utils.getSubdirectories( + this.artifactInfo.modelDir, + ); + // If subdirectories exist, retrieve models from them + if (subdirectories.length > 0) { + for (const subdirectory of subdirectories) { + try { + const subdirectoryModelList = await utils.getArtifactList( + subdirectory, + 'model', + ); + modelList = modelList.concat(subdirectoryModelList); + } catch (err) { + // Handle errors for subdirectory model retrieval + console.error( + `Error retrieving models from subdirectory ${subdirectory}: ${err}`, + ); + } + } + } let repoList; try { debug(`repository list dir ${this.artifactInfo.repoDir}`); diff --git a/packages/cli/generators/repository/index.js b/packages/cli/generators/repository/index.js index 6a0619f59021..1ecf64e8f9d7 100644 --- a/packages/cli/generators/repository/index.js +++ b/packages/cli/generators/repository/index.js @@ -330,6 +330,28 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator { return this.exit(err); } + // Check if modelDir contains subdirectories + const subdirectories = await utils.getSubdirectories( + this.artifactInfo.modelDir, + ); + // If subdirectories exist, retrieve models from them + if (subdirectories.length > 0) { + for (const subdirectory of subdirectories) { + try { + const subdirectoryModelList = await utils.getArtifactList( + subdirectory, + 'model', + ); + modelList = modelList.concat(subdirectoryModelList); + } catch (err) { + // Handle errors for subdirectory model retrieval + console.error( + `Error retrieving models from subdirectory ${subdirectory}: ${err}`, + ); + } + } + } + if (this.options.model) { debug(`Model name received from command line: ${this.options.model}`); diff --git a/packages/cli/lib/utils.js b/packages/cli/lib/utils.js index 8ce58b2f2d69..a9512dd70a57 100644 --- a/packages/cli/lib/utils.js +++ b/packages/cli/lib/utils.js @@ -6,6 +6,7 @@ 'use strict'; const fs = require('node:fs'); +const fsp = require('fs/promises'); const path = require('node:path'); const util = require('node:util'); const stream = require('node:stream'); @@ -383,6 +384,21 @@ exports.getArtifactList = async function ( }); }; +/** + * Retrieves a list of subdirectories within a given directory. + * + * @param {string} dir The directory path to search for subdirectories. + */ +exports.getSubdirectories = async function (dir) { + const entries = await fsp.readdir(dir, {withFileTypes: true}); + + const subdirectories = entries + .filter(entry => entry.isDirectory()) + .map(entry => path.join(dir, entry.name)); + + return subdirectories; +}; + /** * Check package.json and dependencies.json to find out versions for generated * dependencies