generatePackageBanner
Generate a standardized banner comment from package.json metadata for bundled output files.
Overview
generatePackageBanner generates a standardized banner comment using metadata from the nearest package.json file or a provided package object.
It automatically resolves package information, applies safe fallbacks for missing fields, and returns a consistent, readable banner suitable for bundled or distributed output files.
- Resolved banner fields include:
- Title ➔
options.title, otherwisepackage.json.name, otherwiseUnknown Package. - Version ➔
package.json.version, otherwiseUnknown. - Author ➔
options.author,package.json.author, orpackage.json.author.name, otherwiseUnknown. - Repository ➔
package.json.homepage,repository, orrepository.url(normalized), otherwiseUnknown.
- Title ➔
- Additional behavior:
- Automatically strips
git+prefixes from repository URLs. - Automatically removes trailing
.gitsuffixes. - Supports optional final newline output via
withEof.
- Automatically strips
Why use this?
- Automatically reads metadata from the nearest
package.json. - Accepts a custom
packageJsonobject when needed. - Provides safe fallbacks for missing fields.
- Produces consistent banner output across builds.
- Normalizes repository URLs automatically.
- Supports optional final newline output.
- Ideal for bundler banners (
tsdown,tsup,rollup,esbuild, etc.). - Useful for generated artifacts and distributed library files.
Importing
Syntax
Parameters
Prop
Type
options
Optional configuration object that defines how the utility function should behave in various scenarios, allowing customization and greater control at runtime.
title
-
Description:
Display title shown in the banner. -
Required:
-
Type:
string -
Default:
stringNotes
- Defaults to the
namefield frompackage.jsonwhen available, otherwise return"Unknown Package".
- Defaults to the
-
Example:
TypeScript/JavaScript
Notes
- Invalid values or empty-string are automatically reset to default.
author
-
Description:
Author name displayed in the banner.When omitted, attempts to resolve from:
author(If empty-string or unset will check toauthor.name).author.name(If empty-string or unset will return"Unknown").
-
Required:
-
Type:
string -
Default:
string -
Example:
TypeScript/JavaScript
Notes
- Invalid values or empty-string are automatically reset to default.
packageJson
-
Description:
Custom package metadata used instead of the automatically loadedpackage.json.Overrides the default behavior of loading
package.jsonviagetPackageJson(). -
Required:
-
Type:
object -
Default:
Loaded fromgetPackageJson().Notes
- Automatically locates and loads the nearest
package.json. - Throws an error if:
- No
package.jsonis found. - The file exists but contains invalid JSON.
- No
- Automatically locates and loads the nearest
-
Example:
TypeScript/JavaScript
Notes
- Invalid values are automatically reset to default.
withEof
-
Description:
Append an end-of-file newline to the banner output.- When enabled, a trailing newline is appended to ensure:
- POSIX-compliant text files.
- Clean concatenation with subsequent source content.
- Consistent formatting during code injection.
- When enabled, a trailing newline is appended to ensure:
-
Required:
-
Type:
boolean -
Default:
true -
Example:
TypeScript/JavaScript
Returns
Returns a Promise<string>.
The function performs asynchronous file processing and return a string value of banner generated.
Throws
Throws a built-in JavaScript error in the following cases:
- No
package.jsonfile can be found while walking up the directory tree. - The located
package.jsoncontains invalid JSON and cannot be parsed.
Version information
- Since:
v0.0.7. - Category:
generate. - Stability: ✅
Stable(Production-ready).
