func buildFn()

in cmd/nodejs/firebasenextjs/main.go [86:135]


func buildFn(ctx *gcp.Context) error {
	appDir := util.ApplicationDirectory(ctx)

	nodeDeps, err := nodejs.ReadNodeDependencies(ctx, appDir)
	if err != nil {
		return err
	}
	if nodeDeps.LockfilePath == "" {
		return gcp.UserErrorf("%w", faherror.MissingLockFileError(appDir))
	}

	version, err := nodejs.Version(nodeDeps, "next")
	if err != nil {
		ctx.Warnf("Error parsing version from lock file, defaulting to package.json version")
		version = nodeDeps.PackageJSON.Dependencies["next"]
	}
	err = validateVersion(ctx, version)
	if err != nil {
		return err
	}

	// TODO(b/357644160) We we should consider adding a validation step to double check that the adapter version works for the framework version.
	if version, exists := nodeDeps.PackageJSON.Dependencies["@apphosting/adapter-nextjs"]; exists {
		ctx.Logf("*** You already have @apphosting/adapter-nextjs@%s listed as a dependency, skipping installation ***", version)
		ctx.Logf("*** Your package.json build command will be run as is, please make sure it is set to apphosting-adapter-nextjs-build if you intend to build your app using the adapter ***")
		return nil
	}

	buildScript, exists := nodeDeps.PackageJSON.Scripts["build"]
	if exists && buildScript != "next build" && buildScript != "apphosting-adapter-nextjs-build" {
		ctx.Warnf("*** You are using a custom build command (your build command is NOT 'next build'), we will accept it as is but will error if output structure is not as expected ***")
	}

	njsl, err := ctx.Layer("npm_modules", gcp.BuildLayer, gcp.CacheLayer)
	if err != nil {
		return err
	}
	err = nodejs.InstallNextJsBuildAdaptor(ctx, njsl, version)
	if err != nil {
		return err
	}

	// pass nextjs version as environment variable that will configure the build for version matching
	njsl.BuildEnvironment.Override(frameworkVersion, version)

	// This env var indicates to the package manager buildpack that a different command needs to be run
	nodejs.OverrideNextjsBuildScript(njsl)

	return nil
}