Skip to content

Commit c0542e9

Browse files
committed
feat: discover models from subdirectories
Signed-off-by: warisniz02 <[email protected]> Signed-off-by: warisniz02 <[email protected]>
1 parent ec7c6a0 commit c0542e9

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

packages/cli/generators/relation/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,27 @@ module.exports = class RelationGenerator extends ArtifactGenerator {
230230
/* istanbul ignore next */
231231
return this.exit(err);
232232
}
233+
// Check if modelDir contains subdirectories
234+
const subdirectories = await utils.getSubdirectories(
235+
this.artifactInfo.modelDir,
236+
);
237+
// If subdirectories exist, retrieve models from them
238+
if (subdirectories.length > 0) {
239+
for (const subdirectory of subdirectories) {
240+
try {
241+
const subdirectoryModelList = await utils.getArtifactList(
242+
subdirectory,
243+
'model',
244+
);
245+
modelList = modelList.concat(subdirectoryModelList);
246+
} catch (err) {
247+
// Handle errors for subdirectory model retrieval
248+
console.error(
249+
`Error retrieving models from subdirectory ${subdirectory}: ${err}`,
250+
);
251+
}
252+
}
253+
}
233254
let repoList;
234255
try {
235256
debug(`repository list dir ${this.artifactInfo.repoDir}`);

packages/cli/generators/repository/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
330330
return this.exit(err);
331331
}
332332

333+
// Check if modelDir contains subdirectories
334+
const subdirectories = await utils.getSubdirectories(
335+
this.artifactInfo.modelDir,
336+
);
337+
// If subdirectories exist, retrieve models from them
338+
if (subdirectories.length > 0) {
339+
for (const subdirectory of subdirectories) {
340+
try {
341+
const subdirectoryModelList = await utils.getArtifactList(
342+
subdirectory,
343+
'model',
344+
);
345+
modelList = modelList.concat(subdirectoryModelList);
346+
} catch (err) {
347+
// Handle errors for subdirectory model retrieval
348+
console.error(
349+
`Error retrieving models from subdirectory ${subdirectory}: ${err}`,
350+
);
351+
}
352+
}
353+
}
354+
333355
if (this.options.model) {
334356
debug(`Model name received from command line: ${this.options.model}`);
335357

packages/cli/lib/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const fs = require('node:fs');
9+
const fsp = require('fs/promises');
910
const path = require('node:path');
1011
const util = require('node:util');
1112
const stream = require('node:stream');
@@ -383,6 +384,21 @@ exports.getArtifactList = async function (
383384
});
384385
};
385386

387+
/**
388+
* Retrieves a list of subdirectories within a given directory.
389+
*
390+
* @param {string} dir The directory path to search for subdirectories.
391+
*/
392+
exports.getSubdirectories = async function (dir) {
393+
const entries = await fsp.readdir(dir, {withFileTypes: true});
394+
395+
const subdirectories = entries
396+
.filter(entry => entry.isDirectory())
397+
.map(entry => path.join(dir, entry.name));
398+
399+
return subdirectories;
400+
};
401+
386402
/**
387403
* Check package.json and dependencies.json to find out versions for generated
388404
* dependencies

0 commit comments

Comments
 (0)