@teambit/generator 1.0.9 → 1.0.11
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.
- package/dist/generator.composition.d.ts +2 -2
- package/dist/generator.main.runtime.js +2 -3
- package/dist/generator.main.runtime.js.map +1 -1
- package/dist/preview-1695901055662.js +7 -0
- package/dist/templates/basic/basic.starter.js +4 -8
- package/dist/templates/basic/basic.starter.js.map +1 -1
- package/dist/templates/basic/template/files/workspace-config.js +2 -2
- package/dist/templates/basic/template/files/workspace-config.js.map +1 -1
- package/dist/templates/starter/files/starter.js +0 -4
- package/dist/templates/starter/files/starter.js.map +1 -1
- package/dist/templates/starter/files/workspace-config-tpl.js +9 -19
- package/dist/templates/starter/files/workspace-config-tpl.js.map +1 -1
- package/dist/templates/starter-standalone/files/starter.js +0 -9
- package/dist/templates/starter-standalone/files/starter.js.map +1 -1
- package/dist/templates/starter-standalone/files/workspace-config-tpl.js +8 -19
- package/dist/templates/starter-standalone/files/workspace-config-tpl.js.map +1 -1
- package/package.json +24 -23
- package/templates/basic/basic.starter.ts +5 -10
- package/templates/basic/template/files/workspace-config.ts +2 -2
- package/templates/starter/files/starter.ts +1 -5
- package/templates/starter/files/workspace-config-tpl.ts +10 -20
- package/templates/starter-standalone/files/starter.ts +1 -10
- package/templates/starter-standalone/files/workspace-config-tpl.ts +9 -20
- package/tsconfig.json +40 -0
- package/types/asset.d.ts +29 -0
- package/types/style.d.ts +42 -0
- package/.bit-capsule-ready +0 -0
- package/component-generator.ts +0 -271
- package/component-template.ts +0 -122
- package/create.cmd.ts +0 -88
- package/generator-env-type.ts +0 -14
- package/generator.aspect.ts +0 -5
- package/generator.graphql.ts +0 -62
- package/generator.main.runtime.ts +0 -577
- package/index.ts +0 -14
- package/new.cmd.ts +0 -114
- package/package-tar/teambit-generator-1.0.9.tgz +0 -0
- package/schema.json +0 -10304
- package/starter-list.ts +0 -21
- package/starter.plugin.ts +0 -16
- package/teambit_generator_generator-preview.js +0 -11
- package/template-list.ts +0 -21
- package/templates.cmd.ts +0 -55
- package/types.ts +0 -3
- package/workspace-generator.ts +0 -205
- package/workspace-template.ts +0 -145
@@ -1,577 +0,0 @@
|
|
1
|
-
import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
|
2
|
-
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
|
3
|
-
import WorkspaceAspect, { OutsideWorkspaceError, Workspace } from '@teambit/workspace';
|
4
|
-
import { EnvDefinition, EnvsAspect, EnvsMain } from '@teambit/envs';
|
5
|
-
import ComponentConfig from '@teambit/legacy/dist/consumer/config';
|
6
|
-
import WorkspaceConfigFilesAspect, { WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';
|
7
|
-
|
8
|
-
import ComponentAspect, { ComponentID } from '@teambit/component';
|
9
|
-
import type { ComponentMain, Component } from '@teambit/component';
|
10
|
-
|
11
|
-
import { isCoreAspect, loadBit } from '@teambit/bit';
|
12
|
-
import { Slot, SlotRegistry } from '@teambit/harmony';
|
13
|
-
import GitAspect, { GitMain } from '@teambit/git';
|
14
|
-
import { BitError } from '@teambit/bit-error';
|
15
|
-
import AspectLoaderAspect, { AspectLoaderMain } from '@teambit/aspect-loader';
|
16
|
-
import TrackerAspect, { TrackerMain } from '@teambit/tracker';
|
17
|
-
import NewComponentHelperAspect, { NewComponentHelperMain } from '@teambit/new-component-helper';
|
18
|
-
import { compact } from 'lodash';
|
19
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
20
|
-
import { ComponentTemplate } from './component-template';
|
21
|
-
import { GeneratorAspect } from './generator.aspect';
|
22
|
-
import { CreateCmd, CreateOptions } from './create.cmd';
|
23
|
-
import { TemplatesCmd } from './templates.cmd';
|
24
|
-
import { generatorSchema } from './generator.graphql';
|
25
|
-
import { ComponentGenerator, GenerateResult, OnComponentCreateFn } from './component-generator';
|
26
|
-
import { WorkspaceGenerator } from './workspace-generator';
|
27
|
-
import { WorkspaceTemplate } from './workspace-template';
|
28
|
-
import { NewCmd, NewOptions } from './new.cmd';
|
29
|
-
import {
|
30
|
-
componentGeneratorTemplate,
|
31
|
-
componentGeneratorTemplateStandalone,
|
32
|
-
starterTemplate,
|
33
|
-
starterTemplateStandalone,
|
34
|
-
} from './templates';
|
35
|
-
import { BasicWorkspaceStarter } from './templates/basic';
|
36
|
-
import { StarterPlugin } from './starter.plugin';
|
37
|
-
import { GeneratorService } from './generator.service';
|
38
|
-
|
39
|
-
export type ComponentTemplateSlot = SlotRegistry<ComponentTemplate[]>;
|
40
|
-
export type WorkspaceTemplateSlot = SlotRegistry<WorkspaceTemplate[]>;
|
41
|
-
export type OnComponentCreateSlot = SlotRegistry<OnComponentCreateFn>;
|
42
|
-
|
43
|
-
export type TemplateDescriptor = {
|
44
|
-
aspectId: string;
|
45
|
-
titlePrefix?: string;
|
46
|
-
name: string;
|
47
|
-
description?: string;
|
48
|
-
hidden?: boolean;
|
49
|
-
};
|
50
|
-
|
51
|
-
type TemplateWithId = { id: string; envName?: string };
|
52
|
-
type WorkspaceTemplateWithId = TemplateWithId & { template: WorkspaceTemplate };
|
53
|
-
type ComponentTemplateWithId = TemplateWithId & { template: ComponentTemplate };
|
54
|
-
type AnyTemplateWithId = TemplateWithId & { template: ComponentTemplate | WorkspaceTemplate };
|
55
|
-
|
56
|
-
export type GenerateWorkspaceTemplateResult = { workspacePath: string; appName?: string };
|
57
|
-
|
58
|
-
export type GeneratorConfig = {
|
59
|
-
/**
|
60
|
-
* array of aspects to include in the list of templates.
|
61
|
-
*/
|
62
|
-
aspects: string[];
|
63
|
-
|
64
|
-
/**
|
65
|
-
* by default core templates are shown.
|
66
|
-
* use this to hide them unless `--show-all` flag of `bit templates` was used
|
67
|
-
*/
|
68
|
-
hideCoreTemplates: boolean;
|
69
|
-
|
70
|
-
/**
|
71
|
-
* default envs.
|
72
|
-
*/
|
73
|
-
envs?: string[];
|
74
|
-
};
|
75
|
-
|
76
|
-
export class GeneratorMain {
|
77
|
-
private aspectLoaded = false;
|
78
|
-
constructor(
|
79
|
-
private componentTemplateSlot: ComponentTemplateSlot,
|
80
|
-
private workspaceTemplateSlot: WorkspaceTemplateSlot,
|
81
|
-
private onComponentCreateSlot: OnComponentCreateSlot,
|
82
|
-
private config: GeneratorConfig,
|
83
|
-
private workspace: Workspace,
|
84
|
-
private envs: EnvsMain,
|
85
|
-
private aspectLoader: AspectLoaderMain,
|
86
|
-
private newComponentHelper: NewComponentHelperMain,
|
87
|
-
private componentAspect: ComponentMain,
|
88
|
-
private tracker: TrackerMain,
|
89
|
-
private logger: Logger,
|
90
|
-
private git: GitMain,
|
91
|
-
private wsConfigFiles: WorkspaceConfigFilesMain
|
92
|
-
) {}
|
93
|
-
|
94
|
-
/**
|
95
|
-
* register a new component template.
|
96
|
-
*/
|
97
|
-
registerComponentTemplate(templates: ComponentTemplate[]) {
|
98
|
-
this.componentTemplateSlot.register(templates);
|
99
|
-
return this;
|
100
|
-
}
|
101
|
-
|
102
|
-
/**
|
103
|
-
* register a new workspace starter.
|
104
|
-
*/
|
105
|
-
registerWorkspaceTemplate(templates: WorkspaceTemplate[]) {
|
106
|
-
this.workspaceTemplateSlot.register(templates);
|
107
|
-
return this;
|
108
|
-
}
|
109
|
-
|
110
|
-
registerOnComponentCreate(fn: OnComponentCreateFn) {
|
111
|
-
this.onComponentCreateSlot.register(fn);
|
112
|
-
return this;
|
113
|
-
}
|
114
|
-
|
115
|
-
/**
|
116
|
-
* list all component templates registered in the workspace or workspace templates in case the
|
117
|
-
* workspace is not available
|
118
|
-
*/
|
119
|
-
async listTemplates({ aspect }: { aspect?: string } = {}): Promise<TemplateDescriptor[]> {
|
120
|
-
if (this.isRunningInsideWorkspace()) {
|
121
|
-
return this.getAllComponentTemplatesDescriptorsFlattened(aspect);
|
122
|
-
}
|
123
|
-
return this.getAllWorkspaceTemplatesDescriptorFlattened(aspect);
|
124
|
-
}
|
125
|
-
|
126
|
-
private getTemplateDescriptor = ({ id, template }: AnyTemplateWithId): TemplateDescriptor => {
|
127
|
-
const shouldBeHidden = () => {
|
128
|
-
if (template.hidden) return true;
|
129
|
-
if (this.config.hideCoreTemplates && isCoreAspect(id)) return true;
|
130
|
-
return false;
|
131
|
-
};
|
132
|
-
return {
|
133
|
-
aspectId: id,
|
134
|
-
name: template.name,
|
135
|
-
description: template.description,
|
136
|
-
hidden: shouldBeHidden(),
|
137
|
-
};
|
138
|
-
};
|
139
|
-
|
140
|
-
/**
|
141
|
-
* @deprecated use this.listTemplates()
|
142
|
-
*/
|
143
|
-
async listComponentTemplates(opts: { aspect?: string }): Promise<TemplateDescriptor[]> {
|
144
|
-
return this.listTemplates(opts);
|
145
|
-
}
|
146
|
-
|
147
|
-
isRunningInsideWorkspace(): boolean {
|
148
|
-
return Boolean(this.workspace);
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
* get all component templates registered by a specific aspect ID.
|
153
|
-
*/
|
154
|
-
getComponentTemplateByAspect(aspectId: string): ComponentTemplate[] {
|
155
|
-
return this.componentTemplateSlot.get(aspectId) || [];
|
156
|
-
}
|
157
|
-
|
158
|
-
/**
|
159
|
-
* returns a specific component template.
|
160
|
-
*/
|
161
|
-
async getComponentTemplate(name: string, aspectId?: string): Promise<ComponentTemplateWithId | undefined> {
|
162
|
-
const fromEnv = await this.listEnvComponentTemplates([], aspectId);
|
163
|
-
if (fromEnv && fromEnv.length) {
|
164
|
-
const found = this.findTemplateByAspectIdAndName<ComponentTemplateWithId>(aspectId, name, fromEnv);
|
165
|
-
if (found) return found;
|
166
|
-
}
|
167
|
-
// fallback to aspect id not from env if provided
|
168
|
-
const templates = await this.getAllComponentTemplatesFlattened();
|
169
|
-
const found = this.findTemplateByAspectIdAndName<ComponentTemplateWithId>(aspectId, name, templates);
|
170
|
-
return found;
|
171
|
-
}
|
172
|
-
|
173
|
-
private findTemplateByAspectIdAndName<T>(
|
174
|
-
aspectId: string | undefined,
|
175
|
-
name: string,
|
176
|
-
templates: Array<T>
|
177
|
-
): T | undefined {
|
178
|
-
// @ts-ignore (should set T to be extends ComponentTemplateWithId or WorkspaceTemplateWithId)
|
179
|
-
const found = templates.find(({ id, template }) => {
|
180
|
-
// When doing something like:
|
181
|
-
// bit create react-env my-env --aspect teambit.react/react-env
|
182
|
-
// we will get the aspectId without version
|
183
|
-
// but the env might be loaded from the global scope then it will be with a version
|
184
|
-
// so it won't found if we don't look for it like this
|
185
|
-
const idWithoutVersion = id.split('@')[0];
|
186
|
-
if (aspectId && id !== aspectId && idWithoutVersion !== aspectId) return false;
|
187
|
-
return template.name === name;
|
188
|
-
});
|
189
|
-
return found;
|
190
|
-
}
|
191
|
-
|
192
|
-
/**
|
193
|
-
* Get the generator aspect and the envs aspect from an harmony instance of the global scope
|
194
|
-
*/
|
195
|
-
private async getGlobalGeneratorEnvs(
|
196
|
-
aspectId: string
|
197
|
-
): Promise<{ remoteGenerator: GeneratorMain; fullAspectId: string; remoteEnvsAspect: EnvsMain; aspect: any }> {
|
198
|
-
const { globalScopeHarmony, components } = await this.aspectLoader.loadAspectsFromGlobalScope([aspectId]);
|
199
|
-
const remoteGenerator = globalScopeHarmony.get<GeneratorMain>(GeneratorAspect.id);
|
200
|
-
const remoteEnvsAspect = globalScopeHarmony.get<EnvsMain>(EnvsAspect.id);
|
201
|
-
const aspect = components[0];
|
202
|
-
const fullAspectId = aspect.id.toString();
|
203
|
-
return { remoteGenerator, fullAspectId, remoteEnvsAspect, aspect };
|
204
|
-
}
|
205
|
-
|
206
|
-
/**
|
207
|
-
* in the case the aspect-id is given and this aspect doesn't exist locally, import it to the
|
208
|
-
* global scope and load it from the capsule
|
209
|
-
*/
|
210
|
-
async findWorkspaceTemplateInGlobalScope(
|
211
|
-
aspectId: string,
|
212
|
-
name?: string
|
213
|
-
): Promise<{ workspaceTemplate?: WorkspaceTemplate; aspect?: Component }> {
|
214
|
-
const { remoteGenerator, fullAspectId, remoteEnvsAspect, aspect } = await this.getGlobalGeneratorEnvs(aspectId);
|
215
|
-
const fromGlobal = await remoteGenerator.searchRegisteredWorkspaceTemplate.call(
|
216
|
-
remoteGenerator,
|
217
|
-
name,
|
218
|
-
fullAspectId,
|
219
|
-
remoteEnvsAspect
|
220
|
-
);
|
221
|
-
return { workspaceTemplate: fromGlobal, aspect };
|
222
|
-
}
|
223
|
-
|
224
|
-
async findTemplateInOtherWorkspace(workspacePath: string, name: string, aspectId?: string) {
|
225
|
-
if (!aspectId)
|
226
|
-
throw new BitError(
|
227
|
-
`to load a template from a different workspace, please provide the aspect-id using --aspect flag`
|
228
|
-
);
|
229
|
-
const harmony = await loadBit(workspacePath);
|
230
|
-
let workspace: Workspace;
|
231
|
-
try {
|
232
|
-
workspace = harmony.get<Workspace>(WorkspaceAspect.id);
|
233
|
-
} catch (err: any) {
|
234
|
-
throw new Error(`fatal: "${workspacePath}" is not a valid Bit workspace, make sure the path is correct`);
|
235
|
-
}
|
236
|
-
const aspectComponentId = await workspace.resolveComponentId(aspectId);
|
237
|
-
await workspace.loadAspects([aspectId], true);
|
238
|
-
const aspectComponent = await workspace.get(aspectComponentId);
|
239
|
-
const aspectFullId = aspectComponentId.toString();
|
240
|
-
const generator = harmony.get<GeneratorMain>(GeneratorAspect.id);
|
241
|
-
const workspaceTemplate = await generator.searchRegisteredWorkspaceTemplate(name, aspectFullId);
|
242
|
-
return { workspaceTemplate, aspect: aspectComponent };
|
243
|
-
}
|
244
|
-
|
245
|
-
/**
|
246
|
-
* returns a specific workspace template.
|
247
|
-
*/
|
248
|
-
async getWorkspaceTemplate(
|
249
|
-
name: string,
|
250
|
-
aspectId?: string
|
251
|
-
): Promise<{ workspaceTemplate: WorkspaceTemplate; aspect?: Component }> {
|
252
|
-
const registeredTemplate = await this.searchRegisteredWorkspaceTemplate(name, aspectId);
|
253
|
-
if (registeredTemplate) {
|
254
|
-
return { workspaceTemplate: registeredTemplate };
|
255
|
-
}
|
256
|
-
if (!aspectId) {
|
257
|
-
throw new BitError(
|
258
|
-
`template "${name}" was not found, please use --aspect flag to load from an env i.e --aspect teambit.react/react-env\n Learn more about component templates here: https://bit.dev/reference/generator/create-generator`
|
259
|
-
);
|
260
|
-
}
|
261
|
-
|
262
|
-
const { workspaceTemplate, aspect } = await this.findWorkspaceTemplateInGlobalScope(aspectId, name);
|
263
|
-
if (workspaceTemplate) {
|
264
|
-
return { workspaceTemplate, aspect };
|
265
|
-
}
|
266
|
-
throw new BitError(
|
267
|
-
`template "${name}" was not found, please use --aspect flag to load from an env i.e --aspect teambit.react/react-env\n Learn more about component templates here: https://bit.dev/reference/generator/create-generator`
|
268
|
-
);
|
269
|
-
}
|
270
|
-
|
271
|
-
async searchRegisteredWorkspaceTemplate(
|
272
|
-
name?: string,
|
273
|
-
aspectId?: string,
|
274
|
-
remoteEnvsAspect?: EnvsMain
|
275
|
-
): Promise<WorkspaceTemplate | undefined> {
|
276
|
-
let fromEnv;
|
277
|
-
|
278
|
-
if (aspectId) {
|
279
|
-
fromEnv = await this.listEnvWorkspaceTemplates(aspectId, remoteEnvsAspect);
|
280
|
-
}
|
281
|
-
const templates = fromEnv && fromEnv.length ? fromEnv : this.getAllWorkspaceTemplatesFlattened();
|
282
|
-
const found = templates.find(({ id, template: tpl }) => {
|
283
|
-
if (aspectId && name) return aspectId === id && name === tpl.name;
|
284
|
-
if (aspectId) return aspectId === id;
|
285
|
-
if (name) return name === tpl.name;
|
286
|
-
throw new Error(`searchRegisteredWorkspaceTemplate expects to get "name" or "aspectId"`);
|
287
|
-
});
|
288
|
-
|
289
|
-
return found?.template;
|
290
|
-
}
|
291
|
-
|
292
|
-
async generateComponentTemplate(
|
293
|
-
componentNames: string[],
|
294
|
-
templateName: string,
|
295
|
-
options: Partial<CreateOptions>
|
296
|
-
): Promise<GenerateResult[]> {
|
297
|
-
if (!this.workspace) throw new OutsideWorkspaceError();
|
298
|
-
await this.loadAspects();
|
299
|
-
const { namespace, aspect } = options;
|
300
|
-
|
301
|
-
const componentConfigLoadingRegistry = ComponentConfig.componentConfigLoadingRegistry;
|
302
|
-
|
303
|
-
const templateWithId = await this.getComponentTemplate(templateName, aspect);
|
304
|
-
|
305
|
-
ComponentConfig.componentConfigLoadingRegistry = componentConfigLoadingRegistry;
|
306
|
-
|
307
|
-
if (!templateWithId) throw new BitError(`template "${templateName}" was not found`);
|
308
|
-
|
309
|
-
const componentIds = componentNames.map((componentName) =>
|
310
|
-
this.newComponentHelper.getNewComponentId(componentName, namespace, options.scope)
|
311
|
-
);
|
312
|
-
|
313
|
-
const componentGenerator = new ComponentGenerator(
|
314
|
-
this.workspace,
|
315
|
-
componentIds,
|
316
|
-
options,
|
317
|
-
templateWithId.template,
|
318
|
-
this.envs,
|
319
|
-
this.newComponentHelper,
|
320
|
-
this.tracker,
|
321
|
-
this.wsConfigFiles,
|
322
|
-
this.logger,
|
323
|
-
this.onComponentCreateSlot,
|
324
|
-
templateWithId.id,
|
325
|
-
templateWithId.envName ? ComponentID.fromString(templateWithId.id) : undefined
|
326
|
-
);
|
327
|
-
return componentGenerator.generate();
|
328
|
-
}
|
329
|
-
|
330
|
-
async generateWorkspaceTemplate(
|
331
|
-
workspaceName: string,
|
332
|
-
templateName: string,
|
333
|
-
options: NewOptions & { aspect?: string }
|
334
|
-
): Promise<GenerateWorkspaceTemplateResult> {
|
335
|
-
if (this.workspace) {
|
336
|
-
throw new BitError('Error: unable to generate a new workspace inside of an existing workspace');
|
337
|
-
}
|
338
|
-
const { aspect: aspectId, loadFrom } = options;
|
339
|
-
const { workspaceTemplate, aspect } = loadFrom
|
340
|
-
? await this.findTemplateInOtherWorkspace(loadFrom, templateName, aspectId)
|
341
|
-
: await this.getWorkspaceTemplate(templateName, aspectId);
|
342
|
-
|
343
|
-
if (!workspaceTemplate) throw new BitError(`template "${templateName}" was not found`);
|
344
|
-
const workspaceGenerator = new WorkspaceGenerator(workspaceName, options, workspaceTemplate, aspect);
|
345
|
-
const workspacePath = await workspaceGenerator.generate();
|
346
|
-
return { workspacePath, appName: workspaceTemplate.appName };
|
347
|
-
}
|
348
|
-
|
349
|
-
private async getAllComponentTemplatesDescriptorsFlattened(aspectId?: string): Promise<Array<TemplateDescriptor>> {
|
350
|
-
const envTemplates = await this.listEnvComponentTemplateDescriptors([], aspectId);
|
351
|
-
if (envTemplates && envTemplates.length) {
|
352
|
-
if (!aspectId) return envTemplates;
|
353
|
-
const filtered = envTemplates.filter((template) => template.aspectId === aspectId);
|
354
|
-
|
355
|
-
if (filtered.length) return filtered;
|
356
|
-
}
|
357
|
-
|
358
|
-
const flattened = this.getAllComponentTemplatesFlattened();
|
359
|
-
const filtered = aspectId ? flattened.filter((template) => template.id === aspectId) : flattened;
|
360
|
-
return filtered.map((template) => this.getTemplateDescriptor(template));
|
361
|
-
}
|
362
|
-
|
363
|
-
private getAllComponentTemplatesFlattened(): Array<{ id: string; template: ComponentTemplate }> {
|
364
|
-
const templatesByAspects = this.componentTemplateSlot.toArray();
|
365
|
-
const flattened = templatesByAspects.flatMap(([id, componentTemplates]) => {
|
366
|
-
return componentTemplates.map((template) => ({
|
367
|
-
id,
|
368
|
-
template,
|
369
|
-
}));
|
370
|
-
});
|
371
|
-
return flattened;
|
372
|
-
}
|
373
|
-
|
374
|
-
private async getAllWorkspaceTemplatesDescriptorFlattened(aspectId?: string): Promise<Array<TemplateDescriptor>> {
|
375
|
-
let envTemplates;
|
376
|
-
if (aspectId) {
|
377
|
-
envTemplates = await this.listEnvWorkspaceTemplates(aspectId);
|
378
|
-
}
|
379
|
-
|
380
|
-
const templates = envTemplates && envTemplates.length ? envTemplates : this.getAllWorkspaceTemplatesFlattened();
|
381
|
-
return templates.map((template) => this.getTemplateDescriptor(template));
|
382
|
-
}
|
383
|
-
|
384
|
-
private getAllWorkspaceTemplatesFlattened(): Array<{ id: string; template: WorkspaceTemplate }> {
|
385
|
-
const templatesByAspects = this.workspaceTemplateSlot.toArray();
|
386
|
-
return templatesByAspects.flatMap(([id, workspaceTemplates]) => {
|
387
|
-
return workspaceTemplates.map((template) => ({
|
388
|
-
id,
|
389
|
-
template,
|
390
|
-
}));
|
391
|
-
});
|
392
|
-
}
|
393
|
-
|
394
|
-
/**
|
395
|
-
* list all starter templates registered by an env.
|
396
|
-
*/
|
397
|
-
async listEnvWorkspaceTemplates(envId: string, remoteEnvsAspect?: EnvsMain): Promise<Array<WorkspaceTemplateWithId>> {
|
398
|
-
const envs = await this.loadEnvs([envId], remoteEnvsAspect);
|
399
|
-
const workspaceTemplates = envs.flatMap((env) => {
|
400
|
-
if (!env.env.getGeneratorStarters) return undefined;
|
401
|
-
const envStarters = env.env.getGeneratorStarters();
|
402
|
-
return envStarters.map((starter) => {
|
403
|
-
const componentId = ComponentID.fromString(env.id);
|
404
|
-
return {
|
405
|
-
id: componentId.toString(),
|
406
|
-
envName: env.name,
|
407
|
-
template: starter,
|
408
|
-
};
|
409
|
-
});
|
410
|
-
});
|
411
|
-
|
412
|
-
return compact(workspaceTemplates);
|
413
|
-
}
|
414
|
-
|
415
|
-
/**
|
416
|
-
* list all component templates registered by an env.
|
417
|
-
*/
|
418
|
-
async listEnvComponentTemplateDescriptors(ids: string[] = [], aspectId?: string): Promise<TemplateDescriptor[]> {
|
419
|
-
const envTemplates = await this.listEnvComponentTemplates(ids, aspectId);
|
420
|
-
const templates: TemplateDescriptor[] = envTemplates.map((envTemplate) => {
|
421
|
-
const componentId = ComponentID.fromString(envTemplate.id);
|
422
|
-
return {
|
423
|
-
aspectId: componentId.toStringWithoutVersion(),
|
424
|
-
titlePrefix: envTemplate.envName,
|
425
|
-
...envTemplate.template,
|
426
|
-
};
|
427
|
-
});
|
428
|
-
|
429
|
-
return templates;
|
430
|
-
}
|
431
|
-
|
432
|
-
async listEnvComponentTemplates(ids: string[] = [], aspectId?: string): Promise<Array<ComponentTemplateWithId>> {
|
433
|
-
const configEnvs = this.config.envs || [];
|
434
|
-
let remoteEnvsAspect;
|
435
|
-
let fullAspectId;
|
436
|
-
if (aspectId && !configEnvs.includes(aspectId)) {
|
437
|
-
const globals = await this.getGlobalGeneratorEnvs(aspectId);
|
438
|
-
remoteEnvsAspect = globals.remoteEnvsAspect;
|
439
|
-
fullAspectId = globals.fullAspectId;
|
440
|
-
}
|
441
|
-
|
442
|
-
const allIds = [...new Set(configEnvs.concat(ids).concat([aspectId, fullAspectId]).filter(Boolean))];
|
443
|
-
|
444
|
-
const envs = await this.loadEnvs(allIds, remoteEnvsAspect);
|
445
|
-
const uniqueEnvs = envs.filter((v, i, a) => a.findIndex(t => (t.id === v.id)) === i);
|
446
|
-
const templates = uniqueEnvs.flatMap((env) => {
|
447
|
-
if (!env.env.getGeneratorTemplates) return [];
|
448
|
-
const tpls = env.env.getGeneratorTemplates() || [];
|
449
|
-
const componentId = ComponentID.fromString(env.id);
|
450
|
-
return tpls.map((template) => {
|
451
|
-
return {
|
452
|
-
id: componentId.toString(),
|
453
|
-
envName: env.name,
|
454
|
-
template,
|
455
|
-
};
|
456
|
-
});
|
457
|
-
});
|
458
|
-
return templates;
|
459
|
-
}
|
460
|
-
|
461
|
-
async loadEnvs(ids: string[] = this.config.envs || [], remoteEnvsAspect?: EnvsMain): Promise<EnvDefinition[]> {
|
462
|
-
// In case we have remoteEnvsAspect it means that we are running from the global scope
|
463
|
-
// in that case the aspect / env were already loaded before to the global scope harmony instance
|
464
|
-
// so no reason to load it here
|
465
|
-
if (!remoteEnvsAspect) {
|
466
|
-
const host = this.componentAspect.getHost();
|
467
|
-
if (!host) return [];
|
468
|
-
await host.loadAspects(ids);
|
469
|
-
}
|
470
|
-
|
471
|
-
const envsAspect = remoteEnvsAspect || this.envs;
|
472
|
-
|
473
|
-
const potentialEnvs = ids.map((id) => {
|
474
|
-
const componentId = ComponentID.fromString(id);
|
475
|
-
return envsAspect.getEnvDefinition(componentId);
|
476
|
-
});
|
477
|
-
|
478
|
-
return compact(potentialEnvs);
|
479
|
-
}
|
480
|
-
|
481
|
-
async loadAspects() {
|
482
|
-
if (this.aspectLoaded) return;
|
483
|
-
await this.workspace.loadAspects(this.config.aspects);
|
484
|
-
this.aspectLoaded = true;
|
485
|
-
}
|
486
|
-
|
487
|
-
static slots = [
|
488
|
-
Slot.withType<ComponentTemplate[]>(),
|
489
|
-
Slot.withType<WorkspaceTemplate[]>(),
|
490
|
-
Slot.withType<OnComponentCreateFn>(),
|
491
|
-
];
|
492
|
-
|
493
|
-
static dependencies = [
|
494
|
-
WorkspaceAspect,
|
495
|
-
CLIAspect,
|
496
|
-
GraphqlAspect,
|
497
|
-
EnvsAspect,
|
498
|
-
AspectLoaderAspect,
|
499
|
-
NewComponentHelperAspect,
|
500
|
-
ComponentAspect,
|
501
|
-
TrackerAspect,
|
502
|
-
LoggerAspect,
|
503
|
-
GitAspect,
|
504
|
-
WorkspaceConfigFilesAspect,
|
505
|
-
];
|
506
|
-
|
507
|
-
static runtime = MainRuntime;
|
508
|
-
|
509
|
-
static async provider(
|
510
|
-
[
|
511
|
-
workspace,
|
512
|
-
cli,
|
513
|
-
graphql,
|
514
|
-
envs,
|
515
|
-
aspectLoader,
|
516
|
-
newComponentHelper,
|
517
|
-
componentAspect,
|
518
|
-
tracker,
|
519
|
-
loggerMain,
|
520
|
-
git,
|
521
|
-
wsConfigFiles,
|
522
|
-
]: [
|
523
|
-
Workspace,
|
524
|
-
CLIMain,
|
525
|
-
GraphqlMain,
|
526
|
-
EnvsMain,
|
527
|
-
AspectLoaderMain,
|
528
|
-
NewComponentHelperMain,
|
529
|
-
ComponentMain,
|
530
|
-
TrackerMain,
|
531
|
-
LoggerMain,
|
532
|
-
GitMain,
|
533
|
-
WorkspaceConfigFilesMain
|
534
|
-
],
|
535
|
-
config: GeneratorConfig,
|
536
|
-
[componentTemplateSlot, workspaceTemplateSlot, onComponentCreateSlot]: [
|
537
|
-
ComponentTemplateSlot,
|
538
|
-
WorkspaceTemplateSlot,
|
539
|
-
OnComponentCreateSlot
|
540
|
-
]
|
541
|
-
) {
|
542
|
-
const logger = loggerMain.createLogger(GeneratorAspect.id);
|
543
|
-
const generator = new GeneratorMain(
|
544
|
-
componentTemplateSlot,
|
545
|
-
workspaceTemplateSlot,
|
546
|
-
onComponentCreateSlot,
|
547
|
-
config,
|
548
|
-
workspace,
|
549
|
-
envs,
|
550
|
-
aspectLoader,
|
551
|
-
newComponentHelper,
|
552
|
-
componentAspect,
|
553
|
-
tracker,
|
554
|
-
logger,
|
555
|
-
git,
|
556
|
-
wsConfigFiles
|
557
|
-
);
|
558
|
-
const commands = [new CreateCmd(generator), new TemplatesCmd(generator), new NewCmd(generator)];
|
559
|
-
cli.register(...commands);
|
560
|
-
graphql.register(generatorSchema(generator));
|
561
|
-
aspectLoader.registerPlugins([new StarterPlugin(generator)]);
|
562
|
-
envs.registerService(new GeneratorService());
|
563
|
-
|
564
|
-
if (generator)
|
565
|
-
generator.registerComponentTemplate([
|
566
|
-
componentGeneratorTemplate,
|
567
|
-
componentGeneratorTemplateStandalone,
|
568
|
-
starterTemplate,
|
569
|
-
starterTemplateStandalone,
|
570
|
-
]);
|
571
|
-
generator.registerWorkspaceTemplate([BasicWorkspaceStarter]);
|
572
|
-
|
573
|
-
return generator;
|
574
|
-
}
|
575
|
-
}
|
576
|
-
|
577
|
-
GeneratorAspect.addRuntime(GeneratorMain);
|
package/index.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
export type { GeneratorMain } from './generator.main.runtime';
|
2
|
-
export {
|
3
|
-
ComponentContext,
|
4
|
-
ComponentTemplate,
|
5
|
-
ComponentFile,
|
6
|
-
ComponentConfig,
|
7
|
-
ConfigContext,
|
8
|
-
} from './component-template';
|
9
|
-
export { WorkspaceContext, WorkspaceTemplate, WorkspaceTemplate as Starter, WorkspaceFile } from './workspace-template';
|
10
|
-
export { GeneratorEnv } from './generator-env-type';
|
11
|
-
export { GeneratorAspect } from './generator.aspect';
|
12
|
-
export { TemplateList } from './template-list';
|
13
|
-
export { StarterList } from './starter-list';
|
14
|
-
export type { GenerateResult } from './component-generator';
|
package/new.cmd.ts
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
2
|
-
import chalk from 'chalk';
|
3
|
-
import { GeneratorMain } from './generator.main.runtime';
|
4
|
-
import { BaseWorkspaceOptions } from './workspace-template';
|
5
|
-
|
6
|
-
/**
|
7
|
-
* NewOptions combines foundational properties with additional options for creating a workspace.
|
8
|
-
*/
|
9
|
-
export type NewOptions = BaseWorkspaceOptions;
|
10
|
-
|
11
|
-
export class NewCmd implements Command {
|
12
|
-
name = 'new <template-name> <workspace-name>';
|
13
|
-
description = 'create a new workspace from a template';
|
14
|
-
arguments = [
|
15
|
-
{
|
16
|
-
name: 'template-name',
|
17
|
-
description:
|
18
|
-
"the name of the workspace template (run 'bit templates' outside of a workspace to get a list of available workspace templates)",
|
19
|
-
},
|
20
|
-
{
|
21
|
-
name: 'workspace-name',
|
22
|
-
description: 'the name for the new workspace and workspace directory that will be created',
|
23
|
-
},
|
24
|
-
];
|
25
|
-
alias = '';
|
26
|
-
loader = true;
|
27
|
-
group = 'start';
|
28
|
-
options = [
|
29
|
-
[
|
30
|
-
'a',
|
31
|
-
'aspect <aspect-id>',
|
32
|
-
'id of the aspect that registered the template, mandatory for non-core aspects. helpful for core aspects in case of a name collision',
|
33
|
-
],
|
34
|
-
['t', 'template <env-id>', "env-id of the template's owner. Alias for --env."],
|
35
|
-
['', 'env <env-id>', 'env-id of the template. Alias -t'],
|
36
|
-
['d', 'default-scope <scope-name>', `set defaultScope in the new workspace's workspace.jsonc`],
|
37
|
-
['', 'standalone', 'DEPRECATED. use --skip-git instead'],
|
38
|
-
['s', 'skip-git', 'skip generation of Git repository in the new workspace'],
|
39
|
-
[
|
40
|
-
'e',
|
41
|
-
'empty',
|
42
|
-
"skip template's default component creation (relevant for templates that add components by default)",
|
43
|
-
],
|
44
|
-
[
|
45
|
-
'',
|
46
|
-
'load-from <path-to-template>',
|
47
|
-
'local path to the workspace containing the template. Helpful during a development of a workspace-template',
|
48
|
-
],
|
49
|
-
] as CommandOptions;
|
50
|
-
|
51
|
-
constructor(private generator: GeneratorMain) {}
|
52
|
-
|
53
|
-
async report(
|
54
|
-
[templateName, workspaceName]: [string, string],
|
55
|
-
options: NewOptions & {
|
56
|
-
standalone: boolean;
|
57
|
-
env?: string;
|
58
|
-
template?: string;
|
59
|
-
aspect?: string;
|
60
|
-
}
|
61
|
-
) {
|
62
|
-
options.skipGit = options.skipGit ?? options.standalone;
|
63
|
-
options.aspect = options.aspect ?? options.env ?? options.template;
|
64
|
-
const { workspacePath, appName } = await this.generator.generateWorkspaceTemplate(
|
65
|
-
workspaceName,
|
66
|
-
templateName,
|
67
|
-
options
|
68
|
-
);
|
69
|
-
return chalk.white(
|
70
|
-
`${chalk.green(`
|
71
|
-
|
72
|
-
Congrats! A new workspace has been created successfully at '${workspacePath}'`)}
|
73
|
-
|
74
|
-
Inside the directory '${workspaceName}' you can run various commands including:
|
75
|
-
|
76
|
-
${chalk.yellow('bit start')}
|
77
|
-
Starts the local dev server
|
78
|
-
|
79
|
-
${chalk.yellow('bit install')}
|
80
|
-
Installs any missing dependencies
|
81
|
-
|
82
|
-
${chalk.yellow('bit status')}
|
83
|
-
Shows the status of the components
|
84
|
-
|
85
|
-
${chalk.yellow('bit compile')}
|
86
|
-
Compiles the components
|
87
|
-
|
88
|
-
${chalk.yellow('bit test')}
|
89
|
-
Runs the tests on all your components
|
90
|
-
|
91
|
-
${chalk.yellow('bit templates')}
|
92
|
-
Shows all available component templates
|
93
|
-
|
94
|
-
${chalk.yellow('bit help')}
|
95
|
-
Shows all available commands
|
96
|
-
|
97
|
-
|
98
|
-
${chalk.green.bold("Let's get started!")}
|
99
|
-
|
100
|
-
${getBottomSection(workspaceName, appName)}
|
101
|
-
`
|
102
|
-
);
|
103
|
-
}
|
104
|
-
}
|
105
|
-
|
106
|
-
function getBottomSection(workspaceName: string, appName: string | undefined) {
|
107
|
-
const cdLine = chalk.yellow(`cd ${workspaceName}`);
|
108
|
-
const parts = [cdLine];
|
109
|
-
if (appName) {
|
110
|
-
parts.push(chalk.yellow(` bit run ${appName}`));
|
111
|
-
}
|
112
|
-
parts.push(chalk.yellow(` bit start`));
|
113
|
-
return parts.join('\n');
|
114
|
-
}
|
Binary file
|