<commit_before>// import * as fs from 'fs'; // type import

import { BuildEvent, Builder, BuilderConfiguration, BuilderContext } from '@angular-devkit/architect';
// https://github.com/angular/devkit/issues/963
// import { BrowserBuilder } from '@angular-devkit/build-angular/src/browser';
// import { BrowserBuilderSchema, NormalizedBrowserBuilderSchema } from '@angular-devkit/build-angular/src/browser/schema';
const { BrowserBuilder } = require('@angular-devkit/build-angular/src/browser'); // tslint:disable-line
// import { Path, resolve, virtualFs } from '@angular-devkit/core';

import { Observable, of } from 'rxjs';
import { concatMap, tap } from 'rxjs/operators';
// import * as webpack from 'webpack';

import { CordovaBuilderSchema } from './schema';

export class CordovaBuilder implements Builder<CordovaBuilderSchema> {
  constructor(public context: BuilderContext) {}

  run(builderConfig: BuilderConfiguration<CordovaBuilderSchema>): Observable<BuildEvent> {
    // const { root } = this.context.workspace;
    // const projectRoot = resolve(root, builderConfig.root);
    // const host = new virtualFs.AliasHost(this.context.host as virtualFs.Host<fs.Stats>);
    const browserBuilder = new BrowserBuilder(this.context);
    let browserOptions: /* BrowserBuilderSchema */any;

    return of(null).pipe(// tslint:disable-line:no-null-keyword
      concatMap(() => this._getBrowserConfig(builderConfig.options)),
      tap(opts => browserOptions = opts),
      concatMap(() => browserBuilder.run(browserOptions))
      // concatMap(() => new Observable(obs => {
      //   const webpackConfig = this.buildWebpackConfig(root, projectRoot, host, browserOptions);
      //   const webpackCompiler = webpack(webpackConfig);

      //   webpackCompiler.run((err, stats) => {
      //     if (err) {
      //       return obs.error(err);
      //     }

      //     obs.next({ success: !stats.hasErrors() });
      //     obs.complete();
      //   });
      // }))
    );
  }

  // buildWebpackConfig(root: Path, projectRoot: Path, host: virtualFs.Host<fs.Stats>, browserOptions: /* BrowserBuilderSchema */any) {
  //   const browserBuilder = new BrowserBuilder(this.context);
  //   const webpackConfig = browserBuilder.buildWebpackConfig(root, projectRoot, host, browserOptions as /* NormalizedBrowserBuilderSchema */any);

  //   return webpackConfig;
  // }

  protected _getBrowserConfig(options: CordovaBuilderSchema): Observable</* BrowserBuilderSchema */any> {
    const { architect } = this.context;
    const [ project, target, configuration ] = options.browserTarget.split(':');
    const browserTargetSpec = { project, target, configuration, overrides: {} };
    const builderConfig = architect.getBuilderConfiguration</* BrowserBuilderSchema */any>(browserTargetSpec);

    return architect.getBuilderDescription(builderConfig).pipe(
      concatMap(browserDescription => architect.validateBuilderOptions(builderConfig, browserDescription))
    );
  }
}

export default CordovaBuilder;<commit_msg>wip: extract webpack config from browser builder<commit_after>import * as fs from 'fs'; // type import

import { BuildEvent, Builder, BuilderConfiguration, BuilderContext } from '@angular-devkit/architect';
// https://github.com/angular/devkit/issues/963
const { BrowserBuilder } = require('@angular-devkit/build-angular/src/browser'); // tslint:disable-line
const { statsErrorsToString, statsToString, statsWarningsToString } = require('@angular-devkit/build-angular/src/angular-cli-files/utilities/stats'); // tslint:disable-line
const { getWebpackStatsConfig } = require('@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/utils'); // tslint:disable-line

import { Path, resolve, virtualFs } from '@angular-devkit/core';

import { Observable, of } from 'rxjs';
import { concatMap, tap } from 'rxjs/operators';
import * as webpack from 'webpack';

import { CordovaBuilderSchema } from './schema';

export class CordovaBuilder implements Builder<CordovaBuilderSchema> {
  constructor(public context: BuilderContext) {}

  run(builderConfig: BuilderConfiguration<CordovaBuilderSchema>): Observable<BuildEvent> {
    const { root } = this.context.workspace;
    const projectRoot = resolve(root, builderConfig.root);
    const host = new virtualFs.AliasHost(this.context.host as virtualFs.Host<fs.Stats>);
    let browserConfig: /* BrowserBuilderSchema */any;

    return of(null).pipe(// tslint:disable-line:no-null-keyword
      concatMap(() => this._getBrowserConfig(builderConfig.options)),
      tap(config => browserConfig = config),
      concatMap(() => new Observable(obs => {
        const { options } = browserConfig;

        if (options.watch) {
          throw new Error('The `--watch` option is not implemented for Cordova builds.');
        }

        const webpackConfig = this.buildWebpackConfig(root, projectRoot, host, options);
        const webpackCompiler = webpack(webpackConfig);

        webpackCompiler.run((err, stats) => {
          if (err) {
            return obs.error(err);
          }

          const statsConfig = getWebpackStatsConfig(options.verbose);
          const json = stats.toJson(statsConfig);

          if (options.verbose) {
            this.context.logger.info(stats.toString(statsConfig));
          } else {
            this.context.logger.info(statsToString(json, statsConfig));
          }

          if (stats.hasWarnings()) {
            this.context.logger.warn(statsWarningsToString(json, statsConfig));
          }

          if (stats.hasErrors()) {
            this.context.logger.error(statsErrorsToString(json, statsConfig));
          }

          obs.next({ success: !stats.hasErrors() });
          obs.complete();
        });
      }))
    );
  }

  buildWebpackConfig(root: Path, projectRoot: Path, host: virtualFs.Host<fs.Stats>, browserOptions: /* BrowserBuilderSchema */any) {
    const browserBuilder = new BrowserBuilder(this.context);
    const webpackConfig = browserBuilder.buildWebpackConfig(root, projectRoot, host, browserOptions as /* NormalizedBrowserBuilderSchema */any);

    return webpackConfig;
  }

  protected _getBrowserConfig(options: CordovaBuilderSchema): Observable</* BrowserBuilderSchema */any> {
    const { architect } = this.context;
    const [ project, target, configuration ] = options.browserTarget.split(':');
    const browserTargetSpec = { project, target, configuration, overrides: {} };
    const builderConfig = architect.getBuilderConfiguration</* BrowserBuilderSchema */any>(browserTargetSpec);

    return architect.getBuilderDescription(builderConfig).pipe(
      concatMap(browserDescription => architect.validateBuilderOptions(builderConfig, browserDescription))
    );
  }
}

export default CordovaBuilder;
{'fraction_non_alphanumeric': 0.12780732860520094, 'fraction_numerical': 0.0008865248226950354, 'mean_word_length': 3.810945273631841, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '817782', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1797, 'n_words': 385}