Angular Schematics relative path

Discussão em 'Angular' iniciado por efarley, Janeiro 1, 2025.

  1. efarley

    efarley Guest

    I would like to create a custom schematic for Angular that will create a file in the same directory as the schematic was executed from. I followed this article, but I'm not sure how I would get the new file to be added in the desired directory. (https://developer.okta.com/blog/2019/02/13/angular-schematics#create-your-first-schematic)

    For example if I have the following directory structure:

    src/app/ !-- Angular Root
    |- modules
    |_ foo
    |- foo.component.ts
    |_ foo.component.html
    |- app.component.ts
    |- app.component.html
    |_ app.module.ts


    If I were do do the following commands:

    > cd /src/app/modules/foo
    > ng g my-component:my-component


    I would like the newly created/updated files to be inside the /src/app/modules/foo directory instead of the root directory. Think of how ng generate works. If I execute ng g component bar from inside the directory /src/app/modules/foo then a new component will be generated inside that directory. This is the behavior I need to replicate.

    Here's my factory. Right now it's obviously targeting the root directory with project.root however there isn't any alternative that I've found, and if I don't provide a path then I get an error. How do I get the current path (src/app/modules/foo) to store in options.path?

    export function setupOptions(host: Tree, options: any): Tree {
    const workspace = getWorkspace(host);
    if (!options.project) {
    options.project = Object.keys(workspace.projects)[0];
    }
    const project = workspace.projects[options.project];
    options.path = join(normalize(project.root), 'src');
    return host;
    }

    // You don't have to export the function as default. You can also have more than one rule factory
    // per file.
    export function myComponent(_options: any): Rule {
    return (tree: Tree, _context: SchematicContext) => {
    setupOptions(tree, _options);
    const movePath = normalize(_options.path + '/');
    const templateSource = apply(url('./files/src'), [
    template({ ..._options }),
    move(movePath),
    forEach((fileEntry: FileEntry) => {
    if (tree.exists(fileEntry.path)) {
    tree.overwrite(fileEntry.path, fileEntry.content);
    }
    return fileEntry;
    })
    ]);
    const rule = mergeWith(templateSource, MergeStrategy.Overwrite);
    return rule(tree, _context);
    };
    }

    Continue reading...

Compartilhe esta Página