@teambit/workspace-config-files 1.0.106 → 1.0.108

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.
@@ -0,0 +1,429 @@
1
+ import fs from 'fs-extra';
2
+ import { join } from 'path';
3
+ import globby from 'globby';
4
+ import chalk from 'chalk';
5
+ import { PromptCanceled } from '@teambit/legacy/dist/prompts/exceptions';
6
+ import pMapSeries from 'p-map-series';
7
+ import { ConsumerNotFound } from '@teambit/legacy/dist/consumer/exceptions';
8
+ import yesno from 'yesno';
9
+ import { defaults, flatMap, isFunction, pick, uniq } from 'lodash';
10
+ import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
11
+ import { WorkspaceAspect } from '@teambit/workspace';
12
+ import type { Workspace } from '@teambit/workspace';
13
+ import { Environment, EnvsAspect, ExecutionContext } from '@teambit/envs';
14
+ import type { EnvsMain } from '@teambit/envs';
15
+ import { Logger, LoggerAspect } from '@teambit/logger';
16
+ import type { LoggerMain } from '@teambit/logger';
17
+ import { WorkspaceConfigFilesAspect } from './workspace-config-files.aspect';
18
+ import { ConfigWriterEntry } from './config-writer-entry';
19
+ import { WsConfigCleanCmd, WsConfigCmd, WsConfigListCmd, WsConfigWriteCmd } from './ws-config.cmd';
20
+ import WriteConfigFilesFailed from './exceptions/write-failed';
21
+ import { WorkspaceConfigFilesService } from './workspace-config-files.service';
22
+ import {
23
+ handleRealConfigFiles,
24
+ handleExtendingConfigFiles,
25
+ EnvsWrittenExtendingConfigFiles,
26
+ EnvsWrittenRealConfigFiles,
27
+ } from './writers';
28
+
29
+ /**
30
+ * Configs that can be configured in the workspace.jsonc file
31
+ */
32
+ export type WorkspaceConfigFilesAspectConfig = {
33
+ configsRootDir?: string;
34
+ enableWorkspaceConfigWrite?: boolean;
35
+ };
36
+
37
+ export type EnvConfigWriter = {
38
+ envId: string;
39
+ executionContext: ExecutionContext;
40
+ configWriters: ConfigWriterEntry[];
41
+ };
42
+
43
+ export type EnvConfigWriterEntry = {
44
+ envId: string;
45
+ configWriter: ConfigWriterEntry;
46
+ executionContext: ExecutionContext;
47
+ };
48
+
49
+ type WriterIdsToEnvEntriesMap = {
50
+ [writerId: string]: EnvConfigWriterEntry[];
51
+ };
52
+
53
+ export type EnvConfigWritersList = Array<EnvConfigWriter>;
54
+
55
+ export type CleanConfigFilesOptions = {
56
+ silent?: boolean; // no prompt
57
+ dryRun?: boolean;
58
+ writers?: string[];
59
+ };
60
+
61
+ export type WriteConfigFilesOptions = {
62
+ clean?: boolean;
63
+ silent?: boolean; // no prompt
64
+ dedupe?: boolean;
65
+ dryRun?: boolean;
66
+ throw?: boolean;
67
+ writers?: string[];
68
+ };
69
+
70
+ export type CompPathExtendingHashMap = { [compPath: string]: string };
71
+
72
+ export type EnvMapValue = { env: Environment; id: string[]; paths: string[] };
73
+ export type EnvCompsDirsMap = { [envId: string]: EnvMapValue };
74
+
75
+ export type OneConfigWriterIdResult = {
76
+ writerId: string;
77
+ totalWrittenFiles: number;
78
+ realConfigFiles: EnvsWrittenRealConfigFiles;
79
+ totalRealConfigFiles: number;
80
+ extendingConfigFiles: EnvsWrittenExtendingConfigFiles;
81
+ totalExtendingConfigFiles: number;
82
+ };
83
+
84
+ export type WriteResults = {
85
+ writersResult: OneConfigWriterIdResult[];
86
+ totalWrittenFiles: number;
87
+ totalRealConfigFiles: number;
88
+ totalExtendingConfigFiles: number;
89
+ };
90
+
91
+ export type WriteConfigFilesResult = {
92
+ cleanResults?: string[];
93
+ writeResults: WriteResults;
94
+ wsDir: string;
95
+ err?: Error;
96
+ };
97
+
98
+ export class WorkspaceConfigFilesMain {
99
+ private envsNotImplementing = {};
100
+
101
+ constructor(
102
+ private workspace: Workspace,
103
+ private envs: EnvsMain,
104
+ private logger: Logger,
105
+ private config: WorkspaceConfigFilesAspectConfig
106
+ ) {}
107
+
108
+ /**
109
+ * It writes the configuration files for the workspace
110
+ * for example: tsconfig, eslint config and prettier config files.
111
+ * @param {WriteConfigFilesOptions} options - WriteConfigFilesOptions = {}
112
+ * @returns An object with the following properties:
113
+ * - writeResults: An object with the following properties:
114
+ * - aspectsWritersResults: An array of objects with the following properties:
115
+ * - aspect: The aspect that was written
116
+ * - totalWrittenFiles: The number of files that were written
117
+ * - totalWrittenFiles: The total number of files that were written
118
+ * - cleanResults: array of deleted paths
119
+ */
120
+ async writeConfigFiles(options: WriteConfigFilesOptions = {}): Promise<WriteConfigFilesResult> {
121
+ if (!this.workspace) {
122
+ throw new ConsumerNotFound();
123
+ }
124
+ const defaultOpts: WriteConfigFilesOptions = {
125
+ clean: false,
126
+ dedupe: false,
127
+ silent: false,
128
+ dryRun: false,
129
+ throw: true,
130
+ };
131
+ const optionsWithDefaults = defaults(options, defaultOpts);
132
+ const execContext = await this.getExecContext();
133
+
134
+ let pathsToClean: string[] | undefined = [];
135
+ if (optionsWithDefaults.clean) {
136
+ pathsToClean = await this.calcPathsToClean({ writers: optionsWithDefaults.writers });
137
+ }
138
+
139
+ let writeErr;
140
+ let writeResults;
141
+ try {
142
+ writeResults = await this.write(execContext, optionsWithDefaults);
143
+ const allWrittenFiles = writeResults.writersResult.flatMap((writerResult) => {
144
+ return writerResult.extendingConfigFiles.flatMap((extendingConfigFile) => {
145
+ return extendingConfigFile.extendingConfigFile.filePaths;
146
+ });
147
+ });
148
+ // Avoid delete and re-create files that were written by other config writers
149
+ // instead of deleting at the beginning then write all
150
+ // we write all and then delete the files that were not written by the config writers
151
+ // This reduces the config files that re-created (as many times no changes needed)
152
+ // which prevent issues with needing to restart the ts-server in the ide
153
+ pathsToClean = pathsToClean.filter(
154
+ (pathToClean) => !allWrittenFiles.includes(join(this.workspace.path, pathToClean))
155
+ );
156
+ await this.deleteFiles(pathsToClean);
157
+ } catch (err) {
158
+ this.logger.info('writeConfigFiles failed', err);
159
+ if (optionsWithDefaults.throw) {
160
+ throw new WriteConfigFilesFailed();
161
+ }
162
+ writeErr = err;
163
+ }
164
+
165
+ return { writeResults, cleanResults: pathsToClean, wsDir: this.workspace.path, err: writeErr };
166
+ }
167
+
168
+ /**
169
+ * This will check the config.enableWorkspaceConfigWrite before writing the config files.
170
+ */
171
+ async writeConfigFilesIfEnabled(options: WriteConfigFilesOptions = {}): Promise<WriteConfigFilesResult | undefined> {
172
+ const shouldWrite = this.isWorkspaceConfigWriteEnabled();
173
+ if (!shouldWrite) return undefined;
174
+ return this.writeConfigFiles(options);
175
+ }
176
+
177
+ /**
178
+ * It cleans (delete) the config files from the workspace.
179
+ * This will check each file and will only delete it in case it was generated by bit
180
+ * @param {CleanConfigFilesOptions} options - CleanConfigFilesOptions = {}
181
+ * @returns An array of strings.
182
+ */
183
+ async cleanConfigFiles(options: CleanConfigFilesOptions = {}): Promise<string[]> {
184
+ // const execContext = await this.getExecContext();
185
+ if (!this.workspace) {
186
+ throw new ConsumerNotFound();
187
+ }
188
+ const cleanResults = await this.clean(options);
189
+ return cleanResults;
190
+ }
191
+
192
+ /**
193
+ * The function checks if the auto writing of workspace configuration is enabled.
194
+ * if it's enabled we will re-generate the configuration files upon bit create
195
+ * @returns the boolean value of `!!this.config.enableWorkspaceConfigWrite`.
196
+ */
197
+ isWorkspaceConfigWriteEnabled() {
198
+ return !!this.config.enableWorkspaceConfigWrite;
199
+ }
200
+
201
+ /**
202
+ * It returns a list of all the config writers that have been registered with the config writer slot
203
+ * @returns An array of objects with aspectId and configWriter properties.
204
+ */
205
+ async listConfigWriters(): Promise<EnvConfigWritersList> {
206
+ if (!this.workspace) {
207
+ throw new ConsumerNotFound();
208
+ }
209
+ const execContexts = await this.getExecContext();
210
+
211
+ const result: EnvConfigWritersList = execContexts.map((executionContext) => {
212
+ const configWriters = this.getConfigWriters(executionContext);
213
+ return { envId: executionContext.envRuntime.id, executionContext, configWriters };
214
+ });
215
+ return result;
216
+ }
217
+
218
+ private groupByWriterId(writerList: EnvConfigWritersList): WriterIdsToEnvEntriesMap {
219
+ return writerList.reduce((acc, envConfigWriter: EnvConfigWriter) => {
220
+ envConfigWriter.configWriters.forEach((configWriter: ConfigWriterEntry) => {
221
+ acc[configWriter.id] = acc[configWriter.id] || [];
222
+ acc[configWriter.id].push({ configWriter, envId: envConfigWriter.envId });
223
+ });
224
+ return acc;
225
+ }, {});
226
+ }
227
+
228
+ private async write(envsExecutionContext: ExecutionContext[], opts: WriteConfigFilesOptions): Promise<WriteResults> {
229
+ const envCompDirsMap = this.getEnvComponentsDirsMap(envsExecutionContext);
230
+ const configsRootDir = this.getConfigsRootDir();
231
+ const configWriters = await this.listConfigWriters();
232
+ const writerIdsToEnvEntriesMap = this.groupByWriterId(configWriters);
233
+ const filteredWriterIdsToEnvEntriesMap = opts.writers
234
+ ? pick(writerIdsToEnvEntriesMap, opts.writers)
235
+ : writerIdsToEnvEntriesMap;
236
+ let totalRealConfigFiles = 0;
237
+ let totalExtendingConfigFiles = 0;
238
+ const results = await pMapSeries(
239
+ Object.entries(filteredWriterIdsToEnvEntriesMap),
240
+ async ([writerId, envEntries]) => {
241
+ const oneResult = await this.handleOneIdWriter(writerId, envEntries, envCompDirsMap, configsRootDir, opts);
242
+ totalRealConfigFiles += oneResult.totalRealConfigFiles;
243
+ totalExtendingConfigFiles += oneResult.totalExtendingConfigFiles;
244
+ return oneResult;
245
+ }
246
+ );
247
+
248
+ const totalWrittenFiles = totalRealConfigFiles + totalExtendingConfigFiles;
249
+ return { writersResult: results, totalWrittenFiles, totalRealConfigFiles, totalExtendingConfigFiles };
250
+ }
251
+
252
+ private async handleOneIdWriter(
253
+ writerId: string,
254
+ envEntries: EnvConfigWriterEntry[],
255
+ envCompsDirsMap: EnvCompsDirsMap,
256
+ configsRootDir: string,
257
+ opts: WriteConfigFilesOptions
258
+ ): Promise<OneConfigWriterIdResult> {
259
+ const writtenRealConfigFilesMap = await handleRealConfigFiles(envEntries, envCompsDirsMap, configsRootDir, opts);
260
+ const writtenExtendingConfigFiles = await handleExtendingConfigFiles(
261
+ envEntries,
262
+ envCompsDirsMap,
263
+ writtenRealConfigFilesMap,
264
+ configsRootDir,
265
+ this.workspace.path,
266
+ opts
267
+ );
268
+
269
+ const writtenRealConfigFiles = Object.values(writtenRealConfigFilesMap);
270
+ const totalRealConfigFiles = writtenRealConfigFiles.length;
271
+ const totalExtendingConfigFiles = writtenExtendingConfigFiles.reduce(
272
+ (acc, curr) => acc + curr.extendingConfigFile.filePaths.length,
273
+ 0
274
+ );
275
+ const totalWrittenFiles = totalRealConfigFiles + totalExtendingConfigFiles;
276
+ return {
277
+ writerId,
278
+ totalWrittenFiles,
279
+ realConfigFiles: writtenRealConfigFiles,
280
+ totalRealConfigFiles,
281
+ extendingConfigFiles: writtenExtendingConfigFiles,
282
+ totalExtendingConfigFiles,
283
+ };
284
+ }
285
+
286
+ private getConfigsRootDir(): string {
287
+ const userConfiguredDir = this.config.configsRootDir;
288
+ return userConfiguredDir ? join(this.workspace.path, userConfiguredDir) : this.getCacheDir(this.workspace.path);
289
+ }
290
+
291
+ private getCacheDir(rootDir): string {
292
+ return join(rootDir, 'node_modules', '.cache');
293
+ }
294
+
295
+ private async getExecContext(): Promise<ExecutionContext[]> {
296
+ const components = await this.workspace.list();
297
+ const runtime = await this.envs.createEnvironment(components);
298
+ const execContext = runtime.getEnvExecutionContext();
299
+ return execContext;
300
+ }
301
+
302
+ private getEnvComponentsDirsMap(envsExecutionContext: ExecutionContext[]): EnvCompsDirsMap {
303
+ const envCompDirsMap = envsExecutionContext.reduce((acc, envExecution) => {
304
+ const envRuntime = envExecution.envRuntime;
305
+ const envId = envRuntime.id.toString();
306
+ const value = {
307
+ id: envRuntime.id,
308
+ env: envRuntime.env,
309
+ paths: envRuntime.components.map((c) => this.workspace.componentDir(c.id, undefined, { relative: true })),
310
+ };
311
+ acc[envId] = value;
312
+ return acc;
313
+ }, {});
314
+ return envCompDirsMap;
315
+ }
316
+
317
+ private getConfigWriters(envExecutionContext: ExecutionContext): ConfigWriterEntry[] {
318
+ if (envExecutionContext.env.workspaceConfig && isFunction(envExecutionContext.env.workspaceConfig)) {
319
+ return envExecutionContext.env.workspaceConfig();
320
+ }
321
+ this.addToEnvsNotImplementing(envExecutionContext.env.id);
322
+ return [];
323
+ }
324
+
325
+ private getFlatConfigWriters(envsExecutionContext: ExecutionContext[]): ConfigWriterEntry[] {
326
+ return flatMap(envsExecutionContext, (envExecutionContext) => {
327
+ return this.getConfigWriters(envExecutionContext);
328
+ });
329
+ }
330
+
331
+ /**
332
+ * Clean config files written by the config-writers
333
+ * @param envsExecutionContext
334
+ * @param param1
335
+ * @returns Array of paths of deleted config files
336
+ */
337
+ async clean({ dryRun, silent, writers }: WriteConfigFilesOptions): Promise<string[]> {
338
+ const paths = await this.calcPathsToClean({ writers });
339
+ if (dryRun) return paths;
340
+ if (!silent) await this.promptForCleaning(paths);
341
+ await this.deleteFiles(paths);
342
+ return paths;
343
+ }
344
+
345
+ private async calcPathsToClean({ writers }: WriteConfigFilesOptions): Promise<string[]> {
346
+ const execContext = await this.getExecContext();
347
+ const configWriters = this.getFlatConfigWriters(execContext);
348
+ const filteredConfigWriters = writers
349
+ ? configWriters.filter((configWriter) => writers.includes(configWriter.id))
350
+ : configWriters;
351
+
352
+ const paths = uniq(
353
+ filteredConfigWriters
354
+ .map((configWriter) => {
355
+ const patterns = configWriter.patterns;
356
+ const currPaths = globby.sync(patterns, {
357
+ cwd: this.workspace.path,
358
+ dot: true,
359
+ onlyFiles: true,
360
+ ignore: ['**/node_modules/**'],
361
+ });
362
+ const filteredPaths = currPaths.filter((path) => {
363
+ const fullPath = join(this.workspace.path, path);
364
+ return configWriter.isBitGenerated ? configWriter.isBitGenerated(fullPath) : true;
365
+ });
366
+ return filteredPaths;
367
+ })
368
+ .flat()
369
+ );
370
+ return paths;
371
+ }
372
+
373
+ private addToEnvsNotImplementing(envId: string) {
374
+ this.envsNotImplementing[envId] = true;
375
+ }
376
+
377
+ getEnvsNotImplementing() {
378
+ return Object.keys(this.envsNotImplementing);
379
+ }
380
+
381
+ private async promptForCleaning(paths: string[]) {
382
+ this.logger.clearStatusLine();
383
+ const ok = await yesno({
384
+ question: `${chalk.underline('The following files will be deleted:')}
385
+ ${paths.join('\n')}
386
+ ${chalk.bold('Do you want to continue? [yes(y)/no(n)]')}`,
387
+ });
388
+ if (!ok) {
389
+ throw new PromptCanceled();
390
+ }
391
+ }
392
+
393
+ private async deleteFiles(paths: string[]) {
394
+ await Promise.all(paths.map((f) => fs.remove(join(this.workspace.path, f))));
395
+ }
396
+
397
+ static slots = [];
398
+ // define your aspect dependencies here.
399
+ // in case you need to use another aspect API.
400
+ static dependencies = [CLIAspect, WorkspaceAspect, EnvsAspect, LoggerAspect];
401
+
402
+ static runtime = MainRuntime;
403
+
404
+ static defaultConfig: Partial<WorkspaceConfigFilesAspectConfig> = {
405
+ enableWorkspaceConfigWrite: false,
406
+ };
407
+
408
+ static async provider(
409
+ [cli, workspace, envs, loggerAspect]: [CLIMain, Workspace, EnvsMain, LoggerMain],
410
+ config: WorkspaceConfigFilesAspectConfig
411
+ ) {
412
+ const logger = loggerAspect.createLogger(WorkspaceConfigFilesAspect.id);
413
+ envs.registerService(new WorkspaceConfigFilesService(logger));
414
+
415
+ const workspaceConfigFilesMain = new WorkspaceConfigFilesMain(workspace, envs, logger, config);
416
+ const wsConfigCmd = new WsConfigCmd();
417
+ wsConfigCmd.commands = [
418
+ new WsConfigWriteCmd(workspaceConfigFilesMain),
419
+ new WsConfigCleanCmd(workspaceConfigFilesMain),
420
+ new WsConfigListCmd(workspaceConfigFilesMain),
421
+ ];
422
+ cli.register(wsConfigCmd);
423
+ return workspaceConfigFilesMain;
424
+ }
425
+ }
426
+
427
+ WorkspaceConfigFilesAspect.addRuntime(WorkspaceConfigFilesMain);
428
+
429
+ export default WorkspaceConfigFilesMain;
@@ -0,0 +1,186 @@
1
+ /* eslint-disable max-classes-per-file */
2
+
3
+ import { Command, CommandOptions } from '@teambit/cli';
4
+ import chalk from 'chalk';
5
+ import { WorkspaceConfigFilesMain, WriteConfigFilesResult } from './workspace-config-files.main.runtime';
6
+ import { formatCleanOutput, formatListOutput, formatWriteOutput, verboseFormatWriteOutput } from './outputs';
7
+
8
+ export type CleanConfigCmdFlags = {
9
+ dryRun?: boolean;
10
+ silent?: boolean;
11
+ };
12
+
13
+ export type WriteConfigCmdFlags = {
14
+ dryRun?: boolean;
15
+ writers?: string;
16
+ noDedupe?: boolean;
17
+ dryRunWithContent?: boolean;
18
+ clean?: boolean;
19
+ silent?: boolean;
20
+ verbose?: boolean;
21
+ };
22
+
23
+ const COMMAND_NAME = 'ws-config';
24
+
25
+ export class WsConfigCmd implements Command {
26
+ name = `${COMMAND_NAME} <sub-command>`;
27
+ alias = 'workspace-config';
28
+ description = 'manage workspace config files';
29
+ options = [];
30
+ group = 'development';
31
+ commands: Command[] = [];
32
+ // helpUrl = '';
33
+
34
+ async report([unrecognizedSubcommand]: [string]) {
35
+ return chalk.red(
36
+ `"${unrecognizedSubcommand}" is not a subcommand of "ws-config", please run "bit ws-config --help" to list the subcommands`
37
+ );
38
+ }
39
+ }
40
+
41
+ export class WsConfigWriteCmd implements Command {
42
+ name = 'write';
43
+ description = 'write config files in the workspace. useful for IDEs';
44
+ alias = '';
45
+ group = 'development';
46
+ options = [
47
+ [
48
+ 'c',
49
+ 'clean',
50
+ 'delete existing config files from the workspace. highly recommended to run it with "--dry-run" first',
51
+ ],
52
+ [
53
+ 'w',
54
+ 'writers <writers>',
55
+ `only write config files for the given writers. use comma to separate multiple writers. use ${COMMAND_NAME} list to see all writers`,
56
+ ],
57
+ ['s', 'silent', 'do not prompt for confirmation'],
58
+ ['', 'no-dedupe', "write configs inside each one of the component's dir, avoid deduping"],
59
+ ['', 'dry-run', 'show the paths that configs will be written per env'],
60
+ [
61
+ '',
62
+ 'dry-run-with-content',
63
+ 'use with --json flag. show the config content and the paths that will be written per env',
64
+ ],
65
+ ['v', 'verbose', 'showing verbose output for writing'],
66
+ ['j', 'json', 'json format'],
67
+ ] as CommandOptions;
68
+
69
+ constructor(private workspaceConfigFilesMain: WorkspaceConfigFilesMain) {}
70
+
71
+ async report(_args, flags: WriteConfigCmdFlags) {
72
+ const results = (await this.json(_args, flags)) as WriteConfigFilesResult;
73
+ if (flags.dryRunWithContent) {
74
+ throw new Error(`use --json flag along with --dry-run-with-content`);
75
+ }
76
+ const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
77
+ const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
78
+ const output = flags.verbose ? verboseFormatWriteOutput(results, flags) : formatWriteOutput(results, flags);
79
+ return warning + output;
80
+ }
81
+
82
+ async json(_args, flags: WriteConfigCmdFlags) {
83
+ const { clean, silent, noDedupe, dryRunWithContent, writers } = flags;
84
+ const dryRun = dryRunWithContent ? true : !!flags.dryRun;
85
+ const { cleanResults, writeResults, wsDir } = await this.workspaceConfigFilesMain.writeConfigFiles({
86
+ clean,
87
+ dedupe: !noDedupe,
88
+ dryRun,
89
+ silent,
90
+ writers: writers?.split(','),
91
+ });
92
+
93
+ if (dryRun) {
94
+ const updatedWriteResults = writeResults;
95
+ if (!dryRunWithContent) {
96
+ updatedWriteResults.writersResult = updatedWriteResults.writersResult.map((oneWriterResult) => {
97
+ oneWriterResult.realConfigFiles.forEach((realConfigFile) => {
98
+ realConfigFile.writtenRealConfigFile.content = '';
99
+ });
100
+ oneWriterResult.extendingConfigFiles.forEach((extendingConfigFile) => {
101
+ extendingConfigFile.extendingConfigFile.content = '';
102
+ });
103
+ return oneWriterResult;
104
+ });
105
+ }
106
+
107
+ return {
108
+ wsDir,
109
+ cleanResults,
110
+ writeResults: updatedWriteResults,
111
+ };
112
+ }
113
+ return { wsDir, cleanResults, writeResults };
114
+ }
115
+ }
116
+
117
+ export class WsConfigCleanCmd implements Command {
118
+ name = 'clean';
119
+ description = 'clean (delete) written config files in the workspace. useful for IDEs';
120
+ alias = '';
121
+ group = 'development';
122
+ options = [
123
+ ['s', 'silent', 'do not prompt for confirmation'],
124
+ [
125
+ 'w',
126
+ 'writers <writers>',
127
+ `only clean config files for the given writers. use comma to separate multiple writers. use ${COMMAND_NAME} list to see all writers`,
128
+ ],
129
+ ['', 'dry-run', 'show the paths of configs that will be cleaned'],
130
+ ['j', 'json', 'json format'],
131
+ ] as CommandOptions;
132
+
133
+ constructor(private workspaceConfigFilesMain: WorkspaceConfigFilesMain) {}
134
+
135
+ async report(_args, flags: CleanConfigCmdFlags) {
136
+ const results = await this.json(_args, flags);
137
+ const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
138
+ const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
139
+ const output = formatCleanOutput(results, flags);
140
+ return warning + output;
141
+ }
142
+
143
+ async json(_args, flags: WriteConfigCmdFlags) {
144
+ const { silent, dryRun } = flags;
145
+ const cleanResults = await this.workspaceConfigFilesMain.cleanConfigFiles({
146
+ dryRun,
147
+ silent,
148
+ writers: flags.writers?.split(','),
149
+ });
150
+ return cleanResults;
151
+ }
152
+ }
153
+
154
+ export class WsConfigListCmd implements Command {
155
+ name = 'list';
156
+ description = 'list config writers';
157
+ alias = '';
158
+ group = 'development';
159
+ options = [['j', 'json', 'json format']] as CommandOptions;
160
+
161
+ constructor(private workspaceConfigFilesMain: WorkspaceConfigFilesMain) {}
162
+
163
+ async report() {
164
+ const results = await this.json();
165
+ const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
166
+ const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
167
+ const output = formatListOutput(results);
168
+ return warning + output;
169
+ }
170
+
171
+ async json() {
172
+ const cleanResults = await this.workspaceConfigFilesMain.listConfigWriters();
173
+ return cleanResults;
174
+ }
175
+ }
176
+
177
+ function getWarningForNonImplementingEnvs(envsNotImplementing: string[]) {
178
+ if (!envsNotImplementing.length) return '';
179
+ const message =
180
+ chalk.yellow(`Bit cannot determine the correct contents for the config files to write. this may result in incorrect content.
181
+ The following environments need to add support for config files: ${chalk.cyan(envsNotImplementing.join(', '))}.
182
+ Read here how to correct and improve dev-ex - LINK
183
+
184
+ `);
185
+ return message;
186
+ }