Next.js Codebase Analysis <> create-next-app <> index.ts explained — Part 1.13
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:
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.
I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com
My Github — https://github.com/ramu-narasinga
My website — https://ramunarasinga.com
My Youtube channel — https://www.youtube.com/@ramu-narasinga
Learning platform — https://thinkthroo.com
Codebase Architecture — https://app.thinkthroo.com/architecture
Best practices — https://app.thinkthroo.com/best-practices
Production-grade projects — https://app.thinkthroo.com/production-grade-projects