2

我创建了一个角度示意图,用于使用 ng generate 命令创建样板代码,但是尽管我已经阅读了所有文章并搞砸了,但我无法创建文件。

我的原理图工厂中有以下代码:

export function createComponent(_options: any): Rule {
    return (tree: Tree, _context: SchematicContext) => {
        setupOptions(tree, _options);

        const movePath = normalize(_options.path + '/' + strings.dasherize(_options.name));

        const templateSource = apply(url('./files'), [
            template({
                ...strings,
                ..._options,
            }),
            move(movePath)
        ]);

        return mergeWith(templateSource);
    };
}

在与此相同的目录中是包含我的“__name@dasherize__.component.ts”模板的文件目录,我希望将其作为 url('./files') 函数的一部分拉入,但我不确定这正在发生。

我的模板文件如下:

import { Component } from '@angular/core';

@Component({
    selector: 'wx-<%= dasherize(componentName) %>'
})
export class <%= classify(componentName) %>Component {

}

我希望在运行原理图时会出现“CREATE”,但我得到的只是“无事可做”。

奇怪的是,使用 tree.create() 成功运行以创建文件,所以我想知道 url() 或 apply() 函数是否未按预期运行。

4

4 回答 4

4

确保“./files”是相对于dist文件夹中原理图文件的正确路径。

也许您需要将其更改为:“../../src/my-schematic/files”。

于 2020-01-08T20:41:51.157 回答
2

有同样的问题,但同时查看源代码和文档Schematics for Libraries模板文件应该以.ts.template扩展名结尾。

因此,在您的情况下,该文件应命名为__name@dasherize__.component.ts.template.

输出文件将包含.template扩展名。

链接到@angular/devkit 源:applyTemplates()

于 2020-01-19T18:50:57.433 回答
2

一位同事也有类似的问题,无论我如何更改他的代码,它似乎仍然在做同样烦人的事情。

它归结为最初用于运行原理图的命令。

前:

node_modules/.bin/schematics ./schematics:my-schematic-name --parameter

它应该是什么:

ng generate schematics:my-schematic-name --parameter

您的原理图需要在 Angular CLI 的上下文中运行,只需在 Node 中执行它,它似乎不会输出任何文件,但仍然以某种方式成功执行。

于 2020-11-30T13:50:06.177 回答
0

查看您的 .npmignore 和 .gitignore。也许您忽略了所有 .ts ,这使得 /files 文件夹下所有带有 .ts 的文件都将被忽略。

您的 .npmignore 可能如下所示:

# .npmignore
# Ignores TypeScript files, but keeps definitions and .ts under /files folder.
*.ts
!*.d.ts
!/src/**/files/*.ts

记得在更新你的 npm 包之前做一个 npm run build 来构建你的 ts 定义。

于 2019-10-29T13:06:10.717 回答