Skip to content

Commit

Permalink
feat: make some changes in generators/repository/index.js
Browse files Browse the repository at this point in the history
Signed-off-by: warisniz02 <[email protected]>
  • Loading branch information
warisniz02 committed Sep 22, 2024
1 parent c0542e9 commit d08bcdd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/cli/generators/relation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down
47 changes: 40 additions & 7 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);
}

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

/**
Expand Down Expand Up @@ -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}`,
);
Expand Down

0 comments on commit d08bcdd

Please sign in to comment.