<commit_before>'use strict';

/*
*
* Usage -- see README.md
*
* Normal usage:                node bin/plugins/checkPlugin.js ep_whatever
* Auto fix the things it can:  node bin/plugins/checkPlugin.js ep_whatever autofix
* Auto commit, push and publish(to npm) * highly dangerous:
node bin/plugins/checkPlugin.js ep_whatever autofix autocommit

*/

const fs = require('fs');
const childProcess = require('child_process');

// get plugin name & path from user input
const pluginName = process.argv[2];

if (!pluginName) {
  console.error('no plugin name specified');
  process.exit(1);
}

const pluginPath = `node_modules/${pluginName}`;

console.log(`Checking the plugin: ${pluginName}`);

const optArgs = process.argv.slice(3);

// Should we autofix?
const autoFix = optArgs.indexOf('autofix') !== -1;

// Should we update files where possible?
const autoUpdate = optArgs.indexOf('autoupdate') !== -1;

// Should we automcommit and npm publish?!
const autoCommit = optArgs.indexOf('autocommit') !== -1;

const execSync = (cmd, opts = {}) => (childProcess.execSync(cmd, {
  cwd: `${pluginPath}/`,
  ...opts,
}) || '').toString().replace(/\n+$/, '');

const prepareRepo = () => {
  let branch = execSync('git symbolic-ref HEAD');
  if (branch !== 'refs/heads/master' && branch !== 'refs/heads/main') {
    throw new Error('master/main must be checked out');
  }
  branch = branch.replace(/^refs\/heads\//, '');
  execSync('git rev-parse --verify -q HEAD^0 || ' +
           `{ echo "Error: no commits on ${branch}" >&2; exit 1; }`);
  execSync('git rev-parse --verify @{u}'); // Make sure there's a remote tracking branch.
  const dirtyFiles = execSync('git ls-files -dmo --exclude-standard');
  if (dirtyFiles !== '') throw new Error(`working directory is unclean:\n${dirtyFiles}`);
  const indexStatus = execSync('git diff-index --cached --name-status HEAD');
  if (indexStatus !== '') throw new Error(`uncommitted staged changes to files:\n${indexStatus}`);
  execSync('git pull --ff-only', {stdio: 'inherit'});
  if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits');
  if (autoCommit) {
    execSync('git config --get user.name');
    execSync('git config --get user.email');
  }
};

if (autoCommit) {
  console.warn('Auto commit is enabled, I hope you know what you are doing...');
}

fs.readdir(pluginPath, (err, rootFiles) => {
  // handling error
  if (err) {
    return console.log(`Unable to scan directory: ${err}`);
  }

  // rewriting files to lower case
  const files = [];<commit_msg>checkPlugin: Split dirty working directory check into two checks<commit_after>'use strict';

/*
*
* Usage -- see README.md
*
* Normal usage:                node bin/plugins/checkPlugin.js ep_whatever
* Auto fix the things it can:  node bin/plugins/checkPlugin.js ep_whatever autofix
* Auto commit, push and publish(to npm) * highly dangerous:
node bin/plugins/checkPlugin.js ep_whatever autofix autocommit

*/

const fs = require('fs');
const childProcess = require('child_process');

// get plugin name & path from user input
const pluginName = process.argv[2];

if (!pluginName) {
  console.error('no plugin name specified');
  process.exit(1);
}

const pluginPath = `node_modules/${pluginName}`;

console.log(`Checking the plugin: ${pluginName}`);

const optArgs = process.argv.slice(3);

// Should we autofix?
const autoFix = optArgs.indexOf('autofix') !== -1;

// Should we update files where possible?
const autoUpdate = optArgs.indexOf('autoupdate') !== -1;

// Should we automcommit and npm publish?!
const autoCommit = optArgs.indexOf('autocommit') !== -1;

const execSync = (cmd, opts = {}) => (childProcess.execSync(cmd, {
  cwd: `${pluginPath}/`,
  ...opts,
}) || '').toString().replace(/\n+$/, '');

const prepareRepo = () => {
  let branch = execSync('git symbolic-ref HEAD');
  if (branch !== 'refs/heads/master' && branch !== 'refs/heads/main') {
    throw new Error('master/main must be checked out');
  }
  branch = branch.replace(/^refs\/heads\//, '');
  execSync('git rev-parse --verify -q HEAD^0 || ' +
           `{ echo "Error: no commits on ${branch}" >&2; exit 1; }`);
  execSync('git rev-parse --verify @{u}'); // Make sure there's a remote tracking branch.
  const modified = execSync('git diff-files --name-status');
  if (modified !== '') throw new Error(`working directory has modifications:\n${modified}`);
  const untracked = execSync('git ls-files -o --exclude-standard');
  if (untracked !== '') throw new Error(`working directory has untracked files:\n${untracked}`);
  const indexStatus = execSync('git diff-index --cached --name-status HEAD');
  if (indexStatus !== '') throw new Error(`uncommitted staged changes to files:\n${indexStatus}`);
  execSync('git pull --ff-only', {stdio: 'inherit'});
  if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits');
  if (autoCommit) {
    execSync('git config --get user.name');
    execSync('git config --get user.email');
  }
};

if (autoCommit) {
  console.warn('Auto commit is enabled, I hope you know what you are doing...');
}

fs.readdir(pluginPath, (err, rootFiles) => {
  // handling error
  if (err) {
    return console.log(`Unable to scan directory: ${err}`);
  }

  // rewriting files to lower case
  const files = [];
{'fraction_non_alphanumeric': 0.17249858142613958, 'fraction_numerical': 0.0034045772649895973, 'mean_word_length': 3.9008341056533826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4196280', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1649, 'n_words': 496}