@bymaksym/loadline 0.1.0 → 1.1.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 (115) hide show
  1. package/README.md +272 -45
  2. package/dist/cli/cli/args.js +97 -116
  3. package/dist/cli/cli/gates.js +80 -14
  4. package/dist/cli/cli/main.js +275 -31
  5. package/dist/cli/cli/memory.js +114 -0
  6. package/dist/cli/cli/read/declared.js +52 -0
  7. package/dist/cli/cli/read/locate.js +214 -0
  8. package/dist/cli/cli/{read-build.js → read/read-build.js} +156 -87
  9. package/dist/cli/cli/read/read-config.js +120 -0
  10. package/dist/cli/cli/render/html-report.js +111 -0
  11. package/dist/cli/cli/render/render-agent.js +114 -0
  12. package/dist/cli/cli/render/render-asked.js +104 -0
  13. package/dist/cli/cli/render/render-badge.js +140 -0
  14. package/dist/cli/cli/{render-json.js → render/render-json.js} +26 -10
  15. package/dist/cli/cli/{render-markdown.js → render/render-markdown.js} +6 -6
  16. package/dist/cli/cli/{render-pr.js → render/render-pr.js} +37 -15
  17. package/dist/cli/cli/{render-sarif.js → render/render-sarif.js} +11 -6
  18. package/dist/cli/cli/{render-text.js → render/render-text.js} +169 -127
  19. package/dist/cli/cli/render/terminal.js +55 -0
  20. package/dist/cli/cli/report.js +76 -29
  21. package/dist/cli/cli/self-check.js +19 -23
  22. package/dist/cli/cli/since-last.js +45 -0
  23. package/dist/cli/cli/text/text-errors.js +124 -0
  24. package/dist/cli/cli/text/text.js +343 -0
  25. package/dist/cli/cli/text/usage.js +240 -0
  26. package/dist/cli/src/app/core/analysis/analysis.js +68 -24
  27. package/dist/cli/src/app/core/analysis/{barrels.js → graph/barrels.js} +10 -3
  28. package/dist/cli/src/app/core/analysis/{blast.js → graph/blast.js} +56 -17
  29. package/dist/cli/src/app/core/analysis/{cycles.js → graph/cycles.js} +1 -1
  30. package/dist/cli/src/app/core/analysis/{importers.js → graph/importers.js} +1 -1
  31. package/dist/cli/src/app/core/analysis/{insights.js → graph/insights.js} +10 -4
  32. package/dist/cli/src/app/core/analysis/invariant.js +1 -1
  33. package/dist/cli/src/app/core/analysis/metafile.types.js +21 -7
  34. package/dist/cli/src/app/core/analysis/{delivery.js → screens/delivery.js} +1 -1
  35. package/dist/cli/src/app/core/analysis/{entries.js → screens/entries.js} +87 -11
  36. package/dist/cli/src/app/core/analysis/screens/marks.js +32 -0
  37. package/dist/cli/src/app/core/analysis/screens/route-files.js +116 -0
  38. package/dist/cli/src/app/core/analysis/{sourcemap.js → sourcemap/sourcemap.js} +106 -7
  39. package/dist/cli/src/app/core/analysis/{split-drift.js → sourcemap/split-drift.js} +13 -7
  40. package/dist/cli/src/app/core/analysis/tool.js +83 -0
  41. package/dist/cli/src/app/core/assets/assets.js +137 -16
  42. package/dist/cli/src/app/core/baseline/baseline.js +0 -0
  43. package/dist/cli/src/app/core/{intake → build-text}/index-html.js +234 -29
  44. package/dist/cli/src/app/core/build-text/route-table.js +178 -0
  45. package/dist/cli/src/app/core/build-text/tool-marks.js +54 -0
  46. package/dist/cli/src/app/core/caching/caching.js +123 -45
  47. package/dist/cli/src/app/core/caching/from-analysis.js +20 -4
  48. package/dist/cli/src/app/core/config/config-text.js +90 -0
  49. package/dist/cli/src/app/core/config/config-values.js +358 -0
  50. package/dist/cli/src/app/core/config/loadline-config.js +81 -16
  51. package/dist/cli/src/app/core/config/merge-config.js +98 -0
  52. package/dist/cli/src/app/core/deps/deps.js +141 -23
  53. package/dist/cli/src/app/core/deps/pins.js +63 -0
  54. package/dist/cli/src/app/core/export/summary.js +54 -12
  55. package/dist/cli/src/app/core/findings/actions.js +65 -6
  56. package/dist/cli/src/app/core/findings/bundle/boot-single.js +76 -0
  57. package/dist/cli/src/app/core/findings/bundle/dupes.js +68 -0
  58. package/dist/cli/src/app/core/findings/{graph.js → bundle/graph.js} +32 -20
  59. package/dist/cli/src/app/core/findings/{own-in-boot.js → bundle/own-in-boot.js} +9 -5
  60. package/dist/cli/src/app/core/findings/{requests.js → bundle/requests.js} +3 -3
  61. package/dist/cli/src/app/core/findings/{shipped.js → bundle/shipped.js} +30 -18
  62. package/dist/cli/src/app/core/findings/compose.js +6 -1
  63. package/dist/cli/src/app/core/findings/effort.js +4 -0
  64. package/dist/cli/src/app/core/findings/finding.types.js +2 -0
  65. package/dist/cli/src/app/core/findings/findings.js +68 -55
  66. package/dist/cli/src/app/core/findings/{assets.js → folder/assets.js} +65 -16
  67. package/dist/cli/src/app/core/findings/{caching.js → folder/caching.js} +17 -10
  68. package/dist/cli/src/app/core/findings/{page.js → folder/page.js} +6 -2
  69. package/dist/cli/src/app/core/findings/{scan.js → folder/scan.js} +37 -13
  70. package/dist/cli/src/app/core/findings/kind-names.js +163 -0
  71. package/dist/cli/src/app/core/findings/{deps.js → supplied/deps.js} +13 -8
  72. package/dist/cli/src/app/core/findings/supplied/forbidden.js +71 -0
  73. package/dist/cli/src/app/core/findings/{situation.js → supplied/situation.js} +2 -2
  74. package/dist/cli/src/app/core/findings/text/causes.js +39 -0
  75. package/dist/cli/src/app/core/findings/text/finding-html.js +33 -0
  76. package/dist/cli/src/app/core/findings/{finding-text.js → text/finding-text.js} +307 -103
  77. package/dist/cli/src/app/core/findings/text/tool-advice.js +97 -0
  78. package/dist/cli/src/app/core/format/format.utils.js +172 -18
  79. package/dist/cli/src/app/core/format/ownership.js +93 -0
  80. package/dist/cli/src/app/core/i18n/en.js +180 -61
  81. package/dist/cli/src/app/core/i18n/es.js +192 -68
  82. package/dist/cli/src/app/core/intake/bundle-graph.js +285 -46
  83. package/dist/cli/src/app/core/intake/dist-files.js +87 -16
  84. package/dist/cli/src/app/core/intake/embedded.js +56 -0
  85. package/dist/cli/src/app/core/intake/unreached.js +139 -0
  86. package/dist/cli/src/app/core/intake/webpack-modules.js +185 -0
  87. package/dist/cli/src/app/core/intake/webpack-stats.js +298 -0
  88. package/dist/cli/src/app/core/project/budget-advice.js +14 -10
  89. package/dist/cli/src/app/core/project/project-context.js +89 -19
  90. package/dist/cli/src/app/core/project/project-name.js +36 -0
  91. package/dist/cli/src/app/core/scan/scan.js +116 -39
  92. package/dist/cli/src/app/core/scan/secrets.js +0 -0
  93. package/dist/cli/src/app/core/situation/situation-text.js +23 -0
  94. package/dist/cli/src/app/core/situation/situation.js +9 -8
  95. package/dist/cli/src/app/core/timing/timing.js +5 -2
  96. package/dist/cli/src/app/core/whatif/defer.js +29 -4
  97. package/dist/cli/src/app/core/whatif/source-importers.js +58 -0
  98. package/dist/cli/src/app/core/whatif/why.js +102 -0
  99. package/dist/cli/src/app/state/report-messages.utils.js +1 -4
  100. package/docs/CONFIG-REFERENCE.md +230 -0
  101. package/docs/CONFIG.md +511 -0
  102. package/docs/HOW-IT-WORKS.md +286 -25
  103. package/loadline.html +26 -24
  104. package/loadline.schema.json +1209 -0
  105. package/package.json +71 -35
  106. package/dist/cli/cli/text.js +0 -180
  107. package/dist/cli/src/app/core/findings/finding-html.js +0 -13
  108. /package/dist/cli/cli/{read-build.types.js → read/read-build.types.js} +0 -0
  109. /package/dist/cli/cli/{render-json.types.js → render/render-json.types.js} +0 -0
  110. /package/dist/cli/cli/{text.types.js → text/text.types.js} +0 -0
  111. /package/dist/cli/src/app/core/analysis/{exclusive.js → graph/exclusive.js} +0 -0
  112. /package/dist/cli/src/app/core/analysis/{insights.types.js → graph/insights.types.js} +0 -0
  113. /package/dist/cli/src/app/core/analysis/{module-graph.js → graph/module-graph.js} +0 -0
  114. /package/dist/cli/src/app/core/analysis/{sourcemap.types.js → sourcemap/sourcemap.types.js} +0 -0
  115. /package/dist/cli/src/app/core/findings/{finding-plain.js → text/finding-plain.js} +0 -0
@@ -7,24 +7,63 @@
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.run = exports.UNUSABLE = exports.FAILED = exports.OK = void 0;
9
9
  const promises_1 = require("node:fs/promises");
10
+ const node_path_1 = require("node:path");
11
+ const loadline_config_1 = require("../src/app/core/config/loadline-config");
12
+ const pins_1 = require("../src/app/core/deps/pins");
10
13
  const summary_1 = require("../src/app/core/export/summary");
14
+ const ownership_1 = require("../src/app/core/format/ownership");
11
15
  const ui_1 = require("../src/app/core/i18n/ui");
16
+ const project_context_1 = require("../src/app/core/project/project-context");
12
17
  const report_messages_utils_1 = require("../src/app/state/report-messages.utils");
13
18
  const args_1 = require("./args");
14
19
  const gates_1 = require("./gates");
15
- const read_build_1 = require("./read-build");
16
- const render_json_1 = require("./render-json");
17
- const render_markdown_1 = require("./render-markdown");
18
- const render_pr_1 = require("./render-pr");
19
- const render_sarif_1 = require("./render-sarif");
20
- const render_text_1 = require("./render-text");
20
+ const memory_1 = require("./memory");
21
+ const declared_1 = require("./read/declared");
22
+ const locate_1 = require("./read/locate");
23
+ const read_build_1 = require("./read/read-build");
24
+ const read_config_1 = require("./read/read-config");
25
+ const html_report_1 = require("./render/html-report");
26
+ const render_agent_1 = require("./render/render-agent");
27
+ const render_badge_1 = require("./render/render-badge");
28
+ const render_json_1 = require("./render/render-json");
29
+ const render_markdown_1 = require("./render/render-markdown");
30
+ const render_pr_1 = require("./render/render-pr");
31
+ const render_sarif_1 = require("./render/render-sarif");
32
+ const render_text_1 = require("./render/render-text");
21
33
  const report_1 = require("./report");
22
34
  const self_check_1 = require("./self-check");
23
- const text_1 = require("./text");
35
+ const since_last_1 = require("./since-last");
36
+ const text_1 = require("./text/text");
37
+ const text_errors_1 = require("./text/text-errors");
38
+ const usage_1 = require("./text/usage");
39
+ /** The formats that print the `--what-if` answers. */
40
+ const WHAT_IF_FORMATS = new Set(['text', 'summary', 'json', 'agent']);
41
+ /** And the `--why` ones: a chain of imports does not fit in a paste, so not the summary. */
42
+ const WHY_FORMATS = new Set(['text', 'json', 'agent']);
24
43
  /** 0 ran clean · 1 a gate broke · 2 the arguments or the files could not be used. */
25
44
  exports.OK = 0;
26
45
  exports.FAILED = 1;
27
46
  exports.UNUSABLE = 2;
47
+ /**
48
+ * The build as the report names it: relative to where the command ran, with `/`. It ends up in the
49
+ * exported baseline and in SARIF, which travel to other machines: an absolute `C:\Users\…` there
50
+ * carried somebody's home folder into an artifact and read differently on a Linux runner.
51
+ */
52
+ const portableName = (target) => {
53
+ const named = (0, node_path_1.relative)(process.cwd(), (0, node_path_1.resolve)(target)).replaceAll('\\', '/');
54
+ return named === '' ? '.' : named;
55
+ };
56
+ /**
57
+ * What went wrong, as a word a script can branch on: the analysis's own codes (`NO_PAGE`,
58
+ * `NOT_ESM_GRAPH`…) as they were thrown, `input` for what the command was given, `unexpected` for
59
+ * the rest. The sentence beside it is for people and changes with the language.
60
+ */
61
+ const codeOf = (error) => {
62
+ if (error instanceof read_build_1.InputError) {
63
+ return 'input';
64
+ }
65
+ return error instanceof Error && /^[A-Z][A-Z_]+$/.test(error.message) ? error.message : 'unexpected';
66
+ };
28
67
  const write = (stream, value) => {
29
68
  stream.write(`${value}\n`);
30
69
  };
@@ -40,31 +79,156 @@ const NO_DIST = {
40
79
  texts: new Map(),
41
80
  maps: [],
42
81
  };
82
+ const fileExists = async (path) => {
83
+ try {
84
+ const info = await (0, promises_1.stat)(path);
85
+ return info.isFile();
86
+ }
87
+ catch {
88
+ return false;
89
+ }
90
+ };
91
+ /**
92
+ * Where each file nothing names lives in the repository, when `angular.json` copies it from an
93
+ * assets folder **and it is really there**. The page can only say "comes from"; the command looks.
94
+ */
95
+ const assetSourcesOf = async (assets, context, project) => {
96
+ const folders = context.angular?.assetFolders ?? [];
97
+ if (!assets || !project || folders.length === 0) {
98
+ return null;
99
+ }
100
+ const found = new Map();
101
+ const mapped = (0, project_context_1.repoPathsOf)(assets.unreferenced, folders);
102
+ const pairs = [...(mapped ?? [])];
103
+ for (const [path, repo] of pairs) {
104
+ if (await fileExists((0, node_path_1.join)(project, repo))) {
105
+ found.set(path, repo);
106
+ }
107
+ }
108
+ return found;
109
+ };
43
110
  /** Everything off the disk, in one place, so the analysis starts with nothing left to fetch. */
44
111
  const readInputs = async (options) => {
45
112
  // A folder is the build itself, so it is both what is analysed and where the figures come from.
46
113
  const derive = await (0, read_build_1.isFolder)(options.target);
47
114
  if (derive && options.dist) {
48
- throw new read_build_1.InputError('--dist is the folder already being analysed; drop one of the two.');
115
+ throw new read_build_1.InputError(t => t.distTwice);
49
116
  }
50
117
  const folder = derive ? options.target : options.dist;
51
- const dist = folder ? await (0, read_build_1.readDist)(folder, derive) : NO_DIST;
118
+ // Before the folder: `build` of `loadline.json` says what the folder cannot say on its own.
119
+ const configRead = await (0, read_config_1.readLoadlineConfig)(options.config, process.cwd(), options.lang);
120
+ // Yours or theirs, for every path named from here on: the default unless the file says more.
121
+ (0, ownership_1.setOwnership)(configRead.config?.build);
122
+ // `--entry` adds to `build.entries`, for one run, what a file would say for every run.
123
+ const said = configRead.config?.build;
124
+ const build = options.entries.length > 0 ? { ...said, entries: [...(said?.entries ?? []), ...options.entries] } : said;
125
+ const dist = folder ? await (0, read_build_1.readDist)(folder, derive, build) : NO_DIST;
126
+ const meta = dist.graph ? dist.graph.meta : await (0, read_build_1.readStats)(options.target, ui_1.UI[options.lang]);
127
+ const deps = await (0, read_build_1.readDepsFiles)(options.lock, options.audit);
128
+ const context = options.project ? await (0, read_build_1.readProject)(options.project) : { angular: null, pkg: null, pipelines: [] };
129
+ // The ranges each package declares. A lock file of npm or yarn records them; pnpm's does not,
130
+ // and then the `package.json` files in `node_modules` are read — only when a lock file or a
131
+ // project folder says where the install is, since that is where the metafile's paths start.
132
+ const installRoot = options.project ?? (options.lock ? (0, node_path_1.dirname)(options.lock) : null);
133
+ const declared = new Map((0, pins_1.declaredFromLock)(deps.lock));
134
+ if (installRoot && (options.lock || options.project)) {
135
+ const fromDisk = await (0, declared_1.readDeclared)(meta, installRoot);
136
+ for (const [name, ranges] of fromDisk) {
137
+ declared.set(name, { ...ranges, ...declared.get(name) });
138
+ }
139
+ }
52
140
  return {
53
- meta: dist.graph ? dist.graph.meta : await (0, read_build_1.readStats)(options.target, ui_1.UI[options.lang]),
141
+ meta,
54
142
  parallel: dist.graph?.parallel ?? null,
55
- statsName: options.target,
143
+ routes: dist.graph?.routes ?? null,
144
+ statsName: portableName(options.target),
145
+ packageName: await (0, read_build_1.nearestPackageName)((await (0, read_build_1.isFolder)(options.target)) ? options.target : (0, node_path_1.dirname)(options.target)),
56
146
  ...dist,
57
- ...(await (0, read_build_1.readLoadlineConfig)(options.config, process.cwd()).then(read => ({
58
- config: read.config,
59
- configProblems: read.problems,
60
- configName: read.name,
61
- }))),
62
- ...(await (0, read_build_1.readDepsFiles)(options.lock, options.audit)),
63
- baseline: options.baseline ? await (0, read_build_1.readBaseline)(options.baseline) : null,
64
- context: options.project ? await (0, read_build_1.readProject)(options.project) : { angular: null, pkg: null, pipelines: [] },
65
- criteria: options.criteria ? await (0, read_build_1.readCriteria)(options.criteria) : null,
147
+ config: configRead.config,
148
+ configProblems: configRead.problems,
149
+ configName: configRead.name,
150
+ ...deps,
151
+ declared: declared.size > 0 ? declared : null,
152
+ assetSources: await assetSourcesOf(dist.assets, context, options.project),
153
+ // Named after the file it was given as, which is the name the person knows: an export keeps
154
+ // the name of the build it was made from, and "compared against dist/app/stats.json" read
155
+ // as the wrong file when base.json is what was passed.
156
+ baseline: options.baseline
157
+ ? { ...(await (0, read_build_1.readBaseline)(options.baseline)), name: portableName(options.baseline) }
158
+ : null,
159
+ // Remembered only when nothing was asked for explicitly: a `--baseline` is a decision, the
160
+ // memory is a convenience, and the two are never mixed in one report.
161
+ ...(await lastRunOf(options)),
162
+ context,
163
+ criteria: options.criteria ? await (0, read_build_1.readCriteria)(options.criteria, options.lang) : null,
66
164
  };
67
165
  };
166
+ /**
167
+ * The build folder that would hold the `--html` page, if any. The next build empties that folder,
168
+ * and the page somebody meant to keep — or to attach to an issue tomorrow — is gone with it.
169
+ */
170
+ const buildFolderHolding = (html, options) => {
171
+ const page = (0, node_path_1.resolve)(html);
172
+ const folders = [options.dist, options.target].filter((folder) => !!folder);
173
+ return (folders.find(folder => {
174
+ const within = (0, node_path_1.relative)((0, node_path_1.resolve)(folder), page);
175
+ return within !== '' && !within.startsWith('..') && !(0, node_path_1.isAbsolute)(within);
176
+ }) ?? null);
177
+ };
178
+ /**
179
+ * The previous run of this build, and its branch when that is not the one checked out now: a run
180
+ * on another branch is still worth comparing against, but not without saying so.
181
+ */
182
+ const lastRunOf = async (options) => {
183
+ const last = options.cache && !options.baseline ? await (0, memory_1.readLastRun)(process.cwd(), options.target) : null;
184
+ if (!last) {
185
+ return { lastRun: null, lastRunBranch: null };
186
+ }
187
+ const now = await (0, memory_1.branchOf)(process.cwd());
188
+ return { lastRun: last.snapshot, lastRunBranch: last.branch && last.branch !== now ? last.branch : null };
189
+ };
190
+ /** The files the page needs to rebuild this report on its own, read once more as they are. */
191
+ const writePage = async (options) => {
192
+ if (!options.html) {
193
+ return;
194
+ }
195
+ const derive = await (0, read_build_1.isFolder)(options.target);
196
+ const folder = derive ? options.target : options.dist;
197
+ const extras = [
198
+ options.project ? (0, node_path_1.join)(options.project, 'angular.json') : null,
199
+ options.project ? (0, node_path_1.join)(options.project, 'package.json') : null,
200
+ options.lock,
201
+ options.audit,
202
+ // With `--entry` the file goes in as the run read it, below, or the page would not find the entry.
203
+ options.entries.length > 0 ? null : options.config,
204
+ ];
205
+ const texts = [];
206
+ for (const path of extras) {
207
+ if (!(path && (await fileExists(path)))) {
208
+ continue;
209
+ }
210
+ const text = await (0, html_report_1.embedText)(path);
211
+ if (text) {
212
+ texts.push(text);
213
+ }
214
+ }
215
+ if (options.entries.length > 0) {
216
+ const { config } = await (0, read_config_1.readLoadlineConfig)(options.config, process.cwd(), options.lang);
217
+ const entries = [...(config?.build?.entries ?? []), ...options.entries];
218
+ texts.push({ name: 'loadline.json', text: (0, loadline_config_1.writeConfig)({ ...config, build: { ...config?.build, entries } }) });
219
+ }
220
+ await (0, html_report_1.writeHtmlReport)(options.html, {
221
+ version: 1,
222
+ lang: options.lang,
223
+ stats: derive ? null : await (0, html_report_1.embedText)(options.target),
224
+ folder: folder ? await (0, html_report_1.embedFolder)(folder) : null,
225
+ extras: texts,
226
+ baseline: await (0, html_report_1.embedText)(options.baseline),
227
+ });
228
+ if (options.open) {
229
+ (0, html_report_1.openFile)((0, node_path_1.resolve)(options.html));
230
+ }
231
+ };
68
232
  /**
69
233
  * A growth gate with nothing to grow against would pass every build, quietly, forever. Saying so
70
234
  * beats a green pipeline that checks nothing.
@@ -72,37 +236,80 @@ const readInputs = async (options) => {
72
236
  const requireBaseline = (options) => {
73
237
  const wantsGrowth = options.gates.maxGrowth !== null || options.gates.maxGrowthRatio !== null;
74
238
  if (wantsGrowth && !options.baseline) {
75
- throw new read_build_1.InputError('--max-growth and --max-growth-pct need a --baseline to compare against.');
239
+ throw new read_build_1.InputError(t => t.growthNeedsBaseline);
240
+ }
241
+ };
242
+ /**
243
+ * `--print-config`: the `loadline.json` this run would use, with everything it extends joined in,
244
+ * as a file that says it all itself. What `eslint --print-config` and `tsc --showConfig` are for:
245
+ * the answer to "which threshold won?" without reading three files. It is also how the page gets
246
+ * a configuration that extends another — the page has no disk to follow `extends` on, and this is
247
+ * the file to drop on it instead. Sizes come out in bytes, which is what they are once read.
248
+ */
249
+ const printConfig = async (options) => {
250
+ try {
251
+ const read = await (0, read_config_1.readLoadlineConfig)(options.config, process.cwd(), options.lang);
252
+ for (const problem of read.problems) {
253
+ write(process.stderr, problem);
254
+ }
255
+ if (!read.config) {
256
+ throw new read_build_1.InputError(t => t.noConfigToPrint);
257
+ }
258
+ write(process.stdout, (0, loadline_config_1.writeConfig)(read.config).trimEnd());
259
+ write(process.stderr, text_1.CLI_TEXT[options.lang].configRead(read.name ?? ''));
260
+ return exports.OK;
261
+ }
262
+ catch (error) {
263
+ if (error instanceof read_build_1.InputError) {
264
+ write(process.stderr, error.say(text_errors_1.ERROR_TEXT[options.lang]));
265
+ return exports.UNUSABLE;
266
+ }
267
+ throw error;
76
268
  }
77
269
  };
78
270
  const run = async (argv, version) => {
79
271
  const parsed = (0, args_1.parseArgs)(argv);
80
272
  if (!parsed.ok) {
81
- write(process.stderr, `${parsed.message}\n\nRun loadline --help to see the options.`);
273
+ write(process.stderr, `${parsed.message}\n\n${text_errors_1.ERROR_TEXT[parsed.lang].seeHelp}`);
82
274
  return exports.UNUSABLE;
83
275
  }
84
- const options = parsed.options;
276
+ let options = parsed.options;
85
277
  if (options.help) {
86
- write(process.stdout, args_1.USAGE);
278
+ write(process.stdout, usage_1.USAGE[options.lang]);
87
279
  return exports.OK;
88
280
  }
89
281
  if (options.version) {
90
282
  write(process.stdout, `loadline ${version}`);
91
283
  return exports.OK;
92
284
  }
285
+ if (options.printConfig) {
286
+ return printConfig(options);
287
+ }
93
288
  try {
94
289
  requireBaseline(options);
290
+ // The root of a build is enough: the metafile and the browser folder are found inside it.
291
+ const located = await (0, locate_1.locateBuild)(options.target, options.dist);
292
+ options = { ...options, target: located.target, dist: located.dist };
293
+ // On stderr: which build was chosen is said, without getting into what a script parses.
294
+ if (located.others) {
295
+ write(process.stderr, text_1.CLI_TEXT[options.lang].alsoFound(located.others.join(', ')));
296
+ }
95
297
  const input = await readInputs(options);
96
298
  // The gates the file asks for, where the command line did not ask for that one. A flag typed
97
299
  // by hand is somebody overriding the committed decision on purpose, so it wins.
98
300
  const gates = (0, gates_1.mergeGates)(options.gates, input.config ?? null);
99
301
  const report = (0, report_1.buildReport)(input, { ...options, gates });
302
+ report.located = located.found;
303
+ const unchecked = (0, gates_1.uncheckedGates)(report, gates, options.gates);
304
+ if (unchecked.refuse) {
305
+ throw new read_build_1.InputError(unchecked.refuse);
306
+ }
100
307
  // The self-check is about Loadline, not about the build, so it replaces the report rather
101
308
  // than being one more section of it — and it owns the exit code while it is asked for.
102
309
  if (options.selfCheck) {
103
- const checked = (0, self_check_1.selfCheck)(input.meta, report.analysis, input.announced);
310
+ const checked = (0, self_check_1.selfCheck)(input.meta, report.analysis, input.announced, text_errors_1.ERROR_TEXT[options.lang]);
104
311
  write(checked.ok ? process.stdout : process.stderr, checked.report);
105
- return checked.ok ? exports.OK : exports.FAILED;
312
+ return checked.ok ? exports.OK : checked.ran ? exports.FAILED : exports.UNUSABLE;
106
313
  }
107
314
  // Before the gates: a build that broke one is still the build that was made, and a
108
315
  // pipeline that only writes the baseline when it is under budget would compare tomorrow
@@ -114,42 +321,79 @@ const run = async (argv, version) => {
114
321
  // sends somebody to check the file that was read fine.
115
322
  const failed = await (0, promises_1.writeFile)(options.export, JSON.stringify(report.snapshot, null, 2), 'utf8').catch((error) => (error instanceof Error ? error.message : String(error)));
116
323
  if (failed) {
117
- throw new read_build_1.InputError(`Could not write ${options.export}: ${failed}`);
324
+ throw new read_build_1.InputError(t => t.cannotWrite(options.export ?? '', failed));
118
325
  }
326
+ write(process.stderr, text_1.CLI_TEXT[options.lang].exportWritten(options.export));
119
327
  }
120
328
  const asked = (0, gates_1.anyGate)(gates);
121
329
  const violations = (0, gates_1.checkGates)(report, gates);
122
330
  // Colour is for a person watching a terminal; a redirected stream is a file or a log.
123
- const color = options.color && process.stdout.isTTY === true && !process.env['NO_COLOR'];
331
+ const color = options.color && process.stdout.isTTY && !process.env['NO_COLOR'];
124
332
  const rendered = {
125
333
  text: () => (0, render_text_1.renderText)(report, violations, asked, color),
126
334
  json: () => (0, render_json_1.renderJson)(report, violations, asked),
127
335
  markdown: () => (0, render_markdown_1.renderMarkdown)(report, violations, asked),
128
336
  'pr-comment': () => (0, render_pr_1.renderPrComment)(report, violations, asked),
129
337
  sarif: () => (0, render_sarif_1.renderSarif)(report),
338
+ agent: () => (0, render_agent_1.renderAgent)(report, violations, asked),
339
+ badge: () => (0, render_badge_1.renderBadge)(report),
130
340
  summary: () => (0, summary_1.summaryText)({
131
341
  analysis: report.analysis,
132
342
  findings: report.findings,
133
343
  comparison: report.comparison,
134
344
  unit: report.unit,
135
- name: report.statsName,
345
+ // The project first, as the text report heads with it, and the build it was read from.
346
+ name: report.projectName ? `${report.projectName} (${report.statsName})` : report.statsName,
136
347
  firstTrip: report.assets?.firstTrip.total ?? null,
137
348
  lang: report.lang,
349
+ since: (0, since_last_1.sinceLastLine)(report),
350
+ whatIf: report.whatIf,
351
+ ...(asked && { gates: violations.map(violation => violation.message) }),
138
352
  }),
139
353
  };
354
+ // `--what-if` is answered in text, summary and json. The other three are a document for
355
+ // somebody else — a merge request, a code-scanning upload — and had nowhere to put it, so
356
+ // the question used to go unanswered without a word. Now it says where to look.
357
+ if (options.whatIf.length > 0 && !WHAT_IF_FORMATS.has(options.format)) {
358
+ write(process.stderr, text_1.CLI_TEXT[options.lang].whatIfNotHere(options.format));
359
+ }
360
+ if (options.why.length > 0 && !WHY_FORMATS.has(options.format)) {
361
+ write(process.stderr, text_1.CLI_TEXT[options.lang].whyNotHere(options.format));
362
+ }
140
363
  // What is wrong with `loadline.json` goes to stderr, never into the report: something is
141
364
  // parsing stdout, and a warning in the middle of a SARIF document breaks it.
365
+ // Each line already names its file: with `extends` there can be more than one.
142
366
  for (const problem of report.configProblems) {
143
- write(process.stderr, text_1.CLI_TEXT[options.lang].configProblem(problem));
367
+ write(process.stderr, problem);
368
+ }
369
+ for (const line of [...(0, gates_1.unmatchedScreenLimits)(report, gates), ...unchecked.skipped]) {
370
+ write(process.stderr, line);
144
371
  }
145
372
  write(process.stdout, rendered[options.format]());
373
+ // After the report, so a page that fails to write never costs the report somebody piped.
374
+ if (options.html) {
375
+ await writePage(options);
376
+ write(process.stderr, text_1.CLI_TEXT[options.lang].htmlWritten(options.html));
377
+ const inside = buildFolderHolding(options.html, options);
378
+ if (inside) {
379
+ write(process.stderr, text_1.CLI_TEXT[options.lang].htmlInsideBuild(inside));
380
+ }
381
+ }
382
+ if (options.cache) {
383
+ await (0, memory_1.writeLastRun)(process.cwd(), options.target, report.snapshot, await (0, memory_1.branchOf)(process.cwd()));
384
+ }
146
385
  return violations.length > 0 ? exports.FAILED : exports.OK;
147
386
  }
148
387
  catch (error) {
149
388
  // The codes the analysis throws are translated with the same table the page uses; anything
150
389
  // else is printed as it came, because an unexpected failure should not be reworded.
151
- const message = error instanceof read_build_1.InputError ? error.message : (0, report_messages_utils_1.errorMessage)(error, ui_1.UI[options.lang]);
390
+ const message = error instanceof read_build_1.InputError ? error.say(text_errors_1.ERROR_TEXT[options.lang]) : (0, report_messages_utils_1.errorMessage)(error, ui_1.UI[options.lang]);
152
391
  write(process.stderr, message);
392
+ // A step piping `--format json` into `jq` got an empty input and an error about that,
393
+ // rather than the reason. The reason goes to stdout as well, in the shape it asked for.
394
+ if (options.format === 'json') {
395
+ write(process.stdout, JSON.stringify({ tool: 'loadline', ok: false, error: { code: codeOf(error), message } }));
396
+ }
153
397
  return exports.UNUSABLE;
154
398
  }
155
399
  };
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ /**
3
+ * The previous run, remembered without being asked to.
4
+ *
5
+ * `--baseline` is the right tool for a pipeline and the wrong one for a person who has just changed
6
+ * one import and wants to know whether the bootstrap went down: they had to export yesterday's
7
+ * build on purpose, or compare two outputs by hand. So every run leaves its snapshot in
8
+ * `node_modules/.cache/loadline` — where build tools keep what they can always rebuild, already
9
+ * ignored by every repository — and the next run of the same build says what moved.
10
+ *
11
+ * It is a convenience, never a gate: nothing here changes the exit code, and an explicit
12
+ * `--baseline` replaces it entirely. A machine without a `node_modules` above it simply has no
13
+ * memory, and every failure to read or write is silent: losing the comparison is not worth an error.
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.writeLastRun = exports.readLastRun = exports.branchOf = void 0;
17
+ const node_crypto_1 = require("node:crypto");
18
+ const promises_1 = require("node:fs/promises");
19
+ const node_path_1 = require("node:path");
20
+ const baseline_1 = require("../src/app/core/baseline/baseline");
21
+ /** The nearest `node_modules` at or above the folder, which is where the cache belongs. */
22
+ const nodeModulesAbove = async (from) => {
23
+ let folder = (0, node_path_1.resolve)(from);
24
+ for (;;) {
25
+ const candidate = (0, node_path_1.join)(folder, 'node_modules');
26
+ try {
27
+ const info = await (0, promises_1.stat)(candidate);
28
+ if (info.isDirectory()) {
29
+ return candidate;
30
+ }
31
+ }
32
+ catch {
33
+ // Not here; one level up.
34
+ }
35
+ const parent = (0, node_path_1.dirname)(folder);
36
+ if (parent === folder) {
37
+ return null;
38
+ }
39
+ folder = parent;
40
+ }
41
+ };
42
+ /** One file per build, keyed by where it is: two apps of one workspace do not overwrite each other. */
43
+ const fileFor = async (cwd, target) => {
44
+ const modules = await nodeModulesAbove(cwd);
45
+ if (!modules) {
46
+ return null;
47
+ }
48
+ const key = (0, node_crypto_1.createHash)('sha1').update((0, node_path_1.resolve)(target)).digest('hex').slice(0, 12);
49
+ return (0, node_path_1.join)(modules, '.cache', 'loadline', `last-${key}.json`);
50
+ };
51
+ /**
52
+ * The branch checked out where the command runs, read from `.git/HEAD` — or from the folder a
53
+ * worktree's `.git` file points at. `null` outside a repository or on a detached HEAD.
54
+ *
55
+ * The memory is kept per build and not per branch, on purpose: the first run on a new branch is
56
+ * the one that most wants yesterday's `main` to compare against. What was wrong was saying nothing
57
+ * about it, so a run on `feature-a` after one on `feature-b` read as "what my change did". The
58
+ * branch is remembered, and the line names it when it is not this one.
59
+ */
60
+ const branchOf = async (cwd) => {
61
+ let folder = (0, node_path_1.resolve)(cwd);
62
+ for (;;) {
63
+ const dotGit = (0, node_path_1.join)(folder, '.git');
64
+ try {
65
+ const info = await (0, promises_1.stat)(dotGit);
66
+ const pointer = info.isDirectory() ? null : await (0, promises_1.readFile)(dotGit, 'utf8');
67
+ const gitDir = pointer === null ? dotGit : (0, node_path_1.resolve)(folder, pointer.replace(/^gitdir:\s*/, '').trim());
68
+ const text = await (0, promises_1.readFile)((0, node_path_1.join)(gitDir, 'HEAD'), 'utf8');
69
+ const head = text.trim();
70
+ return head.startsWith('ref: refs/heads/') ? head.slice('ref: refs/heads/'.length) : null;
71
+ }
72
+ catch {
73
+ // No repository here; one level up.
74
+ }
75
+ const parent = (0, node_path_1.dirname)(folder);
76
+ if (parent === folder) {
77
+ return null;
78
+ }
79
+ folder = parent;
80
+ }
81
+ };
82
+ exports.branchOf = branchOf;
83
+ const readLastRun = async (cwd, target) => {
84
+ const file = await fileFor(cwd, target);
85
+ if (!file) {
86
+ return null;
87
+ }
88
+ try {
89
+ const parsed = JSON.parse(await (0, promises_1.readFile)(file, 'utf8'));
90
+ if (!(0, baseline_1.isSnapshot)(parsed)) {
91
+ return null;
92
+ }
93
+ const branch = parsed.branch;
94
+ return { snapshot: parsed, branch: typeof branch === 'string' ? branch : null };
95
+ }
96
+ catch {
97
+ return null;
98
+ }
99
+ };
100
+ exports.readLastRun = readLastRun;
101
+ const writeLastRun = async (cwd, target, snapshot, branch) => {
102
+ const file = await fileFor(cwd, target);
103
+ if (!file) {
104
+ return;
105
+ }
106
+ try {
107
+ await (0, promises_1.mkdir)((0, node_path_1.dirname)(file), { recursive: true });
108
+ await (0, promises_1.writeFile)(file, JSON.stringify({ ...snapshot, branch }), 'utf8');
109
+ }
110
+ catch {
111
+ // A read-only checkout or a full disk: the report is still right, it just will not remember.
112
+ }
113
+ };
114
+ exports.writeLastRun = writeLastRun;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ /**
3
+ * What each package in the bundle declares as its dependencies, read from its own `package.json`
4
+ * in `node_modules`.
5
+ *
6
+ * pnpm's lock file records which version was resolved and not which range was asked for, and the
7
+ * difference is the whole question when a package is duplicated: `idb` resolved to `7.1.1` says
8
+ * nothing; `@firebase/app` asking for exactly `7.1.1` says the copy cannot move. The `package.json`
9
+ * of the parent is on disk next to the code that was bundled, so the command reads it — only for
10
+ * the packages the metafile names, and only when a lock file or a project folder says where the
11
+ * install lives.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.readDeclared = void 0;
15
+ const promises_1 = require("node:fs/promises");
16
+ const node_path_1 = require("node:path");
17
+ const json_utils_1 = require("../../src/app/core/json/json.utils");
18
+ /** `…/node_modules/@scope/name/` or `…/node_modules/name/`: the folder a package is installed in. */
19
+ const PACKAGE_DIR = /^(.*node_modules\/(?:@[^/]+\/)?[^/]+)\//;
20
+ /**
21
+ * @param meta the metafile: its input paths are where each bundled package is installed, relative
22
+ * to the folder the build ran in.
23
+ * @param root that folder.
24
+ */
25
+ const readDeclared = async (meta, root) => {
26
+ const folders = new Set();
27
+ const inputs = Object.keys(meta.inputs);
28
+ for (const input of inputs) {
29
+ const folder = PACKAGE_DIR.exec(input.replaceAll('\\', '/'))?.[1];
30
+ if (folder) {
31
+ folders.add(folder);
32
+ }
33
+ }
34
+ const declared = new Map();
35
+ for (const folder of folders) {
36
+ try {
37
+ const manifest = (0, json_utils_1.asRecord)(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(root, folder, 'package.json'), 'utf8')));
38
+ const name = (0, json_utils_1.asText)(manifest?.['name']);
39
+ const dependencies = (0, json_utils_1.asRecord)(manifest?.['dependencies']);
40
+ if (!name || !dependencies) {
41
+ continue;
42
+ }
43
+ const ranges = Object.fromEntries(Object.entries(dependencies).filter((pair) => typeof pair[1] === 'string'));
44
+ declared.set(name, { ...declared.get(name), ...ranges });
45
+ }
46
+ catch {
47
+ // Not installed here, or not readable: that package simply has no declared ranges.
48
+ }
49
+ }
50
+ return declared;
51
+ };
52
+ exports.readDeclared = readDeclared;