Skip to content

Commit

Permalink
feat: add env:paths:clear commands (#473)
Browse files Browse the repository at this point in the history
* Clear plugins & exports

* Clear data

* Remove variables

* Add paths to contracts

* Remove config paths

* Cache cache, log, temp

* Cleanup

* Add command to api

* Add clear all

* Fix case
  • Loading branch information
sebastijankuzner authored Mar 7, 2024
1 parent d2130e4 commit 1ffb2c7
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 6 deletions.
59 changes: 59 additions & 0 deletions packages/api/source/commands/env-paths-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// eslint-disable-next-line unicorn/prevent-abbreviations
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import { emptyDirSync, existsSync, readdirSync } from "fs-extra";
import Joi from "joi";
import { join } from "path";

@injectable()
export class Command extends Commands.Command {
public signature = "env:paths:clear";

public description = "Clear data on environment paths.";

public configure(): void {
this.definition.setFlag("plugins", "Clear installed plugins.", Joi.boolean());
this.definition.setFlag("data", "Clear data.", Joi.boolean());
this.definition.setFlag("config", "Clear config.", Joi.boolean());
this.definition.setFlag("cache", "Clear cache.", Joi.boolean());
this.definition.setFlag("log", "Clear log.", Joi.boolean());
this.definition.setFlag("temp", "Clear temp.", Joi.boolean());
this.definition.setFlag("all", "Clear all.", Joi.boolean());
}

public async execute(): Promise<void> {
if (this.hasFlag("data") || this.hasFlag("all")) {
await this.#clear("Data", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).data);
}

if (this.hasFlag("config") || this.hasFlag("all")) {
await this.#clear("Config", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).config);
}

if (this.hasFlag("cache") || this.hasFlag("all")) {
await this.#clear("Cache", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).cache);
}

if (this.hasFlag("log") || this.hasFlag("all")) {
await this.#clear("Log", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).log);
}
if (this.hasFlag("temp") || this.hasFlag("all")) {
await this.#clear("Temp", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).temp);
}

if (this.hasFlag("plugins")) {
await this.#clear(
"Plugins",
join(this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).data, "plugins"),
);
}
}

async #clear(name, path: string) {
if (existsSync(path) && readdirSync(path).length > 0) {
emptyDirSync(path);

this.components.log(`${name} path (${path}) has been cleared.`);
}
}
}
4 changes: 2 additions & 2 deletions packages/api/source/commands/env-paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line unicorn/prevent-abbreviations
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";

@injectable()
Expand All @@ -10,7 +10,7 @@ export class Command extends Commands.Command {

public async execute(): Promise<void> {
this.components.table(["Type", "Path"], (table) => {
for (const [type, path] of Object.entries(this.app.get<{}>(Identifiers.ApplicationPaths))) {
for (const [type, path] of Object.entries(this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths))) {
table.push([type, path]);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/source/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tmpdir } from "os";
// eslint-disable-next-line unicorn/import-style
import { resolve } from "path";

import { Paths } from "./env-paths";
import { Paths } from "./contracts";
import { Identifiers } from "./ioc";

export class Application {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/source/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { interfaces } from "@mainsail/container";
import { AnySchema } from "joi";
export { Paths } from "env-paths";

export type InputValue = any;
export type InputValues = Record<string, InputValue>;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/source/services/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { parse, stringify } from "envfile";
import { existsSync, readFileSync, writeFileSync } from "fs-extra";
import path from "path";

import { envPaths as environmentPaths, Paths } from "../env-paths";
import { Paths } from "../contracts";
import { envPaths as environmentPaths } from "../env-paths";
import { Identifiers } from "../ioc";

@injectable()
Expand Down
67 changes: 67 additions & 0 deletions packages/core/source/commands/env-paths-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// eslint-disable-next-line unicorn/prevent-abbreviations
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import { emptyDirSync, existsSync, readdirSync } from "fs-extra";
import Joi from "joi";
import { join } from "path";

@injectable()
export class Command extends Commands.Command {
public signature = "env:paths:clear";

public description = "Clear data on environment paths.";

public configure(): void {
this.definition.setFlag("state-export", "Clear state exports.", Joi.boolean());
this.definition.setFlag("plugins", "Clear installed plugins.", Joi.boolean());
this.definition.setFlag("data", "Clear data.", Joi.boolean());
this.definition.setFlag("config", "Clear config.", Joi.boolean());
this.definition.setFlag("cache", "Clear cache.", Joi.boolean());
this.definition.setFlag("log", "Clear log.", Joi.boolean());
this.definition.setFlag("temp", "Clear temp.", Joi.boolean());
this.definition.setFlag("all", "Clear all.", Joi.boolean());
}

public async execute(): Promise<void> {
if (this.hasFlag("data") || this.hasFlag("all")) {
await this.#clear("Data", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).data);
}

if (this.hasFlag("config") || this.hasFlag("all")) {
await this.#clear("Config", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).config);
}

if (this.hasFlag("cache") || this.hasFlag("all")) {
await this.#clear("Cache", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).cache);
}

if (this.hasFlag("log") || this.hasFlag("all")) {
await this.#clear("Log", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).log);
}
if (this.hasFlag("temp") || this.hasFlag("all")) {
await this.#clear("Temp", this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).temp);
}

if (this.hasFlag("state-export")) {
await this.#clear(
"State export",
join(this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).data, "state-export"),
);
}

if (this.hasFlag("plugins")) {
await this.#clear(
"Plugins",
join(this.app.get<Contracts.Paths>(Identifiers.ApplicationPaths).data, "plugins"),
);
}
}

async #clear(name, path: string) {
if (existsSync(path) && readdirSync(path).length > 0) {
emptyDirSync(path);

this.components.log(`${name} path (${path}) has been cleared.`);
}
}
}
4 changes: 2 additions & 2 deletions packages/core/source/commands/env-paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line unicorn/prevent-abbreviations
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";

@injectable()
Expand All @@ -10,7 +10,7 @@ export class Command extends Commands.Command {

public async execute(): Promise<void> {
this.components.table(["Type", "Path"], (table) => {
for (const [type, path] of Object.entries<{}>(this.app.get(Identifiers.ApplicationPaths))) {
for (const [type, path] of Object.entries(this.app.get<Contracts.Flags>(Identifiers.ApplicationPaths))) {
table.push([type, path]);
}
});
Expand Down

0 comments on commit 1ffb2c7

Please sign in to comment.