In the previous article, I wrote about isFolderEmpty function that is used to prevent providing conflicting names for your project.
In this article, I will try to understand the following code snippet.
// Remember the example option?
// if there is no example provided as part of your CLI command
// that is where you see prompts for your project configuration
const example = typeof program.example === 'string' && program.example.trim()
// What is conf.get? in the one of previous articles, I wrote about Conf
// package for setting preferences stored specific to your device
const preferences = (conf.get('preferences') || {}) as Record<
string,
boolean | string
>
/**
* If the user does not provide the necessary flags, prompt them for whether
* to use TS or JS.
*/
if (!example) {
// default preferences variable
const defaults: typeof preferences = {
typescript: true,
eslint: true,
tailwind: true,
app: true,
srcDir: false,
importAlias: '@/*',
customizeImportAlias: false,
}
// Interesting variable name
// getPrefOrDefault
// What if you write getConfPreferenceOrDefault? long one
// Should prefer to abbreviate where possible, but not overdo it where it
// meaning changes
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field]
I have provided the corresponding comments in the above code snippet.
Conclusion:
This code snippet uses Conf preferences. If you have a package that takes user input via prompts in the CLI, I recommend this conf package to store user preferences local to their device.
I am building a platform that explains best practices used in open source by elite programmers. Join the waitlist and I will send you the link to the tutorials once they are ready.
If you have any questions, feel free to reach out to me at ramu.narasinga@gmail.com
Get free courses inspired by the best practices used in open source.
About me:
Website: https://ramunarasinga.com/
Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/
Github: https://github.com/Ramu-Narasinga
Email: ramu.narasinga@gmail.com