Skip to content

feat: config manipulation commands #5402

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

Merged
merged 1 commit into from
Sep 30, 2020
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
3 changes: 3 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ injector.requirePublicClass(
injector.require("platformCommandParameter", "./platform-command-param");
injector.requireCommand("create", "./commands/create-project");
injector.requireCommand("clean", "./commands/clean");
injector.requireCommand("config|*list", "./commands/config");
injector.requireCommand("config|get", "./commands/config");
injector.requireCommand("config|set", "./commands/config");
injector.requireCommand("generate", "./commands/generate");
injector.requireCommand("platform|*list", "./commands/list-platforms");
injector.requireCommand("platform|add", "./commands/add-platform");
Expand Down
133 changes: 133 additions & 0 deletions lib/commands/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import { IProjectConfigService } from "../definitions/project";
import { SupportedConfigValues } from "../tools/config-manipulation/config-transformer";
import { IErrors } from "../common/declarations";

export class ConfigListCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(
private $projectConfigService: IProjectConfigService,
private $logger: ILogger
) {}

public async execute(args: string[]): Promise<void> {
try {
const config = this.$projectConfigService.readConfig();
this.$logger.info(this.getValueString(config as SupportedConfigValues));
} catch (error) {
this.$logger.info("Failed to read config. Error is: ", error);
}
}

private getValueString(value: SupportedConfigValues, depth = 0): string {
const indent = () => " ".repeat(depth);
if (typeof value === "object") {
return (
`${depth > 0 ? "\n" : ""}` +
Object.keys(value)
.map((key) => {
return (
`${indent()}${key}: `.green +
this.getValueString(value[key], depth + 1)
);
})
.join("\n")
);
} else {
return `${value}`.yellow as string;
}
}
}

export class ConfigGetCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(
private $projectConfigService: IProjectConfigService,
private $logger: ILogger,
private $errors: IErrors
) {}

public async execute(args: string[]): Promise<void> {
try {
const [key] = args;
const current = this.$projectConfigService.getValue(key);
this.$logger.info(current);
} catch (err) {
// ignore
}
}

public async canExecute(args: string[]): Promise<boolean> {
if (!args[0]) {
this.$errors.failWithHelp("You must specify a key. Eg: ios.id");
}

return true;
}
}

export class ConfigSetCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(
private $projectConfigService: IProjectConfigService,
private $logger: ILogger,
private $errors: IErrors
) {}

public async execute(args: string[]): Promise<void> {
const [key, value] = args;
const current = this.$projectConfigService.getValue(key);
if (current && typeof current === "object") {
this.$errors.fail(
`Unable to change object values. Please update individual values instead.\nEg: ns config set android.codeCache true`
);
}
const convertedValue = this.getConvertedValue(value);
const existingKey = current !== undefined;
const keyDisplay = `${key}`.green;
const currentDisplay = `${current}`.yellow;
const updatedDisplay = `${convertedValue}`.cyan;

this.$logger.info(
`${existingKey ? "Updating" : "Setting"} ${keyDisplay}${
existingKey ? ` from ${currentDisplay} ` : " "
}to ${updatedDisplay}`
);

try {
await this.$projectConfigService.setValue(key, convertedValue);
this.$logger.info("Done");
} catch (error) {
this.$logger.info("Could not update conifg. Error is: ", error);
}
}

public async canExecute(args: string[]): Promise<boolean> {
if (!args[0]) {
this.$errors.failWithHelp("You must specify a key. Eg: ios.id");
}

if (!args[1]) {
this.$errors.failWithHelp("You must specify a value.");
}

return true;
}

private getConvertedValue(v: any): any {
try {
return JSON.parse(v);
} catch (e) {
// just treat it as a string
return `${v}`;
}
}
}

injector.registerCommand("config|*list", ConfigListCommand);
injector.registerCommand("config|get", ConfigGetCommand);
injector.registerCommand("config|set", ConfigSetCommand);