Next.js Codebase Analysis <> create-next-app <> index.ts explained — Part 1.14
In the previous article, I wrote few meaningful comments for a short code snippet from index.ts and talked about Conf package.
In this article, I will write about Typescript prompt.
The following code shows the above prompt shown in the above image, but it is not an obvious prompt, it has some code around it for preferences.
if (!program.typescript && !program.javascript) {
if (ciInfo.isCI) {
// default to TypeScript in CI as we can't prompt to
// prevent breaking setup flows
program.typescript = getPrefOrDefault('typescript')
} else {
const styledTypeScript = blue('TypeScript')
const { typescript } = await prompts(
{
type: 'toggle',
name: 'typescript',
message: `Would you like to use ${styledTypeScript}?`,
initial: getPrefOrDefault('typescript'),
active: 'Yes',
inactive: 'No',
},
{
/**
* User inputs Ctrl+C or Ctrl+D to exit the prompt. We should close the
* process and not write to the file system.
*/
onCancel: () => {
console.error('Exiting.')
process.exit(1)
},
}
)
/**
* Depending on the prompt response, set the appropriate program flags.
*/
program.typescript = Boolean(typescript)
program.javascript = !Boolean(typescript)
preferences.typescript = Boolean(typescript)
}
}
Honestly, the original authors have done a pretty good job in explaining what the above code does, I did not add any.
The only thing I was not sure was program.javascript or program.typescript, then I remembered those are options passed in to the CLI as shown
npx create-next-app --javascript
or
npx create-next-app --typescript
Conclusion:
In this article, I explained about how the ? Would you like to use TypeScript? › No / Yes
prompt is configured. This means that if you want to add your own prompts, you can do so and update the program[your-key] flag, cannot specifically think of any customisation atm, but knowing this won’t hurt.
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