"use babel";

import child_process from "child_process";
import path from "path";
import Builder from "../builder";
import {heredoc} from "../werkzeug";

export default class TexifyBuilder extends Builder {
  constructor() {
    super();
    this.executable = "texify";
  }

  run(filePath) {
    const args = this.constructArgs(filePath);
    const command = `${this.executable} ${args.join(" ")}`;
    const options = this.constructChildProcessOptions();

    options.cwd = path.dirname(filePath); // Run process with sensible CWD.
    options.maxBuffer = 52428800; // Set process' max buffer size to 50 MB.

    return new Promise((resolve) => {
      // TODO: Add support for killing the process.
      child_process.exec(command, options, (error) => {
        resolve((error) ? error.code : 0);
      });
    });
  }

  constructArgs(filePath) {
    const args = [
      "--batch",
      "--pdf",
      "--tex-option=--synctex=1",
      "--tex-option=--max-print-line=1000", // Max log file line length
    ];

    const enableShellEscape = atom.config.get("latex.enableShellEscape");
    if (enableShellEscape) {
      args.push("--tex-option=--enable-write18");
    }

    const customEngine = atom.config.get("latex.customEngine");
    const engine = atom.config.get("latex.engine");
    if (customEngine || engine !== "pdflatex") {
      atom.notifications.addWarning("Engine customization is not supported by texify.");
    }

    let outdir = this.getOutputDirectory(filePath);
    if (outdir) {
      atom.notifications.addWarning(heredoc(`
        Output directory functionality is poorly supported by texify,
        so this functionality is disabled for the foreseeable future.`));
    }

    args.push(`\"${filePath}\"`);
    return args;
  }
}

"use babel";

import child_process from "child_process";
import path from "path";
import Builder from "../builder";

export default class TexifyBuilder extends Builder {
  constructor() {
    super();
    this.executable = "texify";
  }

  run(filePath) {
    const args = this.constructArgs(filePath);
    const command = `${this.executable} ${args.join(" ")}`;
    const options = this.constructChildProcessOptions();

    options.cwd = path.dirname(filePath); // Run process with sensible CWD.
    options.maxBuffer = 52428800; // Set process' max buffer size to 50 MB.

    return new Promise((resolve) => {
      // TODO: Add support for killing the process.
      child_process.exec(command, options, (error) => {
        resolve((error) ? error.code : 0);
      });
    });
  }

  constructArgs(filePath) {
    const args = [
      "--batch",
      "--pdf",
      "--tex-option=--synctex=1",
      "--tex-option=--max-print-line=1000", // Max log file line length
    ];

    const enableShellEscape = atom.config.get("latex.enableShellEscape");
    if (enableShellEscape) {
      args.push("--tex-option=--enable-write18");
    }

    const customEngine = atom.config.get("latex.customEngine");
    const engine = atom.config.get("latex.engine");
    if (customEngine || engine !== "pdflatex") {
      atom.notifications.addWarning(
        "Engine customization is not supported by texify, " +
        "so this functionality is disabled when using the texify builder."
      );
    }

    let outdir = this.getOutputDirectory(filePath);
    if (outdir) {
      atom.notifications.addWarning(
        "Output directory functionality is poorly supported by texify, " +
        "so this functionality is disabled (for the foreseeable future) " +
        "when using the texify builder."
      );
    }

    args.push(`\"${filePath}\"`);
    return args;
  }
}

Improve notification messages