Skip to content

Commit

Permalink
Replaced chalk by ansis to keep Pongo compatible with CommonJS
Browse files Browse the repository at this point in the history
It seems that Chalk from v5 is ESModules-only
  • Loading branch information
oskardudycz committed Dec 3, 2024
1 parent 7e86922 commit 11880df
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 46 deletions.
41 changes: 19 additions & 22 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/pongo-core",
"version": "0.16.3",
"version": "0.16.4-alpha.1",
"description": "Pongo - Mongo with strong consistency on top of Postgres",
"type": "module",
"engines": {
Expand Down Expand Up @@ -88,11 +88,11 @@
"vitepress": "^1.3.3"
},
"peerDependencies": {
"cli-table3": "^0.6.5",
"commander": "^12.1.0",
"pg": "^8.12.0",
"pg-connection-string": "^2.6.4",
"chalk": "^5.3.0",
"cli-table3": "^0.6.5",
"commander": "^12.1.0"
"ansis": "^3.3.2"
},
"dependencies": {
"@types/benchmark": "^2.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/dumbo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/dumbo",
"version": "0.12.0",
"version": "0.12.1-alpha.1",
"description": "Dumbo - tools for dealing with PostgreSQL",
"type": "module",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions src/packages/dumbo/src/core/tracing/printing/color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ansis from 'ansis';

let enableColors = true;

export const color = {
set level(value: 0 | 1) {
enableColors = value === 1;
},
hex: (value: string) => (text: string) =>
enableColors ? ansis.hex(value)(text) : text,
red: (value: string) =>
enableColors ? ansis.red(value) : (text: string) => text,
green: (value: string) =>
enableColors ? ansis.green(value) : (text: string) => text,
blue: (value: string) =>
enableColors ? ansis.blue(value) : (text: string) => text,
yellow: (value: string) =>
enableColors ? ansis.yellow(value) : (text: string) => text,
};

export default color;
1 change: 1 addition & 0 deletions src/packages/dumbo/src/core/tracing/printing/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './color';
export * from './pretty';
2 changes: 1 addition & 1 deletion src/packages/dumbo/src/core/tracing/printing/pretty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from 'chalk';
import chalk from './color';

const TWO_SPACES = ' ';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import chalk from 'chalk';
import { describe, it } from 'node:test';
import chalk from './color';
import { prettyJson } from './pretty';

void describe('prettyPrintJson', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/packages/pongo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/pongo",
"version": "0.16.3",
"version": "0.16.4-alpha.1",
"description": "Pongo - Mongo with strong consistency on top of Postgres",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -87,13 +87,13 @@
"pongo": "./dist/cli.js"
},
"peerDependencies": {
"@event-driven-io/dumbo": "0.12.0",
"@event-driven-io/dumbo": "0.12.1-alpha.1",
"@types/mongodb": "^4.0.7",
"@types/pg": "^8.11.6",
"@types/uuid": "^10.0.0",
"pg": "^8.12.0",
"uuid": "^10.0.0",
"chalk": "^5.3.0",
"ansis": "^3.3.2",
"cli-table3": "^0.6.5",
"commander": "^12.1.0",
"pg-connection-string": "^2.6.4"
Expand Down
28 changes: 14 additions & 14 deletions src/packages/pongo/src/commandLine/shell.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
checkConnection,
color,
LogLevel,
LogStyle,
prettyJson,
SQL,
type MigrationStyle,
} from '@event-driven-io/dumbo';
import chalk from 'chalk';
import Table from 'cli-table3';
import { Command } from 'commander';
import repl from 'node:repl';
Expand Down Expand Up @@ -52,7 +52,7 @@ const printOutput = (obj: any): string =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const displayResultsAsTable = (results: any[]): string => {
if (results.length === 0) {
return chalk.yellow('No documents found.');
return color.yellow('No documents found.');
}

const columnNames = results
Expand All @@ -66,7 +66,7 @@ const displayResultsAsTable = (results: any[]): string => {
const columnWidths = calculateColumnWidths(results, columnNames);

const table = new Table({
head: columnNames.map((col) => chalk.cyan(col)),
head: columnNames.map((col) => color.cyan(col)),
colWidths: columnWidths,
});

Expand Down Expand Up @@ -124,10 +124,10 @@ const startRepl = async (options: {
setLogLevel(process.env.DUMBO_LOG_LEVEL ?? options.logging.logLevel);
setLogStyle(process.env.DUMBO_LOG_STYLE ?? options.logging.logStyle);

console.log(chalk.green('Starting Pongo Shell (version: 0.16.3)'));
console.log(color.green('Starting Pongo Shell (version: 0.16.4-alpha.1)'));

if (options.logging.printOptions) {
console.log(chalk.green('With Options:'));
console.log(color.green('With Options:'));
console.log(prettyJson(options));
}

Expand All @@ -138,7 +138,7 @@ const startRepl = async (options: {

if (!(options.connectionString ?? process.env.DB_CONNECTION_STRING)) {
console.log(
chalk.yellow(
color.yellow(
`No connection string provided, using: 'postgresql://postgres:postgres@localhost:5432/postgres'`,
),
);
Expand All @@ -149,28 +149,28 @@ const startRepl = async (options: {
if (!connectionCheck.successful) {
if (connectionCheck.errorType === 'ConnectionRefused') {
console.error(
chalk.red(
color.red(
`Connection was refused. Check if the PostgreSQL server is running and accessible.`,
),
);
} else if (connectionCheck.errorType === 'Authentication') {
console.error(
chalk.red(
color.red(
`Authentication failed. Check the username and password in the connection string.`,
),
);
} else {
console.error(chalk.red('Error connecting to PostgreSQL server'));
console.error(color.red('Error connecting to PostgreSQL server'));
}
console.log(chalk.red('Exiting Pongo Shell...'));
console.log(color.red('Exiting Pongo Shell...'));
process.exit();
}

console.log(chalk.green(`Successfully connected`));
console.log(chalk.green('Use db.<collection>.<method>() to query.'));
console.log(color.green(`Successfully connected`));
console.log(color.green('Use db.<collection>.<method>() to query.'));

const shell = repl.start({
prompt: chalk.green('pongo> '),
prompt: color.green('pongo> '),
useGlobal: true,
breakEvalOnSigint: true,
writer: printOutput,
Expand Down Expand Up @@ -237,7 +237,7 @@ const startRepl = async (options: {
};

const teardown = async () => {
console.log(chalk.yellow('Exiting Pongo Shell...'));
console.log(color.yellow('Exiting Pongo Shell...'));
await pongo.close();
};

Expand Down

0 comments on commit 11880df

Please sign in to comment.