"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const rxjs_1 = require("rxjs");
function applyWithOverwriteRule(source, rules, overwrite = true) {
    return (tree, _context) => {
        const rule = schematics_1.mergeWith(schematics_1.apply(source, [
            ...rules,
            overwrite ? overwriteAllRule(tree) : schematics_1.noop()
        ]));
        return rule(tree, _context);
    };
}
exports.applyWithOverwriteRule = applyWithOverwriteRule;
function overwriteAllRule(tree) {
    return schematics_1.forEach((fileEntry) => {
        if (tree.exists(fileEntry.path)) {
            tree.overwrite(fileEntry.path, fileEntry.content);
            return null;
        }
        return fileEntry;
    });
}
exports.overwriteAllRule = overwriteAllRule;
function applyRule(source, templateData, url = './') {
    return (tree, _context) => {
        const rule = schematics_1.mergeWith(schematics_1.apply(source, [
            schematics_1.template(templateData),
            schematics_1.move(url)
        ]));
        return rule(tree, _context);
    };
}
exports.applyRule = applyRule;
function toPromiseRule(cb) {
    return (tree, context) => {
        return rxjs_1.Observable.create((observer) => __awaiter(this, void 0, void 0, function* () {
            try {
                yield cb(tree, context);
                observer.next(tree);
                observer.complete();
            }
            catch (err) {
                observer.error(err);
            }
        }));
    };
}
exports.toPromiseRule = toPromiseRule;
//# sourceMappingURL=index.js.map