"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;
  }
}

@@ -112,45 +112,8 @@
 er%22;
-%0Aimport %7Bheredoc%7D from %22../werkzeug%22;
 %0A%0Aex
@@ -1333,16 +1333,25 @@
 Warning(
+%0A        
 %22Engine 
@@ -1394,10 +1394,95 @@
 xify
-.%22
+, %22 +%0A        %22so this functionality is disabled when using the texify builder.%22%0A      
 );%0A 
@@ -1597,17 +1597,8 @@
 ing(
-heredoc(%60
 %0A   
@@ -1602,16 +1602,17 @@
         
+%22
 Output d
@@ -1664,16 +1664,20 @@
  texify,
+ %22 +
 %0A       
@@ -1677,16 +1677,17 @@
         
+%22
 so this 
@@ -1712,16 +1712,17 @@
 isabled 
+(
 for the 
@@ -1743,11 +1743,61 @@
 ture
-.%60)
+) %22 +%0A        %22when using the texify builder.%22%0A      
 );%0A 

Improve notification messages