Skip to main content

Package JSON

Helper functions for working with package.json files.

Functions

getPackageJSON()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:24

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>>


getPackageJSONDependencies()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:53

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<undefined | Record<string, string>>


getPackageJSONField()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:98

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 the field is not a string.

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<undefined | string>


getPackageJSONFieldMandatory()

function getPackageJSONFieldMandatory(filePathOrDirPathOrRecord, fieldName): Promise<string>

Defined in: packages/complete-node/src/functions/packageJSON.ts:144

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

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>


getPackageJSONFieldsMandatory()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:169

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

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

Promise<Record<T, string>>


getPackageJSONScripts()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:203

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<undefined | Record<string, string>>


getPackageJSONVersion()

function getPackageJSONVersion(filePathOrDirPathOrRecord): Promise<string>

Defined in: packages/complete-node/src/functions/packageJSON.ts:257

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

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<string>


packageJSONHasScript()

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

Defined in: packages/complete-node/src/functions/packageJSON.ts:293

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>