Skip to content

Commit

Permalink
chore(deps): bump chalk from 4.1.2 to 5.3.0
Browse files Browse the repository at this point in the history
Bumps [chalk](https://github.com/chalk/chalk) from 4.1.2 to 5.3.0.
- [Release notes](https://github.com/chalk/chalk/releases)
- [Commits](chalk/chalk@v4.1.2...v5.3.0)

---
updated-dependencies:
- dependency-name: chalk
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
  • Loading branch information
dependabot[bot] authored and neilime committed Aug 29, 2023
1 parent 8b1653d commit 368a640
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ version: 2
updates:
- package-ecosystem: npm
open-pull-requests-limit: 20
versioning-strategy: increase
versioning-strategy: widen
directory: "/"
schedule:
interval: weekly
day: friday
time: "04:00"
- package-ecosystem: github-actions
open-pull-requests-limit: 20
directory: "/"
schedule:
interval: weekly
Expand Down
50 changes: 22 additions & 28 deletions src/actions/AbstractActionWithAdapters.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { red } from "chalk";
import { inject, injectable } from "inversify";
import prompts from "prompts";

import container from "../container";
import { ConsoleService } from "../services/ConsoleService";
import { AdapterAction } from "./AdapterAction";
import { RealpathAction, RealpathActionOptions } from "./RealpathAction";
import { CliService } from "../services/CliService";

export type ActionWithAdaptersOptions = RealpathActionOptions;

Expand All @@ -18,7 +17,10 @@ export abstract class AbstractActionWithAdapters<
protected abstract name: string;
protected abstract adapterKey: string;

constructor(@inject(ConsoleService) private readonly consoleService: ConsoleService) {}
constructor(
@inject(ConsoleService) private readonly consoleService: ConsoleService,
@inject(CliService) private readonly cliService: CliService
) {}

getName(): string {
return this.name;
Expand Down Expand Up @@ -46,35 +48,27 @@ export abstract class AbstractActionWithAdapters<

let adapter: A | null = await this.detectAdapter(options.realpath);
if (adapter) {
const { override } = await prompts([
{
type: "confirm",
name: "override",
message: `${name} "${adapter.getName()}" is already added, ${red("override it?")}`,
},
]);
const override = await this.cliService.promptToContinue(
`"${adapter.getName()}" is already added`,
"override it?"
);

if (!override) {
return;
}
} else {
const answer = await prompts([
{
name: "adapter",
message: `Wich ${name} do you want to add?`,
type: "select",
choices: [
...this.getAdapters().map((adapter) => ({
title: adapter.getName(),
value: adapter,
})),
{
title: "None",
value: null,
},
],
},
]);
adapter = answer.adapter;
const choices = {
...this.getAdapters().reduce(
(choice, adapter) => {
choice[adapter.getName()] = adapter;
return choice;
},
{} as Record<string, A>
),
None: null,
};

adapter = await this.cliService.promptToChoose(`Wich ${name} do you want to add?`, choices);
}

if (!adapter) {
Expand Down
6 changes: 4 additions & 2 deletions src/actions/AbstractCommitableActionWithAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ActionWithAdaptersOptions,
} from "./AbstractActionWithAdapters";
import { AdapterAction } from "./AdapterAction";
import { CliService } from "../services/CliService";

export type CommitableActionWithAdaptersOptions = ActionWithAdaptersOptions;

Expand All @@ -17,9 +18,10 @@ export abstract class AbstractCommitableActionWithAdapters<
> extends AbstractActionWithAdapters<A, O> {
constructor(
@inject(GitService) private readonly gitService: GitService,
@inject(ConsoleService) consoleService: ConsoleService
@inject(ConsoleService) consoleService: ConsoleService,
@inject(CliService) cliService: CliService
) {
super(consoleService);
super(consoleService, cliService);
}

async run(options: CommitableActionWithAdaptersOptions): Promise<void> {
Expand Down
38 changes: 14 additions & 24 deletions src/actions/create-app/adapters/CreateAppAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { basename, dirname, join, relative, resolve } from "path";

import { red } from "chalk";
import { LazyServiceIdentifer, inject, injectable } from "inversify";
import prompts from "prompts";

import { CliService } from "../../../services/CliService";
import { ConsoleService } from "../../../services/ConsoleService";
Expand Down Expand Up @@ -147,13 +145,10 @@ export abstract class AbstractCreateAppAdapter
): Promise<boolean | undefined> {
if (this.fileService.dirExistsSync(realpath)) {
if (shouldPrompt) {
const { override } = await prompts([
{
type: "confirm",
name: "override",
message: `Directory "${realpath}" exists already, ${red("override it?")}`,
},
]);
const override = await this.cliService.promptToContinue(
`Directory "${realpath}" exists already`,
"what do you want to do?"
);

if (!override) {
return undefined;
Expand Down Expand Up @@ -246,21 +241,16 @@ export abstract class AbstractCreateAppAdapter
return availablePackageManagers[0];

default: {
// Prompts user to choose package manager
const result = await prompts([
{
name: "packageManager",
message: "Wich package manager do you want to use?",
type: "select",
choices: [
...availablePackageManagers.map((packageManager) => ({
title: packageManager,
value: packageManager,
})),
],
},
]);
return result.packageManager;
return await this.cliService.promptToChoose(
"Wich package manager do you want to use?",
availablePackageManagers.reduce(
(choices, packageManager) => {
choices[packageManager] = packageManager;
return choices;
},
{} as Record<string, PackageManagerType>
)
);
}
}
}
Expand Down
54 changes: 39 additions & 15 deletions src/services/CliService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawn } from "child_process";

import { bgGreenBright, bgRedBright, greenBright, grey, redBright } from "chalk";
import chalk from "chalk";
import { Change } from "diff";
import { inject, injectable } from "inversify";
import prompts from "prompts";
Expand Down Expand Up @@ -81,6 +81,34 @@ export class CliService {
});
}

async promptToContinue(message: string, question: string): Promise<boolean> {
const { shouldContinue } = await prompts([
{
type: "confirm",
name: "shouldContinue",
message: `${message}, ${chalk.red(question)}`,
},
]);

return shouldContinue;
}

async promptToChoose<Choice>(message: string, choices: Record<string, Choice>): Promise<Choice> {
const { choice } = await prompts([
{
name: "choice",
message,
type: "select",
choices: Object.entries(choices).map(([title, value]) => ({
title,
value,
})),
},
]);

return choice;
}

async promptOverwriteFileDiff(file: string, diff: Change[]): Promise<boolean> {
const hasDiff = diff.some((part) => part.added !== undefined || part.removed !== undefined);
if (!hasDiff) {
Expand All @@ -89,18 +117,14 @@ export class CliService {

const shouldPrompt = true;
while (shouldPrompt) {
const { action } = await prompts([
const action = await this.promptToChoose(
`File "${file}" exists already, what do you want to do?`,
{
type: "select",
name: "action",
message: `File "${file}" exists already, what do you want to do?`,
choices: [
{ title: "Show diff", value: "diff" },
{ title: "Overwrite file", value: "overwrite" },
{ title: "Keep original file", value: "cancel" },
],
},
]);
"Show diff": "diff",
"Overwrite file": "overwrite",
"Keep original file": "cancel",
}
);

if (action === "cancel") {
return false;
Expand All @@ -119,13 +143,13 @@ export class CliService {
let colorFunction: (value: string) => string;
switch (true) {
case !!part.added:
colorFunction = isSpaces ? bgGreenBright : greenBright;
colorFunction = isSpaces ? chalk.bgGreenBright : chalk.greenBright;
break;
case !!part.removed:
colorFunction = isSpaces ? bgRedBright : redBright;
colorFunction = isSpaces ? chalk.bgRedBright : chalk.redBright;
break;
default:
colorFunction = grey;
colorFunction = chalk.grey;
break;
}

Expand Down
71 changes: 71 additions & 0 deletions src/services/ConsoleService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ConsoleService } from './ConsoleService';
import chalk from 'chalk';

describe('ConsoleService', () => {
let consoleService: ConsoleService;

beforeEach(() => {
consoleService = new ConsoleService();
});

describe('info', () => {
it('should write an info message to stdout', () => {
const message = 'This is an info message';
const expectedOutput = `\n${chalk.yellow('→ ')}${message}\n`;
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation();

consoleService.info(message);

expect(writeSpy).toHaveBeenCalledWith(expectedOutput);
writeSpy.mockRestore();
});
});

describe('success', () => {
it('should write a success message to stdout', () => {
const message = 'This is a success message';
const expectedOutput = `${chalk.green('✓ ')}${message}\n`;
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation();

consoleService.success(message);

expect(writeSpy).toHaveBeenCalledWith(expectedOutput);
writeSpy.mockRestore();
});
});

describe('error', () => {
it('should write a string error message to stderr', () => {
const errorMessage = 'This is a string error message';
const expectedOutput = `\n${chalk.red(`⚠ ${errorMessage}`)}\n`;
const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation();

consoleService.error(errorMessage);

expect(writeSpy).toHaveBeenCalledWith(expectedOutput);
writeSpy.mockRestore();
});

it('should write an Error object to stderr', () => {
const error = new Error('This is an error message');
const expectedOutput = `\n${chalk.red(`⚠ [${error.name}] ${error.message}`)}\n${error.stack}\n`;
const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation();

consoleService.error(error);

expect(writeSpy).toHaveBeenCalledWith(expectedOutput);
writeSpy.mockRestore();
});

it('should write a JSON error message to stderr', () => {
const error = { message: 'This is a JSON error message' };
const expectedOutput = `\n${chalk.red(`⚠ `)}${JSON.stringify(error)}\n`;
const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation();

consoleService.error(error);

expect(writeSpy).toHaveBeenCalledWith(expectedOutput);
writeSpy.mockRestore();
});
});
});
14 changes: 8 additions & 6 deletions src/services/ConsoleService.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { green, red, yellow } from "chalk";
import chalk from "chalk";
import { injectable } from "inversify";

@injectable()
export class ConsoleService {
info(message: string): void {
process.stdout.write(`\n${yellow("→ ")}${message}\n`);
process.stdout.write(`\n${chalk.yellow("→ ")}${message}\n`);
}

success(message: string): void {
process.stdout.write(`${green("✓ ") + message}\n`);
process.stdout.write(`${chalk.green("✓ ") + message}\n`);
}

error(error: unknown): void {
if (typeof error === "string") {
process.stderr.write(`\n${red(`⚠ ${error}`)}\n`);
process.stderr.write(`\n${chalk.red(`⚠ ${error}`)}\n`);
return;
}
if (error instanceof Error) {
process.stderr.write(`\n${red(`⚠ [${error.name}] ${error.message}`)}\n${error.stack}\n`);
process.stderr.write(
`\n${chalk.red(`⚠ [${error.name}] ${error.message}`)}\n${error.stack}\n`
);
return;
}

process.stderr.write(`\n${red(`⚠ `)}${JSON.stringify(error)}\n`);
process.stderr.write(`\n${chalk.red(`⚠ `)}${JSON.stringify(error)}\n`);
}
}
11 changes: 11 additions & 0 deletions src/services/package-manager/PackageManagerService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe("packageManagerService", () => {
});
});

describe('getPackageManagerCmd', () => {
it('should return the correct package manager command for a given directory', async () => {
const packageName = "test-package";
mockYarnDir({ "package.json": JSON.stringify({ name: packageName }) });
mockYarnCmd();

const cmd = await service.getPackageManagerCmd(mockDirPath);
expect(cmd).toEqual('yarn');
});
});

describe("getPackageName", () => {
it("should return package name", async () => {
const packageName = "test-package";
Expand Down

0 comments on commit 368a640

Please sign in to comment.