From b673bdf0b03f6afbabd3bea7d4aab4b684a93068 Mon Sep 17 00:00:00 2001 From: Yavor Ivanov Date: Wed, 3 Apr 2024 08:52:47 +0300 Subject: [PATCH] refactor: Add updateNotifier to cli middlewares --- .nycrc | 3 --- src/cli.ts | 32 ++------------------------- src/cli/middlewares/base.ts | 3 +++ src/cli/middlewares/updateNotifier.ts | 32 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 src/cli/middlewares/updateNotifier.ts diff --git a/.nycrc b/.nycrc index 3b57f7d39..4b47da6ce 100644 --- a/.nycrc +++ b/.nycrc @@ -2,9 +2,6 @@ "extends": "@istanbuljs/nyc-config-typescript", "reporter": ["lcov", "text", "text-summary"], "include": ["src/**"], - "exclude": [ - "src/cli.ts" - ], "check-coverage": true, "statements": 72, "branches": 61, diff --git a/src/cli.ts b/src/cli.ts index 83bef40d1..5587b9ba2 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,37 +5,7 @@ import {fileURLToPath} from "node:url"; import {setVersion} from "./cli/version.js"; import {createRequire} from "module"; -import type {Package} from "update-notifier"; - export default async function () { - const require = createRequire(import.meta.url); - const pkg = require("../package.json") as Package; - - // Only import update-notifier when it's not disabled - // See https://github.com/yeoman/update-notifier/blob/3046d0f61a57f8270291b6ab271f8a12df8421a6/update-notifier.js#L57-L60 - // The "is-ci" check is not executed, but will be checked by update-notifier itself then - const NO_UPDATE_NOTIFIER = "--no-update-notifier"; - const disableUpdateNotifier = - "NO_UPDATE_NOTIFIER" in process.env || - process.env.NODE_ENV === "test" || - process.argv.includes(NO_UPDATE_NOTIFIER); - - /* istanbul ignore if */ - if (!disableUpdateNotifier) { - const {default: updateNotifier} = await import("update-notifier"); - updateNotifier({ - pkg, - updateCheckInterval: 86400000, // 1 day - shouldNotifyInNpmScript: true, - }).notify(); - } - - // Remove --no-update-notifier from argv as it's not known to yargs, but we still want to support using it - /* istanbul ignore if */ - if (process.argv.includes(NO_UPDATE_NOTIFIER)) { - process.argv = process.argv.filter((v) => v !== NO_UPDATE_NOTIFIER); - } - const cli = yargs(hideBin(process.argv)); cli.parserConfiguration({ "parse-numbers": false, @@ -44,6 +14,8 @@ export default async function () { // Explicitly set CLI version as the yargs default might // be wrong in case a local CLI installation is used // Also add CLI location + const require = createRequire(import.meta.url); + const pkg = require("../package.json") as {version: string}; const ui5LintJsPath = fileURLToPath(new URL("../bin/ui5lint.js", import.meta.url)); const pkgVersion = `${pkg.version} (from ${ui5LintJsPath})`; diff --git a/src/cli/middlewares/base.ts b/src/cli/middlewares/base.ts index 6bb8121e3..62e1d5597 100644 --- a/src/cli/middlewares/base.ts +++ b/src/cli/middlewares/base.ts @@ -1,4 +1,6 @@ import {initLogger} from "./logger.js"; +import updateNotifier from "./updateNotifier.js"; + import type {ArgumentsCamelCase} from "yargs"; import type {LinterArg} from "../base.ts"; /** @@ -8,4 +10,5 @@ import type {LinterArg} from "../base.ts"; */ export default async function (argv: ArgumentsCamelCase) { await initLogger(argv); + await updateNotifier(); } diff --git a/src/cli/middlewares/updateNotifier.ts b/src/cli/middlewares/updateNotifier.ts new file mode 100644 index 000000000..58d6f0a03 --- /dev/null +++ b/src/cli/middlewares/updateNotifier.ts @@ -0,0 +1,32 @@ +import type {Package} from "update-notifier"; +import {createRequire} from "module"; + +export default async function updateNotifier() { + const require = createRequire(import.meta.url); + const pkg = require("../../../package.json") as Package; + + // Only import update-notifier when it's not disabled + // See https://github.com/yeoman/update-notifier/blob/3046d0f61a57f8270291b6ab271f8a12df8421a6/update-notifier.js#L57-L60 + // The "is-ci" check is not executed, but will be checked by update-notifier itself then + const NO_UPDATE_NOTIFIER = "--no-update-notifier"; + const disableUpdateNotifier = + "NO_UPDATE_NOTIFIER" in process.env || + process.env.NODE_ENV === "test" || + process.argv.includes(NO_UPDATE_NOTIFIER); + + /* istanbul ignore if */ + if (!disableUpdateNotifier) { + const {default: updateNotifier} = await import("update-notifier"); + updateNotifier({ + pkg, + updateCheckInterval: 86400000, // 1 day + shouldNotifyInNpmScript: true, + }).notify(); + } + + // Remove --no-update-notifier from argv as it's not known to yargs, but we still want to support using it + /* istanbul ignore if */ + if (process.argv.includes(NO_UPDATE_NOTIFIER)) { + process.argv = process.argv.filter((v) => v !== NO_UPDATE_NOTIFIER); + } +}