From 4883c8509d9d95eba59e15aed50644d20b7cfdeb Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 1 Jan 2022 12:03:43 +0000 Subject: [PATCH 1/6] :sparkles: Improve the way global state is shared. --- actions/auth/login.js | 4 ++-- actions/auth/logout.js | 4 +++- actions/auth/register.js | 4 +++- actions/packages/unload.js | 2 +- actions/publish.js | 4 +--- jscrates.js | 17 +++++------------ lib/base-command.js | 10 ++++++++++ 7 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 lib/base-command.js diff --git a/actions/auth/login.js b/actions/auth/login.js index 78613d8..a101a45 100644 --- a/actions/auth/login.js +++ b/actions/auth/login.js @@ -6,8 +6,8 @@ import { promptCredentials } from '../../utils/prompts/index.js' import { logError, logSuccess } from '../../utils/loggers.js' function login(config) { - return async function (_, command) { - const store = command.__store + return async function (_, options) { + const store = options.parent.appState const spinner = Spinner('Authenticating') try { diff --git a/actions/auth/logout.js b/actions/auth/logout.js index c513476..35b5dff 100644 --- a/actions/auth/logout.js +++ b/actions/auth/logout.js @@ -8,8 +8,10 @@ export default function logout(config) { const prompt = createPrompt() return async function (_, options) { + const store = options.parent.appState + // There was no user in the first place. - if (!options.__store.isAuthed) { + if (!store.isAuthed) { logWarn('You are not logged in.') return process.exit(0) } diff --git a/actions/auth/register.js b/actions/auth/register.js index 0a1537e..92c463f 100644 --- a/actions/auth/register.js +++ b/actions/auth/register.js @@ -10,8 +10,10 @@ function register(config) { const spinner = Spinner('Registering a new user') return async function (_, options) { + const store = options.parent.appState + try { - if (options.__store.isAuthed) { + if (store.isAuthed) { if ( !(await confirmationPrompt( `It seems like you are already logged in`, diff --git a/actions/packages/unload.js b/actions/packages/unload.js index 588cad8..f2a35b6 100644 --- a/actions/packages/unload.js +++ b/actions/packages/unload.js @@ -45,7 +45,7 @@ const getTarballName = (tarballURL) => { async function unloadPackages(packages, ...args) { // Since we are accepting variadic arguments, other arguments can only // be accessing by spreading them. - const store = args[1].__store + const store = args[1].parent.appState const spinner = Spinner(`Downloading packages`) try { diff --git a/actions/publish.js b/actions/publish.js index 21d0284..5a52631 100644 --- a/actions/publish.js +++ b/actions/publish.js @@ -28,7 +28,7 @@ const createTempTarDirIfNotExists = () => { * Action to publish a package to JSCrates repository. */ async function publishPackage(_, options) { - const store = options.__store + const store = options.parent.appState const spinner = Spinner('Publishing package') try { @@ -119,8 +119,6 @@ async function publishPackage(_, options) { } logError(error) - } finally { - return process.exit(1) } } diff --git a/jscrates.js b/jscrates.js index 8aa1061..1d58bdb 100644 --- a/jscrates.js +++ b/jscrates.js @@ -1,9 +1,9 @@ #!/usr/bin/env node -import { Command } from 'commander' import Configstore from 'configstore' import checkOnlineStatus from 'is-online' +import BaseCommand from './lib/base-command.js' import { CONFIG_FILE } from './lib/constants.js' import unloadPackages from './actions/packages/unload.js' import publishPackage from './actions/publish.js' @@ -13,23 +13,16 @@ import logout from './actions/auth/logout.js' async function jscratesApp() { const isOnline = await checkOnlineStatus() - const program = new Command() - const configStore = new Configstore(CONFIG_FILE, { - createdAt: Date.now(), - }) - const appState = { + const configStore = new Configstore(CONFIG_FILE) + const program = new BaseCommand({ isOnline, isAuthed: configStore.has('auth.token'), - } + }) program .name('jscrates') .description(`Welcome to JSCrates 📦, yet another package manager for Node`) - // TODO: Find a way to read version build time. - .version('0.0.0-alpha', '-v, --version', 'display current version') - .hook('preAction', (_, actionCommand) => { - actionCommand['__store'] = appState - }) + .version('v2.6.2', '-v, --version', 'Display installed version of JSCrates') program .command('login') diff --git a/lib/base-command.js b/lib/base-command.js new file mode 100644 index 0000000..ee66fba --- /dev/null +++ b/lib/base-command.js @@ -0,0 +1,10 @@ +// @ts-check + +import { Command } from 'commander' + +export default class BaseCommand extends Command { + constructor(appState) { + super(appState) + this.appState = appState + } +} From 9eecc4f7b75816d9e5483329d8021001fbf8ce93 Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 1 Jan 2022 12:03:57 +0000 Subject: [PATCH 2/6] :bookmark: v2.6.3-beta.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b24fb9f..50d1bb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jscrates/cli", - "version": "2.6.2", + "version": "2.6.3-beta.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@jscrates/cli", - "version": "2.6.2", + "version": "2.6.3-beta.1", "dependencies": { "axios": "^0.24.0", "chalk": "^4.1.2", diff --git a/package.json b/package.json index 13de78b..059dcad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jscrates/cli", - "version": "2.6.2", + "version": "2.6.3-beta.1", "description": "Official CLI client for JSCrates.", "main": "jscrates.js", "author": "Team JSCrates", From 716e8a85589ebd9ee5b72892e61962422488342f Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:26:32 +0000 Subject: [PATCH 3/6] :art: Modify `createCommand` to pass `appState` --- lib/base-command.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/base-command.js b/lib/base-command.js index ee66fba..e8eda87 100644 --- a/lib/base-command.js +++ b/lib/base-command.js @@ -7,4 +7,12 @@ export default class BaseCommand extends Command { super(appState) this.appState = appState } + + // To make properties available in all the subcommands. + // https://github.com/tj/commander.js/issues/1666#issuecomment-1003690872 + createCommand(name) { + const cmd = new BaseCommand(this.appState) + cmd.name(name) + return cmd + } } From 9e5b23fd9761197394a7381931b1360e16e19de6 Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:27:17 +0000 Subject: [PATCH 4/6] :art: Use `appState` from options. --- actions/auth/login.js | 2 +- actions/auth/logout.js | 2 +- actions/auth/register.js | 2 +- actions/packages/unload.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/actions/auth/login.js b/actions/auth/login.js index a101a45..99fa05a 100644 --- a/actions/auth/login.js +++ b/actions/auth/login.js @@ -7,7 +7,7 @@ import { logError, logSuccess } from '../../utils/loggers.js' function login(config) { return async function (_, options) { - const store = options.parent.appState + const store = options.appState const spinner = Spinner('Authenticating') try { diff --git a/actions/auth/logout.js b/actions/auth/logout.js index 35b5dff..866120f 100644 --- a/actions/auth/logout.js +++ b/actions/auth/logout.js @@ -8,7 +8,7 @@ export default function logout(config) { const prompt = createPrompt() return async function (_, options) { - const store = options.parent.appState + const store = options.appState // There was no user in the first place. if (!store.isAuthed) { diff --git a/actions/auth/register.js b/actions/auth/register.js index 92c463f..498d58a 100644 --- a/actions/auth/register.js +++ b/actions/auth/register.js @@ -10,7 +10,7 @@ function register(config) { const spinner = Spinner('Registering a new user') return async function (_, options) { - const store = options.parent.appState + const store = options.appState try { if (store.isAuthed) { diff --git a/actions/packages/unload.js b/actions/packages/unload.js index f2a35b6..6d44f84 100644 --- a/actions/packages/unload.js +++ b/actions/packages/unload.js @@ -45,7 +45,7 @@ const getTarballName = (tarballURL) => { async function unloadPackages(packages, ...args) { // Since we are accepting variadic arguments, other arguments can only // be accessing by spreading them. - const store = args[1].parent.appState + const store = args[1].appState const spinner = Spinner(`Downloading packages`) try { From 8679dbab1f5b3b3e4aa5f3d877e09ea79aa29d0f Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:27:54 +0000 Subject: [PATCH 5/6] :recycle: Move publish to packages. --- actions/{ => packages}/publish.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename actions/{ => packages}/publish.js (96%) diff --git a/actions/publish.js b/actions/packages/publish.js similarity index 96% rename from actions/publish.js rename to actions/packages/publish.js index 5a52631..e79abf3 100644 --- a/actions/publish.js +++ b/actions/packages/publish.js @@ -9,8 +9,8 @@ import { globby } from 'globby' import chalk from 'chalk' import tar from 'tar' -import api from '../lib/api/index.js' -import { logError, logInfo, logSuccess } from '../utils/loggers.js' +import api from '../../lib/api/index.js' +import { logError, logInfo, logSuccess } from '../../utils/loggers.js' //? The tarball created in the process will be placed in the OS's temp directory. // TODO: Do we need to clean this directory post publishing? @@ -28,7 +28,7 @@ const createTempTarDirIfNotExists = () => { * Action to publish a package to JSCrates repository. */ async function publishPackage(_, options) { - const store = options.parent.appState + const store = options.appState const spinner = Spinner('Publishing package') try { From c9e4c4574e14b82832d1f65f65e33df0a4b6b96f Mon Sep 17 00:00:00 2001 From: "D. Kasi Pavan Kumar" <44864604+kasipavankumar@users.noreply.github.com> Date: Sat, 15 Jan 2022 13:28:49 +0000 Subject: [PATCH 6/6] :bookmark: v2.6.3 --- jscrates.js | 4 ++-- package-lock.json | 16 ++++++++-------- package.json | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/jscrates.js b/jscrates.js index 1d58bdb..1108e8e 100644 --- a/jscrates.js +++ b/jscrates.js @@ -6,7 +6,7 @@ import checkOnlineStatus from 'is-online' import BaseCommand from './lib/base-command.js' import { CONFIG_FILE } from './lib/constants.js' import unloadPackages from './actions/packages/unload.js' -import publishPackage from './actions/publish.js' +import publishPackage from './actions/packages/publish.js' import login from './actions/auth/login.js' import register from './actions/auth/register.js' import logout from './actions/auth/logout.js' @@ -22,7 +22,7 @@ async function jscratesApp() { program .name('jscrates') .description(`Welcome to JSCrates 📦, yet another package manager for Node`) - .version('v2.6.2', '-v, --version', 'Display installed version of JSCrates') + .version('v2.6.3', '-v, --version', 'Display installed version of JSCrates') program .command('login') diff --git a/package-lock.json b/package-lock.json index 50d1bb4..3ad2051 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jscrates/cli", - "version": "2.6.3-beta.1", + "version": "2.6.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@jscrates/cli", - "version": "2.6.3-beta.1", + "version": "2.6.3", "dependencies": { "axios": "^0.24.0", "chalk": "^4.1.2", @@ -478,9 +478,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.14.5", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", - "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==", + "version": "1.14.7", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz", + "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==", "funding": [ { "type": "individual", @@ -1738,9 +1738,9 @@ } }, "follow-redirects": { - "version": "1.14.5", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", - "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==" + "version": "1.14.7", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz", + "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==" }, "form-data": { "version": "4.0.0", diff --git a/package.json b/package.json index 059dcad..18b9e4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jscrates/cli", - "version": "2.6.3-beta.1", + "version": "2.6.3", "description": "Official CLI client for JSCrates.", "main": "jscrates.js", "author": "Team JSCrates",