"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.plugin = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const core_1 = require("@angular-devkit/core");
const PLUGINS_DIST = 'projects/plugins/src';
function plugin(_options) {
    return (_tree, _context) => {
        // create plugin's module, config and other stuff from the templates
        const templatesSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
            (0, schematics_1.applyTemplates)({
                classify: core_1.strings.classify,
                dasherize: core_1.strings.dasherize,
                name: _options.name,
                displayName: _options.displayName || _options.name,
            }),
            (0, schematics_1.move)((0, core_1.normalize)(`/${PLUGINS_DIST}/${core_1.strings.dasherize(_options.name)}`))
        ]);
        return (0, schematics_1.chain)([
            // externalSchematic('@schematics/angular', 'component', _options),
            (0, schematics_1.mergeWith)(templatesSource, schematics_1.MergeStrategy.Error),
            addBuildScriptRule(_options)
        ]);
    };
}
exports.plugin = plugin;
/**
 * Adds command for building plugin to scripts section in package.json file.
 */
function addBuildScriptRule(_options) {
    return (_tree, _context) => __awaiter(this, void 0, void 0, function* () {
        const pkgPath = '/package.json';
        const pkg = _tree.readJson(pkgPath);
        if (pkg == null) {
            throw new schematics_1.SchematicsException('Could not find package.json');
        }
        const nameDasherized = core_1.strings.dasherize(_options.name);
        const buildCommand = `ng build plugins --module-path=./${nameDasherized}/${nameDasherized}.module#${core_1.strings.classify(_options.name)}Module`;
        // _context.logger.info(`Расширение успешно создано. Для его сборки воспользуйтесь командной:\n` + buildCommand);
        // const addScriptCommand = `npm pkg set script.build:${nameDasherized}=${buildCommand}`;
        pkg.scripts = Object.assign(Object.assign({}, pkg.scripts), { [`build:${nameDasherized}`]: buildCommand });
        _tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
    });
}
//# sourceMappingURL=index.js.map