简体   繁体   English

扩展 Angular Schematics

[英]Extending Angular Schematics

What is the best way to extend an existing Angular Schematic?扩展现有 Angular Schematic 的最佳方法是什么? I'm looking specifically at adding some default code into a component's spec file at the moment, but a more generalized answer would be appreciated.我目前正在专门研究将一些默认代码添加到组件的规范文件中,但更通用的答案将不胜感激。

In the tutorials I've found, they show the externalSchematic function, which seems to be on the right track, but none of them show how to update/replace that schematic's template file.在我找到的教程中,它们显示了 externalSchematic 函数,这似乎是在正确的轨道上,但没有一个显示如何更新/替换该原理图的模板文件。 I've tried just copying the templates into my schematic and applying/merging them, but that seems a little overkill.我已经尝试将模板复制到我的原理图中并应用/合并它们,但这似乎有点矫枉过正。 Angular's documentation on this matter also seems scarce. Angular 关于这个问题的文档似乎也很少。

Is there a way to do extend the default schematics, or will I need to do everything from scratch?有没有办法扩展默认原理图,还是我需要从头开始做所有事情?

I definitely agree that the documentation is sparse.我绝对同意文档很少。 The most helpful resource I found for extending existing schematics is this article:https://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2我发现扩展现有原理图最有用的资源是这篇文章:https ://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2

Under the heading 'Calling Another Schematic', it has some example code on how to modify a newly create component, which seems like what you're looking for.在“调用另一个原理图”标题下,它有一些关于如何修改新创建的组件的示例代码,这似乎是您正在寻找的。 At the end of the day, you should call the existing angular component schematic and then find the file(s) you want to modify (in your case, the .spec.ts file), and finally insert the new code you want:归根结底,您应该调用现有的角度组件原理图,然后找到要修改的文件(在您的情况下为 .spec.ts 文件),最后插入您想要的新代码:

import { Rule, SchematicContext, Tree, chain, externalSchematic } from '@angular-devkit/schematics';

const sText = "test to insert";

return chain([
    // Here you can modify options or do any preprocessing before calling the schematic (optional)
    (cTree: Tree, _context: SchematicContext) => {     
        return cTree;
    },

    // Call the external schematic
    externalSchematic('@schematics/angular', 'component', _options),

    // Do whatever downstream processing you need to 
    (cTree: Tree, _context: SchematicContext) => {

        // Find new component, which depends on where you put it in the tree
        // Some approaches prefer to scan the entire tree looking for the new file,
        // I prefer to narrow my search down a bit.
        cTree.getDir('.')
            .visit((sTempFilePath) => {

                if (!sTempFilePath.endsWith(dasherize(_options['name']) + '.component.spec.ts')) {
                    // Skip anything but the newly added typescript spec file
                    return;
                }

                // Now that we've found our new component file, read in the content
                const cContentBuffer = cTree.read(sTempFilePath);
                if (!cContentBuffer) {
                    return;
                }

                // Add text at beginning of file (can customize to add anywhere)
                cTree.overwrite(
                    sTempFilePath,
                    sText + cContentBuffer);

            });

        return cTree;       
    }

]);

One final note - sometimes I've found that I need to include a schema.json in my own schematic in order to utilize the angular schematics.最后一个注意事项 - 有时我发现我需要在我自己的原理图中包含一个 schema.json 以利用角度原理图。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM