首页 > 解决方案 > 角度原理图无法删除文件

问题描述

我有一个自定义原理图,我想在其中将一些文件添加到标准角度应用原理图中。我通过运行 mergeWith 来添加一些我自己的自定义文件来做到这一点。稍后由于某些设置,我希望删除我自己的一个文件。删除操作不会给出任何错误或警告,如果我之后检查树,它看起来好像文件已被删除。但是,该文件仍存储在文件系统中。我无法弄清楚为什么它不起作用。相关代码如下:

这是运行我的收藏的“ng new”时执行的操作。

import {
    apply,
    chain,
    empty,
    externalSchematic,
    mergeWith,
    move,
    //noop,
    Rule,
    schematic, SchematicContext,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { Schema as AngularNgNewSchema, Style } from '@schematics/angular/ng-new/schema';
import { CamcoApplicationSchema } from '../camco-application/schema';
//import { CamcoLibrarySchema } from '../library/schema';
//import { CamcoPortalApplicationSchema } from '../portal-application/schema';
//import { CamcoVisualStudioSchema } from '../visual-studio/schema';
import { CamcoNewSchema } from './schema';

/**
 * This schematic generates a new application.
 * It wraps the Angular ng-new command and modifies the output and configuration to generate a Camco web application.
 *
 * @param options
 */
export default function run(options: CamcoNewSchema): Rule {
    if(options.library && options.portal)
        throw new Error(`Cannot scaffold both a library project and a portal module in the same project. Provide either 'library' or 'portal' as argument.`);
    const angularOptions: AngularNgNewSchema = {
        name: options.name, // Use the supplied name.
        directory: '.', // Generate in the "current" directory, this is because other schematics operate on "." and would otherwise have to traverse the tree.
        version: '9.0.1', // The version of the Angular CLI to set in the project, we use the version used to generate these schematics.
        minimal: true, // Generate absolutely minimal project, no testing, linting, ...
        style: Style.Scss, // Use SCSS for stylesheets
        routing: true, // Scaffold routing
        skipTests: true, // We don't want pesky "spec" files all over
        skipInstall: true, // Do not run "yarn install" since we change the directory of the project after it's generation, yarn would run in the wrong folder.
        skipGit: true, // Do not initialize Git, usually projects are scaffolded *inside* a Git repo, and you don't want them to nest.
    };  
    //let libraryName: string | undefined = options.library || options.portal;
    return chain([
        mergeWith(apply(empty(), [ // Start from an empty file structure
            externalSchematic('@schematics/angular', 'ng-new', angularOptions), // Generate a standard Angular project
            schematic<CamcoApplicationSchema>('camco-application', { name: options.name }, { interactive: true }), // Scaffold Camco boilerplate stuff
            //(libraryName !== undefined ? schematic<CamcoLibrarySchema>('library', { project: options.name, name: libraryName }, { interactive: true }) : noop()), // If a library or portal name was passed, run the library schematic. Otherwise run a noop()
            //(options.portal !== undefined ? schematic<CamcoPortalApplicationSchema>('portal-application', { project: options.name, name: options.portal }, { interactive: true }) : noop()), // If a portal name was passed, run the PortalApplication schematic. Otherwise run a noop()
            //schematic<CamcoVisualStudioSchema>('visual-studio', { name: options.name, type: options['project-type'] }, { interactive: true }), // Generate a Visual Studio project file
            move((options.directory || options.name)), // Move the files to the project folder instead of generating in the folder the command was ran in.
        ])),
        (_, context: SchematicContext) => {
            if (options.install !== false) {
                context.addTask(new NodePackageInstallTask({
                    packageManager: 'yarn',
                    workingDirectory: (options.directory || options.name),
                }));
            }
        },
    ]);
}

这是我要运行的自定义代码。请注意,我已经尝试不使用“move”和“overwriteIfExists”。'overwriteIfExists' 从另一个 SO 复制到解决文件已存在错误。请参阅:如何用角度示意图覆盖文件?

import {strings} from '@angular-devkit/core';
import {apply, chain, MergeStrategy, mergeWith, Rule, template, Tree, url, move} from '@angular-devkit/schematics';
//import { configureAngularJson } from './operations/angular-json';
//import { configurePackageJson } from './operations/package-json';
import { CamcoApplicationSchema } from './schema';
import {overwriteIfExists} from "../utils/filesystem";


/**
 * This schematic handles the Camco specific configuration of a standard Angular project.
 * It assumes the project was generated using the "minimal" flag of the Angular ng-new schematic.
 *
 * @param options
 */
export default function run(options: CamcoApplicationSchema): Rule {
    return (host: Tree) => {
        return chain([
            // Scaffold Camco boilerplate in the project
            mergeWith(apply(url('./files'), [
                template({
                    ...strings,
                    name: options.name,
                }),
                overwriteIfExists(host),
                move('.')
            ]), MergeStrategy.Overwrite), // If there's a file already in the tree that we scaffold, scaffold gets priority
            //configurePackageJson(options), // Configure the package.json
            //configureAngularJson(options), // Configure the Angular.json
            deleteOneFile()
        ]);
    }   
}

export function  deleteOneFile(): Rule {
    return (host: Tree) => {
        console.log(host.exists('/src/app/app-routing.module.ts'));
        host.delete('/src/app/app-routing.module.ts');
        console.log(host.exists('/src/app/app-routing.module.ts'));
    }

}

这很奇怪,因为如果我注释掉默认的角度示意图(externalSchematics),我的文件就会被删除。但是有了它,我似乎无法摆脱我的文件!?它似乎与“移动”方法有关。这个的输出是:

"C:\Program Files\nodejs\node.exe" F:\Git\Web\BuildTools\schematics\node_modules\@angular-devkit\schematics-cli\bin\schematics.js .:ng-new --directory F:\SCHEMATICS --name aNewHope
true
false
CREATE F:/SCHEMATICS/angular.json (3043 bytes)
CREATE F:/SCHEMATICS/package.json (908 bytes)
CREATE F:/SCHEMATICS/README.md (1025 bytes)
CREATE F:/SCHEMATICS/tsconfig.json (651 bytes)
CREATE F:/SCHEMATICS/.gitignore (549 bytes)
CREATE F:/SCHEMATICS/browserslist (429 bytes)
CREATE F:/SCHEMATICS/tsconfig.app.json (210 bytes)
CREATE F:/SCHEMATICS/.editorconfig (419 bytes)
CREATE F:/SCHEMATICS/CHANGELOG.md (508 bytes)
CREATE F:/SCHEMATICS/tslint.json (2183 bytes)
CREATE F:/SCHEMATICS/src/favicon.ico (5430 bytes)
CREATE F:/SCHEMATICS/src/index.html (6315 bytes)
CREATE F:/SCHEMATICS/src/main.ts (558 bytes)
CREATE F:/SCHEMATICS/src/polyfills.ts (3167 bytes)
CREATE F:/SCHEMATICS/src/styles.scss (80 bytes)
CREATE F:/SCHEMATICS/src/browserslist (396 bytes)
CREATE F:/SCHEMATICS/src/config.json (288 bytes)
CREATE F:/SCHEMATICS/src/assets/.gitkeep (0 bytes)
CREATE F:/SCHEMATICS/src/assets/logo-inverted.svg (7389 bytes)
CREATE F:/SCHEMATICS/src/assets/logo.svg (7343 bytes)
CREATE F:/SCHEMATICS/src/assets/icons/material-icons.eot (66816 bytes)
CREATE F:/SCHEMATICS/src/assets/icons/material-icons.ttf (170104 bytes)
CREATE F:/SCHEMATICS/src/assets/icons/material-icons.woff (77312 bytes)
CREATE F:/SCHEMATICS/src/assets/icons/material-icons.woff2 (59000 bytes)
CREATE F:/SCHEMATICS/src/environments/environment.prod.ts (51 bytes)
CREATE F:/SCHEMATICS/src/environments/environment.ts (662 bytes)
CREATE F:/SCHEMATICS/src/app/app-routing.module.ts (246 bytes)
CREATE F:/SCHEMATICS/src/app/app.module.ts (683 bytes)
CREATE F:/SCHEMATICS/src/app/app.component.ts (627 bytes)
CREATE F:/SCHEMATICS/src/app/app.component.html (534 bytes)
CREATE F:/SCHEMATICS/src/app/shared/services/configuration.service.ts (649 bytes)
CREATE F:/SCHEMATICS/src/styles/index.scss (91 bytes)
CREATE F:/SCHEMATICS/src/styles/_material-icons.scss (1023 bytes)
CREATE F:/SCHEMATICS/src/styles/_variables.scss (0 bytes)

Process finished with exit code 0

我尝试删除的文件:'src/app/app-routing.module.ts'。但它不适用于我的任何文件。文件夹结构:

在此处输入图像描述

任何帮助或建议将不胜感激!谢谢

标签: angularangular-cliangular-schematics

解决方案


我认为你必须添加完整的路径:

camco-application/files/src/app/app-routing.module.ts

为此,您可以使用rootDir树中的信息:

${libsDir(tree)}/src/app/app-routing.module.ts

推荐阅读