From 867f97278e098d8ab9be449c3b0db04689ade4d8 Mon Sep 17 00:00:00 2001 From: Luca Lanziani Date: Thu, 22 Jul 2021 08:12:11 +0200 Subject: [PATCH 1/3] Add websites commands --- bin/cli.js | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 bin/cli.js diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 0000000..6fb840c --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,42 @@ +#!/usr/bin/env node + +const yargs = require("yargs"); +const checkKit = require("check-kit"); +const open = require("open"); + +const locations = { + website: "http://romajs.org", + twitter: "https://twitter.com/roma_js", + facebook: "https://www.facebook.com/romajs.org", + youtube: "https://www.youtube.com/channel/UCFm8OPi5USbFybw9SaTLxeA", + slack: "https://romajs.slack.com/", + "slack-signup": "https://romajs.herokuapp.com/", + github: "https://github.com/Roma-JS", + meetup: "http://www.meetup.com/RomaJS/", +}; + +(async () => { + const { current, name, latest, updateAvailable } = await checkKit.check({ + pkg: require("../package.json"), + }); + + if (updateAvailable) { + console.log(`New version of ${name} available! ${current} -> ${latest}`); + } else { + for (const [name, url] of Object.entries(locations)) { + yargs.command({ + command: name, + desc: `Open RomaJS ${name}`, + handler: async () => { + await open(url); + }, + }); + } + yargs + .wrap(yargs.terminalWidth()) + .showHelpOnFail(true) + .demandCommand() + .recommendCommands() + .strict().argv; + } +})(); diff --git a/package.json b/package.json index 1e9d5cc..cf28345 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,10 @@ "bugs": { "url": "https://github.com/Roma-JS/romajs-cli/issues" }, - "homepage": "https://github.com/Roma-JS/romajs-cli#readme" + "homepage": "https://github.com/Roma-JS/romajs-cli#readme", + "dependencies": { + "check-kit": "^1.2.1", + "open": "^8.2.1", + "yargs": "^17.0.1" + } } From 917a8a8aafa18fef37449e69e2074f50ecbbb873 Mon Sep 17 00:00:00 2001 From: Luca Lanziani Date: Thu, 22 Jul 2021 08:15:54 +0200 Subject: [PATCH 2/3] Add bin section to package.json --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index cf28345..7d23e1a 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "bin": { + "romajs": "bin/cli.js" + }, "author": "RomaJS", "license": "MIT", "repository": { From 602bd5f1320a949e0e3ab733ed2aae662fe6b2cf Mon Sep 17 00:00:00 2001 From: Luca Lanziani Date: Sat, 24 Jul 2021 23:46:23 +0200 Subject: [PATCH 3/3] Refactor commands into a single open command --- bin/cli.js | 22 +--------------------- cmds/open.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 cmds/open.js diff --git a/bin/cli.js b/bin/cli.js index 6fb840c..e4b8009 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -2,18 +2,6 @@ const yargs = require("yargs"); const checkKit = require("check-kit"); -const open = require("open"); - -const locations = { - website: "http://romajs.org", - twitter: "https://twitter.com/roma_js", - facebook: "https://www.facebook.com/romajs.org", - youtube: "https://www.youtube.com/channel/UCFm8OPi5USbFybw9SaTLxeA", - slack: "https://romajs.slack.com/", - "slack-signup": "https://romajs.herokuapp.com/", - github: "https://github.com/Roma-JS", - meetup: "http://www.meetup.com/RomaJS/", -}; (async () => { const { current, name, latest, updateAvailable } = await checkKit.check({ @@ -23,16 +11,8 @@ const locations = { if (updateAvailable) { console.log(`New version of ${name} available! ${current} -> ${latest}`); } else { - for (const [name, url] of Object.entries(locations)) { - yargs.command({ - command: name, - desc: `Open RomaJS ${name}`, - handler: async () => { - await open(url); - }, - }); - } yargs + .commandDir("../cmds") .wrap(yargs.terminalWidth()) .showHelpOnFail(true) .demandCommand() diff --git a/cmds/open.js b/cmds/open.js new file mode 100644 index 0000000..395407f --- /dev/null +++ b/cmds/open.js @@ -0,0 +1,30 @@ +"use strict"; +const open = require("open"); + +const locations = { + website: "http://romajs.org", + twitter: "https://twitter.com/roma_js", + facebook: "https://www.facebook.com/romajs.org", + youtube: "https://www.youtube.com/channel/UCFm8OPi5USbFybw9SaTLxeA", + slack: "https://romajs.slack.com/", + slackSignup: "https://romajs.herokuapp.com/", + github: "https://github.com/Roma-JS", + meetup: "http://www.meetup.com/RomaJS/", +}; + +exports.command = "open "; + +exports.desc = `open in a browser one of the following locations ${Object.keys( + locations, +)}`; + +exports.builder = (yargs) => { + yargs.option("location", { + describe: "one of the RomaJS pages", + choices: Object.keys(locations), + }); +}; + +exports.handler = async function (args) { + await open(locations[args.location]); +};