From 285d5f74379134811fe1ae196612ad6c0cf62a80 Mon Sep 17 00:00:00 2001 From: Charlike Mike Reagent Date: Sat, 3 Nov 2018 12:31:37 +0200 Subject: [PATCH] fix: update deps Signed-off-by: Charlike Mike Reagent --- package.json | 2 +- src/index.js | 34 +++++++++++++++++++++++----------- yarn.lock | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 31c8e2f..2bc27c3 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "dependencies": { "@tunnckocore/execa": "^2.1.1", "esm": "^3.0.84", - "is-installed-globally": "^0.1.0", + "global-dirs": "^0.1.1", "update-notifier": "^2.5.0" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 0211bcc..b1c623d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ -// import proc from 'process'; +import fs from 'fs'; +import path from 'path'; import unpdateNotifier from 'update-notifier'; -import isInstalledGlobally from 'is-installed-globally'; import { exec } from '@tunnckocore/execa'; +import globalDirs from 'global-dirs'; export default function updater(options) { const opts = Object.assign({}, options); @@ -14,7 +15,7 @@ export default function updater(options) { if (err) { throw err; } - if (info.type !== 'latest') { + if (isInstalledGlobally(opts.pkg) && info.type !== 'latest') { autoupdate(opts.pkg, opts.manager); } }; @@ -22,25 +23,36 @@ export default function updater(options) { return unpdateNotifier(opts); } +function isInstalledGlobally({ name }) { + /* eslint-disable no-restricted-syntax */ + + let exists = false; + for (const [, dirs] of Object.entries(globalDirs)) { + for (const [, globalPath] of Object.entries(dirs)) { + const fp = path.join(globalPath, name); + if (fs.existsSync(fp)) { + exists = true; + break; + } + } + } + return exists; +} + async function autoupdate(pkg, manager) { const isNpm = manager === 'npm'; if (isNpm || manager === 'pnpm') { - await exec( - `${isNpm ? 'npm' : 'pnpm'} install ${ - isInstalledGlobally ? '--global' : '' - } ${pkg.name}`, - ); + await exec(`${isNpm ? 'npm' : 'pnpm'} install --global ${pkg.name}`); } if (manager === 'yarn') { - const g = isInstalledGlobally ? 'global' : ''; await exec([ // ensure it is cleanest one - `yarn ${g} remove ${pkg.name}`, + `yarn global remove ${pkg.name}`, // install it after that ensurance - `yarn ${g} add ${pkg.name}`, + `yarn global add ${pkg.name}`, ]); } } diff --git a/yarn.lock b/yarn.lock index fd74d22..011c113 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1604,7 +1604,7 @@ glob@^7.0.3, glob@^7.0.5, glob@^7.1.2, glob@^7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" -global-dirs@^0.1.0: +global-dirs@^0.1.0, global-dirs@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-0.1.1.tgz#b319c0dd4607f353f3be9cca4c72fc148c49f445" integrity sha1-sxnA3UYH81PzvpzKTHL8FIxJ9EU=