#!/usr/bin/env node

var program = require('commander');
var updateNotifier = require('update-notifier');
var cint = require('cint');
var ncu = require('../lib/npm-check-updates');
var pkg = require('../package.json');

// check if a new version of ncu is available and print an update notification
updateNotifier({pkg: pkg})
    .notify({defer: false});

program
    .description('[filter] is a list or regex of package names to check (all others will be ignored).')
    .usage('[options] [filter]')
    .option('-d, --dev', 'check only devDependencies')
    .option('-e, --error-level <n>', 'set the error-level. 1: exits with error code 0 if no errors occur. 2: exits with error code 0 if no packages need updating (useful for continuous integration). Default is 1.', cint.partialAt(parseInt, 1, 10), 1)
    .option('-f, --filter <matches>', 'include only package names matching the given string, comma-delimited list, or regex')
    .option('-g, --global', 'check global packages instead of in the current project')
    // program.json is set to true in programInit if any options that begin with 'json' are true
    .option('-j, --jsonAll', 'output new package file instead of human-readable message')
    .option('--jsonUpgraded', 'output upgraded dependencies in json')
    .option('-l, --loglevel <n>', 'what level of logs to report: silent, error, minimal, warn, info, verbose, silly (default: warn)', 'warn')
    .option('-m, --packageManager <name>', 'npm (default) or bower', 'npm')
    .option('-n, --newest', 'find the newest versions available instead of the latest stable versions')
    .option('-o, --optional', 'check only optionalDependencies')
    .option('--packageData', 'include stringified package file (use stdin instead)')
    .option('--packageFile <filename>', 'package file location (default: ./package.json)')
    .option('--packageFileDir', 'use same directory as packageFile to compare against installed modules. See #201.')
    .option('-p, --prod', 'check only dependencies (not devDependencies)')
    .option('-r, --registry <url>', 'specify third-party npm registry')
    .option('-s, --silent', "don't output anything (--loglevel silent)")
    .option('-t, --greatest', 'find the highest versions available instead of the latest stable versions')
    .option('--timeout <ms>', 'a global timeout in ms')
    .option('-u, --upgrade', 'overwrite package file')
    .option('-x, --reject <matches>', 'exclude packages matching the given string, comma-delimited list, or regex')
    .option('-a, --upgradeAll', 'include even those dependencies whose latest version satisfies the declared semver dependency')
    .option('--semverLevel <level>', 'find the highest version within "major" or "minor"')
    .option('--removeRange', 'remove version ranges from the final package version')
    .option('-v, --version', pkg.version, function() { console.log(pkg.version); process.exit(0); })
    .option('-V', '', function() { console.log(pkg.version); process.exit(0); });

program.parse(process.argv);

program.cli = true;
program.filter = program.args.join(' ') || program.filter;

ncu.run(program);
