#! /usr/bin/env node

/**
 * The script in FIX mode (--fix) updates versions in the packages.json file to those found in package-lock.json file
 * to CHECK if have never versions installed and locked add to your scripts
 *  "check-packages": "check-packages"
 * to CHECK and FIX add:
 *  "check-packages": "check-packages --fix"
 * and run `npm run check-packages`
 *
 **/

'use strict';

const commandLineArgs = require('command-line-args');
const path = require('path');
const rootPath = __dirname.split('node_modules')[0];
const checkAndUpdatePackagesVersions = require('./functions/checkAndUpdatePackagesVersions');

const optionDefinitions = [
  { name: 'fix', alias: 'f',  type: Boolean },
];
const options = commandLineArgs(optionDefinitions);

try {
  const pkg = require(path.join(rootPath, 'package.json'));
  const lockPkg = require(path.join(rootPath, 'package-lock.json'));
  
  checkAndUpdatePackagesVersions(pkg, lockPkg, options);
} catch (error) {
  console.error(error);
}