Skip to content

Commit

Permalink
feat: change version command to give update information (cloudflare#1498
Browse files Browse the repository at this point in the history
)

* feat: change version command to give update information

* Prettify code

* Conform to prettier style in .changeset

* Add resiliance to snapshot against version number changing

* Conform to prettier style

* Read version from package.json, increase test coverage

* Remove unneeded helper function

* bump a fixture version to generate a new npm cache on CI

Co-authored-by: Cameron Robey <[email protected]>
Co-authored-by: Cameron Robey <[email protected]>
Co-authored-by: Sunil Pai <[email protected]>
  • Loading branch information
4 people authored Jul 20, 2022
1 parent e09abbc commit fe3fbd9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
10 changes: 10 additions & 0 deletions .changeset/curly-houses-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

feat: change version command to give update information
When running version command, we want to display update information if current version is not up to date. Achieved by replacing default output with the wrangler banner.
Previous behaviour (just outputting current version) reamins when !isTTY.
Version command changed from inbuilt .version() from yargs, to a regular command to allow for asynchronous behaviour.

Implements https://github.com/cloudflare/wrangler2/issues/1492
28 changes: 14 additions & 14 deletions fixtures/worker-app/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "worker-app",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "npx jest"
},
"author": "",
"license": "ISC",
"private": true,
"jest": {
"testRegex": ".*.test\\.[jt]sx?$",
"testEnvironment": "wrangler"
}
"name": "worker-app",
"version": "1.0.1",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"main": "src/index.js",
"scripts": {
"test": "npx jest"
},
"jest": {
"testEnvironment": "wrangler",
"testRegex": ".*.test\\.[jt]sx?$"
}
}
2 changes: 1 addition & 1 deletion package-lock.json

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

35 changes: 35 additions & 0 deletions packages/wrangler/src/__tests__/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { version } from "./../../package.json";
import { mockConsoleMethods } from "./helpers/mock-console";
import { useMockIsTTY } from "./helpers/mock-istty";
import { runWrangler } from "./helpers/run-wrangler";

describe("version", () => {
const std = mockConsoleMethods();
const { setIsTTY } = useMockIsTTY();

// We cannot test output of version banner,
// as it is disabled in jest environments

// it("should output version banner", async () => {
// await runWrangler("-v");
// expect(std.out).toMatchInlineSnapshot(`
// " ⛅️ wrangler 2.0.22
// --------------------"
// `);
// });

it("should output current version if !isTTY calling -v", async () => {
setIsTTY(false);

await runWrangler("-v");
expect(std.out).toMatch(version);
});

// This run separately as command handling is different
it("should output current version if !isTTY calling --version", async () => {
setIsTTY(false);

await runWrangler("--version");
expect(std.out).toMatch(version);
});
});
40 changes: 37 additions & 3 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,22 @@ function createCLIParser(argv: string[]) {
["*"],
false,
() => {},
(args) => {
async (args) => {
if (args._.length > 0) {
throw new CommandLineArgsError(`Unknown command: ${args._}.`);
} else {
wrangler.showHelp("log");
// args.v will exist and be true in the case that no command is called, and the -v
// option is present. This is to allow for running asynchronous printWranglerBanner
// in the version command.
if (args.v) {
if (process.stdout.isTTY) {
await printWranglerBanner();
} else {
logger.log(wranglerVersion);
}
} else {
wrangler.showHelp("log");
}
}
}
);
Expand Down Expand Up @@ -1905,6 +1916,29 @@ function createCLIParser(argv: string[]) {
}
);

// version
wrangler.command(
"version",
false,
() => {},
async () => {
if (process.stdout.isTTY) {
await printWranglerBanner();
} else {
logger.log(wranglerVersion);
}
}
);

wrangler.option("v", {
describe: "Show version number",
alias: "version",
type: "boolean",
});

// This set to false to allow overwrite of default behaviour
wrangler.version(false);

wrangler.option("config", {
alias: "c",
describe: "Path to .toml configuration file",
Expand All @@ -1914,7 +1948,7 @@ function createCLIParser(argv: string[]) {

wrangler.group(["config", "help", "version"], "Flags:");
wrangler.help().alias("h", "help");
wrangler.version(wranglerVersion).alias("v", "version");

wrangler.exitProcess(false);

return wrangler;
Expand Down

0 comments on commit fe3fbd9

Please sign in to comment.