DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on

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]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)