Skip to main content

functions/packageJSON

Functions

getPackageJSON()

function getPackageJSON(filePathOrDirPath): Record<string, unknown>

Helper function to synchronously 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

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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

Record<string, unknown>

Defined in

functions/packageJSON.ts:23


getPackageJSONAsync()

function getPackageJSONAsync(filePathOrDirPath): Promise<Record<string, unknown>>

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

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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>>

Defined in

functions/packageJSON.ts:46


getPackageJSONDependencies()

function getPackageJSONDependencies(filePathOrDirPathOrRecord, dependencyType): Record<string, string> | undefined

Helper function to synchronously get the "dependencies" or "devDependencies" or "peerDependencies" field from a "package.json" file. If the corresponding field does not exist, undefined will be returned.

This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>undefinedEither 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.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to get. Defaults to "dependencies".

Returns

Record<string, string> | undefined

Defined in

functions/packageJSON.ts:75


getPackageJSONDependenciesAsync()

function getPackageJSONDependenciesAsync(filePathOrDirPathOrRecord, dependencyType): Promise<Record<string, string> | undefined>

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.

This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>undefinedEither 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.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to get. Defaults to "dependencies".

Returns

Promise<Record<string, string> | undefined>

Defined in

functions/packageJSON.ts:104


getPackageJSONField()

function getPackageJSONField(filePathOrDirPathOrRecord, fieldName): string | undefined

Helper function to synchronously get an arbitrary string field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

string | undefined

Defined in

functions/packageJSON.ts:156


getPackageJSONFieldAsync()

function getPackageJSONFieldAsync(filePathOrDirPathOrRecord, fieldName): Promise<string | undefined>

Helper function to asynchronously get an arbitrary string field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

Promise<string | undefined>

Defined in

functions/packageJSON.ts:202


getPackageJSONFieldMandatory()

function getPackageJSONFieldMandatory(filePathOrDirPathOrRecord, fieldName): string

Helper function to synchronously get an arbitrary string field from a "package.json" file. This will throw an error if the field does not exist or if the "package.json" file cannot be found.

Also see the getPackageJSONField function.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

string

Defined in

functions/packageJSON.ts:249


getPackageJSONFieldsMandatory()

function getPackageJSONFieldsMandatory<T>(filePathOrDirPath, ...fieldNames): Record<T, string>

Helper function to synchronously get N arbitrary string fields from a "package.json" file. This will throw an error if any of the fields do not exist or if the "package.json" file cannot be found.

Also see the getPackageJSONFieldMandatory function.

Type Parameters

Type Parameter
T extends string

Parameters

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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.
...fieldNamesreadonly T[]The names of the fields to retrieve.

Returns

Record<T, string>

Defined in

functions/packageJSON.ts:277


getPackageJSONScripts()

function getPackageJSONScripts(filePathOrDirPathOrRecord): Record<string, string> | undefined

Helper function to synchronously get the "scripts" field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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

Record<string, string> | undefined

Defined in

functions/packageJSON.ts:308


getPackageJSONScriptsAsync()

function getPackageJSONScriptsAsync(filePathOrDirPathOrRecord): Promise<Record<string, string> | undefined>

Helper function to asynchronously get the "scripts" field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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>

Defined in

functions/packageJSON.ts:359


getPackageJSONVersion()

function getPackageJSONVersion(filePathOrDirPathOrRecord): string

Helper function to synchronously get the "version" field from a "package.json" file. This will throw an error if the "package.json" file cannot be found or is otherwise invalid. It will also throw an error if the "version" field does not exist.

If you want to allow for the "version" field not existing, use the getPackageJSONField helper function instead.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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

string

Defined in

functions/packageJSON.ts:413


isPackageJSONDependency()

function isPackageJSONDependency(filePathOrDirPathOrRecord, ...dependencyNames): boolean

Helper function to synchronously check if a "package.json" file has a particular dependency. Both the "dependencies" and the "devDependencies" fields will be checked. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

This function is variadic, meaning that you can pass as many dependency names as you want to check for. This function will return true if one or more dependencies were found.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
...dependencyNamesreadonly string[]The name of the dependency to check for.

Returns

boolean

Defined in

functions/packageJSON.ts:451


packageJSONHasScript()

function packageJSONHasScript(filePathOrDirPathOrRecord, scriptName): boolean

Helper function to synchronously 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

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
scriptNamestringThe name of the script to check for.

Returns

boolean

Defined in

functions/packageJSON.ts:493


packageJSONHasScriptAsync()

function packageJSONHasScriptAsync(filePathOrDirPathOrRecord, scriptName): Promise<boolean>

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

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
scriptNamestringThe name of the script to check for.

Returns

Promise<boolean>

Defined in

functions/packageJSON.ts:524


setPackageJSONDependency()

function setPackageJSONDependency(
filePathOrDirPath,
dependencyName,
version,
dependencyType): void

Helper function to synchronously set a dependency in a "package.json" file to a new value. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathundefined | stringundefinedEither 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.
dependencyNamestringundefinedThe name of the dependency to update.
versionstringundefinedThe new value for the dependency field. Note that most of the time, the version should have a "^" character prefix to indicate that patch updates should automatically be downloaded by the package manager.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to update. Defaults to "dependencies".

Returns

void

Defined in

functions/packageJSON.ts:559


setPackageJSONDependencyAsync()

function setPackageJSONDependencyAsync(
filePathOrDirPath,
dependencyName,
version,
dependencyType): Promise<void>

Helper function to asynchronously set a dependency in a "package.json" file to a new value. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathundefined | stringundefinedEither 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.
dependencyNamestringundefinedThe name of the dependency to update.
versionstringundefinedThe new value for the dependency field. Note that most of the time, the version should have a "^" character prefix to indicate that patch updates should automatically be downloaded by the package manager.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to update. Defaults to "dependencies".

Returns

Promise<void>

Defined in

functions/packageJSON.ts:596