node-appdmg

repository·master·Indexed 23 days ago

https://github.com/linusu/node-appdmg

A tool for generating macOS DMG (Disk Image) files using a JSON specification. It provides both a CLI and a Node.js API to programmatically define the layout, background, icons, and contents of a DMG image. Supports retina backgrounds, code signing, and custom window configurations. This library only works on macOS (darwin).

Tokens
2.3K
Snippets
5
Records
13
Agent score
83%

What's inside node-appdmg

  1. Enable Retina backgrounds

    master

    To support retina backgrounds in Finder, appdmg looks for a file with the same name as your background appended with @2x. If found, it will automatically package it as a .tiff file.

    Example: If your JSON contains "background": "TestBkg.png", place a file named TestBkg@2x.png in the same directory.

  2. Configure the DMG JSON specification

    master

    The JSON input defines the appearance and contents of the DMG. All paths within the JSON are relative to the location of the JSON file itself.

    Root Properties

    • title (string, required): The title of the produced DMG shown when mounted.
    • icon (string, optional): Path to the icon shown when mounted.
    • background (string, optional): Path to the background image.
    • background-color (string, optional): Background color (accepts CSS colors).
    • icon-size (number, optional): Size of all icons inside the DMG.
    • format (enum[string], optional): Disk image format (e.g., UDRW, UDRO, UDZO, ULFO).
    • filesystem (enum[string], optional): Disk image filesystem (HFS+ or APFS).
    • window (object, optional): Window positioning and size.
    • code-sign (object, optional): Options for code signing.
    • contents (array[object], required): The files or links to include in the DMG.

    Window Configuration

    • window.position: { x: number, y: number } (x is relative to left of screen, y is relative to bottom).
    • window.size: { width: number, height: number }.

    Contents Configuration

    Each object in the contents array requires:

    • x (number, required): X position relative to icon center.
    • y (number, required): Y position relative to icon center.
    • type (enum[string], required): link (creates a link), file (adds a file), or position (positions a present file).
    • path (string, required): Path to the file or target.
    • name (string, optional): Name of the file within the DMG.

    Code Signing

    • code-sign.signing-identity (string, required): The identity used to sign the DMG.
    • code-sign.identifier (string, optional): Explicit unique identifier string.
    {
      "title": "Test Application",
      "icon": "test-app.icns",
      "background": "test-background.png",
      "contents": [
        { "x": 448, "y": 344, "type": "link", "path": "/Applications" },
        { "x": 192, "y": 344, "type": "file", "path": "TestApp.app" }
      ]
    }
  3. Define DMG contents using files and links

    master

    The contents array in your specification defines what appears inside the DMG. Each entry can be a file (copied into the image) or a link (a symbolic link created in the image).

    File Entry:

    • type: Must be 'file'.
    • path: The path to the file (relative to basepath or absolute).
    • name: (Optional) The name the file will have inside the DMG. If omitted, the basename of the path is used.
    • x: (Optional) The X coordinate for the icon position in the UI.
    • y: (Optional) The Y coordinate for the icon position in the UI.

    Link Entry:

    • type: Must be 'link'.
    • path: The path to the target of the symlink.
    • name: (Optional) The name of the symlink inside the DMG. If omitted, the basename of the path is used.
    • x: (Optional) The X coordinate for the icon position in the UI.
    • y: (Optional) The Y coordinate for the icon position in the UI.
  4. Handle hidden files in DMG

    master

    By default, hidden files (like .DS_Store or .Trashes) may be visible to users with Finder settings enabled to show all files.

    To hide these, use the type: "position" content type to move them outside the initial window size. This will enable a scrollbar in the DMG window, allowing the hidden files to remain out of view.

  5. Use the appdmg Node.js API

    master

    You can integrate appdmg into your JavaScript applications. The API accepts either a source file path or a specification object directly.

    If using a specification object, provide a basepath to resolve relative paths.

    The function returns an EventEmitter that emits several events:

    • progress: Emits { current, total, type, title, status }. type is step-begin or step-end. status is ok, skip, or fail.
    • finish: Emitted when the DMG is successfully created.
    • error: Emitted when an error occurs.
    const appdmg = require('appdmg');
    
    // Using a source file
    const ee = appdmg({ source: 'test/appdmg.json', target: 'test.dmg' });
    
    // OR using a specification object directly
    const ee = appdmg({
      target: 'test.dmg',
      basepath: __dirname,
      specification: {
        "title": "Test Title"
      }
    });
    
    ee.on('progress', function (info) {
      // info.current, info.total, info.type, info.title, info.status
    });
    
    ee.on('finish', function () {
      // DMG is ready
    });
    
    ee.on('error', function (err) {
      // Handle error
    });
  6. Configure appdmg via JSON specification

    master

    The appdmg function consumes a specification object (or a JSON file) to define the DMG's contents and appearance. Based on the source code, the following top-level keys are used to configure the image:

    KeyTypeDescription
    titlestringThe title of the disk image.
    filesystemstringThe filesystem type (e.g., APFS, HFS+). Note: bless does not work with APFS.
    formatstringThe disk image format (defaults to UDZO).
    backgroundstringPath to a background image. Supports Retina if a @2x version exists.
    background-colorstringA CSS-compatible color string (e.g., #ffffff, rgb(0,0,0)).
    iconstringPath to the .icns icon file.
    icon-sizenumberSize of the icon in the .DS_Store (defaults to 80).
    windowobjectConfiguration for the window size and position.
    code-signobjectOptions for code signing.
    contentsarrayAn array of file or link objects to include in the DMG.

    Window Configuration Object:

    • window.size: { width: number, height: number }
    • window.position: { x: number, y: number }
  7. Configure code signing for the DMG

    master

    To sign the generated DMG, include a code-sign object in your specification. This requires both a signing-identity and an identifier.

    {
      "code-sign": {
        "signing-identity": "Developer ID Application: Your Name (ID)",
        "identifier": "com.yourcompany.app"
      }
    }
  8. Use the appdmg CLI

    master

    Run the appdmg command from your terminal by providing a path to a JSON specification file and the desired output path for the DMG.

    Arguments:

    • json-path: Path to the JSON Specification file.
    • dmg-path: Path at which to place the final DMG.

    To produce a test DMG to your desktop, you can run:

    appdmg test/assets/appdmg.json ~/Desktop/test.dmg
    appdmg <json-path> <dmg-path>
  9. Generate a DMG image with the appdmg function

    master

    The main entry point of the library is a function that accepts an options object to generate a macOS Disk Image (DMG). The function requires a target path where the resulting DMG will be saved. You must provide either a source (a path to a JSON specification file) OR a combination of basepath and specification (the specification object itself).

    Important Requirements:

    • Platform: This library only works on macOS (darwin).
    • Error Handling: If the target file already exists, the process will throw an error to prevent overwriting.
    • Input Modes:
      1. File Mode: Provide source (string) and target (string).
      2. Object Mode: Provide basepath (string), specification (object), and target (string).
  10. Use node-appdmg programmatically

    master
    The node-appdmg module provides a programmatic interface to create macOS .appdmg files. You can require the module to access the core appdmg function, which accepts a configuration object defining the application contents, window settings, and background images.
  11. Configure appdmg CLI options

    master

    When using the appdmg CLI, you can use the following flags to control output and verbosity:

    FlagAliasDescription
    --verbose-vEnables verbose error output (includes stack traces)
    --quietN/ASuppresses progress output to the terminal
    --helpN/ADisplays usage information and exits
    --versionN/ADisplays the current version and exits