(
name: string,
specifier: string,
options: {
/**
* The type of the dependency determines the section of the `package.json` to which the
* dependency will be added. Defaults to {@link DependencyType.Default} (`dependencies`).
*/
type?: DependencyType;
/**
* The path of the package manifest file (`package.json`) that will be modified.
* Defaults to `/package.json`.
*/
packageJsonPath?: string;
/**
* The dependency installation behavior to use to determine whether a
* {@link NodePackageInstallTask} should be scheduled after adding the dependency.
* Defaults to {@link InstallBehavior.Auto}.
*/
install?: InstallBehavior;
/**
* The behavior to use when the dependency already exists within the `package.json`.
* Defaults to {@link ExistingBehavior.Replace}.
*/
existing?: ExistingBehavior;
} = {},
)
| 139 | * @returns A Schematics {@link Rule} |
| 140 | */ |
| 141 | export function addDependency( |
| 142 | name: string, |
| 143 | specifier: string, |
| 144 | options: { |
| 145 | /** |
| 146 | * The type of the dependency determines the section of the `package.json` to which the |
| 147 | * dependency will be added. Defaults to {@link DependencyType.Default} (`dependencies`). |
| 148 | */ |
| 149 | type?: DependencyType; |
| 150 | |
| 151 | /** |
| 152 | * The path of the package manifest file (`package.json`) that will be modified. |
| 153 | * Defaults to `/package.json`. |
| 154 | */ |
| 155 | packageJsonPath?: string; |
| 156 | |
| 157 | /** |
| 158 | * The dependency installation behavior to use to determine whether a |
| 159 | * {@link NodePackageInstallTask} should be scheduled after adding the dependency. |
| 160 | * Defaults to {@link InstallBehavior.Auto}. |
| 161 | */ |
| 162 | install?: InstallBehavior; |
| 163 | |
| 164 | /** |
| 165 | * The behavior to use when the dependency already exists within the `package.json`. |
| 166 | * Defaults to {@link ExistingBehavior.Replace}. |
| 167 | */ |
| 168 | existing?: ExistingBehavior; |
| 169 | } = {}, |
| 170 | ): Rule { |
| 171 | const { |
| 172 | type = DependencyType.Default, |
| 173 | packageJsonPath = '/package.json', |
| 174 | install = InstallBehavior.Auto, |
| 175 | existing = ExistingBehavior.Replace, |
| 176 | } = options; |
| 177 | |
| 178 | return (tree, context) => { |
| 179 | const manifest = tree.readJson(packageJsonPath) as MinimalPackageManifest; |
| 180 | const dependencySection = manifest[type]; |
| 181 | |
| 182 | if (!dependencySection) { |
| 183 | // Section is not present. The dependency can be added to a new object literal for the section. |
| 184 | manifest[type] = { [name]: specifier }; |
| 185 | } else { |
| 186 | const existingSpecifier = dependencySection[name]; |
| 187 | |
| 188 | if (existingSpecifier === specifier) { |
| 189 | // Already present with same specifier |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | if (existingSpecifier) { |
| 194 | // Already present but different specifier |
| 195 | |
| 196 | if (existing === ExistingBehavior.Skip) { |
| 197 | return; |
| 198 | } |
no test coverage detected