Skip to content

Commit

Permalink
Made to work with rimraf and npm.commands
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiopro committed Nov 10, 2015
1 parent d56e061 commit c545d7d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"esnext": true
"esnext": true,
"node": true
}
21 changes: 21 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# gluttony

Nukes the project's `node_modules` directory and reinstalls latest dependencies from `package.json`.

# Usage

Install

```bash
npm install -g gluttony
```

Use to reinstall dependencies of a project

```bash
gluttony
```

# License

MIT
53 changes: 51 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
'use strict';

const pjson = require('pjson'),
path = require('path'),
fs = require('fs'),
rimraf = require('rimraf'),
npm = require('npm'),
keys = Object.keys || require('object-keys');

console.log(__dirname);
console.log(keys(pjson.dependencies));
const NODE_MODULES = path.resolve('./node_modules'),
DEPS = pjson.dependencies && keys(pjson.dependencies),
DEV_DEPS = pjson.devDependencies && keys(pjson.devDependencies);

function reinstall() {
npm.load({}, function (err) {
if (err) {
console.log(err);
return;
}
[DEPS, DEV_DEPS].forEach(function(deps){
if (deps) {
npm.commands.install(deps, function (err, data) {
if (err) {
return;
}
console.log('Yay! Dependencies reinstalled from scratch.');
});
}
});
});
}

fs.stat(NODE_MODULES, function(err, stats) {
if (err) {
switch (err.code) {
case 'ENOENT':
// node_modules doesn't exist: just reinstall
reinstall();
break;
}
return;
}
// call rm -rf on the NODE_MODULES dir
rimraf(NODE_MODULES, function(err) {
if (err) {
// switch (err.code) {
// case 'ENOENT':
// console.log('');
// break;
// }
console.log(err);
return;
}
reinstall();
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"license": "MIT",
"dependencies": {
"object-keys": "^1.0.9",
"pjson": "^1.0.7"
"path": "^0.12.7",
"pjson": "^1.0.7",
"rimraf": "^2.4.3"
}
}

0 comments on commit c545d7d

Please sign in to comment.