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: load modelina configuration options from a config file #1367

Closed

Conversation

kaushik-rishi
Copy link
Contributor

Description

@netlify
Copy link

netlify bot commented Jun 3, 2023

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit a207ca2
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/654d399f1eba530008219ae1

@kaushik-rishi kaushik-rishi changed the title wip: file helper for loading config feat: load modelina configuration options from a config file Jun 3, 2023
@coveralls
Copy link

Pull Request Test Coverage Report for Build 5161878796

  • 0 of 5 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.08%) to 92.12%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/helpers/FileHelpers.ts 0 5 0.0%
Totals Coverage Status
Change from base Build 5156178606: -0.08%
Covered Lines: 5247
Relevant Lines: 5554

💛 - Coveralls

@kaushik-rishi
Copy link
Contributor Author

@jonaslagoni I've used the require function to import the object exported by the config file.
Post build I've observed that the loadConfigFile function is only present in /lib/cjs and not lib/esm.

Why is it that way ? 🤔

Or, do you want me to import the module like this ?

function readJSFile(filePath) {
  try {
    const fileContent = fs.readFileSync(filePath, 'utf8');
    const moduleExports = {};
    const moduleWrapper = eval('(function(exports) {' + fileContent + '})');
    moduleWrapper(moduleExports);
    return moduleExports;
  } catch (error) {
    console.error('Error reading JS file:', error);
    return null;
  }
}

@jonaslagoni
Copy link
Member

Post build I've observed that the loadConfigFile function is only present in /lib/cjs and not lib/esm.

Which build command did you run?

"build:esm": "tsc --project tsconfig.json --module ESNext --outDir ./lib/esm",

@kaushik-rishi
Copy link
Contributor Author

Which build command did you run?

On running build:esm, i do find the function loadConfigFile.
But previously i think i ran build command, i didn't find the function loadConfigFile in the directory /lib/esm, irrespective it's working now. 🙂

Thanks. 😄

Do you think we should add more documentation on setting up the repository for development ?

@kaushik-rishi
Copy link
Contributor Author

kaushik-rishi commented Jun 5, 2023

@jonaslagoni After i add a function, and build the project (build:esm).
How do i test the imports and stuff ?
Like let's say i create a new project somewhere else in my Desktop, how do i import the newly build version of this project 🤔 ?

Edit:
[RESOLVED]

@kaushik-rishi kaushik-rishi marked this pull request as draft June 5, 2023 17:53
@kaushik-rishi
Copy link
Contributor Author

kaushik-rishi commented Jun 6, 2023

@jonaslagoni I'm kind of stuck / plateaued here. 🙂
What should i further do ? 🤔
Is there any other validation we want to run on the imported config file ?

I'm talking feature wise, i haven't written the tests yet.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 6, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
46.1% 46.1% Duplication

Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, you need to add documentation as well, probably something like the following:

Hope this gives you a direction, otherwise let me know if you want anything clarified 🙂

@@ -64,4 +64,18 @@ export class FileHelpers {
}
});
}

static async getConfigFromFile(configPath: string): Promise<any> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to write some unit tests here 🙂

Make sure you test error states as well:

  • What if the file exist but is not valid JS?
  • What if the file exist but is TypeScript?
  • What if the file exist and is correct JS, but does not expose the right config?
  • What if the file exist and is correct JS, but does not expose the config file the right way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you document this function as well i.e. what it does 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonaslagoni
I'll make sure to document and write the unit tests.

What if the file exist and is correct JS, but does not expose the right config?

and

What if the file exist and is correct JS, but does not expose the config file the right way?

Should be the same thing right ? I mean we wouldn't know if the file is exposing the config the right way, unless we know what it's exposing ? 🤔

The way i put it here might be confusing, feel free to ignore this, i will let you know later more context on this. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try fixing things one after other and contact you back 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way i put it here might be confusing, feel free to ignore this, i will let you know later more context on this. 🙂

The first one is a bit hard to pull off, which is they expose the configuration correctly, but the configuration is incorrect (not sure we actually can do this)

The second one is if they expose it like export { config2: config }; when we expected export config;.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is a bit hard to pull off, which is they expose the configuration correctly, but the configuration is incorrect (not sure we actually can do this)

I'll try to see if we can do the first one

The second one is if they expose it like export { config2: config }; when we expected export config;.

I'll look into this as well 🙂
Seems like a good learning oppurtunity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonaslagoni

We can restrict the users to only allow them to use js file for config right ? i hardly saw places where people used ts files for config.
What do you say ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree 👍

It can also be an improvement down the line if ever requested 🙂

Comment on lines +17 to +18
const config: { config: TypeScriptOptions } =
await FileHelpers.getConfigFromFile('./modelina.config.alternate.js');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should simplify this.

    const config = await FileHelpers.getConfigFromFile('./modelina.config.alternate.js');

Instead of having to do something like config.config, what do you think? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah definitely, it looks absurd.
We can directly export the config. 🙂
Would that work ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 👍

@kaushik-rishi
Copy link
Contributor Author

@jonaslagoni

The config file can be placed anywhere on the users machine, which is probably not a node package and doensn't have modelina installed.

Out getConfigFromFile function will be called let's say from cli, where modelina will be installed, so the require should work right ?

@kaushik-rishi
Copy link
Contributor Author

I will pause working on this PR for 2-3 days, busy with some other things

@jonaslagoni
Copy link
Member

Looks like you are nearly there @kaushik-rishi 🤔

Copy link

sonarqubecloud bot commented Nov 9, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
4.4% 4.4% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@jonaslagoni
Copy link
Member

Hey @kaushik-rishi, want to continue or can someone else take over the progress?

@jonaslagoni
Copy link
Member

Closing as no response for over a month

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants