Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

outputs(ws-config) - improve warning for envs that not implements the workspaceConfig API #7651

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk from 'chalk';
import { PromptCanceled } from '@teambit/legacy/dist/prompts/exceptions';
import pMapSeries from 'p-map-series';
import yesno from 'yesno';
import { flatMap, pick, uniq } from 'lodash';
import { flatMap, isFunction, pick, uniq } from 'lodash';
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import { WorkspaceAspect } from '@teambit/workspace';
import type { Workspace } from '@teambit/workspace';
Expand Down Expand Up @@ -92,6 +92,8 @@ export type WriteConfigFilesResult = {
};

export class WorkspaceConfigFilesMain {
private envsNotImplementing = {};

constructor(
private workspace: Workspace,
private envs: EnvsMain,
Expand Down Expand Up @@ -256,7 +258,11 @@ export class WorkspaceConfigFilesMain {
}

private getConfigWriters(envExecutionContext: ExecutionContext): ConfigWriterEntry[] {
return envExecutionContext.env.workspaceConfig ? envExecutionContext.env.workspaceConfig() : [];
if (envExecutionContext.env.workspaceConfig && isFunction(envExecutionContext.env.workspaceConfig)) {
return envExecutionContext.env.workspaceConfig();
}
this.addToEnvsNotImplementing(envExecutionContext.env.id);
return [];
}

private getFlatConfigWriters(envsExecutionContext: ExecutionContext[]): ConfigWriterEntry[] {
Expand Down Expand Up @@ -302,6 +308,14 @@ export class WorkspaceConfigFilesMain {
return paths;
}

private addToEnvsNotImplementing(envId: string) {
this.envsNotImplementing[envId] = true;
}

getEnvsNotImplementing() {
return Object.keys(this.envsNotImplementing);
}

private async promptForCleaning(paths: string[]) {
this.logger.clearStatusLine();
const ok = await yesno({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ type PkgTransformationMap = ServiceTransformationMap & {

export class WorkspaceConfigFilesService implements EnvService<any> {
name = 'WorkspaceConfigFiles';
private alreadyShownWarning = {};

constructor(private logger: Logger) {}

transform(env: Env, envContext: EnvContext): PkgTransformationMap | undefined {
// Old env
if (!env?.workspaceConfig) {
this.printWarningIfFirstTime(envContext.envId.toString());
return undefined;
}

Expand All @@ -46,12 +44,4 @@ export class WorkspaceConfigFilesService implements EnvService<any> {
},
};
}

private printWarningIfFirstTime(envId: string) {
const message = `the ${envId} env does not implement the workspaceConfig API. Please update your base env, or implement the workspaceConfig API.`;
if (!this.alreadyShownWarning[envId]) {
this.alreadyShownWarning[envId] = true;
this.logger.consoleWarning(message);
}
}
}
27 changes: 23 additions & 4 deletions scopes/workspace/workspace-config-files/ws-config.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ export class WsConfigWriteCmd implements Command {
if (flags.dryRunWithContent) {
throw new Error(`use --json flag along with --dry-run-with-content`);
}
if (flags.verbose) return verboseFormatWriteOutput(results, flags);
return formatWriteOutput(results, flags);
const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
const output = flags.verbose ? verboseFormatWriteOutput(results, flags) : formatWriteOutput(results, flags);
return warning + output;
}

async json(_args, flags: WriteConfigCmdFlags) {
Expand Down Expand Up @@ -132,7 +134,10 @@ export class WsConfigCleanCmd implements Command {

async report(_args, flags: CleanConfigCmdFlags) {
const results = await this.json(_args, flags);
return formatCleanOutput(results, flags);
const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
const output = formatCleanOutput(results, flags);
return warning + output;
}

async json(_args, flags: WriteConfigCmdFlags) {
Expand All @@ -157,11 +162,25 @@ export class WsConfigListCmd implements Command {

async report() {
const results = await this.json();
return formatListOutput(results);
const envsNotImplementing = this.workspaceConfigFilesMain.getEnvsNotImplementing();
const warning = getWarningForNonImplementingEnvs(envsNotImplementing);
const output = formatListOutput(results);
return warning + output;
}

async json() {
const cleanResults = await this.workspaceConfigFilesMain.listConfigWriters();
return cleanResults;
}
}

function getWarningForNonImplementingEnvs(envsNotImplementing: string[]) {
if (!envsNotImplementing.length) return '';
const message =
chalk.yellow(`Bit cannot determine the correct contents for the config files to write. this may result in incorrect content.
The following environments need to add support for config files: ${chalk.cyan(envsNotImplementing.join(', '))}.
Read here how to correct and improve dev-ex - LINK

`);
return message;
}