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
Parameter | Type | Description |
---|---|---|
filePathOrDirPath | undefined | string | 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
Record
<string
, unknown
>
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPath | undefined | string | 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
>>
Defined in
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
Parameter | Type | Default value | Description |
---|---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
dependencyType | DependencyType | "dependencies" | Optional. The specific dependencies field to get. Defaults to "dependencies". |
Returns
Record
<string
, string
> | undefined
Defined in
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
Parameter | Type | Default value | Description |
---|---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
dependencyType | DependencyType | "dependencies" | Optional. The specific dependencies field to get. Defaults to "dependencies". |
Returns
Promise
<Record
<string
, string
> | undefined
>
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
fieldName | string | The name of the field to retrieve. |
Returns
string
| undefined
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
fieldName | string | The name of the field to retrieve. |
Returns
Promise
<string
| undefined
>
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
fieldName | string | The name of the field to retrieve. |
Returns
string
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPath | undefined | string | 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
Record
<T
, string
>
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
...dependencyNames | readonly string [] | The name of the dependency to check for. |
Returns
boolean
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
scriptName | string | The name of the script to check for. |
Returns
boolean
Defined in
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
Parameter | Type | Description |
---|---|---|
filePathOrDirPathOrRecord | undefined | 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. |
scriptName | string | The name of the script to check for. |
Returns
Promise
<boolean
>
Defined in
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
Parameter | Type | Default value | Description |
---|---|---|---|
filePathOrDirPath | undefined | 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. |
dependencyName | string | undefined | The name of the dependency to update. |
version | string | undefined | The 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. |
dependencyType | DependencyType | "dependencies" | Optional. The specific dependencies field to update. Defaults to "dependencies". |
Returns
void
Defined in
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
Parameter | Type | Default value | Description |
---|---|---|---|
filePathOrDirPath | undefined | 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. |
dependencyName | string | undefined | The name of the dependency to update. |
version | string | undefined | The 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. |
dependencyType | DependencyType | "dependencies" | Optional. The specific dependencies field to update. Defaults to "dependencies". |
Returns
Promise
<void
>