From ad8ed1b99f7756e912339ba0b9ad17fb10c8981f Mon Sep 17 00:00:00 2001 From: LitoMore Date: Fri, 10 May 2019 16:16:06 +0800 Subject: [PATCH] Suggest yarn when installed with yarn (#132) --- index.js | 15 ++++++++++++++- package.json | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a24f036..50425ac 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,8 @@ const semverDiff = importLazy('semver-diff'); const latestVersion = importLazy('latest-version'); const isNpm = importLazy('is-npm'); const isInstalledGlobally = importLazy('is-installed-globally'); +const isYarnGlobal = importLazy('is-yarn-global'); +const hasYarn = importLazy('has-yarn'); const boxen = importLazy('boxen'); const xdgBasedir = importLazy('xdg-basedir'); const isCi = importLazy('is-ci'); @@ -126,11 +128,22 @@ class UpdateNotifier { options = { isGlobal: isInstalledGlobally(), + isYarnGlobal: isYarnGlobal()(), ...options }; + let installCommand; + + if (options.isYarnGlobal) { + installCommand = `yarn global add ${this.packageName}`; + } else if (hasYarn()()) { + installCommand = `yarn add ${this.packageName}`; + } else { + installCommand = `npm i ${options.isGlobal ? '-g ' : ''}${this.packageName}`; + } + options.message = options.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') + - chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (options.isGlobal ? '-g ' : '') + this.packageName) + ' to update'; + chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan(installCommand) + ' to update'; options.boxenOpts = options.boxenOpts || { padding: 1, diff --git a/package.json b/package.json index ce1625c..2db3d0a 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,12 @@ "boxen": "^3.0.0", "chalk": "^2.0.1", "configstore": "^4.0.0", + "has-yarn": "^2.1.0", "import-lazy": "^2.1.0", "is-ci": "^2.0.0", "is-installed-globally": "^0.1.0", "is-npm": "^3.0.0", + "is-yarn-global": "^0.3.0", "latest-version": "^5.0.0", "semver-diff": "^2.0.0", "xdg-basedir": "^3.0.0"