"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hookWritePackage = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const transform_1 = require("ng-packagr/lib/graph/transform");
const write_package_di_1 = require("ng-packagr/lib/ng-package/entry-point/write-package.di");
const nodes_1 = require("ng-packagr/lib/ng-package/nodes");
const path_1 = __importDefault(require("path"));
const rxjs_1 = require("rxjs");
const oldFactory = write_package_di_1.WRITE_PACKAGE_TRANSFORM.useFactory;
function hookWritePackage() {
    write_package_di_1.WRITE_PACKAGE_TRANSFORM.useFactory = myWritePackage;
    return write_package_di_1.WRITE_PACKAGE_TRANSFORM;
}
exports.hookWritePackage = hookWritePackage;
function myWritePackage(options) {
    return (0, transform_1.transformFromPromise)((graph) => __awaiter(this, void 0, void 0, function* () {
        // todo 这里理论上不应该这么做,因为rxjs非同依赖会有一些小问题.但是不涉及到unsubscribe遇不到这些小问题,所以先这么做,未来再想办法
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        yield oldFactory(options)((0, rxjs_1.of)(graph)).toPromise();
        const entryPoint = graph.find(
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (0, nodes_1.isEntryPointInProgress)());
        if (!entryPoint.data.entryPoint.isSecondaryEntryPoint) {
            const packageJsonPath = path_1.default.resolve(entryPoint.data.entryPoint.destinationPath, 'package.json');
            const packageJson = JSON.parse(fs_extra_1.default.readFileSync(packageJsonPath).toString());
            delete packageJson.scripts.prepublishOnly;
            fs_extra_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));
        }
    }));
}
