简体   繁体   English

Angular Schematics,修改angular.json的代码并没有结合在一起

[英]Angular Schematics, code to modify angular.json is not coming together

I've been using schematics to generate an standardized project. 我一直在使用原理图来生成标准化项目。 I've had a couple of issues, all of them related to modification of the angular.json file 我有几个问题,所有问题都与angular.json文件的修改有关

The following code is a sample of what I've tried, is executed within a function that does work (except for another thing), and everything in it does gets executed except for this part, which throws an error: 下面的代码是我尝试过的示例,在一个可以工作的函数中执行(除了另一件事),并且其中的所有内容都会被执行,除了这部分,它会抛出一个错误:

Cannot read property 'includePaths' of undefined 无法读取未定义的属性'includePaths'

which doesn't make any sense to me. 这对我没有任何意义。

Another problem you can see here is that I can't seem to capture the name of the project, for I need to modify the content inside "architect" in the angular.json file, i have to go past the project's name, the workaround I have is to manually input the project's name as 你可以在这里看到的另一个问题是我似乎无法捕获项目的名称,因为我需要修改angular.json文件中“架构”内的内容,我必须通过项目的名称,解决方法我有手动输入项目的名称

ng add myschemat --name=new-app

But i should be able to capture this property, which I have tried and do get to see it in the console but when replacing options.name for p (from the commented forEach function) it doesn't work and shows that can't get 'architect property of undefined 但是我应该能够捕获这个属性,我已经尝试过并且确实可以在控制台中看到它但是当替换options.namep时 (来自注释的forEach函数)它不起作用并显示无法获取' undefined的建筑师财产

import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';

[...]


    if(_host.exists('angular.json')){
      const angularStr = _host.read('angular.json') !.toString('utf-8')
      const angular = JSON.parse(angularStr)
      const projs = (Object.keys( angular['projects'] )).slice(0, 1); //also tried [0]

      console.log("App Detection & your options: \n", projs, "\n", options)
      // projs.forEach((p: string | number) => {
        angular['projects'][options.name]['architect']['build']['options']['stylePreprocessorOptions'] = 
        { 
          "includePaths": ["src/scss/"] 
        }
        angular['projects'][options.name]['architect']['build']['options']['es5BrowserSupport']= true;
        angular['projects'][options.name]['architect']['build']['options']['styles'].push(
          [
            "node_modules/devextreme/dist/css/dx.common.css",
            "node_modules/devextreme/dist/css/dx.light.css",
            "src/styles.scss"
          ]
        )
      // });

      _host.overwrite('angular.json', JSON.stringify(angular, null, '\t'))

      _context.addTask( new NodePackageInstallTask )
    }

What should've happenned is to add the object property 应该发生的是添加对象属性

"styles": [
  "node_modules/devextreme/dist/css/dx.common.css",
  "node_modules/devextreme/dist/css/dx.light.css",
  "src/styles.scss"
],
"scripts": [],
"es5BrowserSupport": true,
"stylePreprocessorOptions": {
  "includePaths": [
    "src/scss/"
  ]
}

yet, when I compile ng add myschemat --name=new-app 然而,当我编译时ng add myschemat --name=new-app

the instalation stops and simply shows : Cannot read property 'includePaths' of undefined 安装停止并简单地显示: 无法读取未定义的属性'includePaths'

This is weird because it actually used to work, I just added a couple of styles and that true property and some changes to the package.json (and even though those happen before this angular.json modification, they don't seem to change anything 这很奇怪,因为它实际上曾经工作过,我只是添加了几个样式和真正的属性以及对package.json的一些更改(即使这些发生在此angular.json修改之前,它们似乎没有改变任何东西

Is there something I'm certainly missing here? 有什么我肯定在这里失踪的吗?

这不是一个解决方案,但我只是创建了另一个项目,复制了所有内容并且工作了。

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

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