Skip to content

Commit

Permalink
refactor: remove token and network from application and paths (#432)
Browse files Browse the repository at this point in the history
* Remove token and network from env-paths

* No suffix

* Fix base paths

* Discover config

* Remove token, network, dirPrefix, namespace,

* Remove discover network & token

* Skip network & token detection

* Remove unused discoverers

* Remove token from commands

* Remove token from api commands

* Remove tokens from scripts

* Fix cli tests

* Remove token from config

* Config cli test

* Config-database test

* Env paths test

* Plugin install test

* Plugin remove test

* Plugin update

* Reinstall test

* Top test

* Updater test

* Cli test

* Fix api tests

* Init application paths

* Inject app name into Environment

* Plugin manager tests

* Fix kernel tests

* Fix transactions tests

* Cleanup

* Fix tests

* Fix env tests

* Fix tests
  • Loading branch information
sebastijankuzner authored Feb 16, 2024
1 parent 28d3700 commit 15eac3a
Show file tree
Hide file tree
Showing 93 changed files with 180 additions and 1,199 deletions.
4 changes: 1 addition & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"/distribution"
],
"scripts": {
"api:testnet": "cross-env-shell CORE_PATH_CONFIG=./bin/config/testnet pnpm run mainsail-api api:run -- --token=ark --network=testnet",
"api:testnet": "cross-env-shell CORE_PATH_CONFIG=./bin/config/testnet pnpm run mainsail-api api:run",
"build": "pnpm run clean && tsc",
"build:watch": "pnpm run clean && tsc -w",
"clean": "del distribution",
Expand All @@ -39,7 +39,6 @@
"boxen": "4.2.0",
"cross-env": "7.0.3",
"dayjs": "1.11.10",
"env-paths": "2.2.1",
"envfile": "7.0.0",
"execa": "5.1.1",
"fs-extra": "11.2.0",
Expand All @@ -51,7 +50,6 @@
},
"devDependencies": {
"@types/boxen": "3.0.1",
"@types/env-paths": "2.1.0",
"@types/execa": "2.0.0",
"@types/fs-extra": "11.0.4",
"@types/prompts": "2.4.9",
Expand Down
50 changes: 0 additions & 50 deletions packages/api/source/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,54 +59,4 @@ describe("CLI", ({ beforeEach, it, assert, stub }) => {

mockExit.neverCalled();
});

it("should load CLI plugins from folder using provided token and network", async () => {
const spyOnList = stub(Services.PluginManager.prototype, "list").resolvedValue([
{
path: "test/path",
},
]);
const spyOnFrom = stub(Commands.DiscoverCommands.prototype, "from").returnValueOnce({ plugin: {} });
const token = "dummy";
const network = "testnet";

const cli = new CommandLineInterface(["help", `--token=${token}`, `--network=${network}`]);

await assert.resolves(() => cli.execute("distribution"));
spyOnList.calledWith(token, network);
spyOnFrom.calledOnce();
});

it("should load CLI plugins from folder using CORE_PATH_CONFIG", async () => {
const spyOnList = stub(Services.PluginManager.prototype, "list").resolvedValue([]);
process.env.CORE_PATH_CONFIG = join(__dirname, "../test/config");

const cli = new CommandLineInterface(["help"]);

await assert.resolves(() => cli.execute("distribution"));

spyOnList.calledWith("dummyToken", "testnet");
delete process.env.CORE_PATH_CONFIG;
});

it("should load CLI plugins from folder using detected network folder", async () => {
const spyOnList = stub(Services.PluginManager.prototype, "list").resolvedValue([]);
const spyOnDiscoverNetwork = stub(Commands.DiscoverNetwork.prototype, "discover").resolvedValue("testnet");

const cli = new CommandLineInterface(["help"]);

await assert.resolves(() => cli.execute("distribution"));
spyOnList.calledWith("ark", "testnet");
spyOnDiscoverNetwork.calledWith(envPaths("ark", { suffix: "core" }).config);
});

it("should not load CLI plugins if network is not provided", async () => {
const spyOnList = stub(Services.PluginManager.prototype, "list").resolvedValue([]);
const token = "dummy";

const cli = new CommandLineInterface(["help", `--token=${token}`]);

await assert.resolves(() => cli.execute("distribution"));
spyOnList.neverCalled();
});
});
44 changes: 4 additions & 40 deletions packages/api/source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "@mainsail/cli";
import { Container, injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import envPaths from "env-paths";
import { existsSync } from "fs-extra";
import { platform } from "os";
import { join, resolve } from "path";
Expand Down Expand Up @@ -100,51 +99,16 @@ export class CommandLineInterface {
require("module").Module._initPaths();
}

async #detectNetworkAndToken(flags: any): Promise<{ token: string; network?: string }> {
const temporaryFlags = {
token: "ark",
...flags,
};

if (temporaryFlags.token && temporaryFlags.network) {
return temporaryFlags;
}

const config = await this.#app.resolve(Commands.DiscoverConfig).discover(temporaryFlags.token);
if (config) {
return {
network: config.network,
token: config.token,
};
}

try {
temporaryFlags.network = await this.#app.resolve(Commands.DiscoverNetwork).discover(
envPaths(temporaryFlags.token, {
suffix: "core",
}).config,
);
} catch {}

return temporaryFlags;
}

async #discoverCommands(dirname: string, flags: any): Promise<CliContracts.CommandList> {
const commandsDiscoverer = this.#app.resolve(Commands.DiscoverCommands);
const commands: CliContracts.CommandList = commandsDiscoverer.within(resolve(dirname, "./commands"));

const temporaryFlags = await this.#detectNetworkAndToken(flags);
const plugins = await this.#app.get<CliContracts.PluginManager>(Identifiers.PluginManager).list();

if (temporaryFlags.network) {
const plugins = await this.#app
.get<CliContracts.PluginManager>(Identifiers.PluginManager)
.list(temporaryFlags.token, temporaryFlags.network, this.#app.get(Identifiers.Application.Name));
const commandsFromPlugins = commandsDiscoverer.from(plugins.map((plugin) => plugin.path));

const commandsFromPlugins = commandsDiscoverer.from(plugins.map((plugin) => plugin.path));

for (const [key, value] of Object.entries(commandsFromPlugins)) {
commands[key] = value;
}
for (const [key, value] of Object.entries(commandsFromPlugins)) {
commands[key] = value;
}

this.#app.bind(Identifiers.Commands).toConstantValue(commands);
Expand Down
2 changes: 0 additions & 2 deletions packages/api/source/commands/api-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export class Command extends Commands.Command {

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string().required())
.setFlag("network", "The name of the network.", Joi.string().required())
.setFlag("env", "", Joi.string().default("production"))
.setFlag("skipPrompts", "Skip prompts.", Joi.boolean().default(false));
}
Expand Down
4 changes: 1 addition & 3 deletions packages/api/source/commands/api-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export class Command extends Commands.Command {

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string().required())
.setFlag("network", "The name of the network.", Joi.string().required())
.setFlag("env", "", Joi.string().default("production"))
.setFlag("daemon", "Start the API process as a daemon.", Joi.boolean().default(true))
.setFlag("skipPrompts", "Skip prompts.", Joi.boolean().default(false));
Expand All @@ -21,7 +19,7 @@ export class Command extends Commands.Command {
public async execute(): Promise<void> {
const flags: Contracts.AnyObject = { ...this.getFlags() };

this.actions.abortRunningProcess(`${flags.token}-api`);
this.actions.abortRunningProcess(`mainsail-api`);

await this.actions.daemonizeProcess(
{
Expand Down
10 changes: 0 additions & 10 deletions packages/api/source/commands/config-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ describe<{
context.config = context.cli.app.get<Contracts.Config>(Identifiers.Config);
});

it("should change the token", async ({ cli, config }) => {
await cli.execute(Command);

assert.equal(config.get("token"), "ark");

await cli.withFlags({ token: "btc" }).execute(Command);

assert.equal(config.get("token"), "btc");
});

it("should not set config token if no token is passed to command", async ({ config }) => {
const cli = new Console(false);
stub(cli.app.get(Identifiers.Environment), "getPaths");
Expand Down
20 changes: 6 additions & 14 deletions packages/api/source/commands/config-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@ export class Command extends Commands.Command {

public description = "Update the CLI configuration.";

public requiresNetwork = false;

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string())
.setFlag(
"channel",
"The NPM registry channel that should be used.",
Joi.string().valid(...Constants.Channels),
);
this.definition.setFlag(
"channel",
"The NPM registry channel that should be used.",
Joi.string().valid(...Constants.Channels),
);
}

public async execute(): Promise<void> {
if (this.hasFlag("token")) {
this.config.set("token", this.getFlag("token"));
}

if (this.hasFlag("channel")) {
const newChannel: string = this.getFlag("channel");
const oldChannel: string = this.config.get("channel");
Expand All @@ -49,7 +41,7 @@ export class Command extends Commands.Command {

spinner.succeed();

await this.actions.restartRunningProcessWithPrompt(`${this.getFlag("token")}-api`);
await this.actions.restartRunningProcessWithPrompt(`mainsail-api`);
}
}
}
19 changes: 9 additions & 10 deletions packages/api/source/commands/config-database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ describe<{
beforeEach((context) => {
process.env.CORE_PATH_CONFIG = dirSync().name;

context.envFile = `${process.env.CORE_PATH_CONFIG}/mainsail-api/.env`;
context.envFile = `${process.env.CORE_PATH_CONFIG}/mainsail/.env`;

context.cli = new Console();
context.cli.app.rebind(Identifiers.Application.Name).toConstantValue("mainsail-api");
});

afterAll(() => setGracefulCleanup());
Expand Down Expand Up @@ -73,11 +72,11 @@ describe<{

spyOnUpdateVariables.calledOnce();
spyOnUpdateVariables.calledWith(envFile, {
CORE_DB_DATABASE: "ark_testnet",
CORE_DB_DATABASE: "mainsail-db",
CORE_DB_HOST: "dummy",
CORE_DB_PASSWORD: "password",
CORE_DB_PORT: 5432,
CORE_DB_USERNAME: "ark",
CORE_DB_USERNAME: "mainsail",
});
});

Expand All @@ -89,11 +88,11 @@ describe<{

spyOnUpdateVariables.calledOnce();
spyOnUpdateVariables.calledWith(envFile, {
CORE_DB_DATABASE: "ark_testnet",
CORE_DB_DATABASE: "mainsail-db",
CORE_DB_HOST: "localhost",
CORE_DB_PASSWORD: "password",
CORE_DB_PORT: 5000,
CORE_DB_USERNAME: "ark",
CORE_DB_USERNAME: "mainsail",
});
});

Expand All @@ -109,7 +108,7 @@ describe<{
CORE_DB_HOST: "localhost",
CORE_DB_PASSWORD: "password",
CORE_DB_PORT: 5432,
CORE_DB_USERNAME: "ark",
CORE_DB_USERNAME: "mainsail",
});
});

Expand All @@ -121,7 +120,7 @@ describe<{

spyOnUpdateVariables.calledOnce();
spyOnUpdateVariables.calledWith(envFile, {
CORE_DB_DATABASE: "ark_testnet",
CORE_DB_DATABASE: "mainsail-db",
CORE_DB_HOST: "localhost",
CORE_DB_PASSWORD: "password",
CORE_DB_PORT: 5432,
Expand All @@ -137,11 +136,11 @@ describe<{

spyOnUpdateVariables.calledOnce();
spyOnUpdateVariables.calledWith(envFile, {
CORE_DB_DATABASE: "ark_testnet",
CORE_DB_DATABASE: "mainsail-db",
CORE_DB_HOST: "localhost",
CORE_DB_PASSWORD: "dummy",
CORE_DB_PORT: 5432,
CORE_DB_USERNAME: "ark",
CORE_DB_USERNAME: "mainsail",
});
});

Expand Down
6 changes: 2 additions & 4 deletions packages/api/source/commands/config-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export class Command extends Commands.Command {

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string())
.setFlag("network", "The name of the network.", Joi.string())
.setFlag("host", "The host address of the database.", Joi.string())
.setFlag("port", "The port of the database.", Joi.number())
.setFlag("database", "The name of the database.", Joi.string())
Expand Down Expand Up @@ -50,13 +48,13 @@ export class Command extends Commands.Command {
value < 1 || value > 65_535 ? `The port must be in the range of 1-65535.` : true,
},
{
initial: `${this.getFlag("token")}_${this.getFlag("network")}`,
initial: `mainsail-db`,
message: "What database do you want to use?",
name: "database",
type: "text",
},
{
initial: this.getFlag("token"),
initial: `mainsail`,
message: "What username do you want to use?",
name: "username",
type: "text",
Expand Down
17 changes: 6 additions & 11 deletions packages/api/source/commands/config-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export class Command extends Commands.Command {

public description = "Publish the configuration.";

public requiresNetwork = false;

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string().required())
.setFlag("network", "The name of the network.", Joi.string().required())
.setFlag("reset", "Using the --reset flag will overwrite existing configuration.", Joi.boolean());
this.definition.setFlag(
"reset",
"Using the --reset flag will overwrite existing configuration.",
Joi.boolean(),
);
}

public async execute(): Promise<void> {
Expand Down Expand Up @@ -53,11 +52,7 @@ export class Command extends Commands.Command {
}

async #performPublishment(flags: Contracts.AnyObject): Promise<void> {
this.app
.rebind(Identifiers.ApplicationPaths)
.toConstantValue(
this.environment.getPaths(flags.token, flags.network, this.app.get(Identifiers.Application.Name)),
);
this.app.rebind(Identifiers.ApplicationPaths).toConstantValue(this.environment.getPaths());

const configDestination = this.app.getCorePath("config");
const configSource = resolve(
Expand Down
Loading

0 comments on commit 15eac3a

Please sign in to comment.