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

Next.js Codebase Analysis <> create-next-app <> index.ts explained — Part 1.5

In the previous articles, we took a little detour to understand the program variable used in run() function.

async function run(): Promise<void> {
 
  console.log("program.resetPreferences:", program.resetPreferences);
 
  // a Conf object creation with projectName.
  // We do not know what Conf does yet and it is okay.
  const conf = new Conf({ projectName: 'create-next-app' })
 
  // My first thought, where did the program come from?
  // Let’s find out by looking outside the run() function.
  // We skipped Conf class but the program variable cannot be skipped.
  // I know for a fact it is a global variable.
  if (program.resetPreferences) {
    conf.clear()
    console.log(`Preferences reset successfully`)
    return
  }
 
  if (typeof projectPath === 'string') {
    projectPath = projectPath.trim()
  }

Unknown here is Conf. Let’s find out what it is. You have knowns and unknowns and the goal is to make unknowns in your code to knowns as much as possible.

Conf

conf is a simple npm config handling for your app or module.

Let’s console.log this and find out what is in it.

Prepare a build

npm run build

Execute the command

npx create-my-app

This is what conf has

conf Conf {
  _deserialize: [Function (anonymous)],
  _serialize: [Function (anonymous)],
  events: EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  path: '/Users/ramunarasinga/Library/Preferences/create-next-app-nodejs/config.json'
}

I am thinking conf is used to persist some of your preferences chosen when you run create-next-app because the following is set just before closing the run function:

I initially made this tool to let command-line tools persist some data.

The above quote from the conf package documentation.

Conclusion

Conf is used to persist data such as preferences when you use command line tools

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