mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 11:03:53 +08:00
feat(@angular-devkit/schematics): add applyToSubTree
rule
This rule allows a group of rules to be applied to a scoped subdirectory of the current tree.
This commit is contained in:
parent
49133163e5
commit
0563e96cd2
@ -30,6 +30,8 @@ export declare function applyPathTemplate<T extends PathTemplateData>(data: T, o
|
||||
|
||||
export declare function applyTemplates<T>(options: T): Rule;
|
||||
|
||||
export declare function applyToSubtree(path: string, rules: Rule[]): Rule;
|
||||
|
||||
export declare function asSource(rule: Rule): Source;
|
||||
|
||||
export declare type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null>;
|
||||
|
@ -12,6 +12,7 @@ import { SchematicsException } from '../exception/exception';
|
||||
import { FilteredTree } from '../tree/filtered';
|
||||
import { FilterHostTree, HostTree } from '../tree/host-tree';
|
||||
import { FileEntry, FilePredicate, MergeStrategy, Tree } from '../tree/interface';
|
||||
import { ScopedTree } from '../tree/scoped';
|
||||
import {
|
||||
branch,
|
||||
empty as staticEmpty,
|
||||
@ -202,3 +203,21 @@ export function composeFileOperators(operators: FileOperator[]): FileOperator {
|
||||
return current;
|
||||
};
|
||||
}
|
||||
|
||||
export function applyToSubtree(path: string, rules: Rule[]): Rule {
|
||||
return (tree, context) => {
|
||||
const scoped = new ScopedTree(tree, path);
|
||||
|
||||
return callRule(chain(rules), observableOf(scoped), context).pipe(
|
||||
map(result => {
|
||||
if (result === scoped) {
|
||||
return tree;
|
||||
} else {
|
||||
throw new SchematicsException(
|
||||
'Original tree must be returned from all rules when using "applyToSubtree".',
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
};
|
||||
}
|
||||
|
@ -17,8 +17,9 @@ import { of as observableOf } from 'rxjs';
|
||||
import { Rule, SchematicContext, Source } from '../engine/interface';
|
||||
import { Tree } from '../tree/interface';
|
||||
import { empty } from '../tree/static';
|
||||
import { apply, chain } from './base';
|
||||
import { apply, applyToSubtree, chain } from './base';
|
||||
import { callRule, callSource } from './call';
|
||||
import { move } from './move';
|
||||
|
||||
|
||||
const context: SchematicContext = {
|
||||
@ -163,3 +164,21 @@ describe('partitionApplyMerge', () => {
|
||||
.then(done, done.fail);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyToSubtree', () => {
|
||||
it('works', done => {
|
||||
const tree = new HostTree();
|
||||
tree.create('a/b/file1', 'hello world');
|
||||
tree.create('a/b/file2', 'hello world');
|
||||
tree.create('a/c/file3', 'hello world');
|
||||
|
||||
callRule(applyToSubtree('a/b', [move('x')]), observableOf(tree), context)
|
||||
.toPromise()
|
||||
.then(result => {
|
||||
expect(result.exists('a/b/x/file1')).toBe(true);
|
||||
expect(result.exists('a/b/x/file2')).toBe(true);
|
||||
expect(result.exists('a/c/file3')).toBe(true);
|
||||
})
|
||||
.then(done, done.fail);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user