@teambit/formatter 1.0.108 → 1.0.109

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.
@@ -1,5 +1,5 @@
1
- import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.defender_formatter@1.0.108/dist/formatter.composition.js';
2
- import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.defender_formatter@1.0.108/dist/formatter.docs.mdx';
1
+ import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.defender_formatter@1.0.109/dist/formatter.composition.js';
2
+ import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.defender_formatter@1.0.109/dist/formatter.docs.mdx';
3
3
 
4
4
  export const compositions = [compositions_0];
5
5
  export const overview = [overview_0];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/formatter",
3
- "version": "1.0.108",
3
+ "version": "1.0.109",
4
4
  "homepage": "https://bit.cloud/teambit/defender/formatter",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.defender",
8
8
  "name": "formatter",
9
- "version": "1.0.108"
9
+ "version": "1.0.109"
10
10
  },
11
11
  "dependencies": {
12
12
  "chalk": "2.4.2",
@@ -15,12 +15,12 @@
15
15
  "cli-highlight": "2.1.9",
16
16
  "ink": "3.2.0",
17
17
  "@teambit/harmony": "0.4.6",
18
- "@teambit/cli": "0.0.840",
19
- "@teambit/component": "1.0.108",
20
- "@teambit/envs": "1.0.108",
21
- "@teambit/workspace": "1.0.108",
22
- "@teambit/builder": "1.0.108",
23
- "@teambit/logger": "0.0.933"
18
+ "@teambit/cli": "0.0.841",
19
+ "@teambit/component": "1.0.109",
20
+ "@teambit/envs": "1.0.109",
21
+ "@teambit/workspace": "1.0.109",
22
+ "@teambit/builder": "1.0.109",
23
+ "@teambit/logger": "0.0.934"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/lodash": "4.14.165",
@@ -28,7 +28,7 @@
28
28
  "@types/jest": "^29.2.2",
29
29
  "@types/testing-library__jest-dom": "^5.9.5",
30
30
  "@teambit/defender.content.formatter-overview": "1.96.1",
31
- "@teambit/harmony.envs.core-aspect-env": "0.0.13"
31
+ "@teambit/harmony.envs.core-aspect-env": "0.0.14"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "^17.0.0 || ^18.0.0",
package/format.cmd.ts DELETED
@@ -1,155 +0,0 @@
1
- import { TimerResponse, Timer } from '@teambit/legacy/dist/toolbox/timer';
2
- import { Command, CommandOptions } from '@teambit/cli';
3
- import { ComponentFactory, ComponentID } from '@teambit/component';
4
- import chalk from 'chalk';
5
- import { EnvsExecutionResult } from '@teambit/envs';
6
- import { Workspace } from '@teambit/workspace';
7
- import { compact, flatten } from 'lodash';
8
- import { FormatterMain } from './formatter.main.runtime';
9
- import { ComponentFormatResult, FormatResults, FileFormatResult } from './formatter';
10
- import { FormatterOptions } from './formatter-context';
11
-
12
- export type FormatCmdOptions = {
13
- changed?: boolean;
14
- json?: boolean;
15
- check?: boolean;
16
- };
17
-
18
- type OutputContext = {
19
- check?: boolean;
20
- };
21
-
22
- /**
23
- * A type for result with componentId instead of the entire component, as when output to console, we don't want to print all the component
24
- */
25
- export type JsonComponentFormatResult = Omit<ComponentFormatResult, 'component'> & {
26
- componentId: ComponentID;
27
- };
28
-
29
- export type JsonFormatDataResults = Omit<FormatResults, 'results'> & { results: JsonComponentFormatResult[] };
30
- /**
31
- * A type for result with componentId instead of the entire component, as when output to console, we don't want to print all the component
32
- */
33
- export type JsonFormatResults = {
34
- duration: TimerResponse;
35
- data: JsonFormatDataResults;
36
- code: number;
37
- componentsIdsToFormat: string[];
38
- };
39
-
40
- export class FormatCmd implements Command {
41
- name = 'format [component...]';
42
- description = 'format components in the development workspace';
43
- group = 'development';
44
- helpUrl = 'reference/formatting/formatter-overview';
45
- options = [
46
- ['c', 'changed', 'format only new and modified components'],
47
- ['', 'check', 'will output a human-friendly message and a list of unformatted files, if any'],
48
- ['j', 'json', 'return the format results in json format'],
49
- ] as CommandOptions;
50
-
51
- constructor(
52
- private formatter: FormatterMain,
53
- private componentHost: ComponentFactory,
54
- private workspace: Workspace
55
- ) {}
56
-
57
- async report([components = []]: [string[]], formatterOptions: FormatCmdOptions) {
58
- const { duration, data, code, componentsIdsToFormat } = await this.json([components], formatterOptions);
59
-
60
- const title = chalk.bold(
61
- `formatting total of ${chalk.cyan(
62
- componentsIdsToFormat.length.toString()
63
- )} component(s) in workspace '${chalk.cyan(this.componentHost.name)}`
64
- );
65
-
66
- const componentsOutputs = this.getAllComponentsResultOutput(data.results, { check: formatterOptions.check });
67
-
68
- const { seconds } = duration;
69
- const summery = `formatted ${chalk.cyan(componentsIdsToFormat.length.toString())} components in ${chalk.cyan(
70
- seconds.toString()
71
- )}.`;
72
-
73
- return {
74
- data: `${title}\n\n${componentsOutputs}\n\n${summery}`,
75
- code,
76
- };
77
- }
78
-
79
- async json([components = []]: [string[]], formatterCmdOptions: FormatCmdOptions): Promise<JsonFormatResults> {
80
- const timer = Timer.create();
81
- timer.start();
82
- const componentsIds = await this.getIdsToFormat(components, formatterCmdOptions.changed);
83
- const componentsToFormat = await this.workspace.getMany(componentsIds);
84
- const opts: FormatterOptions = {};
85
- const formatterResults = formatterCmdOptions.check
86
- ? await this.formatter.check(componentsToFormat, opts)
87
- : await this.formatter.format(componentsToFormat, opts);
88
- const jsonFormatterResults = toJsonFormatResults(formatterResults);
89
- const timerResponse = timer.stop();
90
- const statusCode = this.getStatusCode(jsonFormatterResults, formatterCmdOptions.check);
91
-
92
- return {
93
- duration: timerResponse,
94
- data: jsonFormatterResults,
95
- code: statusCode,
96
- componentsIdsToFormat: componentsToFormat.map((comp) => comp.id.toString()),
97
- };
98
- }
99
-
100
- private getStatusCode(results: JsonFormatDataResults, check = false): number {
101
- if (!check) return 0;
102
- const hasIssues = results.results.some((comp) => comp.results.some((file) => file.hasIssues));
103
- if (hasIssues) return 1;
104
- return 0;
105
- }
106
-
107
- private async getIdsToFormat(components: string[], changed = false): Promise<ComponentID[]> {
108
- if (components.length) {
109
- return this.workspace.resolveMultipleComponentIds(components);
110
- }
111
- if (changed) {
112
- return this.workspace.getNewAndModifiedIds();
113
- }
114
- return this.componentHost.listIds();
115
- }
116
-
117
- private getAllComponentsResultOutput(componentsResult: JsonComponentFormatResult[], context: OutputContext) {
118
- const allResults = componentsResult.map((comp) => this.getOneComponentResultOutput(comp, context));
119
- return allResults.join('\n\n');
120
- }
121
-
122
- private getOneComponentResultOutput(componentResult: JsonComponentFormatResult, context: OutputContext) {
123
- const title = chalk.bold.cyan(componentResult.componentId.toString({ ignoreVersion: true }));
124
- const filesWithIssues = componentResult.results.filter((fileResult) => fileResult.hasIssues);
125
- if (!filesWithIssues || !filesWithIssues.length) {
126
- return `${title}\n${chalk.green('no issues found')}`;
127
- }
128
- let subTitle = chalk.green('the following files have been re-formatted:');
129
- if (context.check) {
130
- subTitle = chalk.red('issues found in the following files:');
131
- }
132
- const files = filesWithIssues.map(this.getOneComponentFileResultOutput);
133
- return `${title}\n${subTitle}\n${files.join('\n')}`;
134
- }
135
-
136
- private getOneComponentFileResultOutput(fileResult: FileFormatResult) {
137
- return fileResult.filePath;
138
- }
139
- }
140
-
141
- function toJsonFormatResults(results: EnvsExecutionResult<FormatResults>): JsonFormatDataResults {
142
- const newResults = results.results.map((res) => {
143
- const resultsWithoutComponent = res.data?.results.map((result) => {
144
- return {
145
- componentId: result.component.id,
146
- results: result.results,
147
- };
148
- });
149
- return compact(resultsWithoutComponent);
150
- });
151
- return {
152
- results: compact(flatten(newResults)),
153
- errors: results?.errors,
154
- };
155
- }
package/format.task.ts DELETED
@@ -1,25 +0,0 @@
1
- import { BuildTask, BuiltTaskResult, BuildContext, ComponentResult } from '@teambit/builder';
2
- import { Formatter } from './formatter';
3
-
4
- export class FormatTask implements BuildTask {
5
- constructor(readonly aspectId: string, readonly name = 'format') {}
6
-
7
- async execute(context: BuildContext): Promise<BuiltTaskResult> {
8
- const formatter: Formatter = context.env.getFormatter();
9
- // TODO: add option to select between check and format here
10
- const results = await formatter.check(context);
11
- const componentsResults = results.results.map((formatResult): ComponentResult => {
12
- return {
13
- component: formatResult.component,
14
- metadata: {
15
- results: formatResult.results,
16
- },
17
- errors: [],
18
- };
19
- });
20
-
21
- return {
22
- componentsResults,
23
- };
24
- }
25
- }
@@ -1,6 +0,0 @@
1
- import { ExecutionContext } from '@teambit/envs';
2
-
3
- export type FormatterOptions = { check?: boolean };
4
- export interface FormatterContext extends ExecutionContext, FormatterOptions {}
5
- // export interface FormatterContext extends ExecutionContext {}
6
- // export type FormatterContext = ExecutionContext;
@@ -1,9 +0,0 @@
1
- import { EnvHandler } from '@teambit/envs';
2
- import { Formatter } from './formatter';
3
-
4
- export interface FormatterEnv {
5
- /**
6
- * return a Formatter instance.
7
- */
8
- formatter(): EnvHandler<Formatter>;
9
- }
@@ -1,5 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const FormatterAspect = Aspect.create({
4
- id: 'teambit.defender/formatter',
5
- });
@@ -1,8 +0,0 @@
1
- import gql from 'graphql-tag';
2
-
3
- export function linterSchema() {
4
- return {
5
- typeDefs: gql``,
6
- resolvers: {},
7
- };
8
- }
@@ -1,75 +0,0 @@
1
- import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
2
- import { Component, ComponentAspect, ComponentMain } from '@teambit/component';
3
- import { EnvsAspect, EnvsMain, ExecutionContext } from '@teambit/envs';
4
- import { LoggerAspect, LoggerMain } from '@teambit/logger';
5
- import { Workspace, WorkspaceAspect } from '@teambit/workspace';
6
- import { FormatterAspect } from './formatter.aspect';
7
- import { FormatterService } from './formatter.service';
8
- import { FormatTask } from './format.task';
9
- import { FormatCmd } from './format.cmd';
10
- import { FormatterOptions } from './formatter-context';
11
- import { Formatter } from './formatter';
12
-
13
- export type FormatterConfig = {};
14
- export class FormatterMain {
15
- static runtime = MainRuntime;
16
-
17
- constructor(private envs: EnvsMain, private formatterService: FormatterService) {}
18
-
19
- /**
20
- * format an array of components.
21
- */
22
- async format(components: Component[], opts: FormatterOptions) {
23
- const envsRuntime = await this.envs.createEnvironment(components);
24
- const formatResults = envsRuntime.run(this.formatterService, this.toFormatServiceOptions(opts, false));
25
- return formatResults;
26
- }
27
-
28
- /**
29
- * check format an array of components.
30
- */
31
- async check(components: Component[], opts: FormatterOptions) {
32
- const envsRuntime = await this.envs.createEnvironment(components);
33
- const formatResults = envsRuntime.run(this.formatterService, this.toFormatServiceOptions(opts, true));
34
- return formatResults;
35
- }
36
-
37
- getFormatter(context: ExecutionContext, options: FormatterOptions): Formatter {
38
- return this.formatterService.getFormatter(context, options);
39
- }
40
-
41
- private toFormatServiceOptions(opts: FormatterOptions, check = false): FormatterOptions {
42
- return {
43
- ...opts,
44
- check,
45
- };
46
- }
47
-
48
- /**
49
- * create a format task for build pipelines.
50
- * @param name name of the task.
51
- */
52
- createTask(name?: string): FormatTask {
53
- return new FormatTask(FormatterAspect.id, name);
54
- }
55
-
56
- static dependencies = [EnvsAspect, CLIAspect, ComponentAspect, LoggerAspect, WorkspaceAspect];
57
-
58
- static defaultConfig: FormatterConfig = {};
59
-
60
- static async provider(
61
- [envs, cli, component, loggerAspect, workspace]: [EnvsMain, CLIMain, ComponentMain, LoggerMain, Workspace],
62
- config: FormatterConfig
63
- ) {
64
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
- const logger = loggerAspect.createLogger(FormatterAspect.id);
66
- const formatterService = new FormatterService(config);
67
- const formatterAspect = new FormatterMain(envs, formatterService);
68
- envs.registerService(formatterService);
69
- cli.register(new FormatCmd(formatterAspect, component.getHost(), workspace));
70
-
71
- return formatterAspect;
72
- }
73
- }
74
-
75
- FormatterAspect.addRuntime(FormatterMain);
package/formatter.ts DELETED
@@ -1,43 +0,0 @@
1
- import { Component } from '@teambit/component';
2
- import { FormatterContext } from './formatter-context';
3
-
4
- export type ComponentFormatResult = {
5
- /**
6
- * the formatted component.
7
- */
8
- component: Component;
9
-
10
- /**
11
- * format results for each file of the component.
12
- */
13
- results: FileFormatResult[];
14
- };
15
-
16
- export type FileFormatResult = {
17
- /**
18
- * path of the formatted file.
19
- */
20
- filePath: string;
21
-
22
- /**
23
- * Does the file has formatting issues (needs format)
24
- */
25
- hasIssues: boolean;
26
-
27
- /**
28
- * The new file content after the formatting
29
- */
30
- newContent?: string;
31
- };
32
-
33
- export type FormatResults = {
34
- results: ComponentFormatResult[];
35
- errors: Error[];
36
- };
37
-
38
- export interface Formatter {
39
- id: string;
40
- format(context: FormatterContext): Promise<FormatResults>;
41
- formatSnippet(snippet: string, filepath?: string): Promise<string>;
42
- check(context: FormatterContext): Promise<FormatResults>;
43
- }
package/index.ts DELETED
@@ -1,9 +0,0 @@
1
- import { FormatterAspect } from './formatter.aspect';
2
-
3
- export { FormatterAspect };
4
- export type { FormatterMain, FormatterConfig } from './formatter.main.runtime';
5
- export type { FormatResults, Formatter, FileFormatResult, ComponentFormatResult } from './formatter';
6
- export type { FormatTask } from './format.task';
7
- export type { FormatterEnv } from './formatter-env-type';
8
- export type { FormatterContext, FormatterOptions } from './formatter-context';
9
- export default FormatterAspect;