1

我目前正在尝试创建自己的自定义 Angular Schematics。我有以下内容:

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


// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function pxSchematics(options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
    tree.create(options.name || 'hello', 'world');
    return tree;
  };
}

它只是创建一个带有“hello world”的文件。我将如何更改树的文件路径,以便将文件输出到各种自定义目录中?谢谢你。

4

2 回答 2

4

您只需指定所需的路径:

import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { normalize } from "@angular-devkit/core";

export function pxSchematics(options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
    tree.create(options.name || normalize('sorts/hello'), 'world');
    return tree;
  };
}
于 2018-07-10T19:13:44.093 回答
0

你可以做这样的事情

import {chain, move, Rule, SchematicContext, Tree} from "@angular-devkit/schematics";
import {Schema as ApplicationOptions} from "@schematics/angular/application/schema";

export default function (options: ApplicationOptions): Rule {
    return (tree: Tree, _context: SchematicContext) => {
        console.log(_context);
        const result = (tree: Tree, _context: SchematicContext) => {
            tree.create(options.name || 'hello', 'world')
            return tree;
        }
        // Here you can get whatever from options
        const desiredPath = 'src';
        return chain([
                result,
                move(options.name || 'hello', `${desiredPath}/${options.name || 'hello'}`)
            ]
        )(tree, _context);
    }
}
于 2018-07-10T08:41:15.647 回答