Skip to content

Commit

Permalink
feat: discover models from subdirectories
Browse files Browse the repository at this point in the history
Signed-off-by: warisniz02 <[email protected]>

Signed-off-by: warisniz02 <[email protected]>
  • Loading branch information
warisniz02 committed Sep 12, 2024
1 parent ec7c6a0 commit c0542e9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/cli/generators/relation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/generators/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
16 changes: 16 additions & 0 deletions packages/cli/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c0542e9

Please sign in to comment.