npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

surgery

v0.0.1

Published

Replace text in files using YAML configuration

Downloads

90

Readme

Surgery

A powerful npm module and npx script for replacing text in files using YAML configuration files.

Features

  • 🔧 Text replacement using regex patterns
  • 📁 File glob pattern matching
  • 🌐 Remote YAML config support (URLs)
  • 🔗 GitHub integration with @org/repo syntax
  • 🔍 Dry run mode for previewing changes
  • 📝 Verbose logging
  • ⚡ Fast batch processing

Installation

# Install globally
npm install -g surgery

# Or use with npx (recommended)
npx surgery <config>

# Or install as a dependency for programmatic use
npm install surgery

Usage

NPX Script (Command Line)

# Use local YAML config file
npx surgery config.yml

# Use remote config file
npx surgery https://example.com/config.yml

# Use GitHub repo config (downloads from n-p-x org)
npx surgery @myorg/myconfig

NPM Module (Programmatic)

import surgery from 'surgery';
// or
import { executeSurgery, downloadConfig, performReplacement } from 'surgery';

// Execute surgery with configuration
const result = await surgery.executeSurgery('config.yml', {
  dryRun: true,
  verbose: false
});

console.log('Files processed:', result.totalFilesProcessed);

// Download and parse config separately
const configContent = await surgery.downloadConfig('@myorg/config');

// Apply individual rules
const ruleResult = await surgery.performReplacement(rule, {
  dryRun: false,
  verbose: true
});

Options

# Dry run (preview changes without applying)
npx surgery config.yml --dry-run

# Verbose output
npx surgery config.yml --verbose

# Combine options
npx surgery config.yml --dry-run --verbose

YAML Configuration Format

rules:
  - description: "Human-readable description"
    files: "glob/pattern/**/*.js"
    find: "regex pattern to find"
    replace: "replacement text"
  
  - description: "Another rule"
    files: ["multiple", "patterns/**/*.{js,ts}"]
    find: "another\\s+pattern"
    replace: "replacement"

Configuration Fields

  • rules (array): List of replacement rules
    • description (string, optional): Human-readable description of the rule
    • files (string|array): Glob pattern(s) for files to process
    • find (string): Regex pattern to search for
    • replace (string): Text to replace matches with
    • already (string, optional): Text to check for - if found, skip this file to prevent duplicate modifications

Examples

Replace package names

rules:
  - description: "Update package name"
    files: "**/package.json"
    find: '"name":\s*"old-name"'
    replace: '"name": "new-name"'

Update import statements

rules:
  - description: "Modernize imports"
    files: "src/**/*.js"
    find: "var\\s+(\\w+)\\s*=\\s*require\\(['\"]([^'\"]+)['\"]\\)"
    replace: "import $1 from '$2'"

Remove deprecated code

rules:
  - description: "Remove TODO comments"
    files: "**/*.{js,ts}"
    find: "// TODO:.*\\n"
    replace: ""

Prevent Duplicate Modifications

rules:
  - description: "Add logger import (only if not already present)"
    files: "src/**/*.js"
    find: "^"
    replace: "import { logger } from './logger.js';\n"
    already: "import { logger } from './logger.js'"
  
  - description: "Replace console.log with logger"
    files: "src/**/*.js"
    find: "console\\.log\\("
    replace: "logger.info("
    already: "logger.info("

Remote Configuration

GitHub Integration

Use the @org/repo format to download configs from the n-p-x GitHub organization:

npx surgery @myconfig/rules

This downloads from: https://raw.githubusercontent.com/n-p-x/rules/main/myconfig.yml

Custom URLs

npx surgery https://raw.githubusercontent.com/user/repo/main/config.yml

Development

# Install dependencies
npm install

# Build the project
npm run build

# Test locally
node dist/index.js example-config.yml --dry-run

License

ISC