@build-tracker/api-client
The @build-tracker/api-client
package includes node.js functions for creating and uploading builds to your Build Tracker instance’s API.
Important note: Set up the Build Tracker API configuration close to your application's code (in the same repository and workspace). Remember that the configuration for the API is not the same as the configuration for the server.
Install
Configuration
The Build Tracker API is easily configured using a cosmiconfig compatible file.
Starting from the current working directory, it will look for the following possible sources, in this order:
- a
build-tracker
property in yourpackage.json
- a
.build-tracerrc
file - a
build-tracker.config.js
file exporting a JS object
The ..build-trackerrc
file (without extension) can be in JSON or YAML format.
Alternately, you can add a filename extension to designate JSON, YAML, or JS format: .build-trackerrc.json
, .build-trackerrc.yaml
, .build-trackerrc.yml
, .build-trackerrc.js
. You may want to use an extension so that your text editor can better interpret the file, and help with syntax checking and highlighting.
Once one of these is found and parsed, the search will stop and that object will be used.
The configuration search can also be short-circuited by passing a --config
argument with a path to your configuration file.
applicationUrl: string
The full URL (with scheme) to your application. New builds will be posted to the API available at this URL
artifacts: Array<string>
An array of glob paths to search for your artifacts.
baseDir?: string = process.cwd()
A base directory option to calculate relative paths for your artifacts. This is most often useful for stripping off unwanted paths, like dist
, though it must be an absolute path on your system. This can be most easily accomplished by using the path
node API:
cwd?: string = process.cwd()
The command working directory. Set this to change the script working path to something else other than process.cwd()
getFilenameHash?: (filename: string) => string | null
This optional method allows you to extract a filename hash from the built artifact filename. Many applications built with webpack will include a chunk hash in the filename for cache-busting reasons.
It's important to register filename hashes in Build Tracker builds foe each artifact to know if your service is requiring users to redownload files, even though their functional content has not changed.
For example, if your dist folder looks like this:
You can extract the hash from the filename with a function:
nameMapper?: (filename: string) => string
Similar to the getFilenameHash
method above, you may want to strip out parts of the artifact filenames to make them more legible in reports and the application.
buildUrlFormat?: string
You may want to link each build to a commit. Provide buildUrlFormat
in the following format to map revision value to an url.
onCompare?: (data: APIResponse) => Promise<void>
Take any action on the response from the API.
The data response consists of a lot of useful information. Depending on how you want to report information, you may only need part of it.
More can be done with the data. Check out the Continuous Integration guide for more ideas.
API
getConfig(path?: string): Promise<Config>
Reads your Build Tracker Utility configuration. If a path is not provided, this will use cosmiconfig to find the appropriate file.
createBuild(config: Config, opts: Options): Promise<Build>
Creates a JSON-friendly build configuration, suitable for POSTing to the Build Tracker API.
getBuild(config: Config, revision: string): Promise<Build>
Gets a Build
via a revision string.
statArtifacts(config: Config): Map<string, Stat>
Reads the artifacts from disk and builds a map of filenames to size stats. This is done automatically by createBuild
; it is only included in case you want to create builds with a custom script.
uploadBuild(config: Config, build: Build, apiToken?: string, logger: { log, error }): Promise<ApiReturn>
Uploads a Build to your Build Tracker instance.