All files / builder/library remove-publish-only.ts

100% Statements 19/19
50% Branches 1/2
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x 1x 1x 1x         1x 1x   1x 1x 1x 1x     1x     1x 1x       1x 1x       1x     1x 1x              
import fs from 'fs-extra';
import { transformFromPromise } from 'ng-packagr/lib/graph/transform';
import { WRITE_PACKAGE_TRANSFORM } from 'ng-packagr/lib/ng-package/entry-point/write-package.di';
import {
  EntryPointNode,
  isEntryPointInProgress,
} from 'ng-packagr/lib/ng-package/nodes';
import { NgPackagrOptions } from 'ng-packagr/lib/ng-package/options.di';
import path from 'path';
import { of } from 'rxjs';
 
const oldFactory = WRITE_PACKAGE_TRANSFORM.useFactory;
export function hookWritePackage() {
  WRITE_PACKAGE_TRANSFORM.useFactory = myWritePackage;
  return WRITE_PACKAGE_TRANSFORM;
}
function myWritePackage(options: NgPackagrOptions) {
  return transformFromPromise(async (graph) => {
    // todo 这里理论上不应该这么做,因为rxjs非同依赖会有一些小问题.但是不涉及到unsubscribe遇不到这些小问题,所以先这么做,未来再想办法
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    await oldFactory(options)(of(graph) as any).toPromise();
    const entryPoint: EntryPointNode = graph.find(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      isEntryPointInProgress() as any
    )!;
    Eif (!entryPoint.data.entryPoint.isSecondaryEntryPoint) {
      const packageJsonPath = path.resolve(
        entryPoint.data.entryPoint.destinationPath,
        'package.json'
      );
      const packageJson = JSON.parse(
        fs.readFileSync(packageJsonPath).toString()
      );
      delete packageJson.scripts.prepublishOnly;
      fs.writeFileSync(
        packageJsonPath,
        JSON.stringify(packageJson, undefined, 2)
      );
    }
  });
}