@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/new.cmd.ts DELETED
@@ -1,120 +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
- [
50
- 'c',
51
- 'current-dir',
52
- 'create the new workspace in current directory (default is to create a new directory, inside the current dir)',
53
- ],
54
- ] as CommandOptions;
55
-
56
- constructor(private generator: GeneratorMain) {}
57
-
58
- async report(
59
- [templateName, workspaceName]: [string, string],
60
- options: NewOptions & {
61
- standalone: boolean;
62
- env?: string;
63
- template?: string;
64
- aspect?: string;
65
- currentDir?: boolean;
66
- }
67
- ) {
68
- options.skipGit = options.skipGit ?? options.standalone;
69
- options.aspect = options.aspect ?? options.env ?? options.template;
70
- const { workspacePath, appName } = await this.generator.generateWorkspaceTemplate(
71
- workspaceName,
72
- templateName,
73
- options
74
- );
75
- return chalk.white(
76
- `${chalk.green(`
77
-
78
- Congrats! A new workspace has been created successfully at '${workspacePath}'`)}
79
-
80
- Inside the directory '${workspaceName}' you can run various commands including:
81
-
82
- ${chalk.yellow('bit start')}
83
- Starts the local dev server
84
-
85
- ${chalk.yellow('bit install')}
86
- Installs any missing dependencies
87
-
88
- ${chalk.yellow('bit status')}
89
- Shows the status of the components
90
-
91
- ${chalk.yellow('bit compile')}
92
- Compiles the components
93
-
94
- ${chalk.yellow('bit test')}
95
- Runs the tests on all your components
96
-
97
- ${chalk.yellow('bit templates')}
98
- Shows all available component templates
99
-
100
- ${chalk.yellow('bit help')}
101
- Shows all available commands
102
-
103
-
104
- ${chalk.green.bold("Let's get started!")}
105
-
106
- ${getBottomSection(workspaceName, appName)}
107
- `
108
- );
109
- }
110
- }
111
-
112
- function getBottomSection(workspaceName: string, appName: string | undefined) {
113
- const cdLine = chalk.yellow(`cd ${workspaceName}`);
114
- const parts = [cdLine];
115
- if (appName) {
116
- parts.push(chalk.yellow(` bit run ${appName}`));
117
- }
118
- parts.push(chalk.yellow(` bit start`));
119
- return parts.join('\n');
120
- }
package/starter-list.ts DELETED
@@ -1,21 +0,0 @@
1
- import { EnvContext, EnvHandler } from '@teambit/envs';
2
- import { WorkspaceTemplate } from './workspace-template';
3
-
4
- export type StarterListOptions = {
5
- name?: string;
6
- };
7
-
8
- export class StarterList {
9
- constructor(readonly name: string, private starters: EnvHandler<WorkspaceTemplate>[], private context: EnvContext) {}
10
-
11
- compute(): WorkspaceTemplate[] {
12
- return this.starters.map((starter) => starter(this.context));
13
- }
14
-
15
- static from(starters: EnvHandler<WorkspaceTemplate>[], options: StarterListOptions = {}) {
16
- return (context: EnvContext) => {
17
- const name = options.name || 'starter-list';
18
- return new StarterList(name, starters, context);
19
- };
20
- }
21
- }
package/starter.plugin.ts DELETED
@@ -1,16 +0,0 @@
1
- import { PluginDefinition } from '@teambit/aspect-loader';
2
- import { MainRuntime } from '@teambit/cli';
3
- import { GeneratorMain } from './generator.main.runtime';
4
-
5
- export class StarterPlugin implements PluginDefinition {
6
- constructor(private generator: GeneratorMain) {}
7
-
8
- pattern = '*.starter.*';
9
-
10
- runtimes = [MainRuntime.name];
11
-
12
- register(object: any) {
13
- const templates = Array.isArray(object) ? object : [object];
14
- return this.generator.registerWorkspaceTemplate(templates);
15
- }
16
- }
package/template-list.ts DELETED
@@ -1,21 +0,0 @@
1
- import { EnvContext, EnvHandler } from '@teambit/envs';
2
- import { ComponentTemplate } from './component-template';
3
-
4
- export type TemplateListOptions = {
5
- name?: string;
6
- };
7
-
8
- export class TemplateList {
9
- constructor(readonly name: string, private templates: EnvHandler<ComponentTemplate>[], private context: EnvContext) {}
10
-
11
- compute(): ComponentTemplate[] {
12
- return this.templates.map((template) => template(this.context));
13
- }
14
-
15
- static from(templates: EnvHandler<ComponentTemplate>[], options: TemplateListOptions = {}) {
16
- return (context: EnvContext) => {
17
- const name = options.name || 'template-list';
18
- return new TemplateList(name, templates, context);
19
- };
20
- }
21
- }
package/templates.cmd.ts DELETED
@@ -1,57 +0,0 @@
1
- import { Command, CommandOptions } from '@teambit/cli';
2
- import chalk from 'chalk';
3
- import { groupBy } from 'lodash';
4
- import { GeneratorMain, TemplateDescriptor } from './generator.main.runtime';
5
-
6
- export type TemplatesOptions = {
7
- showAll?: boolean;
8
- aspect?: string;
9
- };
10
-
11
- export class TemplatesCmd implements Command {
12
- name = 'templates';
13
- description = 'list available templates for "bit create" and "bit new"';
14
- extendedDescription =
15
- 'list components templates when inside bit-workspace (for bit-create), otherwise, list workspace templates (for bit-new)';
16
- alias = '';
17
- loader = true;
18
- group = 'development';
19
- options = [
20
- ['s', 'show-all', 'show hidden templates'],
21
- ['a', 'aspect <aspect-id>', 'show templates provided by the aspect-id'],
22
- ] as CommandOptions;
23
-
24
- constructor(private generator: GeneratorMain) {}
25
-
26
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
27
- async report(args: [], templatesOptions: TemplatesOptions) {
28
- let results = await this.generator.listTemplates(templatesOptions);
29
-
30
- // Make sure that we don't list hidden templates
31
- if (!templatesOptions.showAll) {
32
- results = results.filter((template) => !template.hidden);
33
- }
34
-
35
- const grouped = groupBy(results, 'aspectId');
36
- const titleStr = this.generator.isRunningInsideWorkspace()
37
- ? `The following template(s) are available with the command bit create: \nExample - bit create <template-name> <component-name>`
38
- : `The following template(s) are available with the command bit new: \nExample - bit new <template-name> <workspace-name>`;
39
- const title = chalk.green(`\n${titleStr}\n`);
40
- const templateOutput = (template: TemplateDescriptor) => {
41
- const desc = template.description ? ` (${template.description})` : '';
42
- return ` ${template.name}${chalk.dim(desc)}`;
43
- };
44
- const output = Object.keys(grouped)
45
- .map((aspectId) => {
46
- const names = grouped[aspectId].map(templateOutput).join('\n');
47
- const groupTitle = grouped[aspectId][0].titlePrefix
48
- ? `${grouped[aspectId][0].titlePrefix} (${aspectId})`
49
- : aspectId;
50
- return `${chalk.blue.bold(groupTitle)}\n${names}\n`;
51
- })
52
- .join('\n');
53
-
54
- const learnMore = `\nfind and add templates in https://bit.dev/reference/generator/use-component-generator`;
55
- return title + output + learnMore;
56
- }
57
- }
package/types.ts DELETED
@@ -1,3 +0,0 @@
1
- export interface CreateExtConfig {
2
- template: string;
3
- }
@@ -1,220 +0,0 @@
1
- import fs from 'fs-extra';
2
- import { loadBit } from '@teambit/bit';
3
- import { Harmony } from '@teambit/harmony';
4
- import { Component } from '@teambit/component';
5
- import execa from 'execa';
6
- import pMapSeries from 'p-map-series';
7
- import UIAspect, { UiMain } from '@teambit/ui';
8
- import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
9
- import { WorkspaceAspect, Workspace } from '@teambit/workspace';
10
- import ForkingAspect, { ForkingMain } from '@teambit/forking';
11
- import { init } from '@teambit/legacy/dist/api/consumer';
12
- import ImporterAspect, { ImporterMain } from '@teambit/importer';
13
- import { CompilerAspect, CompilerMain } from '@teambit/compiler';
14
- import getGitExecutablePath from '@teambit/legacy/dist/utils/git/git-executable';
15
- import GitNotFound from '@teambit/legacy/dist/utils/git/exceptions/git-not-found';
16
- import { resolve, join } from 'path';
17
- import { ComponentID } from '@teambit/component-id';
18
- import GitAspect, { GitMain } from '@teambit/git';
19
- import { InstallAspect, InstallMain } from '@teambit/install';
20
- import WorkspaceConfigFilesAspect, { WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';
21
- // import { ComponentGenerator } from './component-generator';
22
- import { WorkspaceTemplate, WorkspaceContext } from './workspace-template';
23
- import { NewOptions } from './new.cmd';
24
- import { GeneratorAspect } from './generator.aspect';
25
- import { GeneratorMain } from './generator.main.runtime';
26
-
27
- export type GenerateResult = { id: ComponentID; dir: string; files: string[]; envId: string };
28
-
29
- export class WorkspaceGenerator {
30
- private workspacePath: string;
31
- private harmony: Harmony;
32
- private workspace: Workspace;
33
- private install: InstallMain;
34
- private importer: ImporterMain;
35
- private logger?: Logger;
36
- private forking: ForkingMain;
37
- private git: GitMain;
38
- private wsConfigFiles: WorkspaceConfigFilesMain;
39
- private generator: GeneratorMain;
40
-
41
- // private componentGenerator?: ComponentGenerator;
42
-
43
- constructor(
44
- private workspaceName: string,
45
- private options: NewOptions & { currentDir?: boolean },
46
- private template: WorkspaceTemplate,
47
- private aspectComponent?: Component
48
- ) {
49
- this.workspacePath = options.currentDir ? process.cwd() : resolve(this.workspaceName);
50
- }
51
-
52
- async generate(): Promise<string> {
53
- if (!this.options.currentDir && fs.existsSync(this.workspacePath)) {
54
- throw new Error(`unable to create a workspace at "${this.workspaceName}", this path already exist`);
55
- }
56
- await fs.ensureDir(this.workspacePath);
57
- try {
58
- process.chdir(this.workspacePath);
59
- await this.initGit();
60
- await init(this.workspacePath, this.options.skipGit, false, false, false, false, false, false, {});
61
- await this.writeWorkspaceFiles();
62
- await this.reloadBitInWorkspaceDir();
63
- // Setting the workspace to be in install context to prevent errors during the workspace generation
64
- // the workspace will be in install context until the end of the generation install process
65
- this.workspace.inInstallContext = true;
66
- await this.setupGitBitmapMergeDriver();
67
- await this.createComponentsFromRemote();
68
- await this.forkComponentsFromRemote();
69
- await this.importComponentsFromRemote();
70
- await this.workspace.clearCache();
71
- await this.install.install(undefined, {
72
- dedupe: true,
73
- import: false,
74
- copyPeerToRuntimeOnRoot: true,
75
- copyPeerToRuntimeOnComponents: false,
76
- updateExisting: false,
77
- });
78
-
79
- // compile the components again now that we have the dependencies installed
80
- await this.compileComponents(true);
81
- await this.wsConfigFiles.writeConfigFiles({});
82
- } catch (err: any) {
83
- this.logger?.error(`failed generating a new workspace, will delete the dir ${this.workspacePath}`, err);
84
- await fs.remove(this.workspacePath);
85
- throw err;
86
- }
87
-
88
- return this.workspacePath;
89
- }
90
-
91
- private async initGit() {
92
- if (this.options.skipGit) return;
93
- const gitExecutablePath = getGitExecutablePath();
94
- const params = ['init'];
95
- try {
96
- await execa(gitExecutablePath, params);
97
- } catch (err: any) {
98
- if (err.exitCodeName === 'ENOENT') {
99
- throw new GitNotFound(gitExecutablePath, err);
100
- }
101
- throw err;
102
- }
103
- }
104
-
105
- private async setupGitBitmapMergeDriver() {
106
- if (this.options.skipGit) return;
107
- await this.git.setGitMergeDriver({ global: false });
108
- }
109
-
110
- private async buildUI() {
111
- const uiMain = this.harmony.get<UiMain>(UIAspect.id);
112
- await uiMain.createRuntime({});
113
- }
114
-
115
- private getWorkspaceContext(): WorkspaceContext {
116
- return {
117
- name: this.workspaceName,
118
- defaultScope: this.options.defaultScope,
119
- empty: this.options.empty,
120
- aspectComponent: this.aspectComponent,
121
- template: this.template,
122
- skipGit: this.options.skipGit,
123
- };
124
- }
125
-
126
- /**
127
- * writes the generated template files to the default directory set in the workspace config
128
- */
129
- private async writeWorkspaceFiles(): Promise<void> {
130
- const workspaceContext = this.getWorkspaceContext();
131
- const templateFiles = await this.template.generateFiles(workspaceContext);
132
- await Promise.all(
133
- templateFiles.map(async (templateFile) => {
134
- await fs.outputFile(join(this.workspacePath, templateFile.relativePath), templateFile.content);
135
- })
136
- );
137
- }
138
-
139
- private async reloadBitInWorkspaceDir() {
140
- this.harmony = await loadBit(this.workspacePath);
141
- this.workspace = this.harmony.get<Workspace>(WorkspaceAspect.id);
142
- this.install = this.harmony.get<InstallMain>(InstallAspect.id);
143
- const loggerMain = this.harmony.get<LoggerMain>(LoggerAspect.id);
144
- this.logger = loggerMain.createLogger(GeneratorAspect.id);
145
- this.importer = this.harmony.get<ImporterMain>(ImporterAspect.id);
146
- this.forking = this.harmony.get<ForkingMain>(ForkingAspect.id);
147
- this.git = this.harmony.get<GitMain>(GitAspect.id);
148
- this.wsConfigFiles = this.harmony.get<WorkspaceConfigFilesMain>(WorkspaceConfigFilesAspect.id);
149
- this.generator = this.harmony.get<GeneratorMain>(GeneratorAspect.id);
150
- }
151
-
152
- private async createComponentsFromRemote() {
153
- if (this.options.empty || !this.template.create) return;
154
- const workspaceContext = this.getWorkspaceContext();
155
- const componentsToCreate = this.template.create(workspaceContext);
156
- await pMapSeries(componentsToCreate, async (componentToCreate) => {
157
- return this.generator.generateComponentTemplate(
158
- [componentToCreate.componentName],
159
- componentToCreate.templateName,
160
- {
161
- aspect: componentToCreate.aspect,
162
- env: componentToCreate.env,
163
- path: componentToCreate.path,
164
- scope: componentToCreate.scope,
165
- }
166
- );
167
- });
168
- }
169
-
170
- private async forkComponentsFromRemote() {
171
- if (this.options.empty) return;
172
- const workspaceContext = this.getWorkspaceContext();
173
- const componentsToFork =
174
- this.template?.importComponents?.(workspaceContext) || this.template?.fork?.(workspaceContext) || [];
175
- if (!componentsToFork.length) return;
176
- const componentsToForkRestructured = componentsToFork.map(({ id, targetName, path, env, config, targetScope }) => ({
177
- sourceId: id,
178
- targetId: targetName,
179
- targetScope,
180
- path,
181
- env,
182
- config,
183
- }));
184
- await this.forking.forkMultipleFromRemote(componentsToForkRestructured, {
185
- scope: this.workspace.defaultScope,
186
- refactor: true,
187
- install: false,
188
- });
189
- }
190
-
191
- private async importComponentsFromRemote() {
192
- if (this.options.empty) return;
193
- const workspaceContext = this.getWorkspaceContext();
194
- const componentsToImport = this.template?.import?.(workspaceContext) || [];
195
-
196
- if (!componentsToImport.length) return;
197
-
198
- await pMapSeries(componentsToImport, async (componentToImport) => {
199
- await this.importer.import(
200
- {
201
- ids: [componentToImport.id],
202
- installNpmPackages: false,
203
- writeConfigFiles: false,
204
- writeToPath: componentToImport.path,
205
- },
206
- []
207
- );
208
- });
209
-
210
- await this.workspace.bitMap.write('new');
211
- }
212
-
213
- private async compileComponents(clearCache = true) {
214
- if (clearCache) {
215
- await this.workspace.clearCache();
216
- }
217
- const compiler = this.harmony.get<CompilerMain>(CompilerAspect.id);
218
- await compiler.compileOnWorkspace();
219
- }
220
- }
@@ -1,183 +0,0 @@
1
- import type { Component } from '@teambit/component';
2
- import { ComponentConfig } from './component-template';
3
-
4
- /**
5
- * BaseWorkspaceOptions describes the foundational properties for workspaces.
6
- */
7
- export interface BaseWorkspaceOptions {
8
- /**
9
- * The name of the workspace as provided by the user (e.g., `react-app`).
10
- * This is also used as the directory name for the workspace.
11
- */
12
- name: string;
13
-
14
- /**
15
- * The default scope provided by the user.
16
- * This is set in the workspace.jsonc and is utilized for components within the workspace.
17
- */
18
- defaultScope?: string;
19
-
20
- /**
21
- * Indicates whether the user has opted to avoid creating components (typically with a `--empty` flag).
22
- */
23
- empty?: boolean;
24
-
25
- /**
26
- * Represents the aspect in the context where a remote aspect is imported (often via the `--aspect` flag).
27
- * This is useful for obtaining the aspect-id and other related information.
28
- */
29
- aspectComponent?: Component;
30
-
31
- /**
32
- * Represents the selected template to initialize or create the workspace.
33
- */
34
- template: WorkspaceTemplate;
35
-
36
- /**
37
- * Flag to check if Git repository generation should be skipped.
38
- */
39
- skipGit?: boolean;
40
-
41
- /**
42
- * Local path to the workspace template.
43
- * Useful during the development of a workspace-template.
44
- */
45
- loadFrom?: string;
46
- }
47
-
48
- /**
49
- * WorkspaceContext represents foundational properties for a workspace context.
50
- */
51
- export type WorkspaceContext = BaseWorkspaceOptions;
52
-
53
- export interface WorkspaceFile {
54
- /**
55
- * relative path of the file within the workspace.
56
- */
57
- relativePath: string;
58
-
59
- /**
60
- * file content
61
- */
62
- content: string;
63
- }
64
-
65
- export interface CreateComponentInfo {
66
- /**
67
- * the template for generating the component
68
- */
69
- templateName: string;
70
- /**
71
- * component name to generate
72
- */
73
- componentName: string;
74
- /**
75
- * sets the component's scope-name. if not entered, the default-scope will be used
76
- */
77
- scope?: string;
78
- /**
79
- * relative path in the workspace. by default the path is `<scope>/<namespace>/<name>`
80
- */
81
- path?: string;
82
- /**
83
- * set the component's environment. (overrides the env from variants and the template)
84
- */
85
- env?: string;
86
- /**
87
- * aspect-id of the template.
88
- */
89
- aspect?: string;
90
- }
91
-
92
- export interface ForkComponentInfo extends ImportComponentInfo {
93
- /**
94
- * a new component name. if not specified, use the original id (without the scope)
95
- */
96
- targetName?: string;
97
-
98
- /**
99
- * a new scope for the component. if not specified, use the original scope
100
- */
101
- targetScope?: string;
102
-
103
- /**
104
- * env to use for the component.
105
- */
106
- env?: string;
107
-
108
- /**
109
- * component config. gets saved in the .bitmap file and overrides the workspace.jsonc config.
110
- * for example, you can set the env that will be used for this component as follows:
111
- * "teambit.envs/envs": {
112
- * "env": "teambit.harmony/aspect"
113
- * },
114
- */
115
- config?: ComponentConfig;
116
- }
117
-
118
- /**
119
- * @deprecated use ForkComponentInfo instead.
120
- */
121
- export type ComponentToImport = ForkComponentInfo;
122
-
123
- export interface ImportComponentInfo {
124
- /**
125
- * full component id
126
- */
127
- id: string;
128
-
129
- /**
130
- * path where to write the component
131
- */
132
- path?: string;
133
- }
134
-
135
- export interface WorkspaceTemplate {
136
- /**
137
- * name of the workspace starter. for example: `react-workspace`.
138
- */
139
- name: string;
140
-
141
- /**
142
- * name of an app created in the workspace. for example: `my-app`.
143
- * This will be used to instruct the user to run `bit run <appName>` in the new workspace.
144
- */
145
- appName?: string;
146
-
147
- /**
148
- * short description of the starter. shown in the `bit starter` command when outside of bit-workspace.
149
- */
150
- description?: string;
151
-
152
- /**
153
- * hide this starter so that it is not listed with `bit starter`
154
- */
155
- hidden?: boolean;
156
-
157
- /**
158
- * starter function for generating the template files,
159
- */
160
- generateFiles(context: WorkspaceContext): Promise<WorkspaceFile[]>;
161
-
162
- /**
163
- * @deprecated use `fork()` or `import()` instead
164
- * this is working similarly to `fork()`
165
- */
166
- importComponents?: (context: WorkspaceContext) => ForkComponentInfo[];
167
-
168
- /**
169
- * import components into the new workspace, don't change their source code.
170
- */
171
- import?: (context: WorkspaceContext) => ImportComponentInfo[];
172
-
173
- /**
174
- * populate existing components into the new workspace and add them as new components.
175
- * change their source code and update the dependency names according to the new component names.
176
- */
177
- fork?: (context: WorkspaceContext) => ForkComponentInfo[];
178
-
179
- /**
180
- * populate new components into the new workspace and add them as new components.
181
- */
182
- create?: (context: WorkspaceContext) => CreateComponentInfo[];
183
- }