Skip to content

Commit 74e2e2d

Browse files
author
Peter Smith
committed
chore(general): use insert utility
1 parent 257704c commit 74e2e2d

File tree

4 files changed

+36
-50
lines changed

4 files changed

+36
-50
lines changed
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Rule, Tree } from '@angular-devkit/schematics';
22
import { addDeclarationToModule } from '@schematics/angular/utility/ast-utils';
3-
import { InsertChange } from '@schematics/angular/utility/change';
4-
import { readIntoSourceFile } from '../utils';
3+
import { readIntoSourceFile, insert } from '../utils';
54

65
/**
76
* addDeclaration
@@ -17,19 +16,13 @@ export function addDeclaration(
1716
return (host: Tree) => {
1817
const source = readIntoSourceFile(host, modulePath);
1918

20-
const changes = addDeclarationToModule(
21-
source,
22-
modulePath,
23-
componentToImportName,
24-
componentToImportPath
25-
);
26-
27-
const declarationRecorder = host.beginUpdate(modulePath);
28-
for (const change of changes) {
29-
if (change instanceof InsertChange) {
30-
declarationRecorder.insertLeft(change.pos, change.toAdd);
31-
}
32-
}
33-
host.commitUpdate(declarationRecorder);
19+
insert(host, modulePath, [
20+
...addDeclarationToModule(
21+
source,
22+
modulePath,
23+
componentToImportName,
24+
componentToImportPath
25+
),
26+
]);
3427
};
3528
}
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Rule, Tree } from '@angular-devkit/schematics';
22
import { addExportToModule } from '@schematics/angular/utility/ast-utils';
3-
import { InsertChange } from '@schematics/angular/utility/change';
4-
import { readIntoSourceFile } from '../utils';
3+
import { insert, readIntoSourceFile } from '../utils';
54

65
/**
76
* addExport
@@ -17,19 +16,13 @@ export function addExport(
1716
return (host: Tree) => {
1817
const source = readIntoSourceFile(host, modulePath);
1918

20-
const changes = addExportToModule(
21-
source,
22-
modulePath,
23-
componentToImportName,
24-
componentToImportPath
25-
);
26-
27-
const declarationRecorder = host.beginUpdate(modulePath);
28-
for (const change of changes) {
29-
if (change instanceof InsertChange) {
30-
declarationRecorder.insertLeft(change.pos, change.toAdd);
31-
}
32-
}
33-
host.commitUpdate(declarationRecorder);
19+
insert(host, modulePath, [
20+
...addExportToModule(
21+
source,
22+
modulePath,
23+
componentToImportName,
24+
componentToImportPath
25+
),
26+
]);
3427
};
3528
}
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Rule, Tree } from '@angular-devkit/schematics';
22
import { addImportToModule } from '@schematics/angular/utility/ast-utils';
3-
import { InsertChange } from '@schematics/angular/utility/change';
4-
import { readIntoSourceFile } from '../utils';
3+
import { insert, readIntoSourceFile } from '../utils';
54

65
/**
76
* addImport
@@ -23,19 +22,13 @@ export function addImport(
2322

2423
const source = readIntoSourceFile(host, modulePath);
2524

26-
const changes = addImportToModule(
27-
source,
28-
modulePath,
29-
ngModuleToImportName,
30-
ngModuleToImportPath
31-
);
32-
33-
const declarationRecorder = host.beginUpdate(modulePath);
34-
for (const change of changes) {
35-
if (change instanceof InsertChange) {
36-
declarationRecorder.insertLeft(change.pos, change.toAdd);
37-
}
38-
}
39-
host.commitUpdate(declarationRecorder);
25+
insert(host, modulePath, [
26+
...addImportToModule(
27+
source,
28+
modulePath,
29+
ngModuleToImportName,
30+
ngModuleToImportPath
31+
),
32+
]);
4033
};
4134
}

libs/ddd/src/schematics/utils/insert.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
*/
77
import { Tree } from '@angular-devkit/schematics';
88
import { RemoveChange } from '@nrwl/workspace';
9-
import { Change, InsertChange } from '@schematics/angular/utility/change';
9+
import {
10+
Change,
11+
InsertChange,
12+
NoopChange,
13+
} from '@schematics/angular/utility/change';
1014

1115
/**
1216
* insert
@@ -20,7 +24,10 @@ export function insert(host: Tree, modulePath: string, changes: Change[]) {
2024
}
2125

2226
// sort changes so that the highest pos goes first
23-
const orderedChanges = changes.sort((a, b) => b.order - a.order);
27+
// and filter out noop changes
28+
const orderedChanges = changes
29+
.sort((a, b) => b.order - a.order)
30+
.filter((change) => !(change instanceof NoopChange));
2431

2532
const recorder = host.beginUpdate(modulePath);
2633
for (const change of orderedChanges) {

0 commit comments

Comments
 (0)