Skip to content

Commit 0b0961c

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/schematics): minor cleanup to reduce utility imports
1 parent 37686b6 commit 0b0961c

File tree

2 files changed

+55
-61
lines changed

2 files changed

+55
-61
lines changed

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

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
PathFragment,
1111
PathIsDirectoryException,
1212
PathIsFileException,
13-
clean,
1413
dirname,
1514
join,
1615
normalize,
@@ -369,52 +368,53 @@ export class HostTree implements Tree {
369368
apply(action: Action, strategy?: MergeStrategy): void {
370369
throw new SchematicsException('Apply not implemented on host trees.');
371370
}
371+
372+
private *generateActions(): Iterable<Action> {
373+
for (const record of this._record.records()) {
374+
switch (record.kind) {
375+
case 'create':
376+
yield {
377+
id: this._id,
378+
parent: 0,
379+
kind: 'c',
380+
path: record.path,
381+
content: Buffer.from(record.content),
382+
} as CreateFileAction;
383+
break;
384+
case 'overwrite':
385+
yield {
386+
id: this._id,
387+
parent: 0,
388+
kind: 'o',
389+
path: record.path,
390+
content: Buffer.from(record.content),
391+
} as OverwriteFileAction;
392+
break;
393+
case 'rename':
394+
yield {
395+
id: this._id,
396+
parent: 0,
397+
kind: 'r',
398+
path: record.from,
399+
to: record.to,
400+
} as RenameFileAction;
401+
break;
402+
case 'delete':
403+
yield {
404+
id: this._id,
405+
parent: 0,
406+
kind: 'd',
407+
path: record.path,
408+
} as DeleteFileAction;
409+
break;
410+
}
411+
}
412+
}
413+
372414
get actions(): Action[] {
373415
// Create a list of all records until we hit our original backend. This is to support branches
374416
// that diverge from each others.
375-
const allRecords = [...this._record.records()];
376-
377-
return clean(
378-
allRecords
379-
.map(record => {
380-
switch (record.kind) {
381-
case 'create':
382-
return {
383-
id: this._id,
384-
parent: 0,
385-
kind: 'c',
386-
path: record.path,
387-
content: Buffer.from(record.content),
388-
} as CreateFileAction;
389-
case 'overwrite':
390-
return {
391-
id: this._id,
392-
parent: 0,
393-
kind: 'o',
394-
path: record.path,
395-
content: Buffer.from(record.content),
396-
} as OverwriteFileAction;
397-
case 'rename':
398-
return {
399-
id: this._id,
400-
parent: 0,
401-
kind: 'r',
402-
path: record.from,
403-
to: record.to,
404-
} as RenameFileAction;
405-
case 'delete':
406-
return {
407-
id: this._id,
408-
parent: 0,
409-
kind: 'd',
410-
path: record.path,
411-
} as DeleteFileAction;
412-
413-
default:
414-
return;
415-
}
416-
}),
417-
);
417+
return Array.from(this.generateActions());
418418
}
419419
}
420420

packages/angular_devkit/schematics/tools/file-system-engine-host-base.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
InvalidJsonCharacterException,
1111
JsonObject,
1212
UnexpectedEndOfInputException,
13-
isPromise,
1413
normalize,
1514
virtualFs,
1615
} from '@angular-devkit/core';
@@ -21,10 +20,8 @@ import {
2120
Observable,
2221
from as observableFrom,
2322
isObservable,
24-
of as observableOf,
2523
throwError,
2624
} from 'rxjs';
27-
import { mergeMap } from 'rxjs/operators';
2825
import { Url } from 'url';
2926
import {
3027
HostCreateTree,
@@ -301,20 +298,17 @@ export abstract class FileSystemEngineHostBase implements FileSystemEngineHost {
301298
options: OptionT,
302299
context?: FileSystemSchematicContext,
303300
): Observable<ResultT> {
304-
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
305-
return ((observableOf(options) as any)
306-
.pipe(
307-
...this._transforms.map(tFn => mergeMap((opt: {}) => {
308-
const newOptions = tFn(schematic, opt, context);
309-
if (isObservable(newOptions)) {
310-
return newOptions;
311-
} else if (isPromise(newOptions)) {
312-
return observableFrom(newOptions);
313-
} else {
314-
return observableOf(newOptions);
315-
}
316-
})),
317-
)) as {} as Observable<ResultT>;
301+
const transform = async () => {
302+
let transformedOptions = options;
303+
for (const transformer of this._transforms) {
304+
const transformerResult = transformer(schematic, transformedOptions, context);
305+
transformedOptions = await (isObservable(transformerResult) ? transformerResult.toPromise() : transformerResult);
306+
}
307+
308+
return transformedOptions;
309+
};
310+
311+
return observableFrom(transform()) as unknown as Observable<ResultT>;
318312
}
319313

320314
transformContext(context: FileSystemSchematicContext): FileSystemSchematicContext {

0 commit comments

Comments
 (0)