Skip to content

Commit

Permalink
feat: implement
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 cc0d85e commit db17bdb
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 29 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"node": "^8.11.0 || >=10.13.0"
},
"dependencies": {
"esm": "^3.0.84"
"@tunnckocore/execa": "^2.1.1",
"esm": "^3.0.84",
"is-installed-globally": "^0.1.0",
"update-notifier": "^2.5.0"
},
"devDependencies": {
"@tunnckocore/config": "^0.5.1",
Expand Down
47 changes: 46 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
export default () => {};
// import proc from 'process';
import unpdateNotifier from 'update-notifier';
import isInstalledGlobally from 'is-installed-globally';
import { exec } from '@tunnckocore/execa';

export default async function updater(options) {
const opts = Object.assign({}, options);

if (!opts.pkg.name || !opts.pkg.version) {
throw new Error('unpdateNotifier: pkg.name and pkg.version are required');
}

opts.callback = (err, info) => {
if (err) {
throw err;
}
if (info.type !== 'latest') {
autoupdate(opts.pkg, opts.manager);
}
};

return unpdateNotifier(opts);
}

async function autoupdate(pkg, manager) {
const isNpm = manager === 'npm';

if (isNpm || manager === 'pnpm') {
await exec(
`${isNpm ? 'npm' : 'pnpm'} install ${
isInstalledGlobally ? '--global' : ''
} ${pkg.name}`,
);
}

if (manager === 'yarn') {
const g = isInstalledGlobally ? 'global' : '';
await exec([
// ensure it is cleanest one
`yarn ${g} remove ${pkg.name}`,

// install it after that ensurance
`yarn ${g} add ${pkg.name}`,
]);
}
}
1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import test from 'asia';
import mod from '../src';

Expand Down
1 change: 1 addition & 0 deletions test/snapshots/index.snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"todo":{"skip":false,"todo":false,"run":false,"isPending":false,"isRejected":false,"isFulfilled":true,"id":1,"str":"(t) => {\n t.strictEqual(typeof mod, 'function');\n}","title":"todo"}}
Loading

0 comments on commit db17bdb

Please sign in to comment.