Angular Schematics : create custom tree folders and files

862 views Asked by At

I ve just strated to learn about Schematics , my first task is to generate a custom tree of folders and empty files using a custom shematic inside my Angular app

for example :

i want to generate this tree:

folderOne
|__ fileOne.ts
folderTwo
|__ fileTwo.ts
folderThree
|__ subfolderA
    |__ fileThree.ts

Of course it should depend on the the source place of running the schematic command

(relative path)

Is there any fast (template) way to do it ?

Suggestions ??

1

There are 1 answers

0
Alberto Perez Vazquez On

In your files template

__fistVariable@dasherize__
|__firstVariable@dasherize__.ts
__secondVariable@dasherize__
|__secondVariable@dasherize__.ts

your schematic function:

const templateSource = apply(url("./files"), [
  template({
  ..._options,
  ...strings
  })
]);
return mergeWith(templateSource)(tree,_context);

and in your schema.json

"properties": {
    "one": {
      "type": "string",
      "description": "Variable 1",
      "$default": {
        "$source": "argv",
        "index": 0
      },
      "minLength": 1,
      "x-prompt": "Enter the entity name (camelCase || dasherized)"
    },
    "two":{
      "type": "string",
      "description": "Variable two",
      "$default": {
        "$source": "argv",
        "index": 1
      },
      "minLength": 1,
      "x-prompt": "Enter the entity name (camelCase || dasherized)"
    }
  },
  "required": ["one", "two"]