Skip to content

Commit a31533c

Browse files
committed
fix(@angular-devkit/schematics): respect --force option when schematic contains host.create
Removes the `FileAlreadyExistException` check within the `create` method of `HostTree`. This change allows a schematic to create a file even if one already exists at the same path, effectively overwriting it. This provides more flexibility for schematic authors, particularly in scenarios where files need to be replaced or updated unconditionally. It is intended to be used with schematics that have a `force` or `overwrite` option. Closes #30578 (cherry picked from commit 18bf8e7)
1 parent 72bc4da commit a31533c

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

packages/angular_devkit/schematics/src/tree/host-tree.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
import { ParseError, parse as jsoncParse, printParseErrorCode } from 'jsonc-parser';
2121
import {
2222
ContentHasMutatedException,
23-
FileAlreadyExistException,
2423
FileDoesNotExistException,
2524
InvalidUpdateRecordException,
2625
MergeConflictException,
@@ -407,12 +406,8 @@ export class HostTree implements Tree {
407406

408407
// Structural methods.
409408
create(path: string, content: Buffer | string): void {
410-
const p = this._normalizePath(path);
411-
if (this._recordSync.exists(p)) {
412-
throw new FileAlreadyExistException(p);
413-
}
414409
const c = typeof content == 'string' ? Buffer.from(content) : content;
415-
this._record.create(p, c as {} as virtualFs.FileBuffer).subscribe();
410+
this._record.create(this._normalizePath(path), c as {} as virtualFs.FileBuffer).subscribe();
416411
}
417412
delete(path: string): void {
418413
this._recordSync.delete(this._normalizePath(path));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { appendFile } from 'node:fs/promises';
2+
import { getGlobalVariable } from '../../utils/env';
3+
import { getActivePackageManager, installWorkspacePackages } from '../../utils/packages';
4+
import { ng } from '../../utils/process';
5+
import { isPrereleaseCli, updateJsonFile } from '../../utils/project';
6+
import { expectToFail } from '../../utils/utils';
7+
8+
const snapshots = require('../../ng-snapshot/package.json');
9+
10+
export default async function () {
11+
const isPrerelease = await isPrereleaseCli();
12+
let tag = isPrerelease ? '@next' : '';
13+
if (getActivePackageManager() === 'npm') {
14+
await appendFile('.npmrc', '\nlegacy-peer-deps=true');
15+
}
16+
17+
await ng('add', `@angular/material${tag}`, '--skip-confirmation');
18+
19+
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
20+
if (isSnapshotBuild) {
21+
await updateJsonFile('package.json', (packageJson) => {
22+
const dependencies = packageJson['dependencies'];
23+
// Angular material adds dependencies on other Angular packages
24+
// Iterate over all of the packages to update them to the snapshot version.
25+
for (const [name, version] of Object.entries(snapshots.dependencies)) {
26+
if (name in dependencies) {
27+
dependencies[name] = version;
28+
}
29+
}
30+
});
31+
await installWorkspacePackages();
32+
}
33+
34+
const args: string[] = [
35+
'generate',
36+
'@angular/material:theme-color',
37+
'--primary-color=#0641e6',
38+
'--tertiary-color=#994aff',
39+
'--neutral-color=#313138',
40+
'--error-color=#eb5757',
41+
'--secondary-color=#009096',
42+
'--neutral-variant-color=#b2b2b8',
43+
];
44+
45+
await ng(...args);
46+
47+
// Should fail as file exists
48+
await expectToFail(() => ng(...args));
49+
50+
await ng(...args, '--force');
51+
}

0 commit comments

Comments
 (0)