@teambit/generator 0.0.554 → 0.0.558
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/component-generator.d.ts +3 -0
- package/dist/component-template.d.ts +37 -0
- package/dist/generator.main.runtime.d.ts +29 -0
- package/dist/workspace-generator.d.ts +3 -0
- package/dist/workspace-template.d.ts +45 -0
- package/package.json +28 -20
- package/tsconfig.json +1 -2
- 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.docs.mdx +0 -8
- 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.554.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
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
export function gitIgnoreTemplate() {
|
|
2
|
-
return `export function gitIgnore() {
|
|
3
|
-
return \`# Bit
|
|
4
|
-
.bit
|
|
5
|
-
public
|
|
6
|
-
|
|
7
|
-
# Logs
|
|
8
|
-
logs
|
|
9
|
-
*.log
|
|
10
|
-
npm-debug.log*
|
|
11
|
-
yarn-debug.log*
|
|
12
|
-
yarn-error.log*
|
|
13
|
-
lerna-debug.log*
|
|
14
|
-
|
|
15
|
-
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
16
|
-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
17
|
-
|
|
18
|
-
# Runtime data
|
|
19
|
-
pids
|
|
20
|
-
*.pid
|
|
21
|
-
*.seed
|
|
22
|
-
*.pid.lock
|
|
23
|
-
|
|
24
|
-
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
25
|
-
lib-cov
|
|
26
|
-
|
|
27
|
-
# Coverage directory used by tools like istanbul
|
|
28
|
-
coverage
|
|
29
|
-
*.lcov
|
|
30
|
-
|
|
31
|
-
# nyc test coverage
|
|
32
|
-
.nyc_output
|
|
33
|
-
|
|
34
|
-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
35
|
-
.grunt
|
|
36
|
-
|
|
37
|
-
# Bower dependency directory (https://bower.io/)
|
|
38
|
-
bower_components
|
|
39
|
-
|
|
40
|
-
# node-waf configuration
|
|
41
|
-
.lock-wscript
|
|
42
|
-
|
|
43
|
-
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
44
|
-
build/Release
|
|
45
|
-
|
|
46
|
-
# Dependency directories
|
|
47
|
-
node_modules/
|
|
48
|
-
jspm_packages/
|
|
49
|
-
|
|
50
|
-
# TypeScript v1 declaration files
|
|
51
|
-
typings/
|
|
52
|
-
|
|
53
|
-
# TypeScript cache
|
|
54
|
-
*.tsbuildinfo
|
|
55
|
-
|
|
56
|
-
# Optional npm cache directory
|
|
57
|
-
.npm
|
|
58
|
-
|
|
59
|
-
# Optional eslint cache
|
|
60
|
-
.eslintcache
|
|
61
|
-
|
|
62
|
-
# Microbundle cache
|
|
63
|
-
.rpt2_cache/
|
|
64
|
-
.rts2_cache_cjs/
|
|
65
|
-
.rts2_cache_es/
|
|
66
|
-
.rts2_cache_umd/
|
|
67
|
-
|
|
68
|
-
# Optional REPL history
|
|
69
|
-
.node_repl_history
|
|
70
|
-
|
|
71
|
-
# Output of 'npm pack'
|
|
72
|
-
*.tgz
|
|
73
|
-
|
|
74
|
-
# Yarn Integrity file
|
|
75
|
-
.yarn-integrity
|
|
76
|
-
|
|
77
|
-
# dotenv environment variables file
|
|
78
|
-
.env
|
|
79
|
-
.env.test
|
|
80
|
-
|
|
81
|
-
# parcel-bundler cache (https://parceljs.org/)
|
|
82
|
-
.cache
|
|
83
|
-
|
|
84
|
-
# Next.js build output
|
|
85
|
-
.next
|
|
86
|
-
|
|
87
|
-
# Nuxt.js build / generate output
|
|
88
|
-
.nuxt
|
|
89
|
-
dist
|
|
90
|
-
|
|
91
|
-
# Gatsby files
|
|
92
|
-
.cache/
|
|
93
|
-
|
|
94
|
-
# vuepress build output
|
|
95
|
-
.vuepress/dist
|
|
96
|
-
|
|
97
|
-
# Serverless directories
|
|
98
|
-
.serverless/
|
|
99
|
-
|
|
100
|
-
# FuseBox cache
|
|
101
|
-
.fusebox/
|
|
102
|
-
|
|
103
|
-
# DynamoDB Local files
|
|
104
|
-
.dynamodb/
|
|
105
|
-
|
|
106
|
-
# TernJS port file
|
|
107
|
-
.tern-port
|
|
108
|
-
|
|
109
|
-
.history
|
|
110
|
-
\`;
|
|
111
|
-
}
|
|
112
|
-
`;
|
|
113
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export function indexTemplate() {
|
|
2
|
-
return `import { WorkspaceContext, WorkspaceTemplate } from '@teambit/generator';
|
|
3
|
-
import { workspaceConfig } from './files/workspace-config';
|
|
4
|
-
import { readme } from './files/readme-file';
|
|
5
|
-
import { gitIgnore } from './files/git-ignore';
|
|
6
|
-
|
|
7
|
-
export const workspaceTemplate: WorkspaceTemplate = {
|
|
8
|
-
name: 'template-example',
|
|
9
|
-
description: 'demonstration of a workspace template',
|
|
10
|
-
generateFiles: async (context: WorkspaceContext) => [
|
|
11
|
-
{
|
|
12
|
-
relativePath: 'workspace.jsonc',
|
|
13
|
-
content: await workspaceConfig(context),
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
relativePath: '.gitignore',
|
|
17
|
-
content: gitIgnore(),
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
relativePath: 'README.md',
|
|
21
|
-
content: readme(),
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
importComponents: () => [
|
|
25
|
-
{ id: 'teambit.react/templates/ui/text', path: 'ui/text' },
|
|
26
|
-
],
|
|
27
|
-
};
|
|
28
|
-
`;
|
|
29
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ComponentContext } from '@teambit/generator';
|
|
2
|
-
|
|
3
|
-
export function indexFile({ namePascalCase, name }: ComponentContext) {
|
|
4
|
-
return `import { ${namePascalCase}Aspect } from './${name}.aspect';
|
|
5
|
-
|
|
6
|
-
export type { ${namePascalCase}Main } from './${name}.main.runtime';
|
|
7
|
-
export default ${namePascalCase}Aspect;
|
|
8
|
-
export { ${namePascalCase}Aspect };
|
|
9
|
-
`;
|
|
10
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { ComponentContext } from '@teambit/generator';
|
|
2
|
-
|
|
3
|
-
export function mainRuntime({ name, namePascalCase }: ComponentContext) {
|
|
4
|
-
return `import { MainRuntime } from '@teambit/cli';
|
|
5
|
-
import { GeneratorMain, GeneratorAspect } from '@teambit/generator';
|
|
6
|
-
import { ${namePascalCase}Aspect } from './${name}.aspect';
|
|
7
|
-
import { workspaceTemplate } from './template';
|
|
8
|
-
|
|
9
|
-
export class ${namePascalCase}Main {
|
|
10
|
-
static slots = [];
|
|
11
|
-
|
|
12
|
-
static dependencies = [GeneratorAspect];
|
|
13
|
-
|
|
14
|
-
static runtime = MainRuntime;
|
|
15
|
-
|
|
16
|
-
static async provider([generator]: [GeneratorMain]) {
|
|
17
|
-
generator.registerWorkspaceTemplate([workspaceTemplate]);
|
|
18
|
-
return new ${namePascalCase}Main();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
${namePascalCase}Aspect.addRuntime(${namePascalCase}Main);
|
|
23
|
-
`;
|
|
24
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export function readmeTemplate() {
|
|
2
|
-
return `export function readme() {
|
|
3
|
-
return \`## Workspace Generator
|
|
4
|
-
|
|
5
|
-
Easily generate a new workspace with a single command.
|
|
6
|
-
|
|
7
|
-
## Registering your Workspace
|
|
8
|
-
|
|
9
|
-
Register your workspace template under the aspect environment in the variants section of the workspace.jsonc file.
|
|
10
|
-
|
|
11
|
-
\\\`\\\`\\\`json
|
|
12
|
-
"teambit.workspace/variants": {
|
|
13
|
-
"{workspace-name}": {
|
|
14
|
-
"teambit.harmony/aspect": {}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
\\\`\\\`\\\`
|
|
18
|
-
|
|
19
|
-
## Customizing your Workspace
|
|
20
|
-
|
|
21
|
-
See the docs for more info on [Customizing your Generator](https://harmony-docs.bit.dev/extending-bit/creating-a-custom-workspace-generator)
|
|
22
|
-
|
|
23
|
-
## Using the Workspace Generator
|
|
24
|
-
|
|
25
|
-
How to use this generator locally, essentially for development purposes:
|
|
26
|
-
|
|
27
|
-
\\\`\\\`\\\`js
|
|
28
|
-
bit new <template-name> <workspace-name> --load-from /Users/me/path/to/this/dir --aspect <workspace-template-id>
|
|
29
|
-
\\\`\\\`\\\`
|
|
30
|
-
|
|
31
|
-
How to use this generator after exporting to a remote scope:
|
|
32
|
-
|
|
33
|
-
\\\`\\\`\\\`js
|
|
34
|
-
bit new <template-name> <workspace-name> --aspect <workspace-template-id>
|
|
35
|
-
\\\`\\\`\\\`
|
|
36
|
-
\`;
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export function workspaceConfigTemplate() {
|
|
2
|
-
return `import { WorkspaceContext } from '@teambit/generator';
|
|
3
|
-
import { getWorkspaceConfigTemplateParsed, stringifyWorkspaceConfig } from '@teambit/config';
|
|
4
|
-
|
|
5
|
-
export async function workspaceConfig({ name, defaultScope }: WorkspaceContext) {
|
|
6
|
-
const configParsed = await getWorkspaceConfigTemplateParsed();
|
|
7
|
-
configParsed['teambit.workspace/workspace'].name = name;
|
|
8
|
-
configParsed['teambit.workspace/workspace'].defaultScope = defaultScope || 'company.scope';
|
|
9
|
-
configParsed['teambit.workspace/variants'] = {
|
|
10
|
-
'*': {
|
|
11
|
-
'teambit.react/react': {},
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
return stringifyWorkspaceConfig(configParsed);
|
|
16
|
-
}
|
|
17
|
-
`;
|
|
18
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { ComponentContext, ComponentTemplate } from '@teambit/generator';
|
|
2
|
-
import { indexFile } from './files/index';
|
|
3
|
-
import { aspectFile } from './files/aspect-file';
|
|
4
|
-
import { docsFile } from './files/docs-file';
|
|
5
|
-
import { mainRuntime } from './files/main-runtime';
|
|
6
|
-
import { gitIgnoreTemplate } from './files/git-ignore-tpl';
|
|
7
|
-
import { readmeTemplate } from './files/readme-tpl';
|
|
8
|
-
import { indexTemplate } from './files/index-tpl';
|
|
9
|
-
import { workspaceConfigTemplate } from './files/workspace-config-tpl';
|
|
10
|
-
|
|
11
|
-
export const workspaceGeneratorTemplate: ComponentTemplate = {
|
|
12
|
-
name: 'workspace-generator',
|
|
13
|
-
description:
|
|
14
|
-
'create your own workspace generator - \nDocs: https://harmony-docs.bit.dev/extending-bit/creating-a-custom-workspace-generator',
|
|
15
|
-
generateFiles: (context: ComponentContext) => {
|
|
16
|
-
return [
|
|
17
|
-
{
|
|
18
|
-
relativePath: 'index.ts',
|
|
19
|
-
content: indexFile(context),
|
|
20
|
-
isMain: true,
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
relativePath: `${context.name}.aspect.ts`,
|
|
24
|
-
content: aspectFile(context),
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
relativePath: `${context.name}.main.runtime.ts`,
|
|
28
|
-
content: mainRuntime(context),
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
relativePath: `${context.name}.docs.mdx`,
|
|
32
|
-
content: docsFile(),
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
relativePath: 'template/files/git-ignore.ts',
|
|
36
|
-
content: gitIgnoreTemplate(),
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
relativePath: 'template/files/readme-file.ts',
|
|
40
|
-
content: readmeTemplate(),
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
relativePath: 'template/files/workspace-config.ts',
|
|
44
|
-
content: workspaceConfigTemplate(),
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
relativePath: 'template/index.ts',
|
|
48
|
-
content: indexTemplate(),
|
|
49
|
-
},
|
|
50
|
-
];
|
|
51
|
-
},
|
|
52
|
-
};
|
package/templates.cmd.ts
DELETED
|
@@ -1,48 +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
|
-
};
|
|
9
|
-
|
|
10
|
-
export class TemplatesCmd implements Command {
|
|
11
|
-
name = 'templates';
|
|
12
|
-
description =
|
|
13
|
-
'list components templates when inside bit-workspace (for bit-create), otherwise, list workspace templates (for bit-new)';
|
|
14
|
-
shortDescription = 'list templates for "bit create" and "bit new"';
|
|
15
|
-
alias = '';
|
|
16
|
-
loader = true;
|
|
17
|
-
group = 'development';
|
|
18
|
-
options = [['s', 'show-all', 'show hidden templates']] as CommandOptions;
|
|
19
|
-
|
|
20
|
-
constructor(private generator: GeneratorMain) {}
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
-
async report(args: [], templatesOptions: TemplatesOptions) {
|
|
24
|
-
let results = await this.generator.listTemplates();
|
|
25
|
-
|
|
26
|
-
// Make sure that we don't list hidden templates
|
|
27
|
-
if (!templatesOptions.showAll) {
|
|
28
|
-
results = results.filter((template) => !template.hidden);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const grouped = groupBy(results, 'aspectId');
|
|
32
|
-
const titleStr = this.generator.isRunningInsideWorkspace()
|
|
33
|
-
? `The following template(s) are available with the command bit create: \nExample - bit create <template-name> <component-name>`
|
|
34
|
-
: `The following template(s) are available with the command bit new: \nExample - bit new <template-name> <workspace-name>`;
|
|
35
|
-
const title = chalk.green(`\n${titleStr}\n`);
|
|
36
|
-
const templateOutput = (template: TemplateDescriptor) => {
|
|
37
|
-
const desc = template.description ? ` (${template.description})` : '';
|
|
38
|
-
return ` ${template.name}${chalk.dim(desc)}`;
|
|
39
|
-
};
|
|
40
|
-
const output = Object.keys(grouped)
|
|
41
|
-
.map((aspectId) => {
|
|
42
|
-
const names = grouped[aspectId].map(templateOutput).join('\n');
|
|
43
|
-
return `${chalk.blue.bold(aspectId)}\n${names}\n`;
|
|
44
|
-
})
|
|
45
|
-
.join('\n');
|
|
46
|
-
return title + output;
|
|
47
|
-
}
|
|
48
|
-
}
|
package/types/asset.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
declare module '*.png' {
|
|
2
|
-
const value: any;
|
|
3
|
-
export = value;
|
|
4
|
-
}
|
|
5
|
-
declare module '*.svg' {
|
|
6
|
-
import type { FunctionComponent, SVGProps } from 'react';
|
|
7
|
-
|
|
8
|
-
export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
|
|
9
|
-
const src: string;
|
|
10
|
-
export default src;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// @TODO Gilad
|
|
14
|
-
declare module '*.jpg' {
|
|
15
|
-
const value: any;
|
|
16
|
-
export = value;
|
|
17
|
-
}
|
|
18
|
-
declare module '*.jpeg' {
|
|
19
|
-
const value: any;
|
|
20
|
-
export = value;
|
|
21
|
-
}
|
|
22
|
-
declare module '*.gif' {
|
|
23
|
-
const value: any;
|
|
24
|
-
export = value;
|
|
25
|
-
}
|
|
26
|
-
declare module '*.bmp' {
|
|
27
|
-
const value: any;
|
|
28
|
-
export = value;
|
|
29
|
-
}
|
package/types/style.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
declare module '*.module.css' {
|
|
2
|
-
const classes: { readonly [key: string]: string };
|
|
3
|
-
export default classes;
|
|
4
|
-
}
|
|
5
|
-
declare module '*.module.scss' {
|
|
6
|
-
const classes: { readonly [key: string]: string };
|
|
7
|
-
export default classes;
|
|
8
|
-
}
|
|
9
|
-
declare module '*.module.sass' {
|
|
10
|
-
const classes: { readonly [key: string]: string };
|
|
11
|
-
export default classes;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
declare module '*.module.less' {
|
|
15
|
-
const classes: { readonly [key: string]: string };
|
|
16
|
-
export default classes;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare module '*.less' {
|
|
20
|
-
const classes: { readonly [key: string]: string };
|
|
21
|
-
export default classes;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare module '*.css' {
|
|
25
|
-
const classes: { readonly [key: string]: string };
|
|
26
|
-
export default classes;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module '*.sass' {
|
|
30
|
-
const classes: { readonly [key: string]: string };
|
|
31
|
-
export default classes;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
declare module '*.scss' {
|
|
35
|
-
const classes: { readonly [key: string]: string };
|
|
36
|
-
export default classes;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
declare module '*.mdx' {
|
|
40
|
-
const component: any;
|
|
41
|
-
export default component;
|
|
42
|
-
}
|
package/types.ts
DELETED
package/workspace-generator.ts
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
|
-
import { isBinaryFile } from 'isbinaryfile';
|
|
3
|
-
import { loadBit } from '@teambit/bit';
|
|
4
|
-
import { Harmony } from '@teambit/harmony';
|
|
5
|
-
import { Component } from '@teambit/component';
|
|
6
|
-
import execa from 'execa';
|
|
7
|
-
import { BitId } from '@teambit/legacy-bit-id';
|
|
8
|
-
import pMapSeries from 'p-map-series';
|
|
9
|
-
import UIAspect, { UiMain } from '@teambit/ui';
|
|
10
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
|
11
|
-
import { WorkspaceAspect, Workspace } from '@teambit/workspace';
|
|
12
|
-
import { PkgAspect, PkgMain } from '@teambit/pkg';
|
|
13
|
-
import { init } from '@teambit/legacy/dist/api/consumer';
|
|
14
|
-
import { CompilerAspect, CompilerMain } from '@teambit/compiler';
|
|
15
|
-
import getGitExecutablePath from '@teambit/legacy/dist/utils/git/git-executable';
|
|
16
|
-
import GitNotFound from '@teambit/legacy/dist/utils/git/exceptions/git-not-found';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { DependencyResolverMain, DependencyResolverAspect } from '@teambit/dependency-resolver';
|
|
19
|
-
import { ComponentID } from '@teambit/component-id';
|
|
20
|
-
import { WorkspaceTemplate } from './workspace-template';
|
|
21
|
-
import { NewOptions } from './new.cmd';
|
|
22
|
-
import { GeneratorAspect } from './generator.aspect';
|
|
23
|
-
|
|
24
|
-
export type GenerateResult = { id: ComponentID; dir: string; files: string[]; envId: string };
|
|
25
|
-
|
|
26
|
-
type CompToImportResolved = {
|
|
27
|
-
id: ComponentID;
|
|
28
|
-
path: string;
|
|
29
|
-
targetName?: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export class WorkspaceGenerator {
|
|
33
|
-
private workspacePath: string;
|
|
34
|
-
private harmony: Harmony;
|
|
35
|
-
private workspace: Workspace;
|
|
36
|
-
private logger: Logger;
|
|
37
|
-
constructor(
|
|
38
|
-
private workspaceName: string,
|
|
39
|
-
private options: NewOptions,
|
|
40
|
-
private template: WorkspaceTemplate,
|
|
41
|
-
private aspectComponent?: Component
|
|
42
|
-
) {
|
|
43
|
-
this.workspacePath = path.resolve(this.workspaceName);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async generate(): Promise<string> {
|
|
47
|
-
if (fs.existsSync(this.workspacePath)) {
|
|
48
|
-
throw new Error(`unable to create a workspace at "${this.workspaceName}", this path already exist`);
|
|
49
|
-
}
|
|
50
|
-
await fs.ensureDir(this.workspacePath);
|
|
51
|
-
try {
|
|
52
|
-
process.chdir(this.workspacePath);
|
|
53
|
-
await this.initGit();
|
|
54
|
-
await init(this.workspacePath, this.options.skipGit, false, false, false, false, {});
|
|
55
|
-
await this.writeWorkspaceFiles();
|
|
56
|
-
await this.reloadBitInWorkspaceDir();
|
|
57
|
-
await this.addComponentsFromRemote();
|
|
58
|
-
await this.workspace.install(undefined, {
|
|
59
|
-
dedupe: true,
|
|
60
|
-
import: false,
|
|
61
|
-
copyPeerToRuntimeOnRoot: true,
|
|
62
|
-
copyPeerToRuntimeOnComponents: false,
|
|
63
|
-
updateExisting: false,
|
|
64
|
-
});
|
|
65
|
-
// await this.buildUI(); // disabled for now. it takes too long
|
|
66
|
-
} catch (err: any) {
|
|
67
|
-
await fs.remove(this.workspacePath);
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return this.workspacePath;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private async initGit() {
|
|
75
|
-
if (this.options.skipGit) return;
|
|
76
|
-
const gitExecutablePath = getGitExecutablePath();
|
|
77
|
-
const params = ['init'];
|
|
78
|
-
try {
|
|
79
|
-
await execa(gitExecutablePath, params);
|
|
80
|
-
} catch (err: any) {
|
|
81
|
-
if (err.exitCodeName === 'ENOENT') {
|
|
82
|
-
throw new GitNotFound(gitExecutablePath, err);
|
|
83
|
-
}
|
|
84
|
-
throw err;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private async buildUI() {
|
|
89
|
-
const uiMain = this.harmony.get<UiMain>(UIAspect.id);
|
|
90
|
-
await uiMain.createRuntime({});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* writes the generated template files to the default directory set in the workspace config
|
|
95
|
-
*/
|
|
96
|
-
private async writeWorkspaceFiles(): Promise<void> {
|
|
97
|
-
const workspaceContext = {
|
|
98
|
-
name: this.workspaceName,
|
|
99
|
-
defaultScope: this.options.defaultScope,
|
|
100
|
-
empty: this.options.empty,
|
|
101
|
-
aspectComponent: this.aspectComponent,
|
|
102
|
-
};
|
|
103
|
-
const templateFiles = await this.template.generateFiles(workspaceContext);
|
|
104
|
-
await Promise.all(
|
|
105
|
-
templateFiles.map(async (templateFile) => {
|
|
106
|
-
await fs.outputFile(path.join(this.workspacePath, templateFile.relativePath), templateFile.content);
|
|
107
|
-
})
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private async reloadBitInWorkspaceDir() {
|
|
112
|
-
this.harmony = await loadBit(this.workspacePath);
|
|
113
|
-
this.workspace = this.harmony.get<Workspace>(WorkspaceAspect.id);
|
|
114
|
-
const loggerMain = this.harmony.get<LoggerMain>(LoggerAspect.id);
|
|
115
|
-
this.logger = loggerMain.createLogger(GeneratorAspect.id);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private async addComponentsFromRemote() {
|
|
119
|
-
if (this.options.empty) return;
|
|
120
|
-
const componentsToImport = this.template?.importComponents?.();
|
|
121
|
-
if (!componentsToImport || !componentsToImport.length) return;
|
|
122
|
-
const dependencyResolver = this.harmony.get<DependencyResolverMain>(DependencyResolverAspect.id);
|
|
123
|
-
|
|
124
|
-
const componentsToImportResolved = await Promise.all(
|
|
125
|
-
componentsToImport.map(async (c) => ({
|
|
126
|
-
id: ComponentID.fromLegacy(BitId.parse(c.id, true)),
|
|
127
|
-
path: c.path,
|
|
128
|
-
targetName: c.targetName,
|
|
129
|
-
}))
|
|
130
|
-
);
|
|
131
|
-
const componentIds = componentsToImportResolved.map((c) => c.id);
|
|
132
|
-
// @todo: improve performance by changing `getRemoteComponent` api to accept multiple ids
|
|
133
|
-
const components = await Promise.all(componentIds.map((id) => this.workspace.scope.getRemoteComponent(id)));
|
|
134
|
-
const oldAndNewPackageNames = this.getNewPackageNames(components, componentsToImportResolved);
|
|
135
|
-
const oldAndNewComponentIds = this.getNewComponentIds(components, componentsToImportResolved);
|
|
136
|
-
await Promise.all(
|
|
137
|
-
components.map((comp) =>
|
|
138
|
-
this.replaceOriginalPackageNameWithNew(comp, oldAndNewPackageNames, oldAndNewComponentIds)
|
|
139
|
-
)
|
|
140
|
-
);
|
|
141
|
-
await pMapSeries(components, async (comp) => {
|
|
142
|
-
const compData = componentsToImportResolved.find((c) => c.id._legacy.isEqualWithoutVersion(comp.id._legacy));
|
|
143
|
-
if (!compData) throw new Error(`workspace-generator, unable to find ${comp.id.toString()} in the given ids`);
|
|
144
|
-
await this.workspace.write(compData.path, comp);
|
|
145
|
-
await this.workspace.track({
|
|
146
|
-
rootDir: compData.path,
|
|
147
|
-
componentName: compData.targetName || compData.id.fullName,
|
|
148
|
-
mainFile: comp.state._consumer.mainFile,
|
|
149
|
-
});
|
|
150
|
-
const deps = await dependencyResolver.getDependencies(comp);
|
|
151
|
-
|
|
152
|
-
const currentPackages = Object.keys(oldAndNewPackageNames);
|
|
153
|
-
// only bring auto-resolved dependencies, others should be set in the workspace.jsonc template
|
|
154
|
-
const workspacePolicyEntries = deps
|
|
155
|
-
.filter((dep) => dep.source === 'auto')
|
|
156
|
-
.map((dep) => ({
|
|
157
|
-
dependencyId: dep.getPackageName?.() || dep.id,
|
|
158
|
-
lifecycleType: dep.lifecycle === 'dev' ? 'runtime' : dep.lifecycle,
|
|
159
|
-
value: {
|
|
160
|
-
version: dep.version,
|
|
161
|
-
},
|
|
162
|
-
}))
|
|
163
|
-
.filter((entry) => !currentPackages.includes(entry.dependencyId)); // remove components that are now imported
|
|
164
|
-
dependencyResolver.addToRootPolicy(workspacePolicyEntries, { updateExisting: true });
|
|
165
|
-
});
|
|
166
|
-
await this.workspace.writeBitMap();
|
|
167
|
-
await dependencyResolver.persistConfig(this.workspace.path);
|
|
168
|
-
this.workspace.clearCache();
|
|
169
|
-
await this.compileComponents();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private async compileComponents() {
|
|
173
|
-
const compiler = this.harmony.get<CompilerMain>(CompilerAspect.id);
|
|
174
|
-
await compiler.compileOnWorkspace();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
private getNewPackageNames(
|
|
178
|
-
components: Component[],
|
|
179
|
-
compsData: CompToImportResolved[]
|
|
180
|
-
): { [oldPackageName: string]: string } {
|
|
181
|
-
const pkg = this.harmony.get<PkgMain>(PkgAspect.id);
|
|
182
|
-
const packageToReplace = {};
|
|
183
|
-
const scopeToReplace = this.workspace.defaultScope.replace('.', '/');
|
|
184
|
-
components.forEach((comp) => {
|
|
185
|
-
const newId = this.resolveNewCompId(comp, compsData);
|
|
186
|
-
const currentPackageName = pkg.getPackageName(comp);
|
|
187
|
-
const newName = newId.fullName.replace(/\//g, '.');
|
|
188
|
-
const newPackageName = `@${scopeToReplace}.${newName}`;
|
|
189
|
-
packageToReplace[currentPackageName] = newPackageName;
|
|
190
|
-
});
|
|
191
|
-
return packageToReplace;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private getNewComponentIds(
|
|
195
|
-
components: Component[],
|
|
196
|
-
compsData: CompToImportResolved[]
|
|
197
|
-
): { [oldComponentId: string]: string } {
|
|
198
|
-
const componentToReplace = {};
|
|
199
|
-
components.forEach((comp) => {
|
|
200
|
-
const newId = this.resolveNewCompId(comp, compsData);
|
|
201
|
-
componentToReplace[comp.id.toStringWithoutVersion()] = newId.toStringWithoutVersion();
|
|
202
|
-
});
|
|
203
|
-
return componentToReplace;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
private resolveNewCompId(comp: Component, compsData: CompToImportResolved[]): ComponentID {
|
|
207
|
-
const scopeToReplace = this.workspace.defaultScope;
|
|
208
|
-
const compData = compsData.find((c) => c.id._legacy.isEqualWithoutScopeAndVersion(comp.id._legacy));
|
|
209
|
-
if (!compData) {
|
|
210
|
-
throw new Error(`workspace-generator: unable to find data for "${comp.id._legacy.toString()}"`);
|
|
211
|
-
}
|
|
212
|
-
return compData.targetName
|
|
213
|
-
? ComponentID.fromLegacy(BitId.parse(compData.targetName, false).changeScope(scopeToReplace))
|
|
214
|
-
: comp.id.changeScope(scopeToReplace);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
private async replaceOriginalPackageNameWithNew(
|
|
218
|
-
comp: Component,
|
|
219
|
-
packageToReplace: Record<string, string>,
|
|
220
|
-
oldAndNewComponentIds: Record<string, string>
|
|
221
|
-
) {
|
|
222
|
-
await Promise.all(
|
|
223
|
-
comp.filesystem.files.map(async (file) => {
|
|
224
|
-
const isBinary = await isBinaryFile(file.contents);
|
|
225
|
-
if (isBinary) return;
|
|
226
|
-
const strContent = file.contents.toString();
|
|
227
|
-
let newContent = strContent;
|
|
228
|
-
Object.keys(packageToReplace).forEach((currentPackage) => {
|
|
229
|
-
if (strContent.includes(currentPackage)) {
|
|
230
|
-
const currentPkgRegex = new RegExp(currentPackage, 'g');
|
|
231
|
-
newContent = newContent.replace(currentPkgRegex, packageToReplace[currentPackage]);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
Object.keys(oldAndNewComponentIds).forEach((currentId) => {
|
|
235
|
-
if (strContent.includes(currentId)) {
|
|
236
|
-
const currentIdRegex = new RegExp(currentId, 'g');
|
|
237
|
-
newContent = newContent.replace(currentIdRegex, oldAndNewComponentIds[currentId]);
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
if (strContent !== newContent) {
|
|
241
|
-
file.contents = Buffer.from(newContent);
|
|
242
|
-
}
|
|
243
|
-
})
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
}
|