Invite-Only Early Access — Think Throo GitHub App is currently invite-only. Request access here.
2025February

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. Email: ramu.narasinga@gmail.com

Tired of AI-generated code that works but nobody understands?

I spent 3+ years studying OSS codebases and wrote 350+ articles on what makes them production-grade. I built an open source tool that reviews your PR against your existing codebase patterns.

Your codebase. Your patterns. Enforced.

Get started for free —thinkthroo.com