Skip to content

Commit

Permalink
fix(api): broken entrypoint (#481)
Browse files Browse the repository at this point in the history
* fix api entrypoint

* fix more commands
  • Loading branch information
oXtxNt9U authored Mar 14, 2024
1 parent e4fc997 commit 4fe8a61
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
10 changes: 5 additions & 5 deletions packages/api/bin/run
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const child_process = require("child_process");
import child_process from "child_process";
import { CommandLineInterface } from "../distribution/index.js";

const jemallocPath = child_process
.spawnSync(
Expand All @@ -22,7 +23,7 @@ if (jemallocPath) {
exitCode = error.status;
}
process.exitCode = exitCode;
return;
process.exit();
}
} else {
console.error(
Expand All @@ -32,9 +33,8 @@ if (jemallocPath) {

const main = async () => {
try {
const CommandLineInterface = new (require("../distribution").CommandLineInterface)(process.argv.slice(2));

await CommandLineInterface.execute();
const cmd = new CommandLineInterface(process.argv.slice(2));
await cmd.execute();
} catch (error) {
if (error.name !== "FatalException") {
console.error(error);
Expand Down
15 changes: 13 additions & 2 deletions packages/api/source/commands/api-run.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Commands, Contracts, Utils } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import { Utils as AppUtils } from "@mainsail/kernel";
import { readJSONSync } from "fs-extra/esm";
import Joi from "joi";
import { resolve } from "path";
import path from "path";
import { URL } from "url";

@injectable()
export class Command extends Commands.Command {
Expand All @@ -17,7 +19,16 @@ export class Command extends Commands.Command {
}

public async execute(): Promise<void> {
const { name } = require(resolve(__dirname, "../../package.json"));
const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

const { name } = readJSONSync(path.resolve(dirname, "../../package.json"));
AppUtils.assert.defined<string>(name);

const flags: Contracts.AnyObject = {
Expand Down
11 changes: 10 additions & 1 deletion packages/api/source/commands/api-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ export class Command extends Commands.Command {

this.actions.abortRunningProcess(`mainsail-api`);

const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

await this.actions.daemonizeProcess(
{
args: `api:run ${Utils.Flags.castFlagsToString(flags, ["daemon"])}`,
name: `mainsail-api`,
script: resolve(__dirname, "../../bin/run"),
script: resolve(dirname, "../../bin/run"),
},
flags,
);
Expand Down
11 changes: 10 additions & 1 deletion packages/api/source/commands/config-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ export class Command extends Commands.Command {
async #performPublishment(flags: Contracts.AnyObject): Promise<void> {
this.app.rebind(Identifiers.ApplicationPaths).toConstantValue(this.environment.getPaths());

const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

const configDestination = this.app.getCorePath("config");
const configSource = resolve(__dirname, `../../bin/config/${this.app.get(Identifiers.Application.Name)}`);
const configSource = resolve(dirname, `../../bin/config/${this.app.get<string>(Identifiers.Application.Name)}`);

await this.components.taskList([
{
Expand Down
11 changes: 10 additions & 1 deletion packages/configuration-generator/source/generators/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export class AppGenerator {
generateDefault(packageName = "core"): Contracts.Types.JsonObject {
packageName = packageName.replace("@mainsail/", "");

return readJSONSync(resolve(__dirname, `../../../${packageName}/bin/config/testnet/core/app.json`));
const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

return readJSONSync(resolve(dirname, `../../../${packageName}/bin/config/testnet/core/app.json`));
}

generate(options: Contracts.NetworkGenerator.InternalOptions): Contracts.Types.JsonObject {
Expand Down
13 changes: 11 additions & 2 deletions packages/core/source/commands/config-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ export class Command extends Commands.Command {
async #performPublishment(flags: Contracts.AnyObject): Promise<void> {
this.app.rebind(Identifiers.ApplicationPaths).toConstantValue(this.environment.getPaths());

const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

const configDestination = this.app.getCorePath("config");
const configSource = resolve(
__dirname,
`../../bin/config/${flags.network}/${this.app.get(Identifiers.Application.Name)}`,
dirname,
`../../bin/config/${flags.network}/${this.app.get<string>(Identifiers.Application.Name)}`,
);

await this.components.taskList([
Expand Down
11 changes: 10 additions & 1 deletion packages/core/source/commands/core-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ export class Command extends Commands.Command {
public async execute(): Promise<void> {
const flags: Contracts.AnyObject = { ...this.getFlags() };

const dirname = (() => {
try {
return new URL(".", import.meta.url).pathname;
} catch {
// eslint-disable-next-line unicorn/prefer-module
return __dirname;
}
})();

this.actions.abortRunningProcess(`mainsail`);

await this.actions.daemonizeProcess(
{
args: `core:run ${Utils.Flags.castFlagsToString(flags, ["daemon"])}`,
name: `mainsail`,
script: resolve(__dirname, "../../bin/run"),
script: resolve(dirname, "../../bin/run"),
},
flags,
);
Expand Down

0 comments on commit 4fe8a61

Please sign in to comment.