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. 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