<file_sep>// requires
var bluebird = require('bluebird'),
	glob = require('glob');<file_sep>// requires
var bluebird = require('bluebird');<file_sep>var pJson = require('../package.json'),
    core = require('./core'),
    watch = require('node-watch'),
    installing = false;

function throwError(err) {
    console.log(core.colorizer('error', 'Ranza: ' + err ));
}

var Ranza = new Function();

Ranza.prototype.enableLogs = function(mode) {
    this.logs = (typeof(mode) === 'boolean' ? mode : false);
}

Ranza.prototype.setDebug = function(debug, done, fn) {
    this.debug = (typeof(debug) === 'undefined' ? true : debug);
}

Ranza.prototype.default = function() {
    return core.reader('/../docs/default.md');
}

Ranza.prototype.version = function() {
    return ('Ranza version: ' + pJson.version);
}

Ranza.prototype.help = function() {
    return core.reader('/../docs/help.md');
}

Ranza.prototype.watch = function(install) {
    var save = install || false;
    return core.watcher('/').spread(function(files, root){
        var searcher = core.searcher(files),
            paths = searcher['validPaths'];

        console.log(core.colorizer('success', '\nlivereload...\n'));
        watch(paths, function(filename) {
            var searcher = core.searcher(files),
                requires = core.formater(searcher['requires']);

            if (installing) return;
            installing = true;
            return core.compare(root, requires).spread(function(diffs) {
                if (diffs.length <= 0) {
                    installing = false;
                    return console.log(core.colorizer('message', 'Ranza says: There is no unidentified requires!\n'));
                }

                return core.manager('install', root, diffs, save).then(function() {
                    installing = false;
                });
            });
        });
    }).catch(throwError);
}

Ranza.prototype.status = function(fn) {
    var self = this;
    return core.watcher('/').spread(function(files, root){
        var searcher = core.searcher(files),
            requires = core.formater(searcher['requires']);
        return core.compare(root, requires, {debug: self.debug, where: searcher.where}).spread(function(diffs, unused, reqs, log) {
            if (self.logs) console.log(log + '\n');
            
            var status = {
                undefinedUsed: diffs, 
                definedUnused: unused, 
                definedUsed: reqs
            };
                        
            return ((typeof(fn) === 'function')? fn(status) : status);
        });
    }).catch(throwError);
}

Ranza.prototype.install = function(install) {
    var save = install || false;

    core.asker(function(yes) {
        if (!yes) process.exit();
        return core.watcher('/').spread(function(files, root){
            var searcher = core.searcher(files),
                requires = core.formater(searcher['requires']);

            return core.manager('install', root, requires, save, true);
        }).catch(throwError);
    });
}

Ranza.prototype.clean = function(removeType) {
    var save = removeType || false;
    core.asker(function(yes) {
        if (!yes) process.exit();
        return core.watcher('/').spread(function(files, root){
            var searcher = core.searcher(files),
                requires = core.formater(searcher['requires']);

            return core.compare(root, requires).spread(function(diffs, unused) {
                if (unused.length <= 0) 
                    return console.log(core.colorizer('message', '\nRanza says: There is no unused dependencies!\n'));

                return core.manager('remove', root, unused, save, true);
            });
        }).catch(throwError);
    });
}

module.exports = new Ranza();
<file_sep># Ranza

> The npm's butler

[![NPM Version](https://img.shields.io/npm/v/express.svg?style=flat)](https://www.npmjs.org/package/ranza)
[![Build Status](https://travis-ci.org/raphamorim/ranza.svg)](https://travis-ci.org/raphamorim/ranza)

Quickly spot any dependency required in the project and not listed in `package.json`. And also the other way around: quickly remove listed dependencies that are being used.

Run whenever you need or use the `watch` command to install dependencies automatically as you write `require`s on your project.

A simple **status** example:

![Ranza Status](docs/images/status.png)

## Install

With [node](http://nodejs.org/) and [npm](https://www.npmjs.org/) installed, install ranza with a single command:

```sh
$ npm install -g ranza
```

## Usage

#### Status

Checks all project for required dependencies and confirms if they are listed on `package.json`:

```sh
$ ranza status
```

You can use status with debug option as arguments, to best view requires status showing the occurrence files, ex:

**input:**

```sh
$ ranza status --debug
```

**some output example:**

```sh
Defined and used:
  • babel-core
     => lib/new.js

  • bluebird
     => core/src/comparer.js
     => core/src/manager.js
     => core/src/sentinel.js

Defined, but unused:
  • grunt
  • babel
```

#### Install

Installs all dependencies required throughout the project, but do not save them in `package.json`:

```sh
$ ranza install
```

Installs all dependencies required throughout the project and add them to `package.json` as `dependencies`:

```sh
$ ranza install --save
```

Installs all dependencies required throughout the project and save them in `package.json` as `devDependencies`:

```sh
$ ranza install --save-dev
```

#### Clean

Remove and clean all unused dependencies from `package.json`:

```sh
$ ranza clean
```

#### Watch (only for HARDCORE developers)

**The not recommended way:**

> Installs every single dependencies in each livereload

Livereload in all files, installing undefined dependencies without saving them in `package.json`:

```sh
$ ranza watch
```

**The recommended way:**

> Installs only missed dependencies in each livereload

Livereload in all files, installing undefined dependencies and saving them as `dependencies` in `package.json`:

```sh
$ ranza watch --save
```

Livereload in all files, installing undefined dependencies and saving them as `devDependencies` in `package.json`:

```sh
$ ranza watch --save-dev
```

Example:

![Ranza Watch](docs/images/watch.gif)

## History

See [Changelog](docs/changelog.md) for more details.

## Contributing

Don't be shy, send a Pull Request! Here is how:

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -m 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## About

**License:** MIT ® [<NAME>](https://github.com/raphamorim)
<file_sep>var assert = require('assert'),
	Promises = require('bluebird'),
	Ranza = Promises.promisifyAll(require('../index.js')),
	realCwd = process.cwd();

function fakeCwd(path) {
	process.cwd = function() {
		return realCwd + '/test/fixtures/' + path;
	};
}

describe('Status', function() {
	context('apply status command', function() {
		context('on a project without missed requires and undefined dependencies', function() {
			it('should return only the used dependencies and a empty unused/undefined list', function(done) {
				fakeCwd('perfectDependencies');

				Ranza.status(function(status) {
					assert.deepEqual(typeof(status), 'object');
					assert.deepEqual(status.undefinedUsed, []);
					assert.deepEqual(status.definedUnused, []);
					assert.deepEqual(status.definedUsed, ['bluebird', 'glob']);
					done();
				});
			})
		})
		context('on a project with unused dependencies', function() {
			it('should return the unused dependencies', function(done) {
				fakeCwd('unusedDependecy');

				Ranza.status(function(status) {
					assert.deepEqual(typeof(status), 'object');
					assert.deepEqual(status.undefinedUsed, []);
					assert.deepEqual(status.definedUnused, ['kenobi', 'glob']);
					assert.deepEqual(status.definedUsed, ['bluebird']);
					done();
				});
			})
		})
	});
});
