如何在自定义Angular Schematics中定位文件路径?

时间:2018-07-06 20:34:29

标签: angular angular-cli angular-schematics

我目前正在尝试创建自己的自定义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”创建一个文件。我将如何更改树的文件路径,以使其在自定义目录中输出文件?谢谢。

2 个答案:

答案 0 :(得分:3)

您可以只指定所需的路径:

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;
  };
}

答案 1 :(得分: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);
    }
}