import {Reader} from './reader';
import {ReaderErrors} from './reader-errors';

const fs = typeof window !== 'undefined' ? window.require('fs') : require('fs');


/**
 * Reads contents of a file.
 * Expects a UTF-8 encoded text file.
 *
 * @author Sebastian Cuy
 * @author Jan G. Wieners
 * @author Thomas Kleinke
 */
export class FileSystemReader implements Reader {

    constructor(private file: any) {}


    public go(): Promise<string> {

        return new Promise((resolve, reject) => {

            fs.readFile(this.file.path, 'utf-8', (err: any, content: any) => {
                if (err) {
                    reject([ReaderErrors.FILE_UNREADABLE, this.file.path]);
                } else {
                    resolve(content);
                }
            });
        });
    }
}

import {Reader} from './reader';
import {ReaderErrors} from './reader-errors';

/**
 * Reads contents of a file.
 * Expects a UTF-8 encoded text file.
 *
 * @author Sebastian Cuy
 * @author Jan G. Wieners
 */
export class FileSystemReader implements Reader {

    constructor(private file: File) {}


    /**
     * Read content of file
     *
     * @returns {Promise<string>} file content | msgWithParams
     */
    public go(): Promise<string> {

        return new Promise((resolve, reject) => {

            let reader = new FileReader();

            reader.onload = (event: any) => {
                resolve(event.target.result);
            };

            reader.onerror = (event: any) => {
                console.error(event.target.error);
                reject([ReaderErrors.FILE_UNREADABLE, this.file.name]);
            };

            reader.readAsText(this.file);
        });
    }
}

--- 
+++ 
@@ -1,5 +1,8 @@
 import {Reader} from './reader';
 import {ReaderErrors} from './reader-errors';
+
+const fs = typeof window !== 'undefined' ? window.require('fs') : require('fs');
+
 
 /**
  * Reads contents of a file.
@@ -7,33 +10,24 @@
  *
  * @author Sebastian Cuy
  * @author Jan G. Wieners
+ * @author Thomas Kleinke
  */
 export class FileSystemReader implements Reader {
 
-    constructor(private file: File) {}
+    constructor(private file: any) {}
 
 
-    /**
-     * Read content of file
-     *
-     * @returns {Promise<string>} file content | msgWithParams
-     */
     public go(): Promise<string> {
 
         return new Promise((resolve, reject) => {
 
-            let reader = new FileReader();
-
-            reader.onload = (event: any) => {
-                resolve(event.target.result);
-            };
-
-            reader.onerror = (event: any) => {
-                console.error(event.target.error);
-                reject([ReaderErrors.FILE_UNREADABLE, this.file.name]);
-            };
-
-            reader.readAsText(this.file);
+            fs.readFile(this.file.path, 'utf-8', (err: any, content: any) => {
+                if (err) {
+                    reject([ReaderErrors.FILE_UNREADABLE, this.file.path]);
+                } else {
+                    resolve(content);
+                }
+            });
         });
     }
 }
Use fs instead of FileReader in FileSystemReader to fix a bug where a file could not be loaded if it was changed after selection
