@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.
- package/artifacts/preview/teambit_generator_generator-preview.js +1 -0
- package/dist/{preview-1703647408454.js → preview-1703698405864.js} +2 -2
- package/package.json +22 -22
- package/component-generator.ts +0 -284
- package/component-template.ts +0 -152
- 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 -600
- package/index.ts +0 -23
- package/new.cmd.ts +0 -120
- package/starter-list.ts +0 -21
- package/starter.plugin.ts +0 -16
- package/template-list.ts +0 -21
- package/templates.cmd.ts +0 -57
- package/types.ts +0 -3
- package/workspace-generator.ts +0 -220
- package/workspace-template.ts +0 -183
package/generator.graphql.ts
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
import { Schema } from '@teambit/graphql';
|
2
|
-
import gql from 'graphql-tag';
|
3
|
-
import { GeneratorMain } from './generator.main.runtime';
|
4
|
-
import { CreateOptions } from './create.cmd';
|
5
|
-
|
6
|
-
export type CreateQueryOptions = CreateOptions & { templateName: string };
|
7
|
-
|
8
|
-
export function generatorSchema(generator: GeneratorMain): Schema {
|
9
|
-
return {
|
10
|
-
typeDefs: gql`
|
11
|
-
type GenerateResult {
|
12
|
-
id: String
|
13
|
-
dir: String
|
14
|
-
files: [String]
|
15
|
-
}
|
16
|
-
|
17
|
-
type Mutation {
|
18
|
-
# create Component by template
|
19
|
-
createComponent(
|
20
|
-
name: String!
|
21
|
-
templateName: String!
|
22
|
-
scope: String
|
23
|
-
namespace: String
|
24
|
-
aspect: String
|
25
|
-
): [GenerateResult]
|
26
|
-
}
|
27
|
-
|
28
|
-
type TemplateDescriptor {
|
29
|
-
aspectId: String!
|
30
|
-
name: String!
|
31
|
-
}
|
32
|
-
|
33
|
-
type Generator {
|
34
|
-
templates: [TemplateDescriptor]
|
35
|
-
}
|
36
|
-
|
37
|
-
type Query {
|
38
|
-
generator: Generator
|
39
|
-
}
|
40
|
-
`,
|
41
|
-
resolvers: {
|
42
|
-
Mutation: {
|
43
|
-
createComponent: async (req: any, { name, templateName, ...options }: CreateQueryOptions) => {
|
44
|
-
const res = await generator.generateComponentTemplate([name], templateName, { name, ...options });
|
45
|
-
return res.map((component) => ({
|
46
|
-
id: component.id.toString(),
|
47
|
-
dir: component.dir,
|
48
|
-
files: component.files,
|
49
|
-
}));
|
50
|
-
},
|
51
|
-
},
|
52
|
-
Generator: {
|
53
|
-
templates: async () => {
|
54
|
-
return generator.listTemplates();
|
55
|
-
},
|
56
|
-
},
|
57
|
-
Query: {
|
58
|
-
generator: () => generator,
|
59
|
-
},
|
60
|
-
},
|
61
|
-
};
|
62
|
-
}
|
@@ -1,600 +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, restoreGlobals } 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
|
-
restoreGlobals();
|
204
|
-
|
205
|
-
return { remoteGenerator, fullAspectId, remoteEnvsAspect, aspect };
|
206
|
-
}
|
207
|
-
|
208
|
-
/**
|
209
|
-
* in the case the aspect-id is given and this aspect doesn't exist locally, import it to the
|
210
|
-
* global scope and load it from the capsule
|
211
|
-
*/
|
212
|
-
async findWorkspaceTemplateInGlobalScope(
|
213
|
-
aspectId: string,
|
214
|
-
name?: string
|
215
|
-
): Promise<{ workspaceTemplate?: WorkspaceTemplate; aspect?: Component }> {
|
216
|
-
const { remoteGenerator, fullAspectId, remoteEnvsAspect, aspect } = await this.getGlobalGeneratorEnvs(aspectId);
|
217
|
-
const fromGlobal = await remoteGenerator.searchRegisteredWorkspaceTemplate.call(
|
218
|
-
remoteGenerator,
|
219
|
-
name,
|
220
|
-
fullAspectId,
|
221
|
-
remoteEnvsAspect
|
222
|
-
);
|
223
|
-
return { workspaceTemplate: fromGlobal, aspect };
|
224
|
-
}
|
225
|
-
|
226
|
-
async findTemplateInOtherWorkspace(workspacePath: string, name: string, aspectId?: string) {
|
227
|
-
if (!aspectId)
|
228
|
-
throw new BitError(
|
229
|
-
`to load a template from a different workspace, please provide the aspect-id using --aspect flag`
|
230
|
-
);
|
231
|
-
const harmony = await loadBit(workspacePath);
|
232
|
-
let workspace: Workspace;
|
233
|
-
try {
|
234
|
-
workspace = harmony.get<Workspace>(WorkspaceAspect.id);
|
235
|
-
} catch (err: any) {
|
236
|
-
throw new Error(`fatal: "${workspacePath}" is not a valid Bit workspace, make sure the path is correct`);
|
237
|
-
}
|
238
|
-
const aspectComponentId = await workspace.resolveComponentId(aspectId);
|
239
|
-
await workspace.loadAspects([aspectId], true);
|
240
|
-
const aspectComponent = await workspace.get(aspectComponentId);
|
241
|
-
const aspectFullId = aspectComponentId.toString();
|
242
|
-
const generator = harmony.get<GeneratorMain>(GeneratorAspect.id);
|
243
|
-
const workspaceTemplate = await generator.searchRegisteredWorkspaceTemplate(name, aspectFullId);
|
244
|
-
return { workspaceTemplate, aspect: aspectComponent };
|
245
|
-
}
|
246
|
-
|
247
|
-
/**
|
248
|
-
* returns a specific workspace template.
|
249
|
-
*/
|
250
|
-
async getWorkspaceTemplate(
|
251
|
-
name: string,
|
252
|
-
aspectId?: string
|
253
|
-
): Promise<{ workspaceTemplate: WorkspaceTemplate; aspect?: Component }> {
|
254
|
-
const registeredTemplate = await this.searchRegisteredWorkspaceTemplate(name, aspectId);
|
255
|
-
if (registeredTemplate) {
|
256
|
-
return { workspaceTemplate: registeredTemplate };
|
257
|
-
}
|
258
|
-
if (!aspectId) {
|
259
|
-
throw new BitError(
|
260
|
-
`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`
|
261
|
-
);
|
262
|
-
}
|
263
|
-
|
264
|
-
const { workspaceTemplate, aspect } = await this.findWorkspaceTemplateInGlobalScope(aspectId, name);
|
265
|
-
if (workspaceTemplate) {
|
266
|
-
return { workspaceTemplate, aspect };
|
267
|
-
}
|
268
|
-
throw new BitError(
|
269
|
-
`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`
|
270
|
-
);
|
271
|
-
}
|
272
|
-
|
273
|
-
async searchRegisteredWorkspaceTemplate(
|
274
|
-
name?: string,
|
275
|
-
aspectId?: string,
|
276
|
-
remoteEnvsAspect?: EnvsMain
|
277
|
-
): Promise<WorkspaceTemplate | undefined> {
|
278
|
-
let fromEnv;
|
279
|
-
|
280
|
-
if (aspectId) {
|
281
|
-
fromEnv = await this.listEnvWorkspaceTemplates(aspectId, remoteEnvsAspect);
|
282
|
-
}
|
283
|
-
const templates = fromEnv && fromEnv.length ? fromEnv : this.getAllWorkspaceTemplatesFlattened();
|
284
|
-
const found = templates.find(({ id, template: tpl }) => {
|
285
|
-
if (aspectId && name) return aspectId === id && name === tpl.name;
|
286
|
-
if (aspectId) return aspectId === id;
|
287
|
-
if (name) return name === tpl.name;
|
288
|
-
throw new Error(`searchRegisteredWorkspaceTemplate expects to get "name" or "aspectId"`);
|
289
|
-
});
|
290
|
-
|
291
|
-
return found?.template;
|
292
|
-
}
|
293
|
-
|
294
|
-
async generateComponentTemplate(
|
295
|
-
componentNames: string[],
|
296
|
-
templateName: string,
|
297
|
-
options: Partial<CreateOptions>
|
298
|
-
): Promise<GenerateResult[]> {
|
299
|
-
if (!this.workspace) throw new OutsideWorkspaceError();
|
300
|
-
await this.loadAspects();
|
301
|
-
const { namespace, aspect } = options;
|
302
|
-
|
303
|
-
const componentConfigLoadingRegistry = ComponentConfig.componentConfigLoadingRegistry;
|
304
|
-
|
305
|
-
const templateWithId = await this.getComponentTemplate(templateName, aspect);
|
306
|
-
|
307
|
-
ComponentConfig.componentConfigLoadingRegistry = componentConfigLoadingRegistry;
|
308
|
-
|
309
|
-
if (!templateWithId) throw new BitError(`template "${templateName}" was not found`);
|
310
|
-
|
311
|
-
const componentIds = componentNames.map((componentName) =>
|
312
|
-
this.newComponentHelper.getNewComponentId(componentName, namespace, options.scope)
|
313
|
-
);
|
314
|
-
|
315
|
-
const envId = await this.getEnvIdFromTemplateWithId(templateWithId);
|
316
|
-
|
317
|
-
const componentGenerator = new ComponentGenerator(
|
318
|
-
this.workspace,
|
319
|
-
componentIds,
|
320
|
-
options,
|
321
|
-
templateWithId.template,
|
322
|
-
this.envs,
|
323
|
-
this.newComponentHelper,
|
324
|
-
this.tracker,
|
325
|
-
this.wsConfigFiles,
|
326
|
-
this.logger,
|
327
|
-
this.onComponentCreateSlot,
|
328
|
-
templateWithId.id,
|
329
|
-
envId
|
330
|
-
);
|
331
|
-
return componentGenerator.generate();
|
332
|
-
}
|
333
|
-
|
334
|
-
private async getEnvIdFromTemplateWithId(templateWithId: ComponentTemplateWithId): Promise<ComponentID | undefined> {
|
335
|
-
const envIdFromTemplate = templateWithId.template.env;
|
336
|
-
if (envIdFromTemplate) {
|
337
|
-
const parsedFromTemplate = ComponentID.tryFromString(envIdFromTemplate);
|
338
|
-
if (!parsedFromTemplate) {
|
339
|
-
throw new BitError(
|
340
|
-
`Error: unable to parse envId from template. template name: ${templateWithId.template.name}, envId: ${envIdFromTemplate}`
|
341
|
-
);
|
342
|
-
}
|
343
|
-
const resolvedId = await this.workspace.resolveEnvIdWithPotentialVersionForConfig(parsedFromTemplate);
|
344
|
-
return ComponentID.fromString(resolvedId);
|
345
|
-
}
|
346
|
-
if (templateWithId.envName) {
|
347
|
-
return ComponentID.fromString(templateWithId.id);
|
348
|
-
}
|
349
|
-
return Promise.resolve(undefined);
|
350
|
-
}
|
351
|
-
|
352
|
-
async generateWorkspaceTemplate(
|
353
|
-
workspaceName: string,
|
354
|
-
templateName: string,
|
355
|
-
options: NewOptions & { aspect?: string; currentDir?: boolean }
|
356
|
-
): Promise<GenerateWorkspaceTemplateResult> {
|
357
|
-
if (this.workspace) {
|
358
|
-
throw new BitError('Error: unable to generate a new workspace inside of an existing workspace');
|
359
|
-
}
|
360
|
-
const { aspect: aspectId, loadFrom } = options;
|
361
|
-
const { workspaceTemplate, aspect } = loadFrom
|
362
|
-
? await this.findTemplateInOtherWorkspace(loadFrom, templateName, aspectId)
|
363
|
-
: await this.getWorkspaceTemplate(templateName, aspectId);
|
364
|
-
|
365
|
-
if (!workspaceTemplate) throw new BitError(`template "${templateName}" was not found`);
|
366
|
-
const workspaceGenerator = new WorkspaceGenerator(workspaceName, options, workspaceTemplate, aspect);
|
367
|
-
const workspacePath = await workspaceGenerator.generate();
|
368
|
-
return { workspacePath, appName: workspaceTemplate.appName };
|
369
|
-
}
|
370
|
-
|
371
|
-
private async getAllComponentTemplatesDescriptorsFlattened(aspectId?: string): Promise<Array<TemplateDescriptor>> {
|
372
|
-
const envTemplates = await this.listEnvComponentTemplateDescriptors([], aspectId);
|
373
|
-
if (envTemplates && envTemplates.length) {
|
374
|
-
if (!aspectId) return envTemplates;
|
375
|
-
const filtered = envTemplates.filter((template) => template.aspectId === aspectId);
|
376
|
-
if (filtered.length) return filtered;
|
377
|
-
}
|
378
|
-
|
379
|
-
const flattened = this.getAllComponentTemplatesFlattened();
|
380
|
-
const filtered = aspectId ? flattened.filter((template) => template.id === aspectId) : flattened;
|
381
|
-
return filtered.map((template) => this.getTemplateDescriptor(template));
|
382
|
-
}
|
383
|
-
|
384
|
-
private getAllComponentTemplatesFlattened(): Array<{ id: string; template: ComponentTemplate }> {
|
385
|
-
const templatesByAspects = this.componentTemplateSlot.toArray();
|
386
|
-
const flattened = templatesByAspects.flatMap(([id, componentTemplates]) => {
|
387
|
-
return componentTemplates.map((template) => ({
|
388
|
-
id,
|
389
|
-
template,
|
390
|
-
}));
|
391
|
-
});
|
392
|
-
return flattened;
|
393
|
-
}
|
394
|
-
|
395
|
-
private async getAllWorkspaceTemplatesDescriptorFlattened(aspectId?: string): Promise<Array<TemplateDescriptor>> {
|
396
|
-
let envTemplates;
|
397
|
-
if (aspectId) {
|
398
|
-
envTemplates = await this.listEnvWorkspaceTemplates(aspectId);
|
399
|
-
}
|
400
|
-
|
401
|
-
const templates = envTemplates && envTemplates.length ? envTemplates : this.getAllWorkspaceTemplatesFlattened();
|
402
|
-
return templates.map((template) => this.getTemplateDescriptor(template));
|
403
|
-
}
|
404
|
-
|
405
|
-
private getAllWorkspaceTemplatesFlattened(): Array<{ id: string; template: WorkspaceTemplate }> {
|
406
|
-
const templatesByAspects = this.workspaceTemplateSlot.toArray();
|
407
|
-
return templatesByAspects.flatMap(([id, workspaceTemplates]) => {
|
408
|
-
return workspaceTemplates.map((template) => ({
|
409
|
-
id,
|
410
|
-
template,
|
411
|
-
}));
|
412
|
-
});
|
413
|
-
}
|
414
|
-
|
415
|
-
/**
|
416
|
-
* list all starter templates registered by an env.
|
417
|
-
*/
|
418
|
-
async listEnvWorkspaceTemplates(envId: string, remoteEnvsAspect?: EnvsMain): Promise<Array<WorkspaceTemplateWithId>> {
|
419
|
-
const envs = await this.loadEnvs([envId], remoteEnvsAspect);
|
420
|
-
const workspaceTemplates = envs.flatMap((env) => {
|
421
|
-
if (!env.env.getGeneratorStarters) return undefined;
|
422
|
-
const envStarters = env.env.getGeneratorStarters();
|
423
|
-
return envStarters.map((starter) => {
|
424
|
-
const componentId = ComponentID.fromString(env.id);
|
425
|
-
return {
|
426
|
-
id: componentId.toString(),
|
427
|
-
envName: env.name,
|
428
|
-
template: starter,
|
429
|
-
};
|
430
|
-
});
|
431
|
-
});
|
432
|
-
|
433
|
-
return compact(workspaceTemplates);
|
434
|
-
}
|
435
|
-
|
436
|
-
/**
|
437
|
-
* list all component templates registered by an env.
|
438
|
-
*/
|
439
|
-
async listEnvComponentTemplateDescriptors(ids: string[] = [], aspectId?: string): Promise<TemplateDescriptor[]> {
|
440
|
-
const envTemplates = await this.listEnvComponentTemplates(ids, aspectId);
|
441
|
-
const templates: TemplateDescriptor[] = envTemplates.map((envTemplate) => {
|
442
|
-
const componentId = ComponentID.fromString(envTemplate.id);
|
443
|
-
return {
|
444
|
-
aspectId: componentId.toStringWithoutVersion(),
|
445
|
-
titlePrefix: envTemplate.envName,
|
446
|
-
...envTemplate.template,
|
447
|
-
};
|
448
|
-
});
|
449
|
-
|
450
|
-
return templates;
|
451
|
-
}
|
452
|
-
|
453
|
-
getConfiguredEnvs(): string[] {
|
454
|
-
return this.config.envs ?? [];
|
455
|
-
}
|
456
|
-
|
457
|
-
async listEnvComponentTemplates(ids: string[] = [], aspectId?: string): Promise<Array<ComponentTemplateWithId>> {
|
458
|
-
const configEnvs = this.config.envs || [];
|
459
|
-
let remoteEnvsAspect;
|
460
|
-
let fullAspectId;
|
461
|
-
if (aspectId && !configEnvs.includes(aspectId)) {
|
462
|
-
const globals = await this.getGlobalGeneratorEnvs(aspectId);
|
463
|
-
remoteEnvsAspect = globals.remoteEnvsAspect;
|
464
|
-
fullAspectId = globals.fullAspectId;
|
465
|
-
}
|
466
|
-
const allIds = configEnvs?.concat(ids).concat([aspectId, fullAspectId]).filter(Boolean);
|
467
|
-
const envs = await this.loadEnvs(allIds, remoteEnvsAspect);
|
468
|
-
const templates = envs.flatMap((env) => {
|
469
|
-
if (!env.env.getGeneratorTemplates) return [];
|
470
|
-
const tpls = env.env.getGeneratorTemplates() || [];
|
471
|
-
const componentId = ComponentID.fromString(env.id);
|
472
|
-
return tpls.map((template) => {
|
473
|
-
return {
|
474
|
-
id: componentId.toString(),
|
475
|
-
envName: env.name,
|
476
|
-
template,
|
477
|
-
};
|
478
|
-
});
|
479
|
-
});
|
480
|
-
|
481
|
-
return templates;
|
482
|
-
}
|
483
|
-
|
484
|
-
async loadEnvs(ids: string[] = this.config.envs || [], remoteEnvsAspect?: EnvsMain): Promise<EnvDefinition[]> {
|
485
|
-
// In case we have remoteEnvsAspect it means that we are running from the global scope
|
486
|
-
// in that case the aspect / env were already loaded before to the global scope harmony instance
|
487
|
-
// so no reason to load it here
|
488
|
-
if (!remoteEnvsAspect) {
|
489
|
-
const host = this.componentAspect.getHost();
|
490
|
-
if (!host) return [];
|
491
|
-
await host.loadAspects(ids);
|
492
|
-
}
|
493
|
-
|
494
|
-
const envsAspect = remoteEnvsAspect || this.envs;
|
495
|
-
|
496
|
-
const potentialEnvs = ids.map((id) => {
|
497
|
-
const componentId = ComponentID.fromString(id);
|
498
|
-
return envsAspect.getEnvDefinition(componentId);
|
499
|
-
});
|
500
|
-
|
501
|
-
return compact(potentialEnvs);
|
502
|
-
}
|
503
|
-
|
504
|
-
async loadAspects() {
|
505
|
-
if (this.aspectLoaded) return;
|
506
|
-
await this.workspace.loadAspects(this.config.aspects);
|
507
|
-
this.aspectLoaded = true;
|
508
|
-
}
|
509
|
-
|
510
|
-
static slots = [
|
511
|
-
Slot.withType<ComponentTemplate[]>(),
|
512
|
-
Slot.withType<WorkspaceTemplate[]>(),
|
513
|
-
Slot.withType<OnComponentCreateFn>(),
|
514
|
-
];
|
515
|
-
|
516
|
-
static dependencies = [
|
517
|
-
WorkspaceAspect,
|
518
|
-
CLIAspect,
|
519
|
-
GraphqlAspect,
|
520
|
-
EnvsAspect,
|
521
|
-
AspectLoaderAspect,
|
522
|
-
NewComponentHelperAspect,
|
523
|
-
ComponentAspect,
|
524
|
-
TrackerAspect,
|
525
|
-
LoggerAspect,
|
526
|
-
GitAspect,
|
527
|
-
WorkspaceConfigFilesAspect,
|
528
|
-
];
|
529
|
-
|
530
|
-
static runtime = MainRuntime;
|
531
|
-
|
532
|
-
static async provider(
|
533
|
-
[
|
534
|
-
workspace,
|
535
|
-
cli,
|
536
|
-
graphql,
|
537
|
-
envs,
|
538
|
-
aspectLoader,
|
539
|
-
newComponentHelper,
|
540
|
-
componentAspect,
|
541
|
-
tracker,
|
542
|
-
loggerMain,
|
543
|
-
git,
|
544
|
-
wsConfigFiles,
|
545
|
-
]: [
|
546
|
-
Workspace,
|
547
|
-
CLIMain,
|
548
|
-
GraphqlMain,
|
549
|
-
EnvsMain,
|
550
|
-
AspectLoaderMain,
|
551
|
-
NewComponentHelperMain,
|
552
|
-
ComponentMain,
|
553
|
-
TrackerMain,
|
554
|
-
LoggerMain,
|
555
|
-
GitMain,
|
556
|
-
WorkspaceConfigFilesMain
|
557
|
-
],
|
558
|
-
config: GeneratorConfig,
|
559
|
-
[componentTemplateSlot, workspaceTemplateSlot, onComponentCreateSlot]: [
|
560
|
-
ComponentTemplateSlot,
|
561
|
-
WorkspaceTemplateSlot,
|
562
|
-
OnComponentCreateSlot
|
563
|
-
]
|
564
|
-
) {
|
565
|
-
const logger = loggerMain.createLogger(GeneratorAspect.id);
|
566
|
-
const generator = new GeneratorMain(
|
567
|
-
componentTemplateSlot,
|
568
|
-
workspaceTemplateSlot,
|
569
|
-
onComponentCreateSlot,
|
570
|
-
config,
|
571
|
-
workspace,
|
572
|
-
envs,
|
573
|
-
aspectLoader,
|
574
|
-
newComponentHelper,
|
575
|
-
componentAspect,
|
576
|
-
tracker,
|
577
|
-
logger,
|
578
|
-
git,
|
579
|
-
wsConfigFiles
|
580
|
-
);
|
581
|
-
const commands = [new CreateCmd(generator), new TemplatesCmd(generator), new NewCmd(generator)];
|
582
|
-
cli.register(...commands);
|
583
|
-
graphql.register(generatorSchema(generator));
|
584
|
-
aspectLoader.registerPlugins([new StarterPlugin(generator)]);
|
585
|
-
envs.registerService(new GeneratorService());
|
586
|
-
|
587
|
-
if (generator)
|
588
|
-
generator.registerComponentTemplate([
|
589
|
-
componentGeneratorTemplate,
|
590
|
-
componentGeneratorTemplateStandalone,
|
591
|
-
starterTemplate,
|
592
|
-
starterTemplateStandalone,
|
593
|
-
]);
|
594
|
-
generator.registerWorkspaceTemplate([BasicWorkspaceStarter]);
|
595
|
-
|
596
|
-
return generator;
|
597
|
-
}
|
598
|
-
}
|
599
|
-
|
600
|
-
GeneratorAspect.addRuntime(GeneratorMain);
|
package/index.ts
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
export type { GeneratorMain } from './generator.main.runtime';
|
2
|
-
export type {
|
3
|
-
ComponentContext,
|
4
|
-
ComponentTemplate,
|
5
|
-
ComponentTemplateOptions,
|
6
|
-
ComponentFile,
|
7
|
-
ComponentConfig,
|
8
|
-
ConfigContext,
|
9
|
-
} from './component-template';
|
10
|
-
export type {
|
11
|
-
WorkspaceContext,
|
12
|
-
WorkspaceTemplate,
|
13
|
-
WorkspaceTemplate as Starter,
|
14
|
-
WorkspaceFile,
|
15
|
-
ForkComponentInfo,
|
16
|
-
ImportComponentInfo,
|
17
|
-
CreateComponentInfo,
|
18
|
-
} from './workspace-template';
|
19
|
-
export type { GeneratorEnv } from './generator-env-type';
|
20
|
-
export { GeneratorAspect } from './generator.aspect';
|
21
|
-
export { TemplateList } from './template-list';
|
22
|
-
export { StarterList } from './starter-list';
|
23
|
-
export type { GenerateResult } from './component-generator';
|