DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Edited on

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()
  }
Enter fullscreen mode Exit fullscreen mode

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.

Conf

  1. Prepare a build
npm run build
Enter fullscreen mode Exit fullscreen mode
  1. Execute the command
npx create-my-app
Enter fullscreen mode Exit fullscreen mode

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

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

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:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: ramu.narasinga@gmail.com

Learn the best practices used in open source.

Top comments (0)