Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: discover models from subdirectories #10649

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/cli/generators/relation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ 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) {
console.error(
`Error retrieving models from subdirectory ${subdirectory}: ${err}`,
);
}
}
}
let repoList;
try {
debug(`repository list dir ${this.artifactInfo.repoDir}`);
Expand Down
83 changes: 77 additions & 6 deletions packages/cli/generators/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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);
}
}

// 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.`),
);
}

/**
Expand Down Expand Up @@ -330,6 +364,34 @@ 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',
);
const subdir = subdirectory.split('models')[1];
if (!this.artifactInfo.modelSubDirs) {
this.artifactInfo.modelSubDirs = {};
}
subdirectoryModelList.forEach(model => {
this.artifactInfo.modelSubDirs[model] = subdir;
});
modelList = modelList.concat(subdirectoryModelList);
} catch (err) {
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 Expand Up @@ -529,6 +591,15 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
this.artifactInfo.modelName,
);

const outFile = `${
this.artifactInfo.modelSubDirs[
this.artifactInfo.modelName || this.artifactInfo.name
]
}/${this.artifactInfo.outFile}`;
if (!outFile.includes('undefined')) {
this.artifactInfo.outFile = outFile.slice(1, outFile.length);
}

this.artifactInfo.indexesToBeUpdated.push({
dir: this.artifactInfo.outDir,
file: this.artifactInfo.outFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import {inject} from '@loopback/core';
<%if (isRepositoryBaseBuiltin) { -%>
import {<%= repositoryTypeClass %>} from '@loopback/repository';
<% } -%>
import {<%= dataSourceClassName %>} from '../datasources';
import {<%= modelName %>, <%= modelName %>Relations} from '../models';
<%if (modelSubDirs[modelName]) { -%>
import {<%= dataSourceClassName %>} from '../../datasources';
import {<%= modelName %>, <%= modelName %>Relations} from '../../models';
<% } -%>

<%if (!modelSubDirs[modelName]) { -%>
import {<%= dataSourceClassName %>} from '../datasources';
import {<%= modelName %>, <%= modelName %>Relations} from '../models';
<% } -%>

<%if ( !isRepositoryBaseBuiltin ) { -%>
import {<%=repositoryBaseClass %>} from './<%=repositoryBaseFile %>';
<% } -%>
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
Loading