@code-pushup/cli 0.10.6 → 0.11.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 (3) hide show
  1. package/README.md +4 -5
  2. package/index.js +102 -60
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -40,10 +40,6 @@ _If you're looking for programmatic usage, then refer to the underlying [@code-p
40
40
 
41
41
  ```js
42
42
  export default {
43
- persist: {
44
- outputDir: '.code-pushup',
45
- format: ['json', 'md'],
46
- },
47
43
  plugins: [
48
44
  // ...
49
45
  ],
@@ -154,7 +150,10 @@ Each example is fully tested to demonstrate best practices for plugin testing as
154
150
  | ---------------- | --------- | ----------------------- | ---------------------------------------------------------------------- |
155
151
  | **`--progress`** | `boolean` | `true` | Show progress bar in stdout. |
156
152
  | **`--verbose`** | `boolean` | `false` | When true creates more verbose output. This is helpful when debugging. |
157
- | **`--config`** | `string` | `code-pushup.config.js` | Path to the config file, e.g. code-pushup.config.js |
153
+ | **`--config`** | `string` | `code-pushup.config.ts` | Path to the config file, e.g. code-pushup.config.(ts\|mjs\|js) |
154
+
155
+ > [!NOTE]
156
+ > By default, the CLI loads `code-pushup.config.(ts|mjs|js)` if no config path is provided with `--config`.
158
157
 
159
158
  ### Common Command Options
160
159
 
package/index.js CHANGED
@@ -6,16 +6,6 @@ import { hideBin } from "yargs/helpers";
6
6
  // packages/cli/src/lib/autorun/autorun-command.ts
7
7
  import chalk5 from "chalk";
8
8
 
9
- // packages/core/src/lib/implementation/persist.ts
10
- import { mkdir as mkdir2, stat as stat2, writeFile } from "node:fs/promises";
11
- import { join as join2 } from "node:path";
12
-
13
- // packages/utils/src/lib/execute-process.ts
14
- import { spawn } from "node:child_process";
15
-
16
- // packages/utils/src/lib/reports/utils.ts
17
- import { join } from "node:path";
18
-
19
9
  // packages/models/src/lib/audit.ts
20
10
  import { z as z2 } from "zod";
21
11
 
@@ -502,6 +492,10 @@ var PERSIST_OUTPUT_DIR = ".code-pushup";
502
492
  var PERSIST_FORMAT = ["json"];
503
493
  var PERSIST_FILENAME = "report";
504
494
 
495
+ // packages/models/src/lib/implementation/configuration.ts
496
+ var CONFIG_FILE_NAME = "code-pushup.config";
497
+ var SUPPORTED_CONFIG_FILE_FORMATS = ["ts", "mjs", "js"];
498
+
505
499
  // packages/models/src/lib/report.ts
506
500
  import { z as z12 } from "zod";
507
501
  var auditReportSchema = auditSchema.merge(auditOutputSchema);
@@ -564,6 +558,12 @@ var reportSchema = packageVersionSchema({
564
558
  })
565
559
  );
566
560
 
561
+ // packages/utils/src/lib/execute-process.ts
562
+ import { spawn } from "node:child_process";
563
+
564
+ // packages/utils/src/lib/reports/utils.ts
565
+ import { join } from "node:path";
566
+
567
567
  // packages/utils/src/lib/file-system.ts
568
568
  import { bundleRequire } from "bundle-require";
569
569
  import chalk from "chalk";
@@ -1372,24 +1372,6 @@ function deepClone(obj) {
1372
1372
  }
1373
1373
 
1374
1374
  // packages/utils/src/lib/reports/scoring.ts
1375
- function calculateScore(refs, scoreFn) {
1376
- const { numerator, denominator } = refs.reduce(
1377
- (acc, ref) => {
1378
- const score = scoreFn(ref);
1379
- return {
1380
- numerator: acc.numerator + score * ref.weight,
1381
- denominator: acc.denominator + ref.weight
1382
- };
1383
- },
1384
- { numerator: 0, denominator: 0 }
1385
- );
1386
- if (!numerator && !denominator) {
1387
- throw new Error(
1388
- "0 division for score. This can be caused by refs only weighted with 0 or empty refs"
1389
- );
1390
- }
1391
- return numerator / denominator;
1392
- }
1393
1375
  function scoreReport(report) {
1394
1376
  const allScoredAuditsAndGroups = /* @__PURE__ */ new Map();
1395
1377
  const scoredPlugins = report.plugins.map((plugin) => {
@@ -1439,6 +1421,36 @@ function scoreReport(report) {
1439
1421
  categories: scoredCategories
1440
1422
  };
1441
1423
  }
1424
+ function calculateScore(refs, scoreFn) {
1425
+ const validatedRefs = parseScoringParameters(refs, scoreFn);
1426
+ const { numerator, denominator } = validatedRefs.reduce(
1427
+ (acc, ref) => ({
1428
+ numerator: acc.numerator + ref.score * ref.weight,
1429
+ denominator: acc.denominator + ref.weight
1430
+ }),
1431
+ { numerator: 0, denominator: 0 }
1432
+ );
1433
+ return numerator / denominator;
1434
+ }
1435
+ function parseScoringParameters(refs, scoreFn) {
1436
+ if (refs.length === 0) {
1437
+ throw new Error("Reference array cannot be empty.");
1438
+ }
1439
+ if (refs.some((ref) => ref.weight < 0)) {
1440
+ throw new Error("Weight cannot be negative.");
1441
+ }
1442
+ if (refs.every((ref) => ref.weight === 0)) {
1443
+ throw new Error("All references cannot have zero weight.");
1444
+ }
1445
+ const scoredRefs = refs.map((ref) => ({
1446
+ weight: ref.weight,
1447
+ score: scoreFn(ref)
1448
+ }));
1449
+ if (scoredRefs.some((ref) => ref.score < 0 || ref.score > 1)) {
1450
+ throw new Error("All scores must be in range 0-1.");
1451
+ }
1452
+ return scoredRefs;
1453
+ }
1442
1454
 
1443
1455
  // packages/utils/src/lib/reports/sorting.ts
1444
1456
  function sortReport(report) {
@@ -1515,6 +1527,8 @@ var verboseUtils = (verbose) => ({
1515
1527
  });
1516
1528
 
1517
1529
  // packages/core/src/lib/implementation/persist.ts
1530
+ import { mkdir as mkdir2, stat as stat2, writeFile } from "node:fs/promises";
1531
+ import { join as join2 } from "node:path";
1518
1532
  var PersistDirError = class extends Error {
1519
1533
  constructor(outputDir) {
1520
1534
  super(`outPath: ${outputDir} is no directory.`);
@@ -1691,7 +1705,7 @@ function auditOutputsCorrelateWithPluginOutput(auditOutputs, pluginConfigAudits)
1691
1705
 
1692
1706
  // packages/core/package.json
1693
1707
  var name = "@code-pushup/core";
1694
- var version = "0.10.6";
1708
+ var version = "0.11.0";
1695
1709
 
1696
1710
  // packages/core/src/lib/implementation/collect.ts
1697
1711
  async function collect(options2) {
@@ -1844,29 +1858,80 @@ async function collectAndPersistReports(options2) {
1844
1858
  });
1845
1859
  }
1846
1860
 
1847
- // packages/core/src/lib/implementation/read-code-pushup-config.ts
1861
+ // packages/core/src/lib/implementation/read-rc-file.ts
1862
+ import { join as join4 } from "node:path";
1848
1863
  var ConfigPathError = class extends Error {
1849
1864
  constructor(configPath) {
1850
1865
  super(`Provided path '${configPath}' is not valid.`);
1851
1866
  }
1852
1867
  };
1853
- async function readCodePushupConfig(filepath) {
1868
+ async function readRcByPath(filepath) {
1869
+ if (filepath.length === 0) {
1870
+ throw new Error("The path to the configuration file is empty.");
1871
+ }
1854
1872
  if (!await fileExists(filepath)) {
1855
1873
  throw new ConfigPathError(filepath);
1856
1874
  }
1857
- return coreConfigSchema.parse(await importEsmModule({ filepath }));
1875
+ const cfg = await importEsmModule({ filepath });
1876
+ return coreConfigSchema.parse(cfg);
1877
+ }
1878
+ async function autoloadRc() {
1879
+ let ext = "";
1880
+ for (const extension of SUPPORTED_CONFIG_FILE_FORMATS) {
1881
+ const path = `${CONFIG_FILE_NAME}.${extension}`;
1882
+ const exists2 = await fileExists(path);
1883
+ if (exists2) {
1884
+ ext = extension;
1885
+ break;
1886
+ }
1887
+ }
1888
+ if (!ext) {
1889
+ throw new Error(
1890
+ `No file ${CONFIG_FILE_NAME}.(${SUPPORTED_CONFIG_FILE_FORMATS.join(
1891
+ "|"
1892
+ )}) present in ${process.cwd()}`
1893
+ );
1894
+ }
1895
+ return readRcByPath(join4(process.cwd(), `${CONFIG_FILE_NAME}.${ext}`));
1858
1896
  }
1859
1897
 
1860
1898
  // packages/cli/src/lib/constants.ts
1861
1899
  var CLI_NAME = "Code PushUp CLI";
1862
1900
  var CLI_SCRIPT_NAME = "code-pushup";
1863
1901
 
1902
+ // packages/cli/src/lib/implementation/global.utils.ts
1903
+ function filterKebabCaseKeys(obj) {
1904
+ return Object.entries(obj).filter(([key]) => !key.includes("-")).reduce(
1905
+ (acc, [key, value]) => typeof value === "string" || typeof value === "object" && Array.isArray(obj[key]) ? { ...acc, [key]: value } : typeof value === "object" && !Array.isArray(value) && value != null ? {
1906
+ ...acc,
1907
+ [key]: filterKebabCaseKeys(value)
1908
+ } : { ...acc, [key]: value },
1909
+ {}
1910
+ );
1911
+ }
1912
+ function logErrorBeforeThrow(fn) {
1913
+ return async (...args) => {
1914
+ try {
1915
+ return await fn(...args);
1916
+ } catch (error) {
1917
+ console.error(error);
1918
+ await new Promise((resolve) => process.stdout.write("", resolve));
1919
+ throw error;
1920
+ }
1921
+ };
1922
+ }
1923
+ function coerceArray(param = []) {
1924
+ return [
1925
+ ...new Set(toArray(param).flatMap((f) => f.split(",")) || [])
1926
+ ];
1927
+ }
1928
+
1864
1929
  // packages/cli/src/lib/implementation/only-plugins.options.ts
1865
1930
  var onlyPluginsOption = {
1866
1931
  describe: "List of plugins to run. If not set all plugins are run.",
1867
1932
  type: "array",
1868
1933
  default: [],
1869
- coerce: (arg) => arg.flatMap((v) => v.split(","))
1934
+ coerce: coerceArray
1870
1935
  };
1871
1936
  function yargsOnlyPluginsOptionsDefinition() {
1872
1937
  return {
@@ -1921,28 +1986,6 @@ function yargsCollectCommandObject() {
1921
1986
  };
1922
1987
  }
1923
1988
 
1924
- // packages/cli/src/lib/implementation/global.utils.ts
1925
- function filterKebabCaseKeys(obj) {
1926
- return Object.entries(obj).filter(([key]) => !key.includes("-")).reduce(
1927
- (acc, [key, value]) => typeof value === "string" || typeof value === "object" && Array.isArray(obj[key]) ? { ...acc, [key]: value } : typeof value === "object" && !Array.isArray(value) && value != null ? {
1928
- ...acc,
1929
- [key]: filterKebabCaseKeys(value)
1930
- } : { ...acc, [key]: value },
1931
- {}
1932
- );
1933
- }
1934
- function logErrorBeforeThrow(fn) {
1935
- return async (...args) => {
1936
- try {
1937
- return await fn(...args);
1938
- } catch (error) {
1939
- console.error(error);
1940
- await new Promise((resolve) => process.stdout.write("", resolve));
1941
- throw error;
1942
- }
1943
- };
1944
- }
1945
-
1946
1989
  // packages/cli/src/lib/print-config/print-config-command.ts
1947
1990
  function yargsConfigCommandObject() {
1948
1991
  const command = "print-config";
@@ -2000,12 +2043,12 @@ async function coreConfigMiddleware(processArgs) {
2000
2043
  upload: cliUpload,
2001
2044
  ...remainingCliOptions
2002
2045
  } = args;
2003
- const rcOptions = await readCodePushupConfig(config);
2046
+ const importedRc = config ? await readRcByPath(config) : await autoloadRc();
2004
2047
  const {
2005
2048
  persist: rcPersist,
2006
2049
  upload: rcUpload,
2007
2050
  ...remainingRcConfig
2008
- } = rcOptions;
2051
+ } = importedRc;
2009
2052
  const parsedProcessArgs = {
2010
2053
  config,
2011
2054
  ...remainingRcConfig,
@@ -2149,9 +2192,8 @@ function yargsGlobalOptionsDefinition() {
2149
2192
  default: false
2150
2193
  },
2151
2194
  config: {
2152
- describe: "Path the the config file, e.g. code-pushup.config.js",
2153
- type: "string",
2154
- default: "code-pushup.config.js"
2195
+ describe: "Path the the config file, e.g. code-pushup.config.ts. By default it loads code-pushup.config.(ts|mjs|js).",
2196
+ type: "string"
2155
2197
  }
2156
2198
  };
2157
2199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-pushup/cli",
3
- "version": "0.10.6",
3
+ "version": "0.11.0",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "code-pushup": "index.js"