@code-pushup/utils 0.1.0 → 0.2.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.
package/index.js CHANGED
@@ -548,7 +548,8 @@ function pluralize(text) {
548
548
  return `${text}s`;
549
549
  }
550
550
  function formatBytes(bytes, decimals = 2) {
551
- if (!+bytes)
551
+ bytes = Math.max(bytes, 0);
552
+ if (!bytes)
552
553
  return "0 B";
553
554
  const k = 1024;
554
555
  const dm = decimals < 0 ? 0 : decimals;
@@ -605,6 +606,22 @@ function logPromiseResults(results, logMessage, callback) {
605
606
  }
606
607
 
607
608
  // packages/utils/src/lib/file-system.ts
609
+ async function readTextFile(path) {
610
+ const buffer = await readFile(path);
611
+ return buffer.toString();
612
+ }
613
+ async function readJsonFile(path) {
614
+ const text = await readTextFile(path);
615
+ return JSON.parse(text);
616
+ }
617
+ async function fileExists(path) {
618
+ try {
619
+ const stats = await stat(path);
620
+ return stats.isFile();
621
+ } catch {
622
+ return false;
623
+ }
624
+ }
608
625
  function toUnixPath(path, options) {
609
626
  const unixPath = path.replace(/\\/g, "/");
610
627
  if (options?.toRelative) {
@@ -623,14 +640,6 @@ async function ensureDirectoryExists(baseDir) {
623
640
  }
624
641
  }
625
642
  }
626
- async function readTextFile(path) {
627
- const buffer = await readFile(path);
628
- return buffer.toString();
629
- }
630
- async function readJsonFile(path) {
631
- const text = await readTextFile(path);
632
- return JSON.parse(text);
633
- }
634
643
  function logMultipleFileResults(fileResults, messagePrefix) {
635
644
  const succeededCallback = (result) => {
636
645
  const [fileName, size] = result.value;
@@ -967,60 +976,6 @@ async function getLatestCommit() {
967
976
  return log?.latest;
968
977
  }
969
978
 
970
- // packages/utils/src/lib/progress.ts
971
- import chalk2 from "chalk";
972
- import { MultiProgressBars } from "multi-progress-bars";
973
- var barStyles = {
974
- active: (s) => chalk2.green(s),
975
- done: (s) => chalk2.gray(s),
976
- idle: (s) => chalk2.gray(s)
977
- };
978
- var messageStyles = {
979
- active: (s) => chalk2.black(s),
980
- done: (s) => chalk2.green(chalk2.bold(s)),
981
- idle: (s) => chalk2.gray(s)
982
- };
983
- var mpb;
984
- function getSingletonProgressBars(options) {
985
- if (!mpb) {
986
- mpb = new MultiProgressBars({
987
- initMessage: "",
988
- border: true,
989
- ...options
990
- });
991
- }
992
- return mpb;
993
- }
994
- function getProgressBar(taskName) {
995
- const tasks = getSingletonProgressBars();
996
- tasks.addTask(taskName, {
997
- type: "percentage",
998
- percentage: 0,
999
- message: "",
1000
- barTransformFn: barStyles.idle
1001
- });
1002
- return {
1003
- incrementInSteps: (numPlugins) => {
1004
- tasks.incrementTask(taskName, {
1005
- percentage: 1 / numPlugins,
1006
- barTransformFn: barStyles.active
1007
- });
1008
- },
1009
- updateTitle: (title) => {
1010
- tasks.updateTask(taskName, {
1011
- message: title,
1012
- barTransformFn: barStyles.active
1013
- });
1014
- },
1015
- endProgress: (message = "") => {
1016
- tasks.done(taskName, {
1017
- message: messageStyles.done(message),
1018
- barTransformFn: barStyles.done
1019
- });
1020
- }
1021
- };
1022
- }
1023
-
1024
979
  // packages/utils/src/lib/md/details.ts
1025
980
  function details(title, content, cfg = { open: false }) {
1026
981
  return `<details${cfg.open ? " open" : ""}>
@@ -1098,6 +1053,60 @@ function li(text, order = "unordered") {
1098
1053
  return `${style2} ${text}`;
1099
1054
  }
1100
1055
 
1056
+ // packages/utils/src/lib/progress.ts
1057
+ import chalk2 from "chalk";
1058
+ import { MultiProgressBars } from "multi-progress-bars";
1059
+ var barStyles = {
1060
+ active: (s) => chalk2.green(s),
1061
+ done: (s) => chalk2.gray(s),
1062
+ idle: (s) => chalk2.gray(s)
1063
+ };
1064
+ var messageStyles = {
1065
+ active: (s) => chalk2.black(s),
1066
+ done: (s) => chalk2.green(chalk2.bold(s)),
1067
+ idle: (s) => chalk2.gray(s)
1068
+ };
1069
+ var mpb;
1070
+ function getSingletonProgressBars(options) {
1071
+ if (!mpb) {
1072
+ mpb = new MultiProgressBars({
1073
+ initMessage: "",
1074
+ border: true,
1075
+ ...options
1076
+ });
1077
+ }
1078
+ return mpb;
1079
+ }
1080
+ function getProgressBar(taskName) {
1081
+ const tasks = getSingletonProgressBars();
1082
+ tasks.addTask(taskName, {
1083
+ type: "percentage",
1084
+ percentage: 0,
1085
+ message: "",
1086
+ barTransformFn: barStyles.idle
1087
+ });
1088
+ return {
1089
+ incrementInSteps: (numPlugins) => {
1090
+ tasks.incrementTask(taskName, {
1091
+ percentage: 1 / numPlugins,
1092
+ barTransformFn: barStyles.active
1093
+ });
1094
+ },
1095
+ updateTitle: (title) => {
1096
+ tasks.updateTask(taskName, {
1097
+ message: title,
1098
+ barTransformFn: barStyles.active
1099
+ });
1100
+ },
1101
+ endProgress: (message = "") => {
1102
+ tasks.done(taskName, {
1103
+ message: messageStyles.done(message),
1104
+ barTransformFn: barStyles.done
1105
+ });
1106
+ }
1107
+ };
1108
+ }
1109
+
1101
1110
  // packages/utils/src/lib/report-to-md.ts
1102
1111
  function reportToMd(report, commitData) {
1103
1112
  let md = reportToHeaderSection() + NEW_LINE;
@@ -1403,6 +1412,14 @@ function deepClone(obj) {
1403
1412
  }
1404
1413
  return cloned;
1405
1414
  }
1415
+ function factorOf(items, filterFn) {
1416
+ const itemCount = items.length;
1417
+ if (!itemCount) {
1418
+ return 1;
1419
+ }
1420
+ const filterCount = items.filter(filterFn).length;
1421
+ return filterCount === 0 ? 1 : (itemCount - filterCount) / itemCount;
1422
+ }
1406
1423
 
1407
1424
  // packages/utils/src/lib/scoring.ts
1408
1425
  function calculateScore(refs, scoreFn) {
@@ -1499,6 +1516,8 @@ export {
1499
1516
  distinct,
1500
1517
  ensureDirectoryExists,
1501
1518
  executeProcess,
1519
+ factorOf,
1520
+ fileExists,
1502
1521
  findLineNumberInText,
1503
1522
  formatBytes,
1504
1523
  formatDuration,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "dependencies": {
5
5
  "@code-pushup/models": "*",
6
6
  "bundle-require": "^4.0.1",
package/src/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  export { CliArgsObject, ProcessConfig, ProcessError, ProcessObserver, ProcessResult, executeProcess, objectToCliArgs, } from './lib/execute-process';
2
+ export { FileResult, MultipleFileResults, crawlFileSystem, ensureDirectoryExists, fileExists, findLineNumberInText, importEsmModule, logMultipleFileResults, pluginWorkDir, readJsonFile, readTextFile, toUnixPath, } from './lib/file-system';
3
+ export { formatBytes, formatDuration, pluralize, pluralizeToken, slugify, } from './lib/formatting';
2
4
  export { getLatestCommit, git } from './lib/git';
5
+ export { isPromiseFulfilledResult, isPromiseRejectedResult, } from './lib/guards';
6
+ export { logMultipleResults } from './lib/log-results';
7
+ export { NEW_LINE } from './lib/md';
3
8
  export { ProgressBar, getProgressBar } from './lib/progress';
4
9
  export { CODE_PUSHUP_DOMAIN, FOOTER_PREFIX, README_LINK, calcDuration, compareIssueSeverity, loadReport, } from './lib/report';
5
10
  export { reportToMd } from './lib/report-to-md';
6
11
  export { reportToStdout } from './lib/report-to-stdout';
7
12
  export { ScoredReport, scoreReport } from './lib/scoring';
8
- export { readJsonFile, readTextFile, toUnixPath, ensureDirectoryExists, FileResult, MultipleFileResults, logMultipleFileResults, importEsmModule, pluginWorkDir, crawlFileSystem, findLineNumberInText, } from './lib/file-system';
13
+ export { countOccurrences, distinct, factorOf, objectToEntries, objectToKeys, toArray, } from './lib/transformation';
9
14
  export { verboseUtils } from './lib/verbose-utils';
10
- export { toArray, objectToKeys, objectToEntries, countOccurrences, distinct, } from './lib/transformation';
11
- export { slugify, pluralize, pluralizeToken, formatBytes, formatDuration, } from './lib/formatting';
12
- export { NEW_LINE } from './lib/md';
13
- export { logMultipleResults } from './lib/log-results';
14
- export { isPromiseFulfilledResult, isPromiseRejectedResult, } from './lib/guards';
@@ -1,10 +1,11 @@
1
1
  import { type Options } from 'bundle-require';
2
+ export declare function readTextFile(path: string): Promise<string>;
3
+ export declare function readJsonFile<T = unknown>(path: string): Promise<T>;
4
+ export declare function fileExists(path: string): Promise<boolean>;
2
5
  export declare function toUnixPath(path: string, options?: {
3
6
  toRelative?: boolean;
4
7
  }): string;
5
8
  export declare function ensureDirectoryExists(baseDir: string): Promise<void>;
6
- export declare function readTextFile(path: string): Promise<string>;
7
- export declare function readJsonFile<T = unknown>(path: string): Promise<T>;
8
9
  export type FileResult = readonly [string] | readonly [string, number];
9
10
  export type MultipleFileResults = PromiseSettledResult<FileResult>[];
10
11
  export declare function logMultipleFileResults(fileResults: MultipleFileResults, messagePrefix: string): void;