<commit_before>/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
import * as core from '@angular-devkit/core/node';
import { RuleFactory } from '@angular-devkit/schematics';
import {
  CollectionCannotBeResolvedException,
  CollectionMissingSchematicsMapException,
  SchematicMissingFieldsException,
} from '@angular-devkit/schematics/tools';
import { dirname, join, resolve as resolvePath } from 'path';
import {
  FileSystemCollectionDesc,
  FileSystemSchematicDesc,
} from './description';
import { ExportStringRef } from './export-ref';
import { FileSystemEngineHostBase } from './file-system-engine-host-base';


/**
 * A simple EngineHost that uses NodeModules to resolve collections.
 */
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
  constructor() { super(); }

  protected _resolvePath(name: string, basedir = process.cwd(), extraChecks = true): string {
    // Allow relative / absolute paths.
    if (name.startsWith('.') || name.startsWith('/')) {
      return resolvePath(process.cwd(), name);
    } else {
      return core.resolve(name, {
        basedir,
        checkLocal: extraChecks,
        checkGlobal: extraChecks,
      });
    }
  }

  protected _resolveCollectionPath(name: string): string {
    let packageJsonPath = this._resolvePath(name);
    // If it's a file, use it as is. Otherwise append package.json to it.
    if (!core.fs.isFile(packageJsonPath)) {
      packageJsonPath = join(packageJsonPath, 'package.json');
    }

    try {
      const pkgJsonSchematics = require(packageJsonPath)['schematics'];
      if (pkgJsonSchematics) {
        const resolvedPath = this._resolvePath(pkgJsonSchematics, dirname(packageJsonPath), false);
        require(resolvedPath);

        return resolvedPath;
      }
    } catch (e) {
    }
    throw new CollectionCannotBeResolvedException(name);
  }

  protected _resolveReferenceString(refString: string, parentPath: string) {
    const ref = new ExportStringRef<RuleFactory<{}>>(refString, parentPath);
    if (!ref.ref) {
      return null;
    }

    return { ref: ref.ref, path: ref.module };
  }

  protected _transformCollectionDescription(
    name: string,
    desc: Partial<FileSystemCollectionDesc>,
  ): FileSystemCollectionDesc {<commit_msg>fix(@angular-devkit/schematics): use loose mode for collection.json<commit_after>/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
import * as core from '@angular-devkit/core/node';
import { RuleFactory } from '@angular-devkit/schematics';
import {
  CollectionCannotBeResolvedException,
  CollectionMissingSchematicsMapException,
  SchematicMissingFieldsException,
} from '@angular-devkit/schematics/tools';
import { dirname, join, resolve as resolvePath } from 'path';
import {
  FileSystemCollectionDesc,
  FileSystemSchematicDesc,
} from './description';
import { ExportStringRef } from './export-ref';
import { FileSystemEngineHostBase } from './file-system-engine-host-base';
import { readJsonFile } from './file-system-utility';


/**
 * A simple EngineHost that uses NodeModules to resolve collections.
 */
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
  constructor() { super(); }

  protected _resolvePath(name: string, basedir = process.cwd(), extraChecks = true): string {
    // Allow relative / absolute paths.
    if (name.startsWith('.') || name.startsWith('/')) {
      return resolvePath(process.cwd(), name);
    } else {
      return core.resolve(name, {
        basedir,
        checkLocal: extraChecks,
        checkGlobal: extraChecks,
      });
    }
  }

  protected _resolveCollectionPath(name: string): string {
    let packageJsonPath = this._resolvePath(name);
    // If it's a file, use it as is. Otherwise append package.json to it.
    if (!core.fs.isFile(packageJsonPath)) {
      packageJsonPath = join(packageJsonPath, 'package.json');
    }

    try {
      const pkgJsonSchematics = require(packageJsonPath)['schematics'];
      if (pkgJsonSchematics) {
        const resolvedPath = this._resolvePath(pkgJsonSchematics, dirname(packageJsonPath), false);
        readJsonFile(resolvedPath);

        return resolvedPath;
      }
    } catch (e) {
    }
    throw new CollectionCannotBeResolvedException(name);
  }

  protected _resolveReferenceString(refString: string, parentPath: string) {
    const ref = new ExportStringRef<RuleFactory<{}>>(refString, parentPath);
    if (!ref.ref) {
      return null;
    }

    return { ref: ref.ref, path: ref.module };
  }

  protected _transformCollectionDescription(
    name: string,
    desc: Partial<FileSystemCollectionDesc>,
  ): FileSystemCollectionDesc {
{'fraction_non_alphanumeric': 0.11208121827411167, 'fraction_numerical': 0.0, 'mean_word_length': 3.777885548011639, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4740815', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1370, 'n_words': 363}