"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
const fs = require("fs");
const path_1 = require("path");
const Observable_1 = require("rxjs/Observable");
const virtual_filesystem_1 = require("./virtual-filesystem");
class FileSystemSinkHost {
    constructor(_root) {
        this._root = _root;
    }
    exists(path) {
        return new Observable_1.Observable(observer => {
            fs.exists(path_1.join(this._root, path), exists => {
                observer.next(exists);
                observer.complete();
            });
        });
    }
    delete(path) {
        return new Observable_1.Observable(o => {
            fs.unlink(path_1.join(this._root, path), (err) => {
                if (err) {
                    o.error(err);
                }
                else {
                    o.complete();
                }
            });
        });
    }
    mkDir(path) {
        const paths = [];
        for (; path != path_1.dirname(path); path = path_1.dirname(path)) {
            if (fs.existsSync(path)) {
                break;
            }
            paths.unshift(path);
        }
        paths.forEach(path => {
            fs.mkdirSync(path);
        });
    }
    write(path, content) {
        path = path_1.join(this._root, path);
        return new Observable_1.Observable(o => {
            this.mkDir(path_1.dirname(path));
            fs.writeFile(path, content, (err) => {
                if (err) {
                    o.error(err);
                }
                else {
                    o.complete();
                }
            });
        });
    }
    read(path) {
        path = path_1.join(this._root, path);
        return new Observable_1.Observable(o => {
            fs.readFile(path, (err, data) => {
                if (err) {
                    o.error(err);
                }
                else {
                    o.next(data);
                    o.complete();
                }
            });
        });
    }
    rename(from, to) {
        from = path_1.join(this._root, from);
        to = path_1.join(this._root, to);
        return new Observable_1.Observable(o => {
            fs.rename(from, to, err => {
                if (err) {
                    o.error(err);
                }
                else {
                    o.complete();
                }
            });
        });
    }
}
exports.FileSystemSinkHost = FileSystemSinkHost;
class FileSystemSink extends virtual_filesystem_1.VirtualFileSystemSink {
    constructor(_root, force) {
        super(new FileSystemSinkHost(_root), force);
        this._root = _root;
    }
}
exports.FileSystemSink = FileSystemSink;
//# sourceMappingURL=data:application/json;base64,[encoded data, 7064 characters]