Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for updates weekly on run #402

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"faunadb": "^4.5.4",
"inquirer": "^12.0.0",
"open": "10.1.0",
"update-notifier": "^7.3.1",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import loginCommand from "./commands/login.mjs";
import schemaCommand from "./commands/schema/schema.mjs";
import databaseCommand from "./commands/database.mjs";
import keyCommand from "./commands/key.mjs";
import { logArgv, fixPaths } from "./lib/middleware.mjs";
import { logArgv, fixPaths, checkForUpdates } from "./lib/middleware.mjs";
import { authNZMiddleware } from "./lib/auth/authNZ.mjs";

/** @typedef {import('awilix').AwilixContainer<import('./config/setup-container.mjs').modifiedInjectables>} cliContainer */
Expand Down Expand Up @@ -62,7 +62,7 @@ function buildYargs(argvInput) {

return yargsInstance
.scriptName("fauna")
.middleware([logArgv], true)
.middleware([checkForUpdates, logArgv], true)
.middleware([fixPaths, authNZMiddleware], false)
.command("eval", "evaluate a query", evalCommand)
.command("login", "login via website", loginCommand)
Expand Down
14 changes: 8 additions & 6 deletions src/config/setup-container.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import fs from "node:fs";
import * as fsp from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import { exit } from "node:process";

import * as awilix from "awilix";
import { Lifetime } from "awilix";
import updateNotifier from "update-notifier";
import { confirm } from "@inquirer/prompts";

import { performQuery } from "../commands/eval.mjs";
import logger from "../lib/logger.mjs";
Expand All @@ -15,17 +22,11 @@ import {
deleteUnusedSchemaFiles,
writeSchemaFiles,
} from "../lib/schema.mjs";
import { confirm } from "@inquirer/prompts";
import { makeFaunaRequest } from "../lib/db.mjs";
import fetchWrapper from "../lib/fetch-wrapper.mjs";
import { FaunaAccountClient } from "../lib/fauna-account-client.mjs";
import open from "open";
import OAuthClient from "../lib/auth/oauth-client.mjs";
import { Lifetime } from "awilix";
import fs from "node:fs";
import * as fsp from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import { AccountKey, SecretKey } from "../lib/file-util.mjs";
import { parseYargs } from "../cli.mjs";

Expand Down Expand Up @@ -63,6 +64,7 @@ export const injectables = {
// third-party libraries
confirm: awilix.asValue(confirm),
open: awilix.asValue(open),
updateNotifier: awilix.asValue(updateNotifier),

// generic lib (homemade utilities)
parseYargs: awilix.asValue(parseYargs),
Expand Down
1 change: 1 addition & 0 deletions src/config/setup-test-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function setupTestContainer() {
unlink: stub(),
writeFile: stub(),
}),
updateNotifier: awilix.asValue(stub().returns({ notify: stub() })),
logger: awilix.asValue({
// use these for making dev, support tickets easier.
// they're not mocked because we shouldn't test them
Expand Down
19 changes: 19 additions & 0 deletions src/lib/middleware.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//@ts-check

import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { container } from "../cli.mjs";
import { fixPath } from "../lib/file-util.mjs";

Expand All @@ -16,3 +20,18 @@ export function fixPaths(argv) {
return argv;
}
}

export function checkForUpdates(argv) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packagePath = path.join(__dirname, "../../package.json");
const updateNotifier = container.resolve("updateNotifier");

const notifier = updateNotifier({
pkg: JSON.parse(readFileSync(packagePath, { encoding: "utf-8" })),
updateCheckInterval: 1000 * 60 * 60 * 24 * 7, // 1 week
});

notifier.notify();
return argv;
}
30 changes: 30 additions & 0 deletions test/general-cli.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { expect } from "chai";
import { stub } from "sinon";
import * as awilix from "awilix";

import { run, builtYargs } from "../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs";
import chalk from "chalk";
Expand Down Expand Up @@ -75,6 +82,29 @@ describe("cli operations", function () {
expect(container.resolve("parseYargs")).to.have.been.calledOnce;
});

it("should check for updates when run", async function () {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packagePath = path.join(__dirname, "../package.json");

const packageJson = JSON.parse(
fs.readFileSync(packagePath, {
encoding: "utf-8",
}),
);
const notify = stub();
const updateNotifier = stub().returns({ notify });
container.register({ updateNotifier: awilix.asValue(updateNotifier) });

await run(`schema status --secret "secret"`, container);

expect(updateNotifier).to.have.been.calledWith({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7, // 1 week
})
expect(notify).to.have.been.called;
});

it.skip("should detect color support if the user does not specify", async function () {
// i can't find a way to mock this that doesn't involve setting a flag
// and setting a flag defeats the purpose of testing if it's _detected_ automatically
Expand Down
Loading
Loading