Dangerfile Rule Builder

7.0.4 · active · verified Tue Apr 21

Endanger is a JavaScript/TypeScript library designed to streamline the creation and management of Dangerfiles within a project. Currently at version 7.0.4, it provides a structured approach to defining 'rules' that automate code review processes. Endanger differentiates itself by enabling developers to break down complex checks into modular, reusable rules, which can be configured to execute only when relevant files or commit messages change, improving performance and developer experience. It integrates seamlessly with Danger.js, leveraging its reporting capabilities while abstracting away much of the boilerplate. The library simplifies the process of adding new checks, making automated feedback more accessible, even for team members less familiar with JavaScript. Endanger is actively maintained and ships with TypeScript types for enhanced developer tooling.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up a Dangerfile with `endanger` using a modular rule to warn about changes in critical system files and ensure corresponding security documentation exists.

import { run, Rule } from 'endanger';

// dangerfile.ts
// This file is typically located at the root of your repository.
import myFirstRule from './danger/myFirstRule';

// Execute the defined rules. You can pass multiple rule instances.
run(
  myFirstRule()
);

// danger/myFirstRule.ts
// Create this file inside a 'danger/' directory.
export default function myFirstRule() {
  return new Rule({
    match: {
      // This rule will only run if files matching this glob pattern are created or modified.
      files: ["src/critical-system/**", "docs/security/**"],
      // You can also match commits by regex: commit: [/^fix\(security\):/]
    },
    messages: {
      criticalFileChangeWarning: `
        🚨 Warning: Changes detected in critical system or security documentation files.
        Please ensure thorough review and necessary approvals.
      `,
      missingSecurityDocs: `
        🚫 A new file was added to 'src/critical-system/' without corresponding security documentation.
      `
    },
    async run({ files, context }) {
      // Iterate over newly created files that match the glob
      for (let file of files.created) {
        context.warn("criticalFileChangeWarning", { file });
        // Example: Check for companion security documentation
        const docPath = file.filePath.replace('src/critical-system/', 'docs/security/');
        if (!(await context.git.fileMatch(docPath).createdOrModified())) {
          context.fail("missingSecurityDocs", { file });
        }
      }
      // Iterate over modified files
      for (let file of files.modified) {
        context.message("criticalFileChangeWarning", { file });
      }
    }
  });
}

// To run this example:
// 1. Install dependencies: npm install --save-dev danger endanger typescript
// 2. Set up a simple git repository and make a commit.
// 3. Create or modify a file within 'src/critical-system/' (e.g., 'src/critical-system/new-feature.ts').
// 4. Run Danger locally: npx danger local --dangerfile dangerfile.ts
//    (Ensure your package.json has `"type": "module"` if you face module resolution issues.)

view raw JSON →