Skip to content

Commit

Permalink
refactor: Add updateNotifier to cli middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Apr 9, 2024
1 parent b406083 commit b673bdf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 2 additions & 30 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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})`;

Expand Down
3 changes: 3 additions & 0 deletions src/cli/middlewares/base.ts
Original file line number Diff line number Diff line change
@@ -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";
/**
Expand All @@ -8,4 +10,5 @@ import type {LinterArg} from "../base.ts";
*/
export default async function (argv: ArgumentsCamelCase<LinterArg>) {
await initLogger(argv);
await updateNotifier();
}
32 changes: 32 additions & 0 deletions src/cli/middlewares/updateNotifier.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b673bdf

Please sign in to comment.