Skip to content

Commit

Permalink
fix: update deps
Browse files Browse the repository at this point in the history
Signed-off-by: Charlike Mike Reagent <[email protected]>
  • Loading branch information
Charlike Mike Reagent committed Nov 3, 2018
1 parent 92ba73f commit 285d5f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
34 changes: 23 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -14,33 +15,44 @@ 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);
}
};

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}`,
]);
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 285d5f7

Please sign in to comment.