@supertape/formatter-time 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/lib/time.js +236 -0
  4. package/package.json +57 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @supertape/formatter-time [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@supertape/formatter-time.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@supertape/formatter-time "npm"
5
+
6
+ 📼[`Supertape`](https://github.com/coderaiser/supertape) formatter shows progress bar.
7
+
8
+ ## Install
9
+
10
+ ```
11
+ npm i supertape @supertape/formatter-time
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```
17
+ supertape --format time lib
18
+ ```
19
+
20
+ ## Env Variables
21
+
22
+ - `CI=1` - disable progress bar
23
+ - `SUPERTAPE_TIME=1` - force enable/disable progress bar;
24
+ - `SUPERTAPE_TIME_COLOR` - set color of progress bar;
25
+ - `SUPERTAPE_TIME_MIN=100` - count of tests to show progress bar;
26
+ - `SUPERTAPE_TIME_STACK=1` - force show/hide stack on fail;
27
+ - `SUPERTAPE_TIME_CLOCK=⏳` - set clock icon;
28
+
29
+ ## License
30
+
31
+ MIT
package/lib/time.js ADDED
@@ -0,0 +1,236 @@
1
+ import {Writable} from 'node:stream';
2
+ import cliProgress from 'cli-progress';
3
+ import chalk from 'chalk';
4
+ import fullstore from 'fullstore';
5
+ import {isCI} from 'ci-info';
6
+ import process from 'node:process';
7
+ import {Timer} from 'timer-node';
8
+
9
+ global._isCI = isCI;
10
+
11
+ const OK = '👌';
12
+ const YELLOW = '#218bff';
13
+
14
+ const {red} = chalk;
15
+ const formatErrorsCount = (a) => a ? red(a) : OK;
16
+
17
+ const isStr = (a) => typeof a === 'string';
18
+
19
+ const {stderr} = process;
20
+
21
+ let SUPERTAPE_TIME;
22
+ let SUPERTAPE_TIME_MIN = 100;
23
+ let SUPERTAPE_TIME_COLOR;
24
+ let SUPERTAPE_TIME_STACK = 1;
25
+ let SUPERTAPE_TIME_CLOCK = '⏳';
26
+
27
+ export function createFormatter(bar) {
28
+ ({
29
+ SUPERTAPE_TIME,
30
+ SUPERTAPE_TIME_MIN = 100,
31
+ SUPERTAPE_TIME_COLOR,
32
+ SUPERTAPE_TIME_STACK = 1,
33
+ SUPERTAPE_TIME_CLOCK = '⏳',
34
+ } = process.env);
35
+
36
+ const out = createOutput();
37
+ const store = fullstore();
38
+ const barStore = fullstore(bar);
39
+ const timerStore = fullstore();
40
+
41
+ return {
42
+ start: start({
43
+ barStore,
44
+ timerStore,
45
+ out,
46
+ }),
47
+ test: test({
48
+ store,
49
+ }),
50
+ testEnd: testEnd({
51
+ clock: SUPERTAPE_TIME_CLOCK,
52
+ barStore,
53
+ timerStore,
54
+ }),
55
+ fail: fail({
56
+ out,
57
+ store,
58
+ }),
59
+ end: end({
60
+ barStore,
61
+ out,
62
+ }),
63
+ };
64
+ }
65
+
66
+ export const start = ({barStore, timerStore, out}) => ({total}) => {
67
+ out('TAP version 13');
68
+
69
+ const color = SUPERTAPE_TIME_COLOR || YELLOW;
70
+ const {bar, timer} = _createProgress({
71
+ total,
72
+ color,
73
+ test: '',
74
+ });
75
+
76
+ barStore(bar);
77
+ timerStore(timer);
78
+ };
79
+
80
+ export const test = ({store}) => ({test}) => {
81
+ store(`# ${test}`);
82
+ };
83
+
84
+ export const testEnd = ({barStore, clock, timerStore}) => ({count, total, failed, test}) => {
85
+ const timer = timerStore();
86
+
87
+ barStore().increment({
88
+ count,
89
+ total,
90
+ test,
91
+ failed: formatErrorsCount(failed),
92
+ time: !timer ? '' : getTime({
93
+ clock,
94
+ timer: timerStore(),
95
+ }),
96
+ });
97
+ };
98
+
99
+ export const fail = ({out, store}) => ({at, count, message, operator, result, expected, output, errorStack}) => {
100
+ out('');
101
+ out(store());
102
+ out(`❌ not ok ${count} ${message}`);
103
+ out(' ---');
104
+ out(` operator: ${operator}`);
105
+
106
+ if (output)
107
+ out(output);
108
+
109
+ if (!isStr(output)) {
110
+ out(' expected: |-');
111
+ out(` ${expected}`);
112
+ out(' result: |-');
113
+ out(` ${result}`);
114
+ }
115
+
116
+ out(` ${at}`);
117
+
118
+ if (SUPERTAPE_TIME_STACK !== '0') {
119
+ out(' stack: |-');
120
+ out(errorStack);
121
+ }
122
+
123
+ out(' ...');
124
+ out('');
125
+ };
126
+
127
+ export const end = ({barStore, out}) => ({count, passed, failed, skiped}) => {
128
+ barStore().stop();
129
+
130
+ out('');
131
+
132
+ out(`1..${count}`);
133
+ out(`# tests ${count}`);
134
+ out(`# pass ${passed}`);
135
+
136
+ if (skiped)
137
+ out(`# ⚠️ skip ${skiped}`);
138
+
139
+ out('');
140
+
141
+ if (failed)
142
+ out(`# ❌ fail ${failed}`);
143
+
144
+ if (!failed)
145
+ out('# ✅ ok');
146
+
147
+ out('');
148
+ out('');
149
+
150
+ return `\r${out()}`;
151
+ };
152
+
153
+ function createOutput() {
154
+ let output = [];
155
+
156
+ return (...args) => {
157
+ const [line] = args;
158
+
159
+ if (!args.length) {
160
+ const result = output.join('\n');
161
+
162
+ output = [];
163
+
164
+ return result;
165
+ }
166
+
167
+ output.push(line);
168
+ };
169
+ }
170
+
171
+ const getColorFn = (color) => {
172
+ if (color.startsWith('#'))
173
+ return chalk.hex(color);
174
+
175
+ return chalk[color];
176
+ };
177
+
178
+ const defaultStreamOptions = {
179
+ total: Infinity,
180
+ };
181
+
182
+ const getStream = ({total} = defaultStreamOptions) => {
183
+ const is = total >= SUPERTAPE_TIME_MIN;
184
+
185
+ if (is && !global._isCI || SUPERTAPE_TIME === '1')
186
+ return stderr;
187
+
188
+ return new Writable();
189
+ };
190
+
191
+ export const _getStream = getStream;
192
+
193
+ function _createProgress({total, color, test}) {
194
+ const timer = new Timer({
195
+ label: 'supertape-timer',
196
+ });
197
+
198
+ const colorFn = getColorFn(color);
199
+ const bar = new cliProgress.SingleBar({
200
+ format: `${colorFn('{bar}')} {percentage}% | {failed} | {count}/{total} | {time} | {test}`,
201
+ barCompleteChar: '\u2588',
202
+ barIncompleteChar: '\u2591',
203
+ clearOnComplete: true,
204
+ stopOnComplete: true,
205
+ hideCursor: true,
206
+ stream: getStream({
207
+ total,
208
+ }),
209
+ }, cliProgress.Presets.react);
210
+
211
+ bar.start(total, 0, {
212
+ test,
213
+ total,
214
+ count: 0,
215
+ failed: OK,
216
+ time: getTime({
217
+ clock: SUPERTAPE_TIME_CLOCK,
218
+ timer,
219
+ }),
220
+ });
221
+
222
+ return {
223
+ bar,
224
+ timer,
225
+ };
226
+ }
227
+
228
+ export const maybeZero = (a) => a <= 9 ? '0' : '';
229
+
230
+ function getTime({clock, timer}) {
231
+ const {m, s} = timer.time();
232
+ const minute = `${maybeZero(m)}${m}`;
233
+ const second = `${maybeZero(s)}${s}`;
234
+
235
+ return `${clock} ${minute}:${second}`;
236
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@supertape/formatter-time",
3
+ "version": "1.0.0",
4
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
+ "description": "📼 Supertape formatter progress bar",
6
+ "homepage": "http://github.com/coderaiser/supertape",
7
+ "main": "./lib/time.js",
8
+ "release": false,
9
+ "tag": false,
10
+ "changelog": false,
11
+ "type": "module",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git://github.com/coderaiser/supertape.git"
15
+ },
16
+ "scripts": {
17
+ "test": "madrun test",
18
+ "watch:test": "madrun watch:test",
19
+ "lint": "madrun lint",
20
+ "fix:lint": "madrun fix:lint",
21
+ "coverage": "madrun coverage",
22
+ "report": "madrun report",
23
+ "wisdom": "madrun wisdom"
24
+ },
25
+ "dependencies": {
26
+ "chalk": "^4.1.0",
27
+ "ci-info": "^4.0.0",
28
+ "cli-progress": "^3.8.2",
29
+ "fullstore": "^3.0.0",
30
+ "once": "^1.4.0",
31
+ "timer-node": "^5.0.7"
32
+ },
33
+ "keywords": [
34
+ "formatter",
35
+ "time",
36
+ "supertape"
37
+ ],
38
+ "devDependencies": {
39
+ "c8": "^8.0.0",
40
+ "eslint": "^8.0.0-beta.0",
41
+ "eslint-plugin-n": "^16.0.1",
42
+ "eslint-plugin-putout": "^22.0.0",
43
+ "madrun": "^10.0.0",
44
+ "montag": "^1.0.0",
45
+ "nodemon": "^3.0.1",
46
+ "pullout": "^4.0.0",
47
+ "putout": "^34.0.0",
48
+ "supertape": "^9.0.0"
49
+ },
50
+ "license": "MIT",
51
+ "engines": {
52
+ "node": ">=18"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ }
57
+ }