Skip to content

📝 NormalizeComposerJson ​

Formats composer.json so it conforms to the canonical field order and indentation. Equivalent to running composer normalize (requires the ergebnis/composer-normalize plugin).

When to use ​

Use this when the field order in composer.json is messy, indentation is inconsistent, and you want to auto-organize it into the community-standard format. It is commonly run before commits or in PR checks to maintain consistency.

Signature ​

go
func (c *Composer) NormalizeComposerJson() (string, error)

Parameters ​

This method takes no parameters.

Return value ​

Return valueTypeDescription
First return valuestringThe output of the formatting command
Second return valueerrorReturned when formatting fails (including when the plugin is not installed)

Example ​

go
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("init failed: %v", err)
	}

	output, err := comp.NormalizeComposerJson()
	if err != nil {
		if strings.Contains(err.Error(), "command not found") {
			fmt.Println("please install the plugin first: composer global require ergebnis/composer-normalize")
			return
		}
		log.Fatalf("formatting failed: %v", err)
	}
	fmt.Println("formatting done:", output)
}

Advanced ​

  • To only check whether it is already normalized without modifying the file, use CheckNormalization().
  • This method depends on a third-party plugin; native Composer does not ship a normalize subcommand.
  • After formatting, running ValidateStrict for a strict validation is the recommended flow.

Released under the MIT License