Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Puppeteer Puppeteer NgSchematics Packages Util

From Leeroopedia
Property Value
sources packages/ng-schematics/src/schematics/utils/packages.ts
domains Angular Schematics, Package Management, Configuration Management
last_updated 2026-02-12 00:00 GMT

Overview

Description

The NgSchematics Packages Util module provides utility functions for managing npm package dependencies, scripts, and Angular configuration during the Puppeteer schematics setup process. It handles interactions with package.json and angular.json files within the Angular schematic Tree abstraction.

Key capabilities include:

  • getPackageLatestNpmVersion -- Fetches the latest published version of an npm package from the npm registry (https://registry.npmjs.org). Falls back to "latest" if the request fails.
  • addPackageJsonDependencies -- Adds or updates package dependencies in package.json under a specified dependency type (dependencies, devDependencies, peerDependencies, optionalDependencies)
  • getDependenciesFromOptions -- Determines the required npm packages based on the selected test runner (Jasmine, Jest, Mocha, or Node)
  • addPackageJsonScripts -- Adds npm scripts to package.json
  • updateAngularJsonScripts -- Updates angular.json to register the @puppeteer/ng-schematics:puppeteer builder with appropriate serve targets and test runner configuration for each application project

The module uses the DependencyType enum to categorize npm dependencies and supports selective overwriting of existing values.

Usage

This module is used by the ng-add schematic to install dependencies, add scripts, and configure the Angular workspace during initial Puppeteer integration setup.

Code Reference

Source Location

packages/ng-schematics/src/schematics/utils/packages.ts

Signature

export interface NodePackage {
  name: string;
  version: string;
}

export interface NodeScripts {
  name: string;
  script: string;
}

export enum DependencyType {
  Default = 'dependencies',
  Dev = 'devDependencies',
  Peer = 'peerDependencies',
  Optional = 'optionalDependencies',
}

export function getPackageLatestNpmVersion(name: string): Promise<NodePackage>;

export function addPackageJsonDependencies(
  tree: Tree,
  packages: NodePackage[],
  type: DependencyType,
  overwrite?: boolean,
  fileLocation?: string,
): Tree;

export function getDependenciesFromOptions(options: SchematicsOptions): string[];

export function addPackageJsonScripts(
  tree: Tree,
  scripts: NodeScripts[],
  overwrite?: boolean,
  fileLocation?: string,
): Tree;

export function updateAngularJsonScripts(
  tree: Tree,
  options: SchematicsOptions,
  overwrite?: boolean,
): Tree;

Import

import {
  addPackageJsonDependencies,
  addPackageJsonScripts,
  getDependenciesFromOptions,
  getPackageLatestNpmVersion,
  updateAngularJsonScripts,
  DependencyType,
  type NodePackage,
  type NodeScripts,
} from '../utils/packages.js';

I/O Contract

Inputs

Parameter Type Description
name string npm package name to look up
tree Tree Angular schematic Tree (virtual filesystem)
packages NodePackage[] Array of package name/version pairs
type DependencyType Dependency category in package.json
overwrite boolean (optional) Whether to overwrite existing entries
fileLocation string (optional) Path to package.json, defaults to ./package.json
scripts NodeScripts[] Array of script name/command pairs
options SchematicsOptions Schematic options including testRunner

Outputs

Function Return Type Description
getPackageLatestNpmVersion Promise<NodePackage> Package name and latest version from npm registry
addPackageJsonDependencies Tree Modified Tree with updated package.json dependencies
getDependenciesFromOptions string[] List of npm package names required for the selected test runner
addPackageJsonScripts Tree Modified Tree with updated package.json scripts
updateAngularJsonScripts Tree Modified Tree with updated angular.json builder configuration

Usage Examples

import {
  getPackageLatestNpmVersion,
  addPackageJsonDependencies,
  getDependenciesFromOptions,
  DependencyType,
} from '../utils/packages.js';
import {TestRunner} from '../utils/types.js';

// Fetch the latest version of puppeteer from npm
const pkg = await getPackageLatestNpmVersion('puppeteer');
// => { name: 'puppeteer', version: '23.5.0' }

// Determine dependencies for Jest test runner
const deps = getDependenciesFromOptions({testRunner: TestRunner.Jest});
// => ['puppeteer', 'jest', '@types/jest']

// Add dependencies to package.json
addPackageJsonDependencies(
  tree,
  [{name: 'puppeteer', version: '23.5.0'}],
  DependencyType.Dev,
);

// Update angular.json with Puppeteer builder
updateAngularJsonScripts(tree, {testRunner: TestRunner.Jest});

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment