Skip to content

Commit

Permalink
Merge pull request #335 from tsedio/feat-detach-logger-from-listr
Browse files Browse the repository at this point in the history
Feat detach logger from listr
  • Loading branch information
Romakita committed Jul 7, 2023
2 parents f83db81 + cb4e53b commit 67121ef
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 48 deletions.
6 changes: 3 additions & 3 deletions packages/cli-core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 71.19,
branches: 73.06,
statements: 71.23,
branches: 72.81,
functions: 48.9,
lines: 71.19
lines: 71.23
}
},
};
6 changes: 5 additions & 1 deletion packages/cli-core/src/CliCore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {CliCore} from "./CliCore";
import {CliService} from "./services";
import {normalizePath} from "@tsed/normalize-path";
import execa from "execa";

jest.mock("./utils/loadPlugins");

jest.mock("execa");
describe("CliCore", () => {
beforeEach(() => {
(execa as any as jest.Mock).mockReturnValue({});
});
describe("getProjectRoot()", () => {
it("should return project root (-r)", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-core/src/interfaces/CommandMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ export interface CommandMetadata extends CommandParameters {
enableFeatures: string[];

disableReadUpPkg: boolean;

bindLogger: boolean;
}
1 change: 1 addition & 0 deletions packages/cli-core/src/interfaces/Tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ListrContext, ListrOptions, ListrTask} from "listr2";
export interface TaskOptions<Ctx = ListrContext> extends ListrOptions<Ctx> {
concurrent?: boolean | number;
verbose?: boolean;
bindLogger?: boolean;
}

export type Task = ListrTask<any, any>;
Expand Down
18 changes: 18 additions & 0 deletions packages/cli-core/src/services/CliHttpClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import axios from "axios";
import {DITest} from "@tsed/di";
import {CliHttpClient} from "./CliHttpClient";
import {CliProxyAgent} from "./CliProxyAgent";

jest.mock("axios");
describe("CliHttpClient", () => {
beforeEach(() => DITest.create());
afterEach(() => DITest.reset());

describe("$afterInit()", () => {
it("should call $afterInit method", async () => {
const cliProxyAgent = {
resolveProxySettings: jest.fn()
};
const client = await DITest.invoke<CliHttpClient>(CliHttpClient, [
{
token: CliProxyAgent,
use: cliProxyAgent
}
]);

await client.$afterInit();

expect(cliProxyAgent.resolveProxySettings).toHaveBeenCalled();
});
});
describe("head()", () => {
it("should call head method", async () => {
// GIVEN
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-core/src/services/CliHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CliHttpClient extends CliHttpLogClient {
return stringify(cleanObject(params));
}

async $onInit() {
async $afterInit() {
await this.cliProxyAgent.resolveProxySettings();
}

Expand Down
4 changes: 3 additions & 1 deletion packages/cli-core/src/services/CliPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class CliPlugins {
const tasks = plugins.map((plugin) => {
return {
title: `Run plugin '${chalk.cyan(plugin)}'`,
task: () => this.cliHooks.emit(CommandStoreKeys.ADD, plugin, ctx)
task: () => {
return this.cliHooks.emit(CommandStoreKeys.ADD, plugin, ctx);
}
};
});

Expand Down
6 changes: 3 additions & 3 deletions packages/cli-core/src/services/CliProxyAgent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("CliPlugins", () => {

expect(cliProxyAgent.proxySettings).toEqual({
url: "https://login:password@host:3000",
strictSsl: false
strictSsl: true
});
});
it("should get proxy url from (https-proxy)", async () => {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("CliPlugins", () => {

expect(cliProxyAgent.proxySettings).toEqual({
url: "https://login:password@host:3000",
strictSsl: false
strictSsl: true
});
});
it("should get proxy url from (https-proxy) without credentials", async () => {
Expand Down Expand Up @@ -176,7 +176,7 @@ describe("CliPlugins", () => {

expect(cliProxyAgent.proxySettings).toEqual({
url: "https://host:3000",
strictSsl: false
strictSsl: true
});
});
});
Expand Down
28 changes: 6 additions & 22 deletions packages/cli-core/src/services/CliProxyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@ import {Configuration, Inject, Injectable, Value} from "@tsed/di";
import {CliExeca} from "./CliExeca";
import {camelCase} from "change-case";
import {URL} from "url";
import {coerce} from "../utils/coerce";

export interface CliProxySettings {
url: string;
strictSsl: boolean;
}

function cast(value: any) {
if (["undefined"].includes(value)) {
return undefined;
}
if (["null"].includes(value)) {
return null;
}

if (["false"].includes(value)) {
return false;
}

if (["true"].includes(value)) {
return false;
}

return value;
}

@Injectable()
@Configuration({
proxy: {
Expand Down Expand Up @@ -72,7 +54,9 @@ export class CliProxyAgent {
}

async resolveProxySettings(): Promise<void> {
if (this.hasProxy()) {
const hasProxy = this.hasProxy();

if (hasProxy) {
return;
}

Expand All @@ -83,13 +67,13 @@ export class CliProxyAgent {
this.cliExeca.getAsync("npm", ["config", "get", "strict-ssl"])
]);

const [proxy, httpProxy, httpsProxy, strictSsl] = result.map(cast);
const [proxy, httpProxy, httpsProxy, strictSsl] = result.map(coerce);
const url = httpsProxy || httpProxy || proxy;

if (url) {
this.proxySettings = {
url,
strictSsl: cast(strictSsl) !== false
strictSsl: coerce(strictSsl) !== false
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-core/src/services/CliService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ export class CliService {
this.injector.logger.level = "info";
}

data.bindLogger = $ctx.get("command").bindLogger;

$ctx.set("data", data);

return data;
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-core/src/utils/coerce.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {coerce} from "./coerce";

describe("coerce", () => {
it("should coerce given value", () => {
expect(coerce("undefined")).toBeUndefined();
expect(coerce("null")).toBeNull();
expect(coerce("false")).toBe(false);
expect(coerce("true")).toBe(true);
expect(coerce("")).toBe("");
});
});
18 changes: 18 additions & 0 deletions packages/cli-core/src/utils/coerce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function coerce(value: any) {
if (["undefined"].includes(value)) {
return undefined;
}
if (["null"].includes(value)) {
return null;
}

if (["false"].includes(value)) {
return false;
}

if (["true"].includes(value)) {
return true;
}

return value;
}
13 changes: 7 additions & 6 deletions packages/cli-core/src/utils/createTasksRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ class CustomLogger extends Logger {
}
}

function getOptions(ctx: TaskOptions): any {
function getOptions({bindLogger = true, ...ctx}: TaskOptions): any {
const useRawRenderer = !(!ctx.verbose && !process.env.CI);
const rendererOptions = useRawRenderer
? {
logger: CustomLogger
}
: {};
const rendererOptions =
useRawRenderer && bindLogger
? {
logger: CustomLogger
}
: {};
return {
...ctx,
rendererSilent: process.env.NODE_ENV === "test",
Expand Down
21 changes: 21 additions & 0 deletions packages/cli-core/src/utils/getCommandMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import {getCommandMetadata} from "./getCommandMetadata";
})
class TestCmd {}

@Command({
name: "name",
description: "description",
alias: "g",
bindLogger: false
})
class TestCmd2 {}

describe("getCommandMetadata", () => {
it("should return command metadata", () => {
expect(getCommandMetadata(TestCmd)).toEqual({
Expand All @@ -18,6 +26,19 @@ describe("getCommandMetadata", () => {
alias: "g",
disableReadUpPkg: false,
enableFeatures: [],
bindLogger: true,
options: {}
});

expect(getCommandMetadata(TestCmd2)).toEqual({
args: {},
allowUnknownOption: false,
description: "description",
name: "name",
alias: "g",
disableReadUpPkg: false,
enableFeatures: [],
bindLogger: false,
options: {}
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-core/src/utils/getCommandMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function getCommandMetadata(token: Type<any>): CommandMetadata {
options = {},
enableFeatures,
disableReadUpPkg,
bindLogger = true,
...opts
} = Store.from(token)?.get(CommandStoreKeys.COMMAND) as CommandParameters;

Expand All @@ -25,6 +26,7 @@ export function getCommandMetadata(token: Type<any>): CommandMetadata {
allowUnknownOption: !!allowUnknownOption,
enableFeatures: enableFeatures || [],
disableReadUpPkg: !!disableReadUpPkg,
bindLogger,
...opts
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {CliCore, Command, CommandProvider, Tasks} from "../../src";
import execa from "execa";

jest.mock("execa")

describe("Command", () => {
beforeEach(() => {
(execa as any as jest.Mock).mockReturnValue({})
})
it("should exec a command with expected parsed argument", async () => {
@Command({
name: "test",
Expand Down Expand Up @@ -33,6 +39,7 @@ describe("Command", () => {
})

expect(TestCommand.prototype.$exec).toHaveBeenCalledWith({
"bindLogger": true,
"command": "subcmd",
"rawArgs": [],
"rootDir": undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = {
coverageThreshold: {
global: {
statements: 91.69,
branches: 78.54,
functions: 75.94,
branches: 78.62,
functions: 76.25,
lines: 91.69
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"start:version": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts --version",
"start:init:help": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts init -h",
"start:init:test": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default --features=oidc --arch=default --convention=conv_default --platform=express --skip-prompt .",
"start:init:test:jest": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default --features=jest --arch=default --convention=conv_default --platform=express --skip-prompt .",
"start:init:run": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default",
"start:init:params": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default --skip-prompt --features swagger,jest,lintstaged",
"start:init:run:name": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init awesome --verbose",
Expand Down Expand Up @@ -88,4 +89,4 @@
"jest": "29.5.0"
},
"peerDependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/InitCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class InitCmd implements CommandProvider {
},
{
title: "Install plugins dependencies",
task: createSubTasks(this.cliPlugins.addPluginsDependencies(ctx), {...ctx, concurrent: false})
task: createSubTasks(() => this.cliPlugins.addPluginsDependencies(ctx), {...ctx, concurrent: false})
}
],
ctx
Expand Down
Loading

0 comments on commit 67121ef

Please sign in to comment.