【问题标题】:Angular schematics - file not installing角度示意图 - 文件未安装
【发布时间】:2019-08-07 21:03:20
【问题描述】:

我正在构建一个 Angular 自定义示意图。 我创建了 files 目录,它看起来像这样:


我正在尝试将此组件添加到最终应用程序中。 我的索引文件如下所示:

function addSideNav(options: any) {
return (host: Tree, context: SchematicContext) => {
try {

  host.delete('src/app/app.component.html');

  const workspace = getWorkspace(host);
  if (!options.project) {
    options.project = Object.keys(workspace.projects)[0];
  }
  const project = getProject(host, options.project);

  if (options.path === undefined) {
    options.path = buildDefaultPath(project);
  }

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

  // context.logger.log("info", `✅️ Added SideNav to the tree`);
  return mergeWith(templateSource);

} catch (e) {
  context.logger.log("error", `???? Failed to add the SideNav to the tree`);
  throw new SchematicsException(`Error detailes: ${e}`);
}};}

之后我这样调用这个函数:

export default function (options: any): Rule {
return chain([
    addPackageJsonDependencies(),
    installPackageJsonDependencies(),
    addSideNav(options),
    addMaterialStyle(options),
    addPolyfillToScripts(options),
    addToAppModule(options)

]); }

当我在本地使用 npm 链接对其进行测试时 - 效果很好。一切都创建得很好。

但是当我将它发布到 npm 并从 npm 安装时 - 除了 ts 文件之外的所有文件都已创建:

side.nav.component.ts

我没有收到任何警告。

我在这里错过了什么?

【问题讨论】:

    标签: angular angular-schematics


    【解决方案1】:

    解决了。 事实证明这是发布到 npm 的问题。出于某种未知原因,创建了一个 .npmignore 文件 - 不知道是如何创建的,在其中我看到了这个:

    *.ts
    

    所以基本上它在我发布时忽略了所有的打字稿文件。

    【讨论】:

      【解决方案2】:

      默认情况下它会忽略 .ts 文件,因为 shematics 在构建版本上运行,并且不需要来自原理图 api 的 index.ts 或 schema.ts 文件等源文件。所以要小心,你应该只通过添加类似的东西来忽略想要的目录:

      # Ignores TypeScript files, but keeps definitions.
      *.ts
      !*.d.ts    
      # Not ignoring component files
      !src/your-schematics/files/**/*.ts
      

      【讨论】:

      • 感谢您的解释。我想知道为什么会这样。
      • 哈哈没问题,我最近也遇到了同样的问题!
      猜你喜欢
      • 2019-02-14
      • 2018-12-07
      • 2019-04-21
      • 2018-08-04
      • 1970-01-01
      • 2018-12-26
      • 2019-11-02
      • 2019-08-23
      • 2018-12-24
      相关资源
      最近更新 更多