Add CONVERSION_RATE_CONFIG Support Via CLI Flag And Environment Variable
Introduction
In this article, we will explore the implementation of the CONVERSION_RATE_CONFIG feature, which allows users to configure a conversion rate through either a CLI flag (--conversion-rate
) or an environment variable (CONVERSION_RATE_CONFIG
). This enhancement will provide flexibility to our main script and demonstrate real-world configurability.
Scope of the Change
The scope of this change involves four key areas:
1. Dependencies (package.json)
To implement the CONVERSION_RATE_CONFIG feature, we need to add minimist
as a dependency in our package.json
file. If dotenv
is not already present, we should ensure it is used to load environment variables by default.
// package.json
{
"name": "your-project",
"version": "1.0.0",
"dependencies": {
"dotenv": "^10.0.0",
"minimist": "^1.2.6"
},
"scripts": {
"start": "node src/lib/main.js"
}
}
2. Source (src/lib/main.js
)
In the src/lib/main.js
file, we will import and configure dotenv
at the top of the file. We will also import minimist
to parse CLI arguments. The main(args)
function will be augmented to:
- Parse
--conversion-rate
fromargs
. - Read
process.env.CONVERSION_RATE_CONFIG
. - Determine the effective conversion rate using the following priority:
- CLI flag
- Environment variable
- Default value of
1.0
- Log the final conversion rate alongside the original args.
// src/lib/main.js
const dotenv = require('dotenv');
const minimist = require('minimist');
dotenv.config();
const args = minimist(process.argv.slice(2));
const conversionRate = args['conversion-rate'] || process.env.CONVERSION_RATE_CONFIG || 1.0;
console.log(`Run with: ${JSON.stringify(args)}, conversionRate: ${conversionRate}`);
3. Tests (tests/unit/main.test.js
)
In the tests/unit/main.test.js
file, we will add unit tests to cover:
- Default conversion rate when neither flag nor env var is set.
- Conversion rate from environment variable only.
- Conversion rate from CLI flag only (should override env).
We will use vitest
and manipulate process.argv
/ process.env
accordingly.
// tests/unit/main.test.js
import { test, expect } from 'vitest';
import main from '../src/lib/main.js';
test('default conversion rate', async () => {
process.argv = ['node', 'src/lib/main.js'];
await main();
expect(process.env.CONVERSION_RATE_CONFIG).toBe('1.0');
});
test('conversion rate from environment variable', async () => {
process.argv = ['node', 'src/lib/main.js'];
process.env.CONVERSION_RATE_CONFIG = '2.0';
await main();
expect(process.env.CONVERSION_RATE_CONFIG).toBe('2.0');
});
test('conversion rate from CLI flag', async () => {
process.argv = ['node', 'src/lib/main.js', '--conversion', '3.5'];
await main();
expect(process.env.CONVERSION_RATE_CONFIG).toBe('3.5');
});
4. Documentation (README.md)
In the README.md
file, we will update the Usage section to document:
- How to set
CONVERSION_RATE_CONFIG
in.env
or directly in the shell. - How to pass
--conversion-rate
on the command line. - The priority rules and default value.
We will also provide example commands:
CONVERSION_RATE_CONFIG=2.0 npm start
npm start -- --conversion-rate 3.5
Verification
To verify the implementation, we will run npm test
and ensure all new and existing tests pass. We will also run npm start
with and without flags/env vars to verify logging.
Conclusion
Frequently Asked Questions
In this article, we will address some of the most frequently asked questions related to the CONVERSION_RATE_CONFIG feature.
Q: What is the CONVERSION_RATE_CONFIG feature?
A: The CONVERSION_RATE_CONFIG feature allows users to configure a conversion rate through either a CLI flag (--conversion-rate
) or an environment variable (CONVERSION_RATE_CONFIG
). This enhancement provides flexibility to our main script and demonstrates real-world configurability.
Q: How do I set the CONVERSION_RATE_CONFIG environment variable?
A: You can set the CONVERSION_RATE_CONFIG environment variable in one of the following ways:
- In a
.env
file, add the following line:CONVERSION_RATE_CONFIG=2.0
- Directly in the shell, run the following command:
CONVERSION_RATE_CONFIG=2.0 npm start
Q: How do I pass the --conversion-rate CLI flag?
A: You can pass the --conversion-rate
CLI flag by running the following command: npm start -- --conversion-rate 3.5
Q: What is the priority order for the conversion rate?
A: The priority order for the conversion rate is as follows:
- CLI flag (
--conversion-rate
) - Environment variable (
CONVERSION_RATE_CONFIG
) - Default value of
1.0
Q: How do I verify that the CONVERSION_RATE_CONFIG feature is working correctly?
A: To verify that the CONVERSION_RATE_CONFIG feature is working correctly, you can run the following commands:
npm test
to ensure all new and existing tests passnpm start
with and without flags/env vars to verify logging
Q: What are the benefits of the CONVERSION_RATE_CONFIG feature?
A: The benefits of the CONVERSION_RATE_CONFIG feature include:
- Flexibility: Users can configure the conversion rate through either a CLI flag or an environment variable.
- Real-world configurability: The feature demonstrates real-world configurability and provides a more user-friendly experience.
Q: How do I troubleshoot issues with the CONVERSION_RATE_CONFIG feature?
A: To troubleshoot issues with the CONVERSION_RATE_CONFIG feature, you can:
- Check the console output for any error messages
- Verify that the environment variable is set correctly
- Run the tests to ensure that the feature is working correctly
Conclusion
In this article, we have addressed some of the most frequently asked questions related to the CONVERSION_RATE_CONFIG feature. We hope that this Q&A article has provided you with a better understanding of the feature and how to use it. If you have any further questions or concerns, please don't hesitate to reach out.