Main DMG creation functionality using the DmgTarget class. Handles the complete build process from app packaging to final DMG generation with customization options, code signing, and metadata generation.
The main class for building DMG files within the electron-builder ecosystem. Extends the base Target class and orchestrates the complete DMG creation process.
/**
* Main DMG target class for building DMG files
* Extends Target from app-builder-lib
*/
class DmgTarget extends Target {
/** DMG configuration options from packager config */
readonly options: DmgOptions;
/** Indicates this target does not support async operations during build */
readonly isAsyncSupported: boolean;
/**
* Create a new DMG target instance
* @param packager - MacPackager instance for the build
* @param outDir - Output directory for the generated DMG
*/
constructor(packager: MacPackager, outDir: string);
/**
* Build the DMG file for the specified app and architecture
* @param appPath - Path to the .app bundle to package
* @param arch - Target architecture (x64, arm64, universal)
* @returns Promise that resolves when DMG creation is complete
*/
build(appPath: string, arch: Arch): Promise<void>;
/**
* Compute the volume name for the DMG
* @param arch - Target architecture
* @param custom - Optional custom volume name template with variable substitution
* @returns Computed volume name string
*/
computeVolumeName(arch: Arch, custom?: string | null): string;
/**
* Compute and validate DMG options from configuration
* @param appPath - Path to the app bundle
* @returns Promise resolving to validated DmgOptions
*/
computeDmgOptions(appPath: string): Promise<DmgOptions>;
}Usage Examples:
import { DmgTarget } from "dmg-builder";
import { MacPackager, Arch } from "app-builder-lib";
// Create packager and DMG target
const packager = new MacPackager(info, platform, cleanupTasks);
const dmgTarget = new DmgTarget(packager, "/output/directory");
// Build DMG for x64 architecture
await dmgTarget.build("/path/to/MyApp.app", Arch.x64);
// Custom volume name with variables
const volumeName = dmgTarget.computeVolumeName(
Arch.x64,
"${productName} ${version} ${arch}"
);
// Result: "MyApp 1.0.0 x64"Generates appropriate volume names for DMG files with support for variable substitution.
/**
* Compute the volume name for the DMG with variable substitution
* @param arch - Target architecture for arch suffix
* @param custom - Custom template with variables: ${arch}, ${shortVersion}, ${version}, ${name}, ${productName}
* @returns Computed volume name
*/
computeVolumeName(arch: Arch, custom?: string | null): string;Available Variables:
${arch} - Architecture suffix (e.g., " x64", " arm64", "")${shortVersion} - Bundle short version or app version${version} - Full app version${name} - App name${productName} - Product name from app infoValidates and computes final DMG configuration options with intelligent defaults.
/**
* Compute and validate DMG options from packager configuration
* Applies defaults for icon, background, format, and contents
* @param appPath - Path to the app bundle for content configuration
* @returns Promise resolving to complete DmgOptions configuration
* @throws InvalidConfigurationError for invalid configuration combinations
*/
computeDmgOptions(appPath: string): Promise<DmgOptions>;Default Behaviors:
Usage Example:
const dmgTarget = new DmgTarget(packager, outDir);
const options = await dmgTarget.computeDmgOptions("/path/to/MyApp.app");
// Options will include validated configuration with defaults applied
console.log(options.format); // "UDZO" (default compressed)
console.log(options.contents); // Default app + Applications link layoutenum Arch {
x64 = "x64",
arm64 = "arm64",
universal = "universal"
}/**
* Complete DMG configuration options interface
* Extended from app-builder-lib DmgOptions
*/
interface DmgOptions {
/** Background image path or null to use backgroundColor */
background?: string | null;
/** Background color (hex) when no background image */
backgroundColor?: string | null;
/** DMG icon path */
icon?: string | null;
/** Icon size in pixels (default: platform default) */
iconSize?: number;
/** Icon text size (10-16, default: 12) */
iconTextSize?: number;
/** Window configuration for DMG appearance */
window?: {
x?: number;
y?: number;
width?: number;
height?: number;
};
/** DMG compression format (UDZO, UDRO, UDBZ) */
format?: string;
/** Enable internet-enabled DMG (deprecated) */
internetEnabled?: boolean;
/** Code sign the DMG file */
sign?: boolean;
/** Custom DMG title template */
title?: string;
/** Write update info for auto-updater */
writeUpdateInfo?: boolean;
/** Contents layout configuration */
contents?: Array<{
/** Path to file/directory or special paths like /Applications */
path?: string;
/** X position in DMG window */
x: number;
/** Y position in DMG window */
y: number;
/** Display name (optional) */
name?: string;
/** Type of content item */
type?: "file" | "link" | "dir";
}>;
}The build process includes comprehensive error handling for common DMG creation issues:
InvalidConfigurationError for conflicting optionsDMG files can be automatically code signed during the build process:
// Enable DMG signing in configuration
const dmgOptions = {
sign: true, // Enable code signing
// Other options...
};Requirements: