Angular схемы: применить правила к текущему дереву, а затем изменить дерево - PullRequest
0 голосов
/ 07 января 2020

Я создаю схему Angular. Я хочу применить правило к текущему дереву, а затем изменить дерево.

, как описано в следующем фрагменте, я хочу применить созданное правило, поэтому tree.exists("./file.txt") возвращает true

export default function(options: any): Rule {
  return (tree: Tree, context: SchematicContext) => {
    let tmpl = apply(url("./files"), [template(options),move(".")]);
    let rule = chain([branchAndMerge(chain([mergeWith(tmpl)]))]);
     //how to apply this rule to the current tree, so it contains the files from ./files

    //assume ./files contain a file named myfile.txt
    console.log(tree.exists("./myfile.txt"))    


    tree.create("hello.txt", "");
    return tree;
  };
}

notes

return rule; создает /file.txt, но я хочу создать оба файла file.txt (через применение правил) и hello.txt с помощью tree.create ().

используя tree.create () перед применением правил, создает оба файла, но tree.exists('./file.txt') по-прежнему возвращает false.

1 Ответ

1 голос
/ 07 января 2020

РЕДАКТИРОВАТЬ

Просто вызовите chain без присвоения переменной:

export default function(options: any): Rule {
    return (tree: Tree) => {
        const templateSource = apply(
            url("./files"),
            [template(options), move(".")]
        );

        const rules: Rule[] = [];
        rules.push(mergeWith(templateSource, MergeStrategy.Overwrite));
        rules.push(createFiles());
        return chain(rules);
    };
}

function createFiles(): Rule {
    return (tree: Tree) => {
        // Check if ./files contain a file named myfile.txt
        if (tree.exists("./myfile.txt")) {
            // Do whatever you want.
        }

        // You should check if it exists. If it does, use ovewrite instead.
        tree.create("hello.txt", "");
        return tree;
    }
}

И всегда старайтесь предоставить некоторую стратегию слияния.

...