Skip to content

Commit

Permalink
feat: add support for terminals with installed GPG, reduce npm size b…
Browse files Browse the repository at this point in the history
…y ~3 kB
  • Loading branch information
webdiscus committed Dec 23, 2024
1 parent 88a067c commit 0274117
Show file tree
Hide file tree
Showing 53 changed files with 981 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
os: [ ubuntu-latest, windows-latest ]
# rollup requires node >= 18
node-version: [ 18, 20 ]
node-version: [ 18, 20, 22 ]

runs-on: ${{ matrix.os }}
steps:
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Change log

## 3.5.0-beta.1 (2024-12-23)

- refactor: optimise npm package to reduce the size by ~1.5 kB, from 8.9 kB to 7.5 kB

## 3.5.0-beta.0 (2024-12-21)

- feat: add support for terminals where the GPG is installed.
If GPG is installed, `GPG_TTY` will be set to `ENV` instead of `isTTY`.
- fix: default import in TypeScript, compiled with `tsc`:
`import ansis from 'ansis'` now works so well as `import * as ansis from 'ansis'`
- refactor: optimise npm package to reduce size by ~1.3 kB, from 10.3 kB to 8.9 kB
- refactor: optimize index.d.ts, remove insignificant spaces and words, use the `type` with dynamic properties instead of the `interface`
- test: add integration tests to execute compiled TypeScript and compare outputs with expected strings
- test: add tests for `tsc`, `swc` and `esbuild` compilers

## 3.4.0 (2024-12-14)

- refactor: optimise npm package to reduce size by ~1 KB, from 11.3 kB to 10.3 kB
Expand Down
10 changes: 10 additions & 0 deletions LICENSE.npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ISC License

Copyright (c) 2024, webdiscus

Permission is granted to use, copy, modify, and distribute this software for any
purpose with or without fee, provided the copyright and permission notices appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND FITNESS.
THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES ARISING FROM THE USE OF THIS SOFTWARE.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a id="top" name="top"></a>
<div align="center">
<a href="https://www.npmjs.com/package/ansis">
<img width="323" src="https://github.com/webdiscus/ansis/raw/master/docs/img/ansis-logo.png" alt="ansis">
<img width="323" src="docs/img/logo.png" alt="ansis">
</a>
<h1 align="center">ANSI Styles</h1>
</div>
Expand Down Expand Up @@ -801,7 +801,7 @@ npm run compare
| Npm package | Download tarball size | Unpacked Size | Code size |
|:-----------------------------|----------------------------------------------------------------------:|-------------------------------:|----------:|
| [`picocolors`][picocolors] | [2.6 kB](https://arve0.github.io/npm-download-size/#picocolors) | [6.4 kB][npm-picocolors] | 2.6 kB
| [`ansis`][ansis] | [4.5 kB](https://arve0.github.io/npm-download-size/#ansis) | [10.3 kB][npm-ansis] | 3.4 kB
| [`ansis`][ansis] | [4.0 kB](https://arve0.github.io/npm-download-size/#ansis) | [7.5 kB][npm-ansis] | 3.4 kB
| [`colorette`][colorette] | [4.9 kB](https://arve0.github.io/npm-download-size/#colorette) | [17.0 kB][npm-colorette] | 3.4 kB
| [`kleur`][kleur] | [6.0 kB](https://arve0.github.io/npm-download-size/#kleur) | [20.3 kB][npm-kleur] | 2.7 kB
| [`ansi-colors`][ansi-colors] | [8.5 kB](https://arve0.github.io/npm-download-size/#ansi-colors) | [26.1 kB][npm-ansi-colors] | 5.8 kB
Expand Down
14 changes: 6 additions & 8 deletions README.npm.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<h1 align="center"><img src="docs/img/ansis-logo.png"></h1>
<h1 align="center"><img src="docs/img/logo.png"></h1>

[![](https://codecov.io/gh/webdiscus/ansis/branch/master/graph/badge.svg?token=H7SFJONX1X)](https://codecov.io/gh/webdiscus/ansis)
[![](https://img.shields.io/npm/dm/ansis)](https://www.npmjs.com/package/ansis)
[![](https://img.shields.io/bundlephobia/minzip/ansis)](https://bundlephobia.com/package/ansis)
[![](bit.ly/3ZS2ynW)](https://codecov.io/gh/webdiscus/ansis)
[![](bit.ly/49SsuV0)](https://www.npmjs.com/package/ansis)
[![](bit.ly/3P7e1Lp)](https://bundlephobia.com/package/ansis)

Colorize terminal with ANSI colors & styles, smaller and faster alternative to Chalk.
Colorize terminal with ANSI colors & styles.

📖 [Docs on GitHub](https://github.com/webdiscus/ansis)

---

![](docs/img/screenshot-readme-npm.png)
![](docs/img/npm.png)
File renamed without changes
File renamed without changes
12 changes: 5 additions & 7 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node

import chalk from 'chalk';

import ansis, {
Ansis,
red,
Expand Down Expand Up @@ -258,8 +256,8 @@ DISK: ${hex('#FFAB40')`${disk}%`}
`);

// chalk: non-standard syntax, bad practices, slow because used RegExp
log(chalk`
CPU: {red.bold ${cpu}%}
RAM: {green ${ram}%}
DISK: {hex('#FFAB40') ${disk}%}
`);
// log(chalk`
// CPU: {red.bold ${cpu}%}
// RAM: {green ${ram}%}
// DISK: {hex('#FFAB40') ${disk}%}
// `);
32 changes: 20 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansis",
"version": "3.4.0",
"description": "Colorize terminal output with ANSI colors & styles",
"version": "3.5.0-beta.1",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
"colour",
Expand Down Expand Up @@ -78,25 +78,33 @@
"test:package": "vitest run ./test/package.test.js",
"test:cjs": "node ./test/package/cjs/test.cjs",
"test:esm": "node ./test/package/esm/test.mjs",
"test:tsc": "vitest run ./test/ts-compiler.test.js",
"test:coverage": "vitest run --coverage",
"publish:public": "(npm run build) && npm publish ./dist --access public",
"publish:beta": "(npm run build) && npm publish ./dist --tag beta"
},
"engines": {
"node": ">=15"
"node": ">=16"
},
"devDependencies": {
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-terser": "^0.4.4",
"@types/node": "^20.11.30",
"@vitest/coverage-v8": "^2.1.1",
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "^2.1.8",
"ansis": "file:dist",
"prettier": "^3.3.3",
"rollup": "^4.22.5",
"rollup-plugin-cleanup": "^3.2.1",
"del-cli": "^6.0.0",
"esbuild": "^0.24.2",
"prettier": "^3.4.2",
"rollup": "^4.29.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.1.1",
"terser": "^5.34.1",
"vitest": "^2.1.1"
"swc": "^1.0.11",
"terser": "^5.37.0",
"typescript": "^5.7.2",
"vitest": "^2.1.8"
},
"overrides": {
"rollup-plugin-copy": {
"globby": "^11.0.0"
}
}
}
4 changes: 2 additions & 2 deletions package.npm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ansis",
"version": "3.4.0",
"description": "Colorize terminal output with ANSI colors & styles",
"version": "3.5.0-beta.1",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
"color",
Expand Down
40 changes: 17 additions & 23 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import terser from '@rollup/plugin-terser';
import replace from '@rollup/plugin-replace';
import cleanup from 'rollup-plugin-cleanup';
import copy from 'rollup-plugin-copy';
import dts from 'rollup-plugin-dts';
import { minify } from 'terser';

// last ECMA version compatible with node.js 12
const ecma = 2019;

export default [
// remove comments from d.ts file
{
input: 'src/index.d.ts',
output: [
{
file: 'dist/index.d.ts',
format: 'es',
},
],
plugins: [
cleanup({ extensions: ['ts'] }),
dts(),
],
},
function removeComments(string){
//Takes a string of code, not an actual function.
return string.replace(/\/\*[\s\S]*?\*\/|(?<=[^:])\/\/.*|^\/\/.*/g,'').trim();//Strip comments
}

export default [
{
input: 'src/index.js',
output: [
Expand All @@ -38,8 +26,11 @@ export default [
replace({
preventAssignment: false, // allow modifying exports
// the order of exports is other than is needed
'exports.Ansis = Ansis': 'module.exports = ansis', // firstly must be defined default export
'exports.default = ansis': 'module.exports.Ansis = Ansis', // then on the next line can be named export
// firstly must be defined default export
'exports.Ansis = Ansis': 'module.exports = ansis',
// then on the next line can be named and default export,
// `ansis.default = ansis` is needed for tsc using default import, e.g. `import ansis from 'ansis'`
'exports.default = ansis': 'module.exports.Ansis = Ansis, ansis.default = ansis',
}),
terser({
ecma,
Expand All @@ -59,15 +50,18 @@ export default [

// minify d.ts file generated after cleanup
{
src: 'dist/index.d.ts',
//src: 'src/index.interface.d.ts', // Ansis instance expressed via interface: 3713 bytes
src: 'src/index.type.d.ts', // Ansis instance expressed via type dynamic properties: 3161 bytes
rename: 'index.d.ts',
dest: 'dist/',
transform: (contents, name) => {
return contents.toString().
return removeComments(contents.toString()).
// remove insignificant spaces
replaceAll(/\n/g, '').
replaceAll(/\s{2}/g, ' ').
replaceAll(/\s{2,}/g, ' ').
replaceAll(' | ', '|').
replaceAll(' = ', '=').
replaceAll('=|', '=').
replaceAll(' => ', '=>').
replaceAll(', ', ',').
replaceAll(': ', ':').
Expand All @@ -80,7 +74,7 @@ export default [

{ src: 'package.npm.json', dest: 'dist/', rename: 'package.json' },
{ src: 'README.npm.md', dest: 'dist/', rename: 'README.md' },
{ src: 'LICENSE', dest: 'dist/' },
{ src: 'LICENSE.npm', dest: 'dist/', rename: 'LICENSE' },
],
}),
],
Expand Down
5 changes: 4 additions & 1 deletion src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ export const getColorSpace = (mockThis) => {
// PM2 does not set process.stdout.isTTY, but colors may be supported (depends on actual terminal)
const isPM2 = 'PM2_HOME' in env && 'pm_id' in env;

// is used in any shell if GPG is installed
const isGPG_TTY = 'GPG_TTY' in env;

// whether the output is supported
const isTTY = isNextJS || isPM2 || (isDeno ? Deno.isatty(1) : stdout && 'isTTY' in stdout);
const isTTY = isGPG_TTY || isPM2 || isNextJS || (isDeno ? Deno.isatty(1) : stdout && 'isTTY' in stdout);

// optimisation: placed here to reduce the size of the compiled bundle
if (isForceDisabled) return SPACE_MONO;
Expand Down
Loading

0 comments on commit 0274117

Please sign in to comment.