<commit_before>'use strict';

// Canonical path provides a consistent path (i.e. always forward slashes) across different OSes
const path = require('canonical-path');
const fs = require('fs-extra');
const globby = require('globby');
const jsdom = require('jsdom');
const json5 = require('json5');

const regionExtractor = require('../transforms/examples-package/services/region-parser');

class StackblitzBuilder {
  constructor(basePath, destPath) {
    this.basePath = basePath;
    this.destPath = destPath;

    this.copyrights = this._buildCopyrightStrings();
    this._boilerplatePackageJsons = {};
  }

  build() {
    this._checkForOutdatedConfig();

    // When testing it sometimes helps to look a just one example directory like so:
    // const stackblitzPaths = path.join(this.basePath, '**/testing/*stackblitz.json');
    const stackblitzPaths = path.join(this.basePath, '**/*stackblitz.json');
    const fileNames = globby.sync(stackblitzPaths, { ignore: ['**/node_modules/**'] });
    fileNames.forEach((configFileName) => {
      try {
        // console.log('***'+configFileName)
        this._buildStackblitzFrom(configFileName);
      } catch (e) {
        console.log(e);
      }
    });
  }

  _addDependencies(config, postData) {
    // Extract npm package dependencies
    const exampleType = this._getExampleType(config.basePath);
    const packageJson = this._getBoilerplatePackageJson(exampleType) || this._getBoilerplatePackageJson('cli');
    const exampleDependencies = packageJson.dependencies;

    // Add unit test packages from devDependencies for unit test examples
    const devDependencies = packageJson.devDependencies;
    ['jasmine-core', 'jasmine-marbles'].forEach(dep => exampleDependencies[dep] = devDependencies[dep]);

    postData.dependencies = JSON.stringify(exampleDependencies);
  }

  _getExampleType(exampleDir) {
    const configPath = `${exampleDir}/example-config.json`;
    const configSrc = fs.existsSync(configPath) && fs.readFileSync(configPath, 'utf-8').trim();
    const config = configSrc ? JSON.parse(configSrc) : {};
<commit_msg>build(docs-infra): exit with an error code if generating StackBlitz examples fails (#41725)<commit_after>'use strict';

// Canonical path provides a consistent path (i.e. always forward slashes) across different OSes
const path = require('canonical-path');
const fs = require('fs-extra');
const globby = require('globby');
const jsdom = require('jsdom');
const json5 = require('json5');

const regionExtractor = require('../transforms/examples-package/services/region-parser');

class StackblitzBuilder {
  constructor(basePath, destPath) {
    this.basePath = basePath;
    this.destPath = destPath;

    this.copyrights = this._buildCopyrightStrings();
    this._boilerplatePackageJsons = {};
  }

  build() {
    this._checkForOutdatedConfig();

    // When testing it sometimes helps to look a just one example directory like so:
    // const stackblitzPaths = path.join(this.basePath, '**/testing/*stackblitz.json');
    const stackblitzPaths = path.join(this.basePath, '**/*stackblitz.json');
    const fileNames = globby.sync(stackblitzPaths, { ignore: ['**/node_modules/**'] });
    let failed = false;
    fileNames.forEach((configFileName) => {
      try {
        this._buildStackblitzFrom(configFileName);
      } catch (e) {
        failed = true;
        console.log(e);
      }
    });

    if (failed) {
      process.exit(1);
    }
  }

  _addDependencies(config, postData) {
    // Extract npm package dependencies
    const exampleType = this._getExampleType(config.basePath);
    const packageJson = this._getBoilerplatePackageJson(exampleType) || this._getBoilerplatePackageJson('cli');
    const exampleDependencies = packageJson.dependencies;

    // Add unit test packages from devDependencies for unit test examples
    const devDependencies = packageJson.devDependencies;
    ['jasmine-core', 'jasmine-marbles'].forEach(dep => exampleDependencies[dep] = devDependencies[dep]);

    postData.dependencies = JSON.stringify(exampleDependencies);
  }

  _getExampleType(exampleDir) {
    const configPath = `${exampleDir}/example-config.json`;
    const configSrc = fs.existsSync(configPath) && fs.readFileSync(configPath, 'utf-8').trim();
    const config = configSrc ? JSON.parse(configSrc) : {};

{'fraction_non_alphanumeric': 0.12934631432545202, 'fraction_numerical': 0.0027816411682892906, 'mean_word_length': 4.149164677804296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '2963819', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1209, 'n_words': 274}