@teambit/generator 1.0.109 → 1.0.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["teambit.generator/generator-preview"]=t():e["teambit.generator/generator-preview"]=t()}(self,(()=>(()=>{"use strict";var e={65574:(e,t,n)=>{var o={id:"teambit.generator/generator@1.0.110",homepage:"https://bit.cloud/teambit/generator/generator",exported:!0};function a(){const e=r(n(87363));return a=function(){return e},e}function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.Logo=void 0,a.__bit_component=o,r.__bit_component=o;const l=()=>a().default.createElement("div",{style:{height:"100%",display:"flex",justifyContent:"center"}},a().default.createElement("img",{style:{width:70},src:"https://static.bit.dev/extensions-icons/generator-new.svg"}));l.__bit_component=o,t.Logo=l},28859:(e,t,n)=>{var o={id:"teambit.generator/aspect-docs/generator@0.0.169",homepage:"https://bit.cloud/teambit/generator/aspect-docs/generator",exported:!0};Object.defineProperty(t,"__esModule",{value:!0}),t.default=c,l(n(87363));var a=n(40040),r=["components"];function l(e){return e&&e.__esModule?e:{default:e}}function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},p.apply(this,arguments)}function m(e,t){if(null==e)return{};var n,o,a=i(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(o=0;o<r.length;o++)n=r[o],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}function i(e,t){if(null==e)return{};var n,o,a={},r=Object.keys(e);for(o=0;o<r.length;o++)n=r[o],t.indexOf(n)>=0||(a[n]=e[n]);return a}l.__bit_component=o,p.__bit_component=o,m.__bit_component=o,i.__bit_component=o;var s={},d="wrapper";function c(e){var t=e.components,n=m(e,r);return(0,a.mdx)(d,p({},s,n,{components:t,mdxType:"MDXLayout"}),(0,a.mdx)("p",null,"Generator extension enable generating new components by pre-defined templates"),(0,a.mdx)("h3",null,"Component location"),(0,a.mdx)("p",null,"Component location in the workspace directory tree is defined with the ",(0,a.mdx)("inlineCode",{parentName:"p"},"bit create")," command (see below). For example, a component named ",(0,a.mdx)("inlineCode",{parentName:"p"},"ShoppingCart")," created in the ",(0,a.mdx)("inlineCode",{parentName:"p"},"acme.shopper")," and the namespace ",(0,a.mdx)("inlineCode",{parentName:"p"},"ui")," will be generated in the following directory:"),(0,a.mdx)("pre",null,(0,a.mdx)("code",{parentName:"pre"},"acme.shopper/ui/shopping-cart\n")),(0,a.mdx)("h3",null,"Component name"),(0,a.mdx)("p",null,"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."),(0,a.mdx)("h3",null,"Automatically ",(0,a.mdx)("inlineCode",{parentName:"h3"},"add")," component"),(0,a.mdx)("p",null,"Bit should automatically register the new component to the ",(0,a.mdx)("inlineCode",{parentName:"p"},".bitmap")," file with a symmetrical name to the component-location in the workspace."),(0,a.mdx)("h2",null,"Register a template"),(0,a.mdx)("p",null,"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 ",(0,a.mdx)("inlineCode",{parentName:"p"},"component-template.ts")," file for more info about the exact API."),(0,a.mdx)("ul",null,(0,a.mdx)("li",{parentName:"ul"},"Component name should be available as a param for the file-content-template."),(0,a.mdx)("li",{parentName:"ul"},"TBD: An environment must have a default template (if not set, use first template in array?).")),(0,a.mdx)("p",null,"To register a template, use the Generator API: ",(0,a.mdx)("inlineCode",{parentName:"p"},"registerComponentTemplate(templates: ComponentTemplate[])"),"."),(0,a.mdx)("p",null,"To make the templates of an aspect available on a workspace, they need to be added to the workspace.jsonc. For example:"),(0,a.mdx)("pre",null,(0,a.mdx)("code",{parentName:"pre",className:"language-json"},'"teambit.generator/generator": {\n "aspects": [\n "teambit.harmony/aspect"\n ]\n },\n')),(0,a.mdx)("p",null,"In the example above, the aspect ",(0,a.mdx)("inlineCode",{parentName:"p"},"teambit.harmony/aspect")," is configured to be available for the generator."),(0,a.mdx)("h2",null,"Show all available templates"),(0,a.mdx)("p",null,"Introduce a new command ",(0,a.mdx)("inlineCode",{parentName:"p"},"bit templates"),", which groups all available templates by aspects."),(0,a.mdx)("h2",null,"Hide core templates"),(0,a.mdx)("p",null,"Configure the Generator aspect to hide core templates when running ",(0,a.mdx)("inlineCode",{parentName:"p"},"bit templates"),". They'll be only shown when using ",(0,a.mdx)("inlineCode",{parentName:"p"},"--show-all")," flag."),(0,a.mdx)("pre",null,(0,a.mdx)("code",{parentName:"pre",className:"language-json"},'"teambit.generator/generator": {\n "hideCoreTemplates": true\n },\n')),(0,a.mdx)("h2",null,"Generate a template from CLI"),(0,a.mdx)("p",null,"Introduce a ",(0,a.mdx)("inlineCode",{parentName:"p"},"create")," command to use templates."),(0,a.mdx)("pre",null,(0,a.mdx)("code",{parentName:"pre",className:"language-sh"},"bit create <template-name> <component-name...> [--scope | -s] [--namespace | -n] [--aspect | -a]\n")),(0,a.mdx)("h3",null,"Args"),(0,a.mdx)("h4",null,(0,a.mdx)("inlineCode",{parentName:"h4"},"<component name>")),(0,a.mdx)("p",null,"Name of the component to create. Will be used as the component's dir name and fed to the component template."),(0,a.mdx)("p",null,(0,a.mdx)("strong",{parentName:"p"},"generated file structure should use kebab-case, while the template itself be in camel case"),"."),(0,a.mdx)("h3",null,"Options"),(0,a.mdx)("h4",null,(0,a.mdx)("inlineCode",{parentName:"h4"},"[--scope | -s]")),(0,a.mdx)("p",null,"Sets the component's scope and base directory. If not defined, use the ",(0,a.mdx)("inlineCode",{parentName:"p"},"defaultScope")," from ",(0,a.mdx)("inlineCode",{parentName:"p"},"teambit.workspace/workspace")," config."),(0,a.mdx)("h4",null,(0,a.mdx)("inlineCode",{parentName:"h4"},"[--namespace | -n]")),(0,a.mdx)("p",null,"Sets the component's namespace and nested dirs inside the scope. If not define, use empty string."),(0,a.mdx)("h4",null,(0,a.mdx)("inlineCode",{parentName:"h4"},"[--aspect | -a]")),(0,a.mdx)("p",null,"Aspect ID that registered this template, required only if there are two templates with the same name from several aspects in the workspace."),(0,a.mdx)("h2",null,"Creating a custom template generator"),(0,a.mdx)("p",null,"See our ",(0,a.mdx)("a",{parentName:"p",href:"https://harmony-docs.bit.dev/extending-bit/creating-a-custom-generator"},"tutorial")," on how to Create your own custom component generator."))}c.__bit_component=o,c.isMDXComponent=!0},44207:(e,t,n)=>{Object.defineProperty(t,"R",{enumerable:!0,get:function(){return o.default}});var o=a(n(28859));function a(e){return e&&e.__esModule?e:{default:e}}a.__bit_component={id:"teambit.generator/aspect-docs/generator@0.0.169",homepage:"https://bit.cloud/teambit/generator/aspect-docs/generator",exported:!0}},40040:e=>{e.exports=MdxJsReact},87363:e=>{e.exports=React}},t={};function n(o){var a=t[o];if(void 0!==a)return a.exports;var r=t[o]={exports:{}};return e[o](r,r.exports,n),r.exports}n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{compositions:()=>c,compositions_metadata:()=>h,overview:()=>u});var e={};n.r(e),n.d(e,{default:()=>d});var t=n(65574),a=(n(87363),n(40040));const r=TeambitMdxUiMdxScopeContext;var l=n(44207),p=["components"];function m(){return m=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},m.apply(this,arguments)}var i={},s="wrapper";function d(e){var t=e.components,n=function(e,t){if(null==e)return{};var n,o,a=function(e,t){if(null==e)return{};var n,o,a={},r=Object.keys(e);for(o=0;o<r.length;o++)n=r[o],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(o=0;o<r.length;o++)n=r[o],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}(e,p);return(0,a.mdx)(s,m({},i,n,{components:t,mdxType:"MDXLayout"}),(0,a.mdx)(r.MDXScopeProvider,{components:{Generator:l.R},mdxType:"MDXScopeProvider"},(0,a.mdx)(l.R,{mdxType:"Generator"})))}d.isMDXComponent=!0;const c=[t],u=[e],h={compositions:[{displayName:"Logo",identifier:"Logo"}]}})(),o})()));
@@ -1,5 +1,5 @@
1
- import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.generator_generator@1.0.109/dist/generator.composition.js';
2
- import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.generator_generator@1.0.109/dist/generator.docs.mdx';
1
+ import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.generator_generator@1.0.110/dist/generator.composition.js';
2
+ import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.generator_generator@1.0.110/dist/generator.docs.mdx';
3
3
 
4
4
  export const compositions = [compositions_0];
5
5
  export const overview = [overview_0];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/generator",
3
- "version": "1.0.109",
3
+ "version": "1.0.110",
4
4
  "homepage": "https://bit.cloud/teambit/generator/generator",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.generator",
8
8
  "name": "generator",
9
- "version": "1.0.109"
9
+ "version": "1.0.110"
10
10
  },
11
11
  "dependencies": {
12
12
  "camelcase": "6.2.0",
@@ -21,25 +21,25 @@
21
21
  "@teambit/component-id": "1.2.0",
22
22
  "@teambit/harmony": "0.4.6",
23
23
  "@teambit/git.modules.git-ignore": "1.0.2",
24
- "@teambit/envs": "1.0.108",
25
- "@teambit/logger": "0.0.933",
26
- "@teambit/new-component-helper": "1.0.108",
27
- "@teambit/tracker": "1.0.108",
28
- "@teambit/workspace-config-files": "1.0.108",
29
- "@teambit/workspace.modules.node-modules-linker": "0.0.158",
30
- "@teambit/workspace": "1.0.108",
31
- "@teambit/component": "1.0.108",
32
- "@teambit/cli": "0.0.840",
33
- "@teambit/graphql": "1.0.108",
34
- "@teambit/aspect-loader": "1.0.108",
35
- "@teambit/bit": "1.6.1",
36
- "@teambit/git": "1.0.108",
37
- "@teambit/compiler": "1.0.108",
38
- "@teambit/forking": "1.0.108",
39
- "@teambit/importer": "1.0.108",
40
- "@teambit/install": "1.0.108",
41
- "@teambit/ui": "1.0.108",
42
- "@teambit/config": "0.0.859"
24
+ "@teambit/envs": "1.0.109",
25
+ "@teambit/logger": "0.0.934",
26
+ "@teambit/new-component-helper": "1.0.109",
27
+ "@teambit/tracker": "1.0.109",
28
+ "@teambit/workspace-config-files": "1.0.109",
29
+ "@teambit/workspace.modules.node-modules-linker": "0.0.159",
30
+ "@teambit/workspace": "1.0.109",
31
+ "@teambit/component": "1.0.109",
32
+ "@teambit/cli": "0.0.841",
33
+ "@teambit/graphql": "1.0.109",
34
+ "@teambit/aspect-loader": "1.0.109",
35
+ "@teambit/bit": "1.6.2",
36
+ "@teambit/git": "1.0.109",
37
+ "@teambit/compiler": "1.0.109",
38
+ "@teambit/forking": "1.0.109",
39
+ "@teambit/importer": "1.0.109",
40
+ "@teambit/install": "1.0.109",
41
+ "@teambit/ui": "1.0.109",
42
+ "@teambit/config": "0.0.860"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/fs-extra": "9.0.7",
@@ -47,7 +47,7 @@
47
47
  "@types/mocha": "9.1.0",
48
48
  "@types/jest": "^29.2.2",
49
49
  "@types/testing-library__jest-dom": "^5.9.5",
50
- "@teambit/harmony.envs.core-aspect-env": "0.0.13",
50
+ "@teambit/harmony.envs.core-aspect-env": "0.0.14",
51
51
  "@teambit/generator.aspect-docs.generator": "0.0.169"
52
52
  },
53
53
  "peerDependencies": {
@@ -1,284 +0,0 @@
1
- import Vinyl from 'vinyl';
2
- import fs from 'fs-extra';
3
- import pMapSeries from 'p-map-series';
4
- import path from 'path';
5
- import { Workspace } from '@teambit/workspace';
6
- import EnvsAspect, { EnvsMain } from '@teambit/envs';
7
- import camelcase from 'camelcase';
8
- import { BitError } from '@teambit/bit-error';
9
- import { Logger } from '@teambit/logger';
10
- import { TrackerMain } from '@teambit/tracker';
11
- import { linkToNodeModulesByIds } from '@teambit/workspace.modules.node-modules-linker';
12
- import { PathOsBasedRelative } from '@teambit/legacy/dist/utils/path';
13
- import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
14
- import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
15
- import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
16
- import { NewComponentHelperMain } from '@teambit/new-component-helper';
17
- import { ComponentID } from '@teambit/component-id';
18
- import { WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';
19
-
20
- import { ComponentTemplate, ComponentFile, ComponentConfig } from './component-template';
21
- import { CreateOptions } from './create.cmd';
22
- import { OnComponentCreateSlot } from './generator.main.runtime';
23
-
24
- export type GenerateResult = {
25
- id: ComponentID;
26
- dir: string;
27
- files: string[];
28
- envId: string;
29
- envSetBy: string;
30
- packageName: string;
31
- isApp?: boolean;
32
- isEnv?: boolean;
33
- dependencies?: string[];
34
- installMissingDependencies?: boolean;
35
- };
36
-
37
- export type OnComponentCreateFn = (generateResults: GenerateResult[]) => Promise<void>;
38
-
39
- export class ComponentGenerator {
40
- constructor(
41
- private workspace: Workspace,
42
- private componentIds: ComponentID[],
43
- private options: Partial<CreateOptions>,
44
- private template: ComponentTemplate,
45
- private envs: EnvsMain,
46
- private newComponentHelper: NewComponentHelperMain,
47
- private tracker: TrackerMain,
48
- private wsConfigFiles: WorkspaceConfigFilesMain,
49
- private logger: Logger,
50
- private onComponentCreateSlot: OnComponentCreateSlot,
51
- private aspectId: string,
52
- private envId?: ComponentID
53
- ) {}
54
-
55
- async generate(): Promise<GenerateResult[]> {
56
- const dirsToDeleteIfFailed: string[] = [];
57
- const generateResults = await pMapSeries(this.componentIds, async (componentId) => {
58
- try {
59
- const componentPath = this.newComponentHelper.getNewComponentPath(
60
- componentId,
61
- this.options.path,
62
- this.componentIds.length
63
- );
64
- if (fs.existsSync(path.join(this.workspace.path, componentPath))) {
65
- throw new BitError(`unable to create a component at "${componentPath}", this path already exist`);
66
- }
67
- if (await this.workspace.hasName(componentId.fullName)) {
68
- throw new BitError(
69
- `unable to create a component "${componentId.fullName}", a component with the same name already exist`
70
- );
71
- }
72
- dirsToDeleteIfFailed.push(componentPath);
73
- return await this.generateOneComponent(componentId, componentPath);
74
- } catch (err: any) {
75
- await this.deleteGeneratedComponents(dirsToDeleteIfFailed);
76
- throw err;
77
- }
78
- });
79
-
80
- await this.workspace.bitMap.write(`create (${this.componentIds.length} components)`);
81
-
82
- const ids = generateResults.map((r) => r.id);
83
- await this.tryLinkToNodeModules(ids);
84
- await this.runOnComponentCreateHook(generateResults);
85
- // We are running this after the runOnComponentCreateHook as it require
86
- // the env to be installed to work properly, and the hook might install
87
- // the env.
88
- await this.tryWriteConfigFiles(ids);
89
-
90
- return generateResults;
91
- }
92
-
93
- private async tryLinkToNodeModules(ids: ComponentID[]) {
94
- try {
95
- await linkToNodeModulesByIds(
96
- this.workspace,
97
- ids.map((id) => id)
98
- );
99
- } catch (err: any) {
100
- this.logger.consoleFailure(
101
- `failed linking the new components to node_modules, please run "bit link" manually. error: ${err.message}`
102
- );
103
- }
104
- }
105
-
106
- private async runOnComponentCreateHook(generateResults: GenerateResult[]) {
107
- const fns = this.onComponentCreateSlot.values();
108
- if (!fns.length) return;
109
- await Promise.all(fns.map((fn) => fn(generateResults)));
110
- }
111
-
112
- /**
113
- * The function `tryWriteConfigFiles` attempts to write workspace config files, and if it fails, it logs an error
114
- * message.
115
- * @returns If the condition `!shouldWrite` is true, then nothing is being returned. Otherwise, if the `writeConfigFiles`
116
- * function is successfully executed, nothing is being returned. If an error occurs during the execution of
117
- * `writeConfigFiles`, an error message is being returned.
118
- */
119
- private async tryWriteConfigFiles(ids: ComponentID[]) {
120
- const shouldWrite = this.wsConfigFiles.isWorkspaceConfigWriteEnabled();
121
- if (!shouldWrite) return;
122
- ids.map((id) => this.workspace.clearComponentCache(id));
123
- const { err } = await this.wsConfigFiles.writeConfigFiles({
124
- clean: true,
125
- silent: true,
126
- dedupe: true,
127
- throw: false,
128
- });
129
- if (err) {
130
- this.logger.consoleFailure(
131
- `failed generating workspace config files, please run "bit ws-config write" manually. error: ${err.message}`
132
- );
133
- }
134
- }
135
-
136
- private async deleteGeneratedComponents(dirs: string[]) {
137
- await Promise.all(
138
- dirs.map(async (dir) => {
139
- const absoluteDir = path.join(this.workspace.path, dir);
140
- try {
141
- await fs.remove(absoluteDir);
142
- } catch (err: any) {
143
- if (err.code !== 'ENOENT') {
144
- // if not exist, it's fine
145
- throw err;
146
- }
147
- }
148
- })
149
- );
150
- }
151
-
152
- private async generateOneComponent(componentId: ComponentID, componentPath: string): Promise<GenerateResult> {
153
- const name = componentId.name;
154
- const namePascalCase = camelcase(name, { pascalCase: true });
155
- const nameCamelCase = camelcase(name);
156
- const aspectId = ComponentID.fromString(this.aspectId);
157
-
158
- const files = await this.template.generateFiles({
159
- name,
160
- namePascalCase,
161
- nameCamelCase,
162
- componentId,
163
- aspectId,
164
- envId: this.envId,
165
- });
166
- const mainFile = files.find((file) => file.isMain);
167
- await this.writeComponentFiles(componentPath, files);
168
- const addResults = await this.tracker.track({
169
- rootDir: componentPath,
170
- mainFile: mainFile?.relativePath,
171
- componentName: componentId.fullName,
172
- defaultScope: this.options.scope,
173
- });
174
- const component = await this.workspace.get(componentId);
175
- const hasEnvConfiguredOriginally = this.envs.hasEnvConfigured(component);
176
- if (this.template.isApp) {
177
- await this.workspace.use(componentId.toString());
178
- }
179
- const envBeforeConfigChanges = this.envs.getEnv(component);
180
- let config = this.template.config;
181
- if (config && typeof config === 'function') {
182
- const boundConfig = this.template.config?.bind(this.template);
183
- config = boundConfig({ aspectId: this.aspectId });
184
- }
185
-
186
- const userEnv = this.options.env;
187
-
188
- if (!config && this.envId && !userEnv) {
189
- const isInWorkspace = this.workspace.exists(this.envId);
190
- config = {
191
- [isInWorkspace ? this.envId.toStringWithoutVersion() : this.envId.toString()]: {},
192
- 'teambit.envs/envs': {
193
- env: this.envId.toStringWithoutVersion(),
194
- },
195
- };
196
- }
197
-
198
- const templateEnv = config?.[EnvsAspect.id]?.env;
199
-
200
- if (config && templateEnv && hasEnvConfiguredOriginally) {
201
- // remove the env we got from the template.
202
- delete config[templateEnv];
203
- delete config[EnvsAspect.id].env;
204
- if (Object.keys(config[EnvsAspect.id]).length === 0) delete config[EnvsAspect.id];
205
- if (Object.keys(config).length === 0) config = undefined;
206
- }
207
-
208
- const configWithEnv = await this.addEnvIfProvidedByFlag(config);
209
- if (configWithEnv) this.workspace.bitMap.setEntireConfig(component.id, configWithEnv);
210
-
211
- const getEnvData = () => {
212
- const envFromFlag = this.options.env; // env entered by the user when running `bit create --env`
213
- const envFromTemplate = config?.[EnvsAspect.id]?.env;
214
- if (envFromFlag) {
215
- return {
216
- envId: envFromFlag,
217
- setBy: '--env flag',
218
- };
219
- }
220
- if (envFromTemplate) {
221
- return {
222
- envId: envFromTemplate,
223
- setBy: 'template',
224
- };
225
- }
226
- return {
227
- envId: envBeforeConfigChanges.id,
228
- setBy: hasEnvConfiguredOriginally ? 'workspace variants' : '<default>',
229
- };
230
- };
231
- const { envId, setBy } = getEnvData();
232
- return {
233
- id: componentId,
234
- dir: componentPath,
235
- files: addResults.files,
236
- packageName: componentIdToPackageName(component.state._consumer),
237
- envId,
238
- envSetBy: setBy,
239
- isApp: this.template.isApp,
240
- isEnv: this.template.isEnv,
241
- dependencies: this.template.dependencies,
242
- installMissingDependencies: this.template.installMissingDependencies,
243
- };
244
- }
245
-
246
- private async addEnvIfProvidedByFlag(config?: ComponentConfig): Promise<ComponentConfig | undefined> {
247
- const userEnv = this.options.env; // env entered by the user when running `bit create --env`
248
- const templateEnv = config?.[EnvsAspect.id]?.env;
249
- if (!userEnv || userEnv === templateEnv) {
250
- return config;
251
- }
252
- config = config || {};
253
- if (templateEnv) {
254
- // the component template has an env and the user wants a different env.
255
- delete config[templateEnv];
256
- }
257
- await this.tracker.addEnvToConfig(userEnv, config);
258
-
259
- return config;
260
- }
261
-
262
- /**
263
- * writes the generated template files to the default directory set in the workspace config
264
- */
265
- private async writeComponentFiles(
266
- componentPath: string,
267
- templateFiles: ComponentFile[]
268
- ): Promise<PathOsBasedRelative[]> {
269
- const dataToPersist = new DataToPersist();
270
- const vinylFiles = templateFiles.map((templateFile) => {
271
- const templateFileVinyl = new Vinyl({
272
- base: componentPath,
273
- path: path.join(componentPath, templateFile.relativePath),
274
- contents: Buffer.from(templateFile.content),
275
- });
276
- return AbstractVinyl.fromVinyl(templateFileVinyl);
277
- });
278
- const results = vinylFiles.map((v) => v.path);
279
- dataToPersist.addManyFiles(vinylFiles);
280
- dataToPersist.addBasePath(this.workspace.path);
281
- await dataToPersist.persistAllToFS();
282
- return results;
283
- }
284
- }
@@ -1,152 +0,0 @@
1
- import { ComponentID } from '@teambit/component';
2
-
3
- /**
4
- * BaseComponentTemplateOptions describes the foundational properties for components.
5
- */
6
- export interface BaseComponentTemplateOptions {
7
- /**
8
- * component-name as entered by the user, e.g. `use-date`.
9
- * without the scope and the namespace.
10
- */
11
- name: string;
12
-
13
- /**
14
- * component-name as upper camel case, e.g. `use-date` becomes `UseDate`.
15
- * useful when generating the file content, for example for a class name.
16
- */
17
- namePascalCase: string;
18
-
19
- /**
20
- * component-name as lower camel case, e.g. `use-date` becomes `useDate`.
21
- * useful when generating the file content, for example for a function/variable name.
22
- */
23
- nameCamelCase: string;
24
-
25
- /**
26
- * component id.
27
- * the name is the name+namespace. the scope is the scope entered by --scope flag or the defaultScope
28
- */
29
- componentId: ComponentID;
30
-
31
- /**
32
- * aspect id of the aspect that register the template itself
33
- */
34
- aspectId: ComponentID | string;
35
-
36
- /**
37
- * env id of the env that register the template itself.
38
- * This will be usually identical to the aspectId but aspectId will always exist,
39
- * while envId will be undefined if the template is not registered by an env.
40
- */
41
- envId?: ComponentID;
42
-
43
- /**
44
- * path of the component.
45
- */
46
- path?: string;
47
- /**
48
- * scope of the component.
49
- */
50
- scope?: string;
51
- /**
52
- * namespace of the component.
53
- */
54
- namespace?: string;
55
- }
56
-
57
- /**
58
- * ComponentContext represents foundational properties for a component context.
59
- */
60
- export type ComponentContext = BaseComponentTemplateOptions;
61
-
62
- export interface ComponentFile {
63
- /**
64
- * relative path of the file within the component.
65
- */
66
- relativePath: string;
67
-
68
- /**
69
- * file content
70
- */
71
- content: string;
72
-
73
- /**
74
- * whether this file will be tracked as the main file
75
- */
76
- isMain?: boolean;
77
- }
78
-
79
- export interface ConfigContext {
80
- /**
81
- * Aspect id of the aspect that register the template itself
82
- */
83
- aspectId: string;
84
- }
85
-
86
- export type ComponentConfig = { [aspectName: string]: any };
87
-
88
- export interface ComponentTemplateOptions {
89
- /**
90
- * name of the component template. for example: `hook`, `react-component` or `module`.
91
- */
92
- name?: string;
93
-
94
- /**
95
- * short description of the template. shown in the `bit templates` command.
96
- */
97
- description?: string;
98
-
99
- /**
100
- * hide this template so that it is not listed with `bit templates`
101
- */
102
- hidden?: boolean;
103
-
104
- /**
105
- * env to use for the generated component.
106
- */
107
- env?: string;
108
-
109
- /**
110
- * adds a metadata that the component that this template creates is of type env.
111
- * This will be used later to do further configuration for example:
112
- * - ensure to create the .bit_root for it
113
- */
114
- isEnv?: boolean;
115
-
116
- /**
117
- * adds a metadata that the component that this template creates is of type app.
118
- * This will be used later to do further configuration for example:
119
- * - add it to the workspace.jsonc as app
120
- * - ensure to create the .bit_root for it
121
- */
122
- isApp?: boolean;
123
-
124
- /**
125
- * list of dependencies to install when the component is created.
126
- */
127
- dependencies?: string[];
128
-
129
- /**
130
- * Perform installation of missing dependencies after component generation.
131
- * This is the same as of running `bit install --add-missing-deps` after component generation.
132
- */
133
- installMissingDependencies?: boolean;
134
- }
135
-
136
- export interface ComponentTemplate extends ComponentTemplateOptions {
137
- name: string;
138
-
139
- /**
140
- * template function for generating the file of a certain component.,
141
- */
142
- generateFiles(context: ComponentContext): Promise<ComponentFile[]> | ComponentFile[];
143
-
144
- /**
145
- * component config. gets saved in the .bitmap file and it overrides the workspace.jsonc config.
146
- * for example, you can set the env that will be used for this component as follows:
147
- * "teambit.envs/envs": {
148
- * "env": "teambit.harmony/aspect"
149
- * },
150
- */
151
- config?: ComponentConfig | ((context: ConfigContext) => ComponentConfig);
152
- }
package/create.cmd.ts DELETED
@@ -1,88 +0,0 @@
1
- import { Command, CommandOptions } from '@teambit/cli';
2
- import { ComponentID } from '@teambit/component';
3
- import chalk from 'chalk';
4
- import { GeneratorMain } from './generator.main.runtime';
5
- import type { BaseComponentTemplateOptions } from './component-template';
6
-
7
- /**
8
- * CreateOptions combines foundational properties with additional options for creating a component.
9
- */
10
- export type CreateOptions = BaseComponentTemplateOptions & {
11
- env?: string;
12
- aspect?: string;
13
- };
14
-
15
- export class CreateCmd implements Command {
16
- name = 'create <template-name> <component-names...>';
17
- description = 'create a new component (source files and config) using a template.';
18
- alias = '';
19
- loader = true;
20
- helpUrl = 'reference/starters/create-starter';
21
- arguments = [
22
- {
23
- name: 'template-name',
24
- description:
25
- "the template for generating the component \n(run 'bit templates' for a list of available templates)",
26
- },
27
- {
28
- name: 'component-names...',
29
- description: 'a list of component names to generate',
30
- },
31
- ];
32
- examples = [
33
- {
34
- cmd: 'bit create react ui/button --aspect teambit.react/react-env',
35
- description: "creates a component named 'ui/button' using the 'react' template",
36
- },
37
- {
38
- cmd: 'bit create node utils/is-string utils/is-number --aspect teambit.node/node',
39
- description:
40
- "creates two components, 'utils/is-string' and 'utils/is-number' using the 'node' template from the 'node' aspect(env)",
41
- },
42
- {
43
- cmd: 'bit create mdx docs/create-components --aspect teambit.mdx/mdx-env --scope my-org.my-scope',
44
- description:
45
- "creates an mdx component named 'docs/create-components' and sets it scope to 'my-org.my-scope'. \nby default, the scope is the `defaultScope` value, configured in your `workspace.jsonc`.",
46
- },
47
- {
48
- cmd: 'bit create react ui/button --aspect teambit.react/react-env --env teambit.community/envs/community-react@3.0.3',
49
- description:
50
- "creates a component named 'ui/button' from the teambit.react/react-env env and sets it to use the 'community-react' env. \n(the template's default env is 'teambit.react/react-env').",
51
- },
52
- ];
53
- group = 'development';
54
- options = [
55
- ['n', 'namespace <string>', `sets the component's namespace and nested dirs inside the scope`],
56
- ['s', 'scope <string>', `sets the component's scope-name. if not entered, the default-scope will be used`],
57
- ['a', 'aspect <string>', 'aspect-id of the template. helpful when multiple aspects use the same template name'],
58
- ['t', 'template <string>', 'env-id of the template. alias for --aspect.'],
59
- ['p', 'path <string>', 'relative path in the workspace. by default the path is `<scope>/<namespace>/<name>`'],
60
- ['e', 'env <string>', "set the component's environment. (overrides the env from variants and the template)"],
61
- ] as CommandOptions;
62
-
63
- constructor(private generator: GeneratorMain) {}
64
-
65
- async report(
66
- [templateName, componentNames]: [string, string[]],
67
- options: Partial<CreateOptions> & {
68
- template?: string | ComponentID;
69
- }
70
- ) {
71
- options.aspectId = options.aspectId ?? options.template;
72
- const results = await this.generator.generateComponentTemplate(componentNames, templateName, options);
73
- const title = `${results.length} component(s) were created`;
74
-
75
- const componentsData = results
76
- .map((result) => {
77
- return `${chalk.bold(result.id.toString())}
78
- location: ${result.dir}
79
- env: ${result.envId} (set by ${result.envSetBy})
80
- package: ${result.packageName}
81
- `;
82
- })
83
- .join('\n');
84
- const footer = `env configuration is according to workspace variants, template config or --env flag.`;
85
-
86
- return `${chalk.green(title)}\n\n${componentsData}\n\n${footer}`;
87
- }
88
- }
@@ -1,14 +0,0 @@
1
- import { EnvHandler } from '@teambit/envs';
2
- import { TemplateList } from './template-list';
3
- import { StarterList } from './starter-list';
4
-
5
- export interface GeneratorEnv {
6
- /**
7
- * return a template list instance.
8
- */
9
- generators(): EnvHandler<TemplateList>;
10
- /**
11
- * return a starter list instance.
12
- */
13
- starters(): EnvHandler<StarterList>;
14
- }
@@ -1,5 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const GeneratorAspect = Aspect.create({
4
- id: 'teambit.generator/generator',
5
- });