<commit_before>'use babel';

import * as fs from 'fs';
import SVGO from 'svgo';

function minify(pretty = false) {
  const editor = atom.workspace.getActiveTextEditor();

  if (!editor) {
    return;
  }

  let position = editor.getCursorBufferPosition();
  let text = editor.getText();
  let selectedText = editor.getSelectedText();
  let svgo = new SVGO({ js2svg: { pretty } });

  if (selectedText.length !== 0) {
    svgo.optimize(selectedText, result => {
      if (result.data) {
        let range = editor.getSelectedBufferRange();
        editor.setTextInBufferRange(range, result.data);
        editor.setCursorBufferPosition(position);
      }
    });
  } else {
    svgo.optimize(text, result => {
      if (result.data) {
        editor.setText(result.data);
        editor.setCursorBufferPosition(position);
      }
    });
  }
}

export function activate(state) {
  atom.commands.add('atom-workspace', 'svgo:minify', () => minify(false));
  atom.commands.add('atom-workspace', 'svgo:prettify', () => minify(true));
}
<commit_msg>Execute svgo on child process<commit_after>'use babel';

import { normalize, join } from 'path';
import { spawn } from 'child_process';

const SVGO_PATH = normalize(join(__dirname, 'node_modules', '.bin', 'svgo'));

function minify(pretty = false) {
  const editor = atom.workspace.getActiveTextEditor();

  if (!editor) {
    return;
  }

  let args = ['--input', editor.getPath(), '--output', '-'];
  if (pretty) {
    args.push('--pretty');
  }

  let chunks = [];
  let cp = spawn(SVGO_PATH, args);
  cp.stdout.on('data', chunk => {
    chunks.push(chunk);
  });

  cp.on('error', error => {
    console.error(error);
  });

  cp.on('exit', () => {
    let position = editor.getCursorBufferPosition();
    editor.setText(Buffer.concat(chunks).toString());
    editor.setCursorBufferPosition(position);
  });
}

export function activate(state) {
  atom.commands.add('atom-workspace', 'svgo:minify', () => minify(false));
  atom.commands.add('atom-workspace', 'svgo:prettify', () => minify(true));
}

{'fraction_non_alphanumeric': 0.17359413202933985, 'fraction_numerical': 0.0009779951100244498, 'mean_word_length': 3.6926605504587156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '1574931', 'n_tokens_mistral': 675, 'n_tokens_neox': 660, 'n_words': 129}