package org.codingmatters.value.objects.js.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class JsFileWriter implements AutoCloseable {

    public static final String INDENTATION_UNITY = "    ";
    protected BufferedWriter writer;
    protected int indent;

    public JsFileWriter( String filePath ) {
        this.indent = 0;
        try {
            File file = new File( filePath );
            file.getParentFile().mkdirs();
            if( !file.exists() ) {
                file.createNewFile();
            }
            this.writer = new BufferedWriter( new FileWriter( file ) );
        } catch( IOException e ) {
            e.printStackTrace();
        }
    }

    public void line( String line ) throws IOException {
        if( line.endsWith( "{" ) ) {
            writeLine( line );
            indent++;
        } else if( line.equals( "}" ) ) {
            indent--;
            writeLine( line );
        } else {
            writeLine( line );
        }
    }

    public void writeLine( String line ) throws IOException {
        indent();
        writer.write( line );
        writer.newLine();
    }

    public void string( String line ) throws IOException {
        writer.write( line );
    }

    public void indent() throws IOException {
        for( int i = 0; i < indent; i++ ) {
            writer.write( INDENTATION_UNITY );
        }
    }

    public void newLine() throws IOException {
        writer.newLine();
    }

    public void flush() throws IOException {
        writer.flush();
    }

    @Override
    public void close() throws Exception {
        writer.flush();
        writer.close();
    }
}

@@ -1319,16 +1319,17 @@
  indent(
+ 
 ) throws
@@ -1475,16 +1475,17 @@
 newLine(
+ 
 ) throws
@@ -1554,16 +1554,17 @@
 d flush(
+ 
 ) throws
@@ -1645,16 +1645,17 @@
 d close(
+ 
 ) throws
@@ -1720,11 +1720,66 @@
 );%0A    %7D
+%0A%0A    public void unindent( ) %7B%0A        indent--;%0A    %7D
 %0A%7D%0A

Fix error