| 235 | * @returns A Schematics {@link Rule} |
| 236 | */ |
| 237 | export function removeDependency( |
| 238 | name: string, |
| 239 | options: { |
| 240 | /** |
| 241 | * The path of the package manifest file (`package.json`) that will be modified. |
| 242 | * Defaults to `/package.json`. |
| 243 | */ |
| 244 | packageJsonPath?: string; |
| 245 | |
| 246 | /** |
| 247 | * The dependency installation behavior to use to determine whether a |
| 248 | * {@link NodePackageInstallTask} should be scheduled after removing the dependency. |
| 249 | * Defaults to {@link InstallBehavior.Auto}. |
| 250 | */ |
| 251 | install?: InstallBehavior; |
| 252 | } = {}, |
| 253 | ): Rule { |
| 254 | const { packageJsonPath = '/package.json', install = InstallBehavior.Auto } = options; |
| 255 | |
| 256 | return (tree, context) => { |
| 257 | const manifest = tree.readJson(packageJsonPath) as MinimalPackageManifest; |
| 258 | let wasRemoved = false; |
| 259 | |
| 260 | for (const type of [DependencyType.Default, DependencyType.Dev, DependencyType.Peer]) { |
| 261 | const dependencySection = manifest[type]; |
| 262 | if (dependencySection?.[name]) { |
| 263 | delete dependencySection[name]; |
| 264 | wasRemoved = true; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (wasRemoved) { |
| 269 | tree.overwrite(packageJsonPath, JSON.stringify(manifest, null, 2)); |
| 270 | |
| 271 | const installPaths = installTasks.get(context) ?? new Set<string>(); |
| 272 | if ( |
| 273 | install === InstallBehavior.Always || |
| 274 | (install === InstallBehavior.Auto && !installPaths.has(packageJsonPath)) |
| 275 | ) { |
| 276 | context.addTask( |
| 277 | new NodePackageInstallTask({ workingDirectory: path.dirname(packageJsonPath) }), |
| 278 | ); |
| 279 | installPaths.add(packageJsonPath); |
| 280 | installTasks.set(context, installPaths); |
| 281 | } |
| 282 | } |
| 283 | }; |
| 284 | } |