Package JSON
Helper functions for working with package.json files.
Functions
getPackageJSON()
function getPackageJSON(filePathOrDirPath): Promise<Record<string, unknown>>;
Defined in: functions/packageJSON.ts:27
Helper function to asynchronously get a "package.json" file as an object. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPath | string | undefined | Either the path to a "package.json" file or the path to a directory which contains a "package.json" file. If undefined is passed, the current working directory will be used. |
Returns
Promise<Record<string, unknown>>
getPackageJSONDependencies()
function getPackageJSONDependencies(filePathOrDirPathOrRecord, dependencyType): Promise<Record<string, string> | undefined>;
Defined in: functions/packageJSON.ts:55
Helper function to asynchronously get the "dependencies" or "devDependencies" or
"peerDependencies" field from a "package.json" file. If the corresponding field does not exist,
undefined will be returned.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
dependencyType | DependencyType | "dependencies" | Optional. The specific dependencies field to get. Defaults to "dependencies". |
Returns
Promise<Record<string, string> | undefined>
Throws
If the "package.json" file cannot be found or is otherwise invalid.
getPackageJSONField()
function getPackageJSONField(filePathOrDirPathOrRecord, fieldName): Promise<string | undefined>;
Defined in: functions/packageJSON.ts:103
Helper function to asynchronously get an arbitrary string field from a "package.json" file. If
the field does not exist, undefined will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
fieldName | string | The name of the field to retrieve. |
Returns
Promise<string | undefined>
Throws
If the "package.json" file cannot be found or the field is not a string.
getPackageJSONFieldMandatory()
function getPackageJSONFieldMandatory(filePathOrDirPathOrRecord, fieldName): Promise<string>;
Defined in: functions/packageJSON.ts:143
Helper function to asynchronously get an arbitrary string field from a "package.json" file. This will throw an error if the "package.json" file cannot be found or the field does not exist or if the field is not a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
fieldName | string | The name of the field to retrieve. |
Returns
Promise<string>
getPackageJSONFieldsMandatory()
function getPackageJSONFieldsMandatory<T>(filePathOrDirPath, ...fieldNames): Promise<Record<T, string>>;
Defined in: functions/packageJSON.ts:170
Helper function to asynchronously get N arbitrary string fields from a "package.json" file. This will throw an error if the "package.json" file cannot be found or any of the fields do not exist or any of the fields are not strings.
Type Parameters
| Type Parameter |
|---|
T extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPath | string | undefined | Either the path to a "package.json" file or the path to a directory which contains a "package.json" file. If undefined is passed, the current working directory will be used. |
...fieldNames | readonly T[] | The names of the fields to retrieve. |
Returns
Promise<Record<T, string>>
getPackageJSONScripts()
function getPackageJSONScripts(filePathOrDirPathOrRecord): Promise<Record<string, string> | undefined>;
Defined in: functions/packageJSON.ts:204
Helper function to asynchronously get the "scripts" field from a "package.json" file. If the
field does not exist, undefined will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
Returns
Promise<Record<string, string> | undefined>
Throws
If the "package.json" file cannot be found or is otherwise invalid.
getPackageJSONVersion()
function getPackageJSONVersion(filePathOrDirPathOrRecord): Promise<string>;
Defined in: functions/packageJSON.ts:252
Helper function to asynchronously get the "version" field from a "package.json" file. This will throw an error if the "package.json" file cannot be found or the "version" field does not exist or the "version" field is not a string.
If you want to allow for the "version" field to not exist, use the getPackageJSONField helper
function instead.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
Returns
Promise<string>
packageJSONHasScript()
function packageJSONHasScript(filePathOrDirPathOrRecord, scriptName): Promise<boolean>;
Defined in: functions/packageJSON.ts:283
Helper function to asynchronously check if a "package.json" file has a particular script. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.
Parameters
| Parameter | Type | Description |
|---|---|---|
filePathOrDirPathOrRecord | string | Readonly<Record<string, unknown>> | undefined | Either the path to a "package.json" file, the path to a directory which contains a "package.json" file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used. |
scriptName | string | The name of the script to check for. |
Returns
Promise<boolean>