Lessons from opensource: Log “new version is available” to your CLI
These lessons are picked from next.js/create-next-app open source code. In this article, you will learn how the CLI logs that a new version of create-next-app is available when there is a new version released.
// The following code snippet is from: // https://github.com/vercel/next.js/blob/canary/packages/create-next-app/index.ts#L473import checkForUpdate from 'update-check'const update = checkForUpdate(packageJson).catch(() => null)async function notifyUpdate(): Promise<void> { try { const res = await update if (res?.latest) { const updateMessage = packageManager === 'yarn' ? 'yarn global add create-next-app' : packageManager === 'pnpm' ? 'pnpm add -g create-next-app' : packageManager === 'bun' ? 'bun add -g create-next-app' : 'npm i -g create-next-app' console.log( yellow(bold('A new version of `create-next-app` is available!')) + '\n' + 'You can update by running: ' + cyan(updateMessage) + '\n' ) } process.exit() } catch { // ignore error }}
update-check is an npm package, If there’s a new update available, the package will return the content of latest version’s package.json file. Quite simple and powerful.
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.