|
1 | | -Create extension enable generating new components by a default template or extensions registered to this extension. |
| 1 | +Generator extension enable generating new components by pre-defined templates |
| 2 | + |
| 3 | +### Component location |
| 4 | + |
| 5 | +Component location in the workspace directory tree is defined with the `bit create` command (see below). For example, a component named `ShoppingCart` created in the `acme.shopper` and the namespace `ui` will be generated in the following directory: |
2 | 6 |
|
3 | | -### Configuration |
4 | | -Add to your config the following: |
5 | 7 | ``` |
6 | | -"create": { |
7 | | - "template": "your-extension" |
8 | | -} |
| 8 | +acme.shopper/ui/shopping-cart |
9 | 9 | ``` |
10 | 10 |
|
11 | | -### How to extend |
12 | | -First, add this extension as a dependency to your extension. |
13 | | -Then, in the provider, register to this extension and provide a function that returns the template files. |
14 | | -Here is an example: |
| 11 | +### Component name |
| 12 | + |
| 13 | +When using templates Bit will use CamelCasing for passing the component-name to the template, but generated file structure should be in kebab-case, for better cross-operating-system compatibility of the component. |
| 14 | + |
| 15 | +### Automatically `add` component |
| 16 | + |
| 17 | +Bit should automatically register the new component to the `.bitmap` file with a symmetrical name to the component-location in the workspace. |
| 18 | + |
| 19 | +## Register a template |
| 20 | +Any aspect (include envs) can register templates. Each template should have a name and a list of files. Each file has a relative-path to the component-dir and template content. See the `component-template.ts` file for more info about the exact API. |
| 21 | + |
| 22 | +* Component name should be available as a param for the file-content-template. |
| 23 | +* TBD: An environment must have a default template (if not set, use first template in array?). |
| 24 | + |
| 25 | +To register a template, use the Generator API: `registerComponentTemplate(templates: ComponentTemplate[])`. |
| 26 | + |
| 27 | +To make the templates of an aspect available on a workspace, they need to be added to the workspace.jsonc. For example: |
| 28 | +```json |
| 29 | +"teambit.generator/generator": { |
| 30 | + "aspects": [ |
| 31 | + "teambit.harmony/aspect" |
| 32 | + ] |
| 33 | + }, |
15 | 34 | ``` |
16 | | -create.register({ name: 'extensions/gulp-ts' }, (name) => { |
17 | | - return { |
18 | | - files: [ |
19 | | - { path: `${name}.js`, content: `export default function ${name}() { console.log('hello'); }` }, |
20 | | - { path: `${name}.spec.js`, content: `export default function ${name}() { console.log('hello from spec'); }` } |
21 | | - ], |
22 | | - main: `${name}.js` |
23 | | - }; |
24 | | -}); |
| 35 | +In the example above, the aspect `teambit.harmony/aspect` is configured to be available for the generator. |
| 36 | + |
| 37 | +## Show all available templates |
| 38 | + |
| 39 | +Introduce a new command `bit templates`, which groups all available templates by aspects. |
| 40 | + |
| 41 | +## Generate a template from CLI |
| 42 | + |
| 43 | +Introduce a `create` command to use templates. |
| 44 | + |
| 45 | +```sh |
| 46 | +bit create <template-name> <component-name...> [--scope | -s] [--namespace | -n] [--aspect | -a] |
25 | 47 | ``` |
| 48 | + |
| 49 | +### Args |
| 50 | + |
| 51 | +#### `<component name>` |
| 52 | + |
| 53 | +Name of the component to create. Will be used as the component's dir name and fed to the component template. |
| 54 | + |
| 55 | +**generated file structure should use kebab-case, while the template itself be in camel case**. |
| 56 | + |
| 57 | +### Options |
| 58 | + |
| 59 | +#### `[--scope | -s]` |
| 60 | + |
| 61 | +Sets the component's scope and base directory. If not defined, use the `defaultScope` from `teambit.workspace/workspace` config. |
| 62 | + |
| 63 | +#### `[--namespace | -n]` |
| 64 | + |
| 65 | +Sets the component's namespace and nested dirs inside the scope. If not define, use empty string. |
| 66 | + |
| 67 | +#### `[--aspect | -a]` |
| 68 | + |
| 69 | +Aspect ID that registered this template, required only if there are two templates with the same name from several aspects in the workspace. |
0 commit comments