@teambit/generator 0.0.555 → 0.0.559
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/package.json +35 -20
- package/tsconfig.json +1 -1
- package/component-generator.ts +0 -119
- package/component-template.ts +0 -66
- package/create.cmd.ts +0 -44
- package/generator.aspect.ts +0 -5
- package/generator.graphql.ts +0 -66
- package/generator.main.runtime.ts +0 -279
- package/index.ts +0 -4
- package/new.cmd.ts +0 -79
- package/package-tar/teambit-generator-0.0.555.tgz +0 -0
- package/templates/component-generator/files/aspect-file.ts +0 -10
- package/templates/component-generator/files/docs-file.ts +0 -54
- package/templates/component-generator/files/index.ts +0 -10
- package/templates/component-generator/files/main-runtime.ts +0 -125
- package/templates/component-generator/index.ts +0 -32
- package/templates/workspace-generator/files/aspect-file.ts +0 -10
- package/templates/workspace-generator/files/docs-file.ts +0 -37
- package/templates/workspace-generator/files/git-ignore-tpl.ts +0 -113
- package/templates/workspace-generator/files/index-tpl.ts +0 -29
- package/templates/workspace-generator/files/index.ts +0 -10
- package/templates/workspace-generator/files/main-runtime.ts +0 -24
- package/templates/workspace-generator/files/readme-tpl.ts +0 -39
- package/templates/workspace-generator/files/workspace-config-tpl.ts +0 -18
- package/templates/workspace-generator/index.ts +0 -52
- package/templates.cmd.ts +0 -48
- package/types/asset.d.ts +0 -29
- package/types/style.d.ts +0 -42
- package/types.ts +0 -3
- package/workspace-generator.ts +0 -246
- package/workspace-template.ts +0 -82
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/generator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.559",
|
|
4
4
|
"homepage": "https://bit.dev/teambit/generator/generator",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.generator",
|
|
8
8
|
"name": "generator",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.559"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"camelcase": "6.2.0",
|
|
@@ -21,21 +21,21 @@
|
|
|
21
21
|
"isbinaryfile": "4.0.6",
|
|
22
22
|
"@babel/runtime": "7.12.18",
|
|
23
23
|
"core-js": "^3.0.0",
|
|
24
|
-
"@teambit/bit-error": "0.0.
|
|
25
|
-
"@teambit/component-id": "0.0.
|
|
26
|
-
"@teambit/envs": "0.0.
|
|
27
|
-
"@teambit/workspace": "0.0.
|
|
28
|
-
"@teambit/cli": "0.0.
|
|
29
|
-
"@teambit/graphql": "0.0.
|
|
30
|
-
"@teambit/aspect-loader": "0.0.
|
|
31
|
-
"@teambit/bit": "0.0.
|
|
32
|
-
"@teambit/component": "0.0.
|
|
33
|
-
"@teambit/legacy-bit-id": "0.0.
|
|
34
|
-
"@teambit/compiler": "0.0.
|
|
35
|
-
"@teambit/dependency-resolver": "0.0.
|
|
36
|
-
"@teambit/logger": "0.0.
|
|
37
|
-
"@teambit/pkg": "0.0.
|
|
38
|
-
"@teambit/ui": "0.0.
|
|
24
|
+
"@teambit/bit-error": "0.0.375",
|
|
25
|
+
"@teambit/component-id": "0.0.379",
|
|
26
|
+
"@teambit/envs": "0.0.559",
|
|
27
|
+
"@teambit/workspace": "0.0.559",
|
|
28
|
+
"@teambit/cli": "0.0.389",
|
|
29
|
+
"@teambit/graphql": "0.0.559",
|
|
30
|
+
"@teambit/aspect-loader": "0.0.559",
|
|
31
|
+
"@teambit/bit": "0.0.561",
|
|
32
|
+
"@teambit/component": "0.0.559",
|
|
33
|
+
"@teambit/legacy-bit-id": "0.0.378",
|
|
34
|
+
"@teambit/compiler": "0.0.559",
|
|
35
|
+
"@teambit/dependency-resolver": "0.0.559",
|
|
36
|
+
"@teambit/logger": "0.0.474",
|
|
37
|
+
"@teambit/pkg": "0.0.559",
|
|
38
|
+
"@teambit/ui": "0.0.559"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/fs-extra": "9.0.7",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"@types/react-dom": "^17.0.5",
|
|
47
47
|
"@types/react": "^17.0.8",
|
|
48
48
|
"@types/node": "12.20.4",
|
|
49
|
-
"@teambit/generator.aspect-docs.generator": "0.0.
|
|
49
|
+
"@teambit/generator.aspect-docs.generator": "0.0.111"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@teambit/legacy": "1.0.
|
|
52
|
+
"@teambit/legacy": "1.0.175",
|
|
53
53
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
54
54
|
"react": "^16.8.0 || ^17.0.0"
|
|
55
55
|
},
|
|
@@ -77,12 +77,27 @@
|
|
|
77
77
|
"react": "-"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
|
-
"@teambit/legacy": "1.0.
|
|
80
|
+
"@teambit/legacy": "1.0.175",
|
|
81
81
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
82
82
|
"react": "^16.8.0 || ^17.0.0"
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
|
+
"files": [
|
|
87
|
+
"dist",
|
|
88
|
+
"!dist/tsconfig.tsbuildinfo",
|
|
89
|
+
"**/*.md",
|
|
90
|
+
"**/*.mdx",
|
|
91
|
+
"**/*.js",
|
|
92
|
+
"**/*.json",
|
|
93
|
+
"**/*.sass",
|
|
94
|
+
"**/*.scss",
|
|
95
|
+
"**/*.less",
|
|
96
|
+
"**/*.css",
|
|
97
|
+
"**/*.css",
|
|
98
|
+
"**/*.jpeg",
|
|
99
|
+
"**/*.gif"
|
|
100
|
+
],
|
|
86
101
|
"private": false,
|
|
87
102
|
"engines": {
|
|
88
103
|
"node": ">=12.22.0"
|
package/tsconfig.json
CHANGED
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"skipLibCheck": true,
|
|
16
16
|
"moduleResolution": "node",
|
|
17
17
|
"esModuleInterop": true,
|
|
18
|
-
"outDir": "dist",
|
|
19
18
|
"composite": true,
|
|
20
19
|
"emitDeclarationOnly": true,
|
|
20
|
+
"outDir": "dist",
|
|
21
21
|
"experimentalDecorators": true,
|
|
22
22
|
"emitDecoratorMetadata": true,
|
|
23
23
|
"allowSyntheticDefaultImports": true,
|
package/component-generator.ts
DELETED
|
@@ -1,119 +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 { EnvsMain } from '@teambit/envs';
|
|
7
|
-
import camelcase from 'camelcase';
|
|
8
|
-
import { BitError } from '@teambit/bit-error';
|
|
9
|
-
import { PathOsBasedRelative } from '@teambit/legacy/dist/utils/path';
|
|
10
|
-
import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
|
|
11
|
-
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
|
|
12
|
-
import { composeComponentPath } from '@teambit/legacy/dist/utils/bit/compose-component-path';
|
|
13
|
-
import { ComponentID } from '@teambit/component-id';
|
|
14
|
-
import { ComponentTemplate, ComponentFile } from './component-template';
|
|
15
|
-
import { CreateOptions } from './create.cmd';
|
|
16
|
-
|
|
17
|
-
export type GenerateResult = { id: ComponentID; dir: string; files: string[]; envId: string };
|
|
18
|
-
|
|
19
|
-
export class ComponentGenerator {
|
|
20
|
-
constructor(
|
|
21
|
-
private workspace: Workspace,
|
|
22
|
-
private componentIds: ComponentID[],
|
|
23
|
-
private options: CreateOptions,
|
|
24
|
-
private template: ComponentTemplate,
|
|
25
|
-
private envs: EnvsMain
|
|
26
|
-
) {}
|
|
27
|
-
|
|
28
|
-
async generate(): Promise<GenerateResult[]> {
|
|
29
|
-
const dirsToDeleteIfFailed: string[] = [];
|
|
30
|
-
const generateResults = await pMapSeries(this.componentIds, async (componentId) => {
|
|
31
|
-
try {
|
|
32
|
-
const componentPath = this.getComponentPath(componentId);
|
|
33
|
-
if (fs.existsSync(path.join(this.workspace.path, componentPath))) {
|
|
34
|
-
throw new BitError(`unable to create a component at "${componentPath}", this path already exist`);
|
|
35
|
-
}
|
|
36
|
-
if (await this.workspace.hasName(componentId.fullName)) {
|
|
37
|
-
throw new BitError(
|
|
38
|
-
`unable to create a component "${componentId.fullName}", a component with the same name already exist`
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
dirsToDeleteIfFailed.push(componentPath);
|
|
42
|
-
return await this.generateOneComponent(componentId, componentPath);
|
|
43
|
-
} catch (err: any) {
|
|
44
|
-
await this.deleteGeneratedComponents(dirsToDeleteIfFailed);
|
|
45
|
-
throw err;
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
await this.workspace.writeBitMap();
|
|
50
|
-
|
|
51
|
-
return generateResults;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private async deleteGeneratedComponents(dirs: string[]) {
|
|
55
|
-
await Promise.all(
|
|
56
|
-
dirs.map(async (dir) => {
|
|
57
|
-
const absoluteDir = path.join(this.workspace.path, dir);
|
|
58
|
-
try {
|
|
59
|
-
await fs.remove(absoluteDir);
|
|
60
|
-
} catch (err: any) {
|
|
61
|
-
if (err.code !== 'ENOENT') {
|
|
62
|
-
// if not exist, it's fine
|
|
63
|
-
throw err;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private async generateOneComponent(componentId: ComponentID, componentPath: string): Promise<GenerateResult> {
|
|
71
|
-
const name = componentId.name;
|
|
72
|
-
const namePascalCase = camelcase(name, { pascalCase: true });
|
|
73
|
-
const nameCamelCase = camelcase(name);
|
|
74
|
-
const files = this.template.generateFiles({ name, namePascalCase, nameCamelCase, componentId });
|
|
75
|
-
const mainFile = files.find((file) => file.isMain);
|
|
76
|
-
await this.writeComponentFiles(componentPath, files);
|
|
77
|
-
const addResults = await this.workspace.track({
|
|
78
|
-
rootDir: componentPath,
|
|
79
|
-
mainFile: mainFile?.relativePath,
|
|
80
|
-
componentName: componentId.fullName,
|
|
81
|
-
});
|
|
82
|
-
const component = await this.workspace.get(componentId);
|
|
83
|
-
const env = this.envs.getEnv(component);
|
|
84
|
-
return {
|
|
85
|
-
id: componentId,
|
|
86
|
-
dir: componentPath,
|
|
87
|
-
files: addResults.files,
|
|
88
|
-
envId: env.id,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* writes the generated template files to the default directory set in the workspace config
|
|
94
|
-
*/
|
|
95
|
-
private async writeComponentFiles(
|
|
96
|
-
componentPath: string,
|
|
97
|
-
templateFiles: ComponentFile[]
|
|
98
|
-
): Promise<PathOsBasedRelative[]> {
|
|
99
|
-
const dataToPersist = new DataToPersist();
|
|
100
|
-
const vinylFiles = templateFiles.map((templateFile) => {
|
|
101
|
-
const templateFileVinyl = new Vinyl({
|
|
102
|
-
base: componentPath,
|
|
103
|
-
path: path.join(componentPath, templateFile.relativePath),
|
|
104
|
-
contents: Buffer.from(templateFile.content),
|
|
105
|
-
});
|
|
106
|
-
return AbstractVinyl.fromVinyl(templateFileVinyl);
|
|
107
|
-
});
|
|
108
|
-
const results = vinylFiles.map((v) => v.path);
|
|
109
|
-
dataToPersist.addManyFiles(vinylFiles);
|
|
110
|
-
dataToPersist.addBasePath(this.workspace.path);
|
|
111
|
-
await dataToPersist.persistAllToFS();
|
|
112
|
-
return results;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private getComponentPath(componentId: ComponentID) {
|
|
116
|
-
if (this.options.path) return this.options.path;
|
|
117
|
-
return composeComponentPath(componentId._legacy.changeScope(componentId.scope), this.workspace.defaultDirectory);
|
|
118
|
-
}
|
|
119
|
-
}
|
package/component-template.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { ComponentID } from '@teambit/component-id';
|
|
2
|
-
|
|
3
|
-
export interface ComponentFile {
|
|
4
|
-
/**
|
|
5
|
-
* relative path of the file within the component.
|
|
6
|
-
*/
|
|
7
|
-
relativePath: string;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* file content
|
|
11
|
-
*/
|
|
12
|
-
content: string;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* whether this file will be tracked as the main file
|
|
16
|
-
*/
|
|
17
|
-
isMain?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface ComponentContext {
|
|
21
|
-
/**
|
|
22
|
-
* component-name as entered by the user, e.g. `use-date`.
|
|
23
|
-
* without the scope and the namespace.
|
|
24
|
-
*/
|
|
25
|
-
name: string;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* component-name as upper camel case, e.g. `use-date` becomes `UseDate`.
|
|
29
|
-
* useful when generating the file content, for example for a class name.
|
|
30
|
-
*/
|
|
31
|
-
namePascalCase: string;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* component-name as lower camel case, e.g. `use-date` becomes `useDate`.
|
|
35
|
-
* useful when generating the file content, for example for a function/variable name.
|
|
36
|
-
*/
|
|
37
|
-
nameCamelCase: string;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* component id.
|
|
41
|
-
* the name is the name+namespace. the scope is the scope entered by --scope flag or the defaultScope
|
|
42
|
-
*/
|
|
43
|
-
componentId: ComponentID;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface ComponentTemplate {
|
|
47
|
-
/**
|
|
48
|
-
* name of the component template. for example: `hook`, `react-component` or `module`.
|
|
49
|
-
*/
|
|
50
|
-
name: string;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* short description of the template. shown in the `bit templates` command.
|
|
54
|
-
*/
|
|
55
|
-
description?: string;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* hide this template so that it is not listed with `bit templates`
|
|
59
|
-
*/
|
|
60
|
-
hidden?: boolean;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* template function for generating the file of a certain component.,
|
|
64
|
-
*/
|
|
65
|
-
generateFiles(context: ComponentContext): ComponentFile[];
|
|
66
|
-
}
|
package/create.cmd.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import { GeneratorMain } from './generator.main.runtime';
|
|
4
|
-
|
|
5
|
-
export type CreateOptions = {
|
|
6
|
-
namespace?: string;
|
|
7
|
-
aspect?: string;
|
|
8
|
-
scope?: string;
|
|
9
|
-
path?: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export class CreateCmd implements Command {
|
|
13
|
-
name = 'create <templateName> <componentNames...>';
|
|
14
|
-
description = 'create a new component from a template';
|
|
15
|
-
shortDescription = '';
|
|
16
|
-
alias = '';
|
|
17
|
-
loader = true;
|
|
18
|
-
group = 'development';
|
|
19
|
-
options = [
|
|
20
|
-
['n', 'namespace <string>', `sets the component's namespace and nested dirs inside the scope`],
|
|
21
|
-
['s', 'scope <string>', `sets the component's scope-name. if not entered, the default-scope will be used`],
|
|
22
|
-
['a', 'aspect <string>', 'aspect-id of the template. helpful when multiple aspects use the same template name'],
|
|
23
|
-
['p', 'path <string>', 'relative path in the workspace. by default the path is `<scope>/<namespace>/<name>`'],
|
|
24
|
-
] as CommandOptions;
|
|
25
|
-
|
|
26
|
-
constructor(private generator: GeneratorMain) {}
|
|
27
|
-
|
|
28
|
-
async report([templateName, componentNames]: [string, string[]], options: CreateOptions) {
|
|
29
|
-
const results = await this.generator.generateComponentTemplate(componentNames, templateName, options);
|
|
30
|
-
const title = `${results.length} component(s) were created`;
|
|
31
|
-
|
|
32
|
-
const componentsData = results
|
|
33
|
-
.map((result) => {
|
|
34
|
-
return `${chalk.bold(result.id.toString())}
|
|
35
|
-
location: ${result.dir}
|
|
36
|
-
env: ${result.envId}
|
|
37
|
-
`;
|
|
38
|
-
})
|
|
39
|
-
.join('\n');
|
|
40
|
-
const footer = `env configuration is according to workspace variants. learn more at https://harmony-docs.bit.dev/building-with-bit/environments/#configure-environment-for-components`;
|
|
41
|
-
|
|
42
|
-
return `${chalk.green(title)}\n\n${componentsData}\n\n${footer}`;
|
|
43
|
-
}
|
|
44
|
-
}
|
package/generator.aspect.ts
DELETED
package/generator.graphql.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Schema } from '@teambit/graphql';
|
|
2
|
-
import gql from 'graphql-tag';
|
|
3
|
-
import { GeneratorMain } from './generator.main.runtime';
|
|
4
|
-
|
|
5
|
-
export function generatorSchema(generator: GeneratorMain): Schema {
|
|
6
|
-
return {
|
|
7
|
-
typeDefs: gql`
|
|
8
|
-
type GenerateResult {
|
|
9
|
-
id: String
|
|
10
|
-
dir: String
|
|
11
|
-
files: [String]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type Mutation {
|
|
15
|
-
# create Component by template
|
|
16
|
-
createComponent(
|
|
17
|
-
name: String!
|
|
18
|
-
templateName: String!
|
|
19
|
-
scope: String
|
|
20
|
-
namespace: String
|
|
21
|
-
aspect: String
|
|
22
|
-
): [GenerateResult]
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type TemplateDescriptor {
|
|
26
|
-
aspectId: String!
|
|
27
|
-
name: String!
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type Generator {
|
|
31
|
-
templates: [TemplateDescriptor]
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type Query {
|
|
35
|
-
generator: Generator
|
|
36
|
-
}
|
|
37
|
-
`,
|
|
38
|
-
resolvers: {
|
|
39
|
-
Mutation: {
|
|
40
|
-
createComponent: async (
|
|
41
|
-
req: any,
|
|
42
|
-
{
|
|
43
|
-
name,
|
|
44
|
-
templateName,
|
|
45
|
-
...options
|
|
46
|
-
}: { name: string; templateName: string; scope?: string; namespace?: string; aspect?: string }
|
|
47
|
-
) => {
|
|
48
|
-
const res = await generator.generateComponentTemplate([name], templateName, options);
|
|
49
|
-
return res.map((component) => ({
|
|
50
|
-
id: component.id.toString(),
|
|
51
|
-
dir: component.dir,
|
|
52
|
-
files: component.files,
|
|
53
|
-
}));
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
Generator: {
|
|
57
|
-
templates: async () => {
|
|
58
|
-
return generator.listTemplates();
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
Query: {
|
|
62
|
-
generator: () => generator,
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
}
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
|
|
2
|
-
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
|
|
3
|
-
import WorkspaceAspect, { Workspace } from '@teambit/workspace';
|
|
4
|
-
import { EnvsAspect, EnvsMain } from '@teambit/envs';
|
|
5
|
-
import { ConsumerNotFound } from '@teambit/legacy/dist/consumer/exceptions';
|
|
6
|
-
import { Component } from '@teambit/component';
|
|
7
|
-
import { ComponentID } from '@teambit/component-id';
|
|
8
|
-
import { loadBit } from '@teambit/bit';
|
|
9
|
-
import { Slot, SlotRegistry } from '@teambit/harmony';
|
|
10
|
-
import { BitError } from '@teambit/bit-error';
|
|
11
|
-
import { InvalidScopeName, isValidScopeName } from '@teambit/legacy-bit-id';
|
|
12
|
-
import AspectLoaderAspect, { AspectLoaderMain } from '@teambit/aspect-loader';
|
|
13
|
-
import { ComponentTemplate } from './component-template';
|
|
14
|
-
import { GeneratorAspect } from './generator.aspect';
|
|
15
|
-
import { CreateCmd, CreateOptions } from './create.cmd';
|
|
16
|
-
import { TemplatesCmd } from './templates.cmd';
|
|
17
|
-
import { generatorSchema } from './generator.graphql';
|
|
18
|
-
import { ComponentGenerator, GenerateResult } from './component-generator';
|
|
19
|
-
import { WorkspaceGenerator } from './workspace-generator';
|
|
20
|
-
import { WorkspaceTemplate } from './workspace-template';
|
|
21
|
-
import { NewCmd, NewOptions } from './new.cmd';
|
|
22
|
-
import { componentGeneratorTemplate } from './templates/component-generator';
|
|
23
|
-
import { workspaceGeneratorTemplate } from './templates/workspace-generator';
|
|
24
|
-
|
|
25
|
-
export type ComponentTemplateSlot = SlotRegistry<ComponentTemplate[]>;
|
|
26
|
-
export type WorkspaceTemplateSlot = SlotRegistry<WorkspaceTemplate[]>;
|
|
27
|
-
|
|
28
|
-
export type TemplateDescriptor = { aspectId: string; name: string; description?: string; hidden?: boolean };
|
|
29
|
-
|
|
30
|
-
export type GeneratorConfig = {
|
|
31
|
-
/**
|
|
32
|
-
* array of aspects to include in the list of templates.
|
|
33
|
-
*/
|
|
34
|
-
aspects: string[];
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export class GeneratorMain {
|
|
38
|
-
private aspectLoaded = false;
|
|
39
|
-
constructor(
|
|
40
|
-
private componentTemplateSlot: ComponentTemplateSlot,
|
|
41
|
-
private workspaceTemplateSlot: WorkspaceTemplateSlot,
|
|
42
|
-
private config: GeneratorConfig,
|
|
43
|
-
private workspace: Workspace,
|
|
44
|
-
private envs: EnvsMain,
|
|
45
|
-
private aspectLoader: AspectLoaderMain
|
|
46
|
-
) {}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* register a new component template.
|
|
50
|
-
*/
|
|
51
|
-
registerComponentTemplate(templates: ComponentTemplate[]) {
|
|
52
|
-
this.componentTemplateSlot.register(templates);
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* register a new component template.
|
|
58
|
-
*/
|
|
59
|
-
registerWorkspaceTemplate(templates: WorkspaceTemplate[]) {
|
|
60
|
-
this.workspaceTemplateSlot.register(templates);
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* list all component templates registered in the workspace or workspace templates in case the
|
|
66
|
-
* workspace is not available
|
|
67
|
-
*/
|
|
68
|
-
async listTemplates(): Promise<TemplateDescriptor[]> {
|
|
69
|
-
const getTemplateDescriptor = ({
|
|
70
|
-
id,
|
|
71
|
-
template,
|
|
72
|
-
}: {
|
|
73
|
-
id: string;
|
|
74
|
-
template: WorkspaceTemplate | ComponentTemplate;
|
|
75
|
-
}) => ({
|
|
76
|
-
aspectId: id,
|
|
77
|
-
name: template.name,
|
|
78
|
-
description: template.description,
|
|
79
|
-
hidden: template.hidden,
|
|
80
|
-
});
|
|
81
|
-
return this.isRunningInsideWorkspace()
|
|
82
|
-
? this.getAllComponentTemplatesFlattened().map(getTemplateDescriptor)
|
|
83
|
-
: this.getAllWorkspaceTemplatesFlattened().map(getTemplateDescriptor);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @deprecated use this.listTemplates()
|
|
88
|
-
*/
|
|
89
|
-
async listComponentTemplates(): Promise<TemplateDescriptor[]> {
|
|
90
|
-
return this.listTemplates();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
isRunningInsideWorkspace(): boolean {
|
|
94
|
-
return Boolean(this.workspace);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* get all component templates registered by a specific aspect ID.
|
|
99
|
-
*/
|
|
100
|
-
getComponentTemplateByAspect(aspectId: string): ComponentTemplate[] {
|
|
101
|
-
return this.componentTemplateSlot.get(aspectId) || [];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* returns a specific component template.
|
|
106
|
-
*/
|
|
107
|
-
getComponentTemplate(name: string, aspectId?: string): ComponentTemplate | undefined {
|
|
108
|
-
const templates = this.getAllComponentTemplatesFlattened();
|
|
109
|
-
const found = templates.find(({ id, template }) => {
|
|
110
|
-
if (aspectId && id !== aspectId) return false;
|
|
111
|
-
return template.name === name;
|
|
112
|
-
});
|
|
113
|
-
return found?.template;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* in the case the aspect-id is given and this aspect doesn't exist locally, import it to the
|
|
118
|
-
* global scope and load it from the capsule
|
|
119
|
-
*/
|
|
120
|
-
async findTemplateInGlobalScope(aspectId: string, name?: string): Promise<WorkspaceTemplate | undefined> {
|
|
121
|
-
const aspects = await this.aspectLoader.loadAspectsFromGlobalScope([aspectId]);
|
|
122
|
-
const fullAspectId = aspects[0].id.toString();
|
|
123
|
-
return this.searchRegisteredWorkspaceTemplate(name, fullAspectId);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async findTemplateInOtherWorkspace(workspacePath: string, name: string, aspectId?: string) {
|
|
127
|
-
if (!aspectId)
|
|
128
|
-
throw new BitError(
|
|
129
|
-
`to load a template from a different workspace, please provide the aspect-id using --aspect flag`
|
|
130
|
-
);
|
|
131
|
-
const harmony = await loadBit(workspacePath);
|
|
132
|
-
let workspace: Workspace;
|
|
133
|
-
try {
|
|
134
|
-
workspace = harmony.get<Workspace>(WorkspaceAspect.id);
|
|
135
|
-
} catch (err: any) {
|
|
136
|
-
throw new Error(`fatal: "${workspacePath}" is not a valid Bit workspace, make sure the path is correct`);
|
|
137
|
-
}
|
|
138
|
-
const aspectComponentId = await workspace.resolveComponentId(aspectId);
|
|
139
|
-
await workspace.loadAspects([aspectId], true);
|
|
140
|
-
const aspectFullId = aspectComponentId.toString();
|
|
141
|
-
const generator = harmony.get<GeneratorMain>(GeneratorAspect.id);
|
|
142
|
-
return generator.searchRegisteredWorkspaceTemplate(name, aspectFullId);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* returns a specific workspace template.
|
|
147
|
-
*/
|
|
148
|
-
async getWorkspaceTemplate(
|
|
149
|
-
name: string,
|
|
150
|
-
aspectId?: string
|
|
151
|
-
): Promise<{ workspaceTemplate: WorkspaceTemplate; aspect?: Component }> {
|
|
152
|
-
const registeredTemplate = await this.searchRegisteredWorkspaceTemplate(name, aspectId);
|
|
153
|
-
if (registeredTemplate) {
|
|
154
|
-
return { workspaceTemplate: registeredTemplate };
|
|
155
|
-
}
|
|
156
|
-
if (!aspectId) {
|
|
157
|
-
throw new BitError(`template "${name}" was not found, if this is a custom-template, please use --aspect flag`);
|
|
158
|
-
}
|
|
159
|
-
const aspects = await this.aspectLoader.loadAspectsFromGlobalScope([aspectId]);
|
|
160
|
-
const aspect = aspects[0];
|
|
161
|
-
const fullAspectId = aspect.id.toString();
|
|
162
|
-
const fromGlobal = await this.searchRegisteredWorkspaceTemplate(name, fullAspectId);
|
|
163
|
-
if (fromGlobal) {
|
|
164
|
-
return { workspaceTemplate: fromGlobal, aspect };
|
|
165
|
-
}
|
|
166
|
-
throw new BitError(`template "${name}" was not found`);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async searchRegisteredWorkspaceTemplate(name?: string, aspectId?: string): Promise<WorkspaceTemplate | undefined> {
|
|
170
|
-
const templates = this.getAllWorkspaceTemplatesFlattened();
|
|
171
|
-
const found = templates.find(({ id, template }) => {
|
|
172
|
-
if (aspectId && name) return aspectId === id && name === template.name;
|
|
173
|
-
if (aspectId) return aspectId === id;
|
|
174
|
-
if (name) return name === template.name;
|
|
175
|
-
throw new Error(`searchRegisteredWorkspaceTemplate expects to get "name" or "aspectId"`);
|
|
176
|
-
});
|
|
177
|
-
return found?.template;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async generateComponentTemplate(
|
|
181
|
-
componentNames: string[],
|
|
182
|
-
templateName: string,
|
|
183
|
-
options: CreateOptions
|
|
184
|
-
): Promise<GenerateResult[]> {
|
|
185
|
-
if (!this.workspace) throw new ConsumerNotFound();
|
|
186
|
-
await this.loadAspects();
|
|
187
|
-
const { namespace, aspect: aspectId } = options;
|
|
188
|
-
const template = this.getComponentTemplate(templateName, aspectId);
|
|
189
|
-
if (!template) throw new BitError(`template "${templateName}" was not found`);
|
|
190
|
-
const scope = options.scope || this.workspace.defaultScope;
|
|
191
|
-
if (!isValidScopeName(scope)) {
|
|
192
|
-
throw new InvalidScopeName(scope);
|
|
193
|
-
}
|
|
194
|
-
if (!scope) throw new BitError(`failed finding defaultScope`);
|
|
195
|
-
|
|
196
|
-
const componentIds = componentNames.map((componentName) => {
|
|
197
|
-
const fullComponentName = namespace ? `${namespace}/${componentName}` : componentName;
|
|
198
|
-
return ComponentID.fromObject({ name: fullComponentName }, scope);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const componentGenerator = new ComponentGenerator(this.workspace, componentIds, options, template, this.envs);
|
|
202
|
-
return componentGenerator.generate();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
async generateWorkspaceTemplate(workspaceName: string, templateName: string, options: NewOptions) {
|
|
206
|
-
if (this.workspace) {
|
|
207
|
-
throw new BitError('Error: unable to generate a new workspace inside of an existing workspace');
|
|
208
|
-
}
|
|
209
|
-
const { aspect: aspectId, loadFrom } = options;
|
|
210
|
-
let template: WorkspaceTemplate | undefined;
|
|
211
|
-
let aspectComponent: Component | undefined;
|
|
212
|
-
if (loadFrom) {
|
|
213
|
-
template = await this.findTemplateInOtherWorkspace(loadFrom, templateName, aspectId);
|
|
214
|
-
} else {
|
|
215
|
-
const { workspaceTemplate, aspect } = await this.getWorkspaceTemplate(templateName, aspectId);
|
|
216
|
-
template = workspaceTemplate;
|
|
217
|
-
aspectComponent = aspect;
|
|
218
|
-
}
|
|
219
|
-
if (!template) throw new BitError(`template "${templateName}" was not found`);
|
|
220
|
-
const workspaceGenerator = new WorkspaceGenerator(workspaceName, options, template, aspectComponent);
|
|
221
|
-
const workspacePath = await workspaceGenerator.generate();
|
|
222
|
-
|
|
223
|
-
return workspacePath;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private getAllComponentTemplatesFlattened(): Array<{ id: string; template: ComponentTemplate }> {
|
|
227
|
-
const templatesByAspects = this.componentTemplateSlot.toArray();
|
|
228
|
-
return templatesByAspects.flatMap(([id, componentTemplates]) => {
|
|
229
|
-
return componentTemplates.map((template) => ({
|
|
230
|
-
id,
|
|
231
|
-
template,
|
|
232
|
-
}));
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
private getAllWorkspaceTemplatesFlattened(): Array<{ id: string; template: WorkspaceTemplate }> {
|
|
237
|
-
const templatesByAspects = this.workspaceTemplateSlot.toArray();
|
|
238
|
-
return templatesByAspects.flatMap(([id, workspaceTemplates]) => {
|
|
239
|
-
return workspaceTemplates.map((template) => ({
|
|
240
|
-
id,
|
|
241
|
-
template,
|
|
242
|
-
}));
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
private async loadAspects() {
|
|
247
|
-
if (this.aspectLoaded) return;
|
|
248
|
-
await this.workspace.loadAspects(this.config.aspects);
|
|
249
|
-
this.aspectLoaded = true;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
static slots = [Slot.withType<ComponentTemplate[]>(), Slot.withType<WorkspaceTemplate[]>()];
|
|
253
|
-
|
|
254
|
-
static dependencies = [WorkspaceAspect, CLIAspect, GraphqlAspect, EnvsAspect, AspectLoaderAspect];
|
|
255
|
-
|
|
256
|
-
static runtime = MainRuntime;
|
|
257
|
-
|
|
258
|
-
static async provider(
|
|
259
|
-
[workspace, cli, graphql, envs, aspectLoader]: [Workspace, CLIMain, GraphqlMain, EnvsMain, AspectLoaderMain],
|
|
260
|
-
config: GeneratorConfig,
|
|
261
|
-
[componentTemplateSlot, workspaceTemplateSlot]: [ComponentTemplateSlot, WorkspaceTemplateSlot]
|
|
262
|
-
) {
|
|
263
|
-
const generator = new GeneratorMain(
|
|
264
|
-
componentTemplateSlot,
|
|
265
|
-
workspaceTemplateSlot,
|
|
266
|
-
config,
|
|
267
|
-
workspace,
|
|
268
|
-
envs,
|
|
269
|
-
aspectLoader
|
|
270
|
-
);
|
|
271
|
-
const commands = [new CreateCmd(generator), new TemplatesCmd(generator), new NewCmd(generator)];
|
|
272
|
-
cli.register(...commands);
|
|
273
|
-
graphql.register(generatorSchema(generator));
|
|
274
|
-
generator.registerComponentTemplate([componentGeneratorTemplate, workspaceGeneratorTemplate]);
|
|
275
|
-
return generator;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
GeneratorAspect.addRuntime(GeneratorMain);
|