DMG Builder Utilities

26.8.1 · active · verified Sun Apr 19

dmg-builder is a low-level utility package focused on programmatically creating macOS Disk Image (.dmg) files. It serves as a core component within the electron-builder ecosystem, providing the underlying functionality for packaging Electron applications into DMGs. The package is currently at version 26.8.1 and adheres to the release cadence of electron-builder, receiving frequent updates, typically patch or minor versions, aligning with electron-builder's rapid development cycle. Its primary differentiator is its deep integration and optimization for Electron app packaging, offering robust and configurable options for macOS distribution, often surpassing generic DMG creation tools.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to programmatically use `createDmg` with `DmgOptions` to generate a macOS Disk Image. Note that `dmg-builder` is an internal utility and direct standalone usage may require mocking parts of the `electron-builder` context.

import { createDmg, DmgOptions } from 'dmg-builder';
import { Arch, Platform } from 'builder-util';
import path from 'node:path';
import { promises as fs } from 'node:fs';

async function buildMyDmg() {
  // Assume 'my-app' directory contains your compiled macOS Electron app bundle
  const appDir = path.resolve(process.cwd(), 'dist/mac/my-app.app');
  const outputDir = path.resolve(process.cwd(), 'release');
  const appName = 'My App';
  const appFileName = `${appName}.app`;
  const dmgFileName = `${appName}.dmg`;

  await fs.mkdir(outputDir, { recursive: true });

  const dmgOptions: DmgOptions = {
    // Relative path to the app bundle inside the DMG
    app: path.join('.', appFileName),
    // Path to the app bundle on the local filesystem
    appPath: appDir,
    // Output path for the generated DMG
    destination: path.join(outputDir, dmgFileName),
    // Volume name for the mounted DMG
    volume: appName,
    // Background image for the DMG window (optional)
    background: path.resolve(__dirname, 'assets/background.png'),
    // Icon for the DMG volume (optional)
    icon: path.resolve(__dirname, 'assets/icon.icns'),
    // Window size (optional, default 540x380)
    window: { x: 100, y: 100, width: 600, height: 400 },
    // Define icon positions (optional)
    iconTextSize: 12,
    iconSize: 96,
    // This is a minimal set, electron-builder adds many more options
    contents: [
      { x: 130, y: 220, type: 'file', path: appDir, name: appFileName },
      { x: 400, y: 220, type: 'link', path: '/Applications' }
    ],
    // Add a license file if available
    // license: path.resolve(__dirname, 'LICENSE.txt'),
    // Explicitly set DMG filesystem size if auto-calculation is insufficient
    // size: '1g' 
  };

  console.log(`Building DMG for ${appName}...`);
  try {
    // createDmg expects a MacPackager instance for full context in electron-builder
    // For standalone usage, a mock packager might be necessary or use electron-builder's full build process.
    // This example simulates direct use, which is typically handled by electron-builder itself.
    // In a real electron-builder setup, 'createDmg' would be called internally by the MacPackager.
    // This code might require mocking the 'packager' argument or running within an electron-builder context.
    
    // A simplified call, assuming 'createDmg' can run with just options and an identity (often optional).
    // This is a placeholder for the actual complexity of standalone dmg-builder usage.
    const dmgPath = await createDmg(dmgOptions, { 
        platform: Platform.MAC,
        arch: Arch.x64,
        projectDir: process.cwd(),
        appOutDir: outputDir,
        packager: {} as any // Mocking packager as it's typically an internal dependency from electron-builder
    });
    console.log(`DMG successfully created at: ${dmgPath}`);
  } catch (error) {
    console.error('Failed to create DMG:', error);
    process.exit(1);
  }
}

buildMyDmg();

view raw JSON →