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 configurationconst 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 deviceconst 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.
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.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.