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

Od/show update warning message #105

Merged
merged 21 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
IS_CICD: true
defaults:
run:
working-directory: ./packages/cli

steps:
- uses: actions/checkout@v2
Expand All @@ -34,7 +37,7 @@ jobs:
--global user.name "PCC"

- name: Build
run: pnpm build
run: pnpm build:test

- name: Test
run: pnpm --filter '*cli*' test
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test": "jest --silent",
"build": "tsup --env.NODE_ENV production",
"build:staging": "tsup --env.NODE_ENV staging",
"build:test": "tsup --env.NODE_ENV test",
"lint": "eslint . && prettier --check .",
"lint:fix": "eslint . --fix && prettier --write .",
"preinstall": "node scripts/checkNodeVersion.js"
Expand All @@ -38,6 +39,7 @@
"dependencies": {
"@pantheon-systems/pcc-sdk-core": "latest",
"axios": "^1.4.0",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
"dayjs": "^1.11.9",
"fs-extra": "^11.1.1",
Expand All @@ -48,6 +50,7 @@
"octokit": "^2.1.0",
"open": "^9.1.0",
"ora": "^6.3.1",
"package-json": "^8.1.1",
"server-destroy": "^1.0.1",
"yargs": "^17.7.2"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import checkUpdate from "../lib/checkUpdate";
import { generatePreviewLink } from "./commands/documents";
import init from "./commands/init";
import login from "./commands/login";
Expand All @@ -16,9 +17,17 @@
import { createToken, listTokens, revokeToken } from "./commands/token";
import printWhoAmI from "./commands/whoAmI";

const INSIDE_TEST = process.env.NODE_ENV === "test";

const configureMiddleware = (func: () => void) => {
if (INSIDE_TEST) return () => {};

Check failure on line 23 in packages/cli/src/cli/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected empty arrow function
return func;
};

yargs(hideBin(process.argv))
.scriptName("pcc")
.usage("$0 <cmd>")
.middleware(configureMiddleware(checkUpdate))
.strictCommands()
.demandCommand()
.command(
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/src/lib/checkUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from "fs";
import { dirname } from "path";
import { fileURLToPath } from "url";
import boxen from "boxen";
import chalk from "chalk";
import pkgJson from "package-json";
import semver from "semver";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const checkUpdate = async () => {
const { name, version } = JSON.parse(
fs.readFileSync(__dirname + "/../package.json").toString(),
);
const { version: latestVersion } = await pkgJson(name);

const updateAvailable = semver.lt(version, latestVersion as string);
if (updateAvailable) {
const msg = {
updateAvailable: `Update available! ${chalk.dim(version)} → ${chalk.green(
latestVersion,
)}.`,
runUpdate: `Run ${chalk.cyan(`npm i -g ${name}`)} to update.`,
};

console.log(
boxen(`${msg.updateAvailable}\n${msg.runUpdate}`, {
margin: 1,
padding: 1,
align: "center",
}),
);
}
};

export default checkUpdate;
42 changes: 37 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading