@zharp/cli 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/dist/index.mjs CHANGED
@@ -23,7 +23,7 @@ function resolveConnection(options) {
23
23
  function artifactsUrl(connection) {
24
24
  return `${connection.url}/api/v1/applications/${encodeURIComponent(String(connection.app))}/artifacts`;
25
25
  }
26
- var sleep = (ms) => new Promise((resolve3) => setTimeout(resolve3, ms));
26
+ var sleep = (ms) => new Promise((resolve4) => setTimeout(resolve4, ms));
27
27
  async function uploadArtifact(connection, artifact, options = {}) {
28
28
  const doFetch = options.fetch ?? fetch;
29
29
  const retries = options.retries ?? 3;
@@ -105,6 +105,9 @@ async function eachLimit(items, limit, fn) {
105
105
 
106
106
  // src/debug-id.ts
107
107
  import { createHash } from "crypto";
108
+ import { existsSync, readFileSync, statSync } from "fs";
109
+ import { dirname, isAbsolute, join, resolve } from "path";
110
+ import { fileURLToPath } from "url";
108
111
  var DEBUG_ID_COMMENT = "//# debugId=";
109
112
  var DEBUG_ID_PATTERN = /^\/\/# debugId=([0-9a-fA-F-]{36})\s*$/m;
110
113
  var SOURCE_MAPPING_URL = /^\/\/[#@] sourceMappingURL=(.*?)\s*$/;
@@ -173,15 +176,76 @@ function withoutSourceMappingUrl(code) {
173
176
  if (end > 0 && SOURCE_MAPPING_URL.test(lines[end - 1].trim())) return lines.slice(0, end - 1).join(eol) + eol;
174
177
  return code;
175
178
  }
176
- function injectMap(mapText, debugId) {
179
+ function injectMap(mapText, debugId, mapPath) {
177
180
  const map = JSON.parse(mapText);
178
181
  if (!map || typeof map !== "object") throw new Error("not a source map");
182
+ const filled = mapPath ? fillSourcesContent(map, mapPath) : 0;
179
183
  const hasSourcesContent = Array.isArray(map.sourcesContent) && map.sourcesContent.some((s) => typeof s === "string");
180
- if (map.debugId === debugId && map.debug_id === debugId) return { text: mapText, changed: false, hasSourcesContent };
184
+ if (!filled && map.debugId === debugId && map.debug_id === debugId) return { text: mapText, changed: false, hasSourcesContent };
181
185
  map.debugId = debugId;
182
186
  map.debug_id = debugId;
183
187
  return { text: JSON.stringify(map), changed: true, hasSourcesContent };
184
188
  }
189
+ var MAX_SOURCE_BYTES = 1e6;
190
+ function sourcePathCandidates(source, mapPath, sourceRoot = "") {
191
+ const mapDir = dirname(mapPath);
192
+ let stripped = source;
193
+ let projectRelative = false;
194
+ if (/^turbopack:\/\/\/\[project\]\//.test(source)) {
195
+ stripped = source.replace(/^turbopack:\/\/\/\[project\]\//, "");
196
+ projectRelative = true;
197
+ } else if (/^webpack:\/\//.test(source)) {
198
+ stripped = source.replace(/^webpack:\/\/[^/]*\//, "").replace(/^\.\//, "");
199
+ projectRelative = true;
200
+ } else if (/^file:\/\//.test(source)) {
201
+ try {
202
+ return [fileURLToPath(source)];
203
+ } catch {
204
+ return [];
205
+ }
206
+ } else if (/^[a-z][\w+.-]*:\/\//i.test(source)) {
207
+ return [];
208
+ }
209
+ if (isAbsolute(stripped)) return [stripped];
210
+ const out = [resolve(mapDir, sourceRoot, stripped)];
211
+ if (projectRelative) {
212
+ for (let dir = mapDir, i = 0; i < 12; i++) {
213
+ if (existsSync(join(dir, "package.json"))) {
214
+ out.unshift(join(dir, stripped));
215
+ break;
216
+ }
217
+ const up = dirname(dir);
218
+ if (up === dir) break;
219
+ dir = up;
220
+ }
221
+ }
222
+ return out;
223
+ }
224
+ function fillSourcesContent(map, mapPath) {
225
+ if (!Array.isArray(map.sources)) return 0;
226
+ const contents = Array.isArray(map.sourcesContent) ? [...map.sourcesContent] : [];
227
+ let filled = 0;
228
+ map.sources.forEach((source, i) => {
229
+ if (typeof contents[i] === "string" || typeof source !== "string") return;
230
+ if (/node_modules|^\[externals\]|\(ignored\)|^webpack\/runtime/.test(source)) return;
231
+ for (const path of sourcePathCandidates(source, mapPath, typeof map.sourceRoot === "string" ? map.sourceRoot : "")) {
232
+ if (/[\\/]node_modules[\\/]/.test(path)) continue;
233
+ try {
234
+ const stat = statSync(path);
235
+ if (!stat.isFile() || stat.size > MAX_SOURCE_BYTES) continue;
236
+ contents[i] = readFileSync(path, "utf8");
237
+ filled++;
238
+ break;
239
+ } catch {
240
+ }
241
+ }
242
+ });
243
+ if (filled) {
244
+ while (contents.length < map.sources.length) contents.push(null);
245
+ map.sourcesContent = contents.map((c) => typeof c === "string" ? c : null);
246
+ }
247
+ return filled;
248
+ }
185
249
 
186
250
  // src/elf.ts
187
251
  var NT_GNU_BUILD_ID = 3;
@@ -250,19 +314,19 @@ function hex(bytes) {
250
314
  }
251
315
 
252
316
  // src/files.ts
253
- import { readdirSync, statSync } from "fs";
254
- import { join, relative, sep } from "path";
317
+ import { readdirSync, statSync as statSync2 } from "fs";
318
+ import { join as join2, relative, sep } from "path";
255
319
  function walk(dir) {
256
320
  const out = [];
257
321
  const visit = (current) => {
258
322
  for (const entry of readdirSync(current, { withFileTypes: true })) {
259
- const full = join(current, entry.name);
323
+ const full = join2(current, entry.name);
260
324
  if (entry.isDirectory()) {
261
325
  if (entry.name !== "node_modules") visit(full);
262
326
  } else if (entry.isFile()) out.push(full);
263
327
  else if (entry.isSymbolicLink()) {
264
328
  try {
265
- if (statSync(full).isFile()) out.push(full);
329
+ if (statSync2(full).isFile()) out.push(full);
266
330
  } catch {
267
331
  }
268
332
  }
@@ -390,8 +454,8 @@ function formatUuid(bytes) {
390
454
  }
391
455
 
392
456
  // src/native.ts
393
- import { existsSync, readdirSync as readdirSync2, readFileSync, statSync as statSync2 } from "fs";
394
- import { basename, dirname, join as join2, resolve } from "path";
457
+ import { existsSync as existsSync2, readdirSync as readdirSync2, readFileSync as readFileSync2, statSync as statSync3 } from "fs";
458
+ import { basename, dirname as dirname2, join as join3, resolve as resolve2 } from "path";
395
459
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
396
460
  async function send(connection, artifacts, options) {
397
461
  const logger = options.logger ?? consoleLogger();
@@ -409,16 +473,16 @@ async function send(connection, artifacts, options) {
409
473
  }
410
474
  async function uploadProguard(mappingFile, uuid, connection, options = {}) {
411
475
  if (!UUID.test(uuid)) throw new Error(`--uuid must be a UUID, got "${uuid}".`);
412
- const data = readFileSync(mappingFile);
476
+ const data = readFileSync2(mappingFile);
413
477
  return send(connection, [{ kind: "proguard", debugId: uuid.toLowerCase(), name: basename(mappingFile), release: options.release, data, gzip: true }], options);
414
478
  }
415
479
  var DART_ARCH = { x86_64: "x64", x86: "ia32" };
416
480
  function findDartSymbols(path, logger = consoleLogger()) {
417
- const files = statSync2(path).isDirectory() ? walk(resolve(path)) : [resolve(path)];
481
+ const files = statSync3(path).isDirectory() ? walk(resolve2(path)) : [resolve2(path)];
418
482
  const out = [];
419
483
  for (const file of files) {
420
484
  const named = /\.symbols$/.test(file);
421
- const data = readFileSync(file);
485
+ const data = readFileSync2(file);
422
486
  if (!isElf(data)) {
423
487
  if (named) logger.warn(`Skipped ${basename(file)}: not an ELF file.`);
424
488
  continue;
@@ -434,8 +498,8 @@ function findDartSymbols(path, logger = consoleLogger()) {
434
498
  return out;
435
499
  }
436
500
  function findObfuscationMap(symbolsPath) {
437
- const dir = statSync2(symbolsPath).isDirectory() ? resolve(symbolsPath) : dirname(resolve(symbolsPath));
438
- for (const where of [dir, dirname(dir)]) {
501
+ const dir = statSync3(symbolsPath).isDirectory() ? resolve2(symbolsPath) : dirname2(resolve2(symbolsPath));
502
+ for (const where of [dir, dirname2(dir)]) {
439
503
  let names;
440
504
  try {
441
505
  names = readdirSync2(where).sort();
@@ -443,12 +507,12 @@ function findObfuscationMap(symbolsPath) {
443
507
  continue;
444
508
  }
445
509
  const found = names.find((n) => n === "obfuscation.json") ?? names.find((n) => n.endsWith(".obfuscation.json"));
446
- if (found && statSync2(join2(where, found)).isFile()) return join2(where, found);
510
+ if (found && statSync3(join3(where, found)).isFile()) return join3(where, found);
447
511
  }
448
512
  return void 0;
449
513
  }
450
514
  function readObfuscationMap(file) {
451
- const data = readFileSync(file);
515
+ const data = readFileSync2(file);
452
516
  let parsed;
453
517
  try {
454
518
  parsed = JSON.parse(data.toString("utf8"));
@@ -469,10 +533,10 @@ async function uploadDartSymbols(path, connection, options = {}) {
469
533
  name: basename(f.path),
470
534
  arch: f.arch,
471
535
  release: options.release,
472
- data: readFileSync(f.path),
536
+ data: readFileSync2(f.path),
473
537
  gzip: true
474
538
  }));
475
- if (options.obfuscationMap && !existsSync(options.obfuscationMap)) throw new Error(`${options.obfuscationMap} does not exist.`);
539
+ if (options.obfuscationMap && !existsSync2(options.obfuscationMap)) throw new Error(`${options.obfuscationMap} does not exist.`);
476
540
  const mapFile = options.obfuscationMap ?? findObfuscationMap(path);
477
541
  if (mapFile) {
478
542
  const data = readObfuscationMap(mapFile);
@@ -487,11 +551,11 @@ async function uploadDartSymbols(path, connection, options = {}) {
487
551
  return send(connection, artifacts, options);
488
552
  }
489
553
  function findDsymImages(path) {
490
- const root = resolve(path);
491
- const candidates = statSync2(root).isDirectory() ? walk(root).filter((f) => /[\\/]Contents[\\/]Resources[\\/]DWARF[\\/][^\\/]+$/.test(f)) : [root];
554
+ const root = resolve2(path);
555
+ const candidates = statSync3(root).isDirectory() ? walk(root).filter((f) => /[\\/]Contents[\\/]Resources[\\/]DWARF[\\/][^\\/]+$/.test(f)) : [root];
492
556
  const out = [];
493
557
  for (const file of candidates) {
494
- const data = readFileSync(file);
558
+ const data = readFileSync2(file);
495
559
  if (!isMachO(data)) continue;
496
560
  for (const image of readMachO(data)) out.push({ path: file, ...image });
497
561
  }
@@ -505,7 +569,7 @@ async function uploadDsyms(path, connection, options = {}) {
505
569
  const cache = /* @__PURE__ */ new Map();
506
570
  const artifacts = images.map((image) => {
507
571
  let data = cache.get(image.path);
508
- if (!data) cache.set(image.path, data = readFileSync(image.path));
572
+ if (!data) cache.set(image.path, data = readFileSync2(image.path));
509
573
  return {
510
574
  kind: "dsym",
511
575
  debugId: image.uuid,
@@ -521,13 +585,13 @@ async function uploadDsyms(path, connection, options = {}) {
521
585
  }
522
586
 
523
587
  // src/sourcemaps.ts
524
- import { existsSync as existsSync2, readFileSync as readFileSync2, unlinkSync, writeFileSync } from "fs";
525
- import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
588
+ import { existsSync as existsSync3, readFileSync as readFileSync3, unlinkSync, writeFileSync } from "fs";
589
+ import { dirname as dirname3, isAbsolute as isAbsolute2, resolve as resolve3 } from "path";
526
590
  var JS_FILE = /\.(?:js|mjs|cjs)$/;
527
591
  function mapFor(file, code) {
528
592
  const sibling = `${file}.map`;
529
- if (existsSync2(sibling)) return sibling;
530
- const url = sourceMappingUrl(code ?? readFileSync2(file, "utf8"));
593
+ if (existsSync3(sibling)) return sibling;
594
+ const url = sourceMappingUrl(code ?? readFileSync3(file, "utf8"));
531
595
  if (!url || /^[a-z][a-z0-9+.-]*:/i.test(url)) return void 0;
532
596
  let path;
533
597
  try {
@@ -535,16 +599,16 @@ function mapFor(file, code) {
535
599
  } catch {
536
600
  return void 0;
537
601
  }
538
- const full = isAbsolute(path) ? path : resolve2(dirname2(file), path);
539
- return existsSync2(full) ? full : void 0;
602
+ const full = isAbsolute2(path) ? path : resolve3(dirname3(file), path);
603
+ return existsSync3(full) ? full : void 0;
540
604
  }
541
605
  function findSourceFiles(dir, options = {}) {
542
- const root = resolve2(dir);
606
+ const root = resolve3(dir);
543
607
  const keep = pathFilter(options.include, options.exclude);
544
- const candidates = options.files ? options.files.map((f) => resolve2(f)) : walk(root);
608
+ const candidates = options.files ? options.files.map((f) => resolve3(f)) : walk(root);
545
609
  const out = [];
546
610
  for (const file of candidates) {
547
- if (!JS_FILE.test(file) || !existsSync2(file)) continue;
611
+ if (!JS_FILE.test(file) || !existsSync3(file)) continue;
548
612
  const name = relativePath(root, file);
549
613
  if (!keep(name)) continue;
550
614
  const map = mapFor(file);
@@ -558,11 +622,11 @@ function injectDirectory(dir, options = {}) {
558
622
  const out = [];
559
623
  const noSourcesContent = [];
560
624
  for (const source of files) {
561
- const code = readFileSync2(source.file, "utf8");
625
+ const code = readFileSync3(source.file, "utf8");
562
626
  const injected = injectCode(code, { stripSourceMappingUrl: options.stripSourceMappingUrl });
563
627
  let map;
564
628
  try {
565
- map = injectMap(readFileSync2(source.map, "utf8"), injected.debugId);
629
+ map = injectMap(readFileSync3(source.map, "utf8"), injected.debugId, source.map);
566
630
  } catch (error) {
567
631
  logger.warn(`Skipped ${source.name}: ${source.mapName} is not a readable source map (${error.message}).`);
568
632
  continue;
@@ -590,13 +654,13 @@ async function uploadSourcemaps(dir, connection, options = {}) {
590
654
  const result = { files: 0, artifacts: 0, bytes: 0, deleted: [], skipped: [] };
591
655
  const work = [];
592
656
  for (const source of files) {
593
- const code = readFileSync2(source.file);
657
+ const code = readFileSync3(source.file);
594
658
  const debugId = existingDebugId(code.toString("utf8"));
595
659
  if (!debugId) {
596
660
  result.skipped.push(source.name);
597
661
  continue;
598
662
  }
599
- work.push({ source, debugId, code, map: readFileSync2(source.map) });
663
+ work.push({ source, debugId, code, map: readFileSync3(source.map) });
600
664
  }
601
665
  if (result.skipped.length) {
602
666
  logger.warn(`${result.skipped.length} file(s) have a source map but no debug ID \u2014 run \`zharp sourcemaps inject\` first: ${result.skipped.slice(0, 3).join(", ")}${result.skipped.length > 3 ? ", \u2026" : ""}`);
@@ -633,12 +697,12 @@ async function uploadSourcemaps(dir, connection, options = {}) {
633
697
  function deleteSourcemaps(files, options = {}) {
634
698
  const deleted = [];
635
699
  for (const source of files) {
636
- if (options.stripSourceMappingUrl && existsSync2(source.file)) {
637
- const code = readFileSync2(source.file, "utf8");
700
+ if (options.stripSourceMappingUrl && existsSync3(source.file)) {
701
+ const code = readFileSync3(source.file, "utf8");
638
702
  const stripped = withoutSourceMappingUrl(code);
639
703
  if (stripped !== code) writeFileSync(source.file, stripped);
640
704
  }
641
- if (existsSync2(source.map)) {
705
+ if (existsSync3(source.map)) {
642
706
  unlinkSync(source.map);
643
707
  deleted.push(source.mapName);
644
708
  }
@@ -647,7 +711,7 @@ function deleteSourcemaps(files, options = {}) {
647
711
  }
648
712
 
649
713
  // src/version.ts
650
- var VERSION = "0.1.0";
714
+ var VERSION = "0.2.0";
651
715
 
652
716
  // src/build.ts
653
717
  function defaultRelease(options) {