Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to ESM #67

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
18 changes: 9 additions & 9 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
'use strict';
const chalk = require('chalk');
const boxen = require('.');
import process from 'node:process';
import chalk from 'chalk';
import boxen from './index.js';

console.log('\n\n' + boxen(chalk.cyan('unicorn'), {
padding: 1,
margin: 1,
borderColor: 'yellow'
borderColor: 'yellow',
}) + '\n');

console.log('\n\n' + boxen(chalk.cyan('unicorn'), {
padding: 1,
margin: 1,
borderColor: 'yellow',
borderStyle: 'double'
borderStyle: 'double',
}) + '\n');

console.log('\n\n' + boxen(chalk.cyan('unicorn'), {
padding: 1,
margin: 1,
borderColor: '#eebbaa',
borderStyle: 'double'
borderStyle: 'double',
}) + '\n');

console.log('\n\n' + boxen(chalk.black('unicorn'), {
padding: 1,
margin: 1,
borderColor: '#ffc0cb',
backgroundColor: '#00ffff',
borderStyle: 'double'
borderStyle: 'double',
}) + '\n');

console.log('\n\n' + boxen(chalk.black('unicorn'), {
Expand All @@ -41,8 +41,8 @@ console.log('\n\n' + boxen(chalk.black('unicorn'), {
bottomLeft: '+',
bottomRight: '+',
horizontal: '-',
vertical: '|'
}
vertical: '|',
},
}) + '\n');

const sentences = 'Unbreakable_text_because_it_has_no_spaces '.repeat(5);
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Creates a box in the terminal.

@example
```
import boxen = require('boxen');
import boxen from 'boxen';

console.log(boxen('unicorn', {padding: 1}));
// ┌─────────────┐
Expand All @@ -195,6 +195,7 @@ console.log(boxen('unicorn', {padding: 1, margin: 1, borderStyle: 'double'}));
//
```
*/
// eslint-disable-next-line @typescript-eslint/no-redeclare
declare const boxen: (text: string, options?: boxen.Options) => string;

export = boxen;
LitoMore marked this conversation as resolved.
Show resolved Hide resolved
58 changes: 28 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
const stringWidth = require('string-width');
const chalk = require('chalk');
const widestLine = require('widest-line');
const cliBoxes = require('cli-boxes');
const camelCase = require('camelcase');
const ansiAlign = require('ansi-align');
const wrapAnsi = require('wrap-ansi');
import process from 'node:process';
import stringWidth from 'string-width';
import chalk from 'chalk';
import widestLine from 'widest-line';
import cliBoxes from 'cli-boxes';
import camelCase from 'camelcase';
import ansiAlign from 'ansi-align';
import wrapAnsi from 'wrap-ansi';

const NEWLINE = '\n';
const PAD = ' ';
Expand All @@ -28,19 +28,17 @@ const terminalColumns = () => {
return 80;
};

const getObject = detail => {
return typeof detail === 'number' ? {
top: detail,
right: detail * 3,
bottom: detail,
left: detail * 3
} : {
top: 0,
right: 0,
bottom: 0,
left: 0,
...detail
};
const getObject = detail => typeof detail === 'number' ? {
top: detail,
right: detail * 3,
bottom: detail,
left: detail * 3,
} : {
top: 0,
right: 0,
bottom: 0,
left: 0,
...detail,
};

const getBorderChars = borderStyle => {
Expand All @@ -50,7 +48,7 @@ const getBorderChars = borderStyle => {
'bottomRight',
'bottomLeft',
'vertical',
'horizontal'
'horizontal',
];

let characters;
Expand Down Expand Up @@ -166,11 +164,11 @@ const makeContentText = (text, padding, columns, align) => {
});

if (padding.top > 0) {
lines = new Array(padding.top).fill(PAD.repeat(columns)).concat(lines);
lines = [...Array.from({length: padding.top}).fill(PAD.repeat(columns)), ...lines];
}

if (padding.bottom > 0) {
lines = lines.concat(new Array(padding.bottom).fill(PAD.repeat(columns)));
lines = [...lines, ...Array.from({length: padding.bottom}).fill(PAD.repeat(columns))];
}

return lines.join(NEWLINE);
Expand All @@ -181,15 +179,15 @@ const isColorValid = color => typeof color === 'string' && ((chalk[color]) || is
const getColorFn = color => isHex(color) ? chalk.hex(color) : chalk[color];
const getBGColorFn = color => isHex(color) ? chalk.bgHex(color) : chalk[camelCase(['bg', color])];

module.exports = (text, options) => {
const boxen = (text, options) => {
LitoMore marked this conversation as resolved.
Show resolved Hide resolved
options = {
padding: 0,
borderStyle: 'single',
dimBorder: false,
textAlignment: 'left',
float: 'left',
titleAlignment: 'left',
...options
...options,
};

// This option is deprecated
Expand Down Expand Up @@ -269,11 +267,11 @@ module.exports = (text, options) => {

const lines = text.split(NEWLINE);

const middle = lines.map(line => {
return marginLeft + side + colorizeContent(line) + side;
}).join(LINE_SEPARATOR);
const middle = lines.map(line => marginLeft + side + colorizeContent(line) + side).join(LINE_SEPARATOR);

return top + LINE_SEPARATOR + middle + LINE_SEPARATOR + bottom;
};

module.exports._borderStyles = cliBoxes;
export const _borderStyles = cliBoxes;

export default boxen;
7 changes: 3 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {expectType} from 'tsd';
import boxen = require('.');
import {Spacing, CustomBorderStyle} from '.';
import boxen, {Spacing, CustomBorderStyle} from './index.js';

const border: CustomBorderStyle = {
topLeft: ' ',
topRight: ' ',
bottomLeft: ' ',
bottomRight: ' ',
horizontal: ' ',
vertical: ' '
vertical: ' ',
};

const spacing: Spacing = {
top: 1,
right: 0,
bottom: 1,
left: 0
left: 0,
};

expectType<string>(boxen('unicorns'));
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12.17"
},
"scripts": {
"test": "xo && nyc ava && tsd"
Expand All @@ -35,19 +37,19 @@
"dependencies": {
"ansi-align": "^3.0.0",
"camelcase": "^6.2.0",
"chalk": "^4.1.0",
"chalk": "^4.1.2",
"cli-boxes": "^2.2.1",
"string-width": "^4.2.2",
"type-fest": "^0.20.2",
"string-width": "^5.0.1",
"type-fest": "^2.3.4",
"typescript": "^4.4.3",
"widest-line": "^3.1.0",
"wrap-ansi": "^7.0.0"
"widest-line": "^4.0.0",
"wrap-ansi": "^8.0.1"
},
"devDependencies": {
"ava": "^3.15.0",
"nyc": "^15.1.0",
"tsd": "^0.17.0",
"xo": "^0.36.1"
"xo": "^0.44.0"
},
"ava": {
"snapshotDir": "tests/snapshots",
Expand Down
4 changes: 2 additions & 2 deletions tests/background-option.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('ava');
const boxen = require('..');
import test from 'ava';
import boxen from '../index.js';

test('backgroundColor option', t => {
const box = boxen('foo', {backgroundColor: 'red'});
Expand Down
34 changes: 17 additions & 17 deletions tests/border-option.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const test = require('ava');
const boxen = require('..');
import test from 'ava';
import boxen from '../index.js';

test('border color (red)', t => {
const box = boxen('foo', {
borderColor: 'red'
borderColor: 'red',
});

t.snapshot(box);
});

test('border color (blue)', t => {
const box = boxen('foo', {
borderColor: 'blue'
borderColor: 'blue',
});

t.snapshot(box);
});

test('border color (green)', t => {
const box = boxen('foo', {
borderColor: 'green'
borderColor: 'green',
});

t.snapshot(box);
Expand All @@ -28,7 +28,7 @@ test('border color (green)', t => {
test('border color (yellow + dim)', t => {
const box = boxen('foo', {
borderColor: 'green',
dimBorder: true
dimBorder: true,
});

t.snapshot(box);
Expand All @@ -37,7 +37,7 @@ test('border color (yellow + dim)', t => {
test('border color (hex)', t => {
const box = boxen('foo', {
borderColor: '#FF00FF',
dimBorder: true
dimBorder: true,
});

t.snapshot(box);
Expand All @@ -51,55 +51,55 @@ test('throws on unexpected borderColor', t => {

test('border style (single)', t => {
const box = boxen('foo', {
borderStyle: 'single'
borderStyle: 'single',
});

t.snapshot(box);
});

test('border style (singleDouble)', t => {
const box = boxen('foo', {
borderStyle: 'singleDouble'
borderStyle: 'singleDouble',
});

t.snapshot(box);
});

test('border style (doubleSingle)', t => {
const box = boxen('foo', {
borderStyle: 'doubleSingle'
borderStyle: 'doubleSingle',
});

t.snapshot(box);
});

test('border style (double)', t => {
const box = boxen('foo', {
borderStyle: 'double'
borderStyle: 'double',
});

t.snapshot(box);
});

test('border style (classic)', t => {
const box = boxen('foo', {
borderStyle: 'classic'
borderStyle: 'classic',
});

t.snapshot(box);
});

test('border style (bold)', t => {
const box = boxen('foo', {
borderStyle: 'bold'
borderStyle: 'bold',
});

t.snapshot(box);
});

test('border style (round)', t => {
const box = boxen('foo', {
borderStyle: 'round'
borderStyle: 'round',
});

t.snapshot(box);
Expand All @@ -113,8 +113,8 @@ test('border style (custom ascii style)', t => {
bottomLeft: '3',
bottomRight: '4',
horizontal: '-',
vertical: '|'
}
vertical: '|',
},
});

t.snapshot(box);
Expand All @@ -137,7 +137,7 @@ test('throws on unexpected borderStyle as object', t => {
topRight: '2',
bottomLeft: '3',
horizontal: '-',
vertical: '|'
vertical: '|',
};

t.throws(() => {
Expand Down
Loading