proca
Version:
104 lines (90 loc) • 2.55 kB
JavaScript
import { Args, Flags } from "@oclif/core";
import { error, stdout, ux } from "@oclif/core/ux";
import { get as getConfig, getFilename, load, write } from "#src/config.mjs";
import Command from "#src/procaCommand.mjs";
export default class CampaignList extends Command {
static enableJsonFlag = true;
static aliases = ["config:setup"];
static deprecateAliases = true;
static args = {
key: Args.string({
description: "variable name",
multiple: false,
}),
value: Args.string({
description: "value",
multiple: true,
}),
};
static description =
"update the setting used to authenticate to the servers and services";
static examples = [
"<%= config.bin %> <%= command.id %> --user=xavier@example.org --token=API-12345789",
"<%= config.bin %> <%= command.id %> VAR1 VALUE",
];
static flags = {
// flag with no value (-f, --force)
...super.globalFlags,
environment: Flags.string({
description: "environment",
default: "default",
}),
url: Flags.string({
description: "url of the proca server api",
default: "https://api.proca.app/api",
helpValue: "<url>",
}),
token: Flags.string({
description: "user token on proca server",
helpValue: "<API-token>",
}),
};
format = (obj) => {
const content = ["# generated by proca-api"];
for (const [key, value] of Object.entries(obj)) {
if (value) {
content.push(`${key}='${value.replace(/'/g, "''")}'`);
} else {
content.push(`#${key}= `);
}
}
return content.join("\n");
};
generate = function () {
const mapping = {
...args,
REACT_APP_NAME: "proca",
REACT_APP_API_URL: this.flags.url,
PROCA_TOKEN: this.flags.token,
};
return this.format(mapping);
};
regenerate = function (config, args) {
config[args.key] = args.value;
console.log(this.format(config));
process.exit(1);
return this.format(config);
};
async run() {
const config = this.config;
const { args, flags, raw } = await this.parse();
const rawf = raw.filter((d) => d.type === "flag").map((d) => d.flag);
const file = getFilename(this.config.configDir);
const userConfig = getConfig(file, true);
this.info("config file", file);
if (userConfig) {
if (args.key && args.value) {
write(file, this.regenerate(userConfig, args));
} else {
console.log(userConfig);
this.error("config file exists already", {
code: "CONFIG_ERR",
_ref: "README.md#",
suggestions: ["add KEY VALUE to update or add new variables"],
});
}
} else {
write(file, this.generate());
}
}
}