@zharp/cli 0.1.0 → 0.2.1

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.js CHANGED
@@ -81,7 +81,7 @@ function resolveConnection(options) {
81
81
  function artifactsUrl(connection) {
82
82
  return `${connection.url}/api/v1/applications/${encodeURIComponent(String(connection.app))}/artifacts`;
83
83
  }
84
- var sleep = (ms) => new Promise((resolve3) => setTimeout(resolve3, ms));
84
+ var sleep = (ms) => new Promise((resolve4) => setTimeout(resolve4, ms));
85
85
  async function uploadArtifact(connection, artifact, options = {}) {
86
86
  const doFetch = options.fetch ?? fetch;
87
87
  const retries = options.retries ?? 3;
@@ -163,6 +163,9 @@ async function eachLimit(items, limit, fn) {
163
163
 
164
164
  // src/debug-id.ts
165
165
  var import_node_crypto = require("crypto");
166
+ var import_node_fs = require("fs");
167
+ var import_node_path = require("path");
168
+ var import_node_url = require("url");
166
169
  var DEBUG_ID_COMMENT = "//# debugId=";
167
170
  var DEBUG_ID_PATTERN = /^\/\/# debugId=([0-9a-fA-F-]{36})\s*$/m;
168
171
  var SOURCE_MAPPING_URL = /^\/\/[#@] sourceMappingURL=(.*?)\s*$/;
@@ -231,15 +234,76 @@ function withoutSourceMappingUrl(code) {
231
234
  if (end > 0 && SOURCE_MAPPING_URL.test(lines[end - 1].trim())) return lines.slice(0, end - 1).join(eol) + eol;
232
235
  return code;
233
236
  }
234
- function injectMap(mapText, debugId) {
237
+ function injectMap(mapText, debugId, mapPath) {
235
238
  const map = JSON.parse(mapText);
236
239
  if (!map || typeof map !== "object") throw new Error("not a source map");
240
+ const filled = mapPath ? fillSourcesContent(map, mapPath) : 0;
237
241
  const hasSourcesContent = Array.isArray(map.sourcesContent) && map.sourcesContent.some((s) => typeof s === "string");
238
- if (map.debugId === debugId && map.debug_id === debugId) return { text: mapText, changed: false, hasSourcesContent };
242
+ if (!filled && map.debugId === debugId && map.debug_id === debugId) return { text: mapText, changed: false, hasSourcesContent };
239
243
  map.debugId = debugId;
240
244
  map.debug_id = debugId;
241
245
  return { text: JSON.stringify(map), changed: true, hasSourcesContent };
242
246
  }
247
+ var MAX_SOURCE_BYTES = 1e6;
248
+ function sourcePathCandidates(source, mapPath, sourceRoot = "") {
249
+ const mapDir = (0, import_node_path.dirname)(mapPath);
250
+ let stripped = source;
251
+ let projectRelative = false;
252
+ if (/^turbopack:\/\/\/\[project\]\//.test(source)) {
253
+ stripped = source.replace(/^turbopack:\/\/\/\[project\]\//, "");
254
+ projectRelative = true;
255
+ } else if (/^webpack:\/\//.test(source)) {
256
+ stripped = source.replace(/^webpack:\/\/[^/]*\//, "").replace(/^\.\//, "");
257
+ projectRelative = true;
258
+ } else if (/^file:\/\//.test(source)) {
259
+ try {
260
+ return [(0, import_node_url.fileURLToPath)(source)];
261
+ } catch {
262
+ return [];
263
+ }
264
+ } else if (/^[a-z][\w+.-]*:\/\//i.test(source)) {
265
+ return [];
266
+ }
267
+ if ((0, import_node_path.isAbsolute)(stripped)) return [stripped];
268
+ const out = [(0, import_node_path.resolve)(mapDir, sourceRoot, stripped)];
269
+ if (projectRelative) {
270
+ for (let dir = mapDir, i = 0; i < 12; i++) {
271
+ if ((0, import_node_fs.existsSync)((0, import_node_path.join)(dir, "package.json"))) {
272
+ out.unshift((0, import_node_path.join)(dir, stripped));
273
+ break;
274
+ }
275
+ const up = (0, import_node_path.dirname)(dir);
276
+ if (up === dir) break;
277
+ dir = up;
278
+ }
279
+ }
280
+ return out;
281
+ }
282
+ function fillSourcesContent(map, mapPath) {
283
+ if (!Array.isArray(map.sources)) return 0;
284
+ const contents = Array.isArray(map.sourcesContent) ? [...map.sourcesContent] : [];
285
+ let filled = 0;
286
+ map.sources.forEach((source, i) => {
287
+ if (typeof contents[i] === "string" || typeof source !== "string") return;
288
+ if (/node_modules|^\[externals\]|\(ignored\)|^webpack\/runtime/.test(source)) return;
289
+ for (const path of sourcePathCandidates(source, mapPath, typeof map.sourceRoot === "string" ? map.sourceRoot : "")) {
290
+ if (/[\\/]node_modules[\\/]/.test(path)) continue;
291
+ try {
292
+ const stat = (0, import_node_fs.statSync)(path);
293
+ if (!stat.isFile() || stat.size > MAX_SOURCE_BYTES) continue;
294
+ contents[i] = (0, import_node_fs.readFileSync)(path, "utf8");
295
+ filled++;
296
+ break;
297
+ } catch {
298
+ }
299
+ }
300
+ });
301
+ if (filled) {
302
+ while (contents.length < map.sources.length) contents.push(null);
303
+ map.sourcesContent = contents.map((c) => typeof c === "string" ? c : null);
304
+ }
305
+ return filled;
306
+ }
243
307
 
244
308
  // src/elf.ts
245
309
  var NT_GNU_BUILD_ID = 3;
@@ -308,19 +372,19 @@ function hex(bytes) {
308
372
  }
309
373
 
310
374
  // src/files.ts
311
- var import_node_fs = require("fs");
312
- var import_node_path = require("path");
375
+ var import_node_fs2 = require("fs");
376
+ var import_node_path2 = require("path");
313
377
  function walk(dir) {
314
378
  const out = [];
315
379
  const visit = (current) => {
316
- for (const entry of (0, import_node_fs.readdirSync)(current, { withFileTypes: true })) {
317
- const full = (0, import_node_path.join)(current, entry.name);
380
+ for (const entry of (0, import_node_fs2.readdirSync)(current, { withFileTypes: true })) {
381
+ const full = (0, import_node_path2.join)(current, entry.name);
318
382
  if (entry.isDirectory()) {
319
383
  if (entry.name !== "node_modules") visit(full);
320
384
  } else if (entry.isFile()) out.push(full);
321
385
  else if (entry.isSymbolicLink()) {
322
386
  try {
323
- if ((0, import_node_fs.statSync)(full).isFile()) out.push(full);
387
+ if ((0, import_node_fs2.statSync)(full).isFile()) out.push(full);
324
388
  } catch {
325
389
  }
326
390
  }
@@ -330,7 +394,7 @@ function walk(dir) {
330
394
  return out.sort();
331
395
  }
332
396
  function relativePath(dir, path) {
333
- return (0, import_node_path.relative)(dir, path).split(import_node_path.sep).join("/");
397
+ return (0, import_node_path2.relative)(dir, path).split(import_node_path2.sep).join("/");
334
398
  }
335
399
  function globToRegExp(glob) {
336
400
  let out = "";
@@ -448,8 +512,8 @@ function formatUuid(bytes) {
448
512
  }
449
513
 
450
514
  // src/native.ts
451
- var import_node_fs2 = require("fs");
452
- var import_node_path2 = require("path");
515
+ var import_node_fs3 = require("fs");
516
+ var import_node_path3 = require("path");
453
517
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
454
518
  async function send(connection, artifacts, options) {
455
519
  const logger = options.logger ?? consoleLogger();
@@ -467,46 +531,46 @@ async function send(connection, artifacts, options) {
467
531
  }
468
532
  async function uploadProguard(mappingFile, uuid, connection, options = {}) {
469
533
  if (!UUID.test(uuid)) throw new Error(`--uuid must be a UUID, got "${uuid}".`);
470
- const data = (0, import_node_fs2.readFileSync)(mappingFile);
471
- return send(connection, [{ kind: "proguard", debugId: uuid.toLowerCase(), name: (0, import_node_path2.basename)(mappingFile), release: options.release, data, gzip: true }], options);
534
+ const data = (0, import_node_fs3.readFileSync)(mappingFile);
535
+ return send(connection, [{ kind: "proguard", debugId: uuid.toLowerCase(), name: (0, import_node_path3.basename)(mappingFile), release: options.release, data, gzip: true }], options);
472
536
  }
473
537
  var DART_ARCH = { x86_64: "x64", x86: "ia32" };
474
538
  function findDartSymbols(path, logger = consoleLogger()) {
475
- const files = (0, import_node_fs2.statSync)(path).isDirectory() ? walk((0, import_node_path2.resolve)(path)) : [(0, import_node_path2.resolve)(path)];
539
+ const files = (0, import_node_fs3.statSync)(path).isDirectory() ? walk((0, import_node_path3.resolve)(path)) : [(0, import_node_path3.resolve)(path)];
476
540
  const out = [];
477
541
  for (const file of files) {
478
542
  const named = /\.symbols$/.test(file);
479
- const data = (0, import_node_fs2.readFileSync)(file);
543
+ const data = (0, import_node_fs3.readFileSync)(file);
480
544
  if (!isElf(data)) {
481
- if (named) logger.warn(`Skipped ${(0, import_node_path2.basename)(file)}: not an ELF file.`);
545
+ if (named) logger.warn(`Skipped ${(0, import_node_path3.basename)(file)}: not an ELF file.`);
482
546
  continue;
483
547
  }
484
548
  const elf = readElf(data);
485
549
  if (!elf.buildId) {
486
- logger.warn(`Skipped ${(0, import_node_path2.basename)(file)}: it has no GNU build ID.`);
550
+ logger.warn(`Skipped ${(0, import_node_path3.basename)(file)}: it has no GNU build ID.`);
487
551
  continue;
488
552
  }
489
- const match = /\.([a-z]+)-([a-z0-9_]+)\.symbols$/.exec((0, import_node_path2.basename)(file));
553
+ const match = /\.([a-z]+)-([a-z0-9_]+)\.symbols$/.exec((0, import_node_path3.basename)(file));
490
554
  out.push({ path: file, buildId: elf.buildId, os: match?.[1], arch: match?.[2] ?? (elf.arch && (DART_ARCH[elf.arch] ?? elf.arch)) });
491
555
  }
492
556
  return out;
493
557
  }
494
558
  function findObfuscationMap(symbolsPath) {
495
- const dir = (0, import_node_fs2.statSync)(symbolsPath).isDirectory() ? (0, import_node_path2.resolve)(symbolsPath) : (0, import_node_path2.dirname)((0, import_node_path2.resolve)(symbolsPath));
496
- for (const where of [dir, (0, import_node_path2.dirname)(dir)]) {
559
+ const dir = (0, import_node_fs3.statSync)(symbolsPath).isDirectory() ? (0, import_node_path3.resolve)(symbolsPath) : (0, import_node_path3.dirname)((0, import_node_path3.resolve)(symbolsPath));
560
+ for (const where of [dir, (0, import_node_path3.dirname)(dir)]) {
497
561
  let names;
498
562
  try {
499
- names = (0, import_node_fs2.readdirSync)(where).sort();
563
+ names = (0, import_node_fs3.readdirSync)(where).sort();
500
564
  } catch {
501
565
  continue;
502
566
  }
503
567
  const found = names.find((n) => n === "obfuscation.json") ?? names.find((n) => n.endsWith(".obfuscation.json"));
504
- if (found && (0, import_node_fs2.statSync)((0, import_node_path2.join)(where, found)).isFile()) return (0, import_node_path2.join)(where, found);
568
+ if (found && (0, import_node_fs3.statSync)((0, import_node_path3.join)(where, found)).isFile()) return (0, import_node_path3.join)(where, found);
505
569
  }
506
570
  return void 0;
507
571
  }
508
572
  function readObfuscationMap(file) {
509
- const data = (0, import_node_fs2.readFileSync)(file);
573
+ const data = (0, import_node_fs3.readFileSync)(file);
510
574
  let parsed;
511
575
  try {
512
576
  parsed = JSON.parse(data.toString("utf8"));
@@ -524,20 +588,20 @@ async function uploadDartSymbols(path, connection, options = {}) {
524
588
  const artifacts = files.map((f) => ({
525
589
  kind: "dart-symbols",
526
590
  debugId: f.buildId,
527
- name: (0, import_node_path2.basename)(f.path),
591
+ name: (0, import_node_path3.basename)(f.path),
528
592
  arch: f.arch,
529
593
  release: options.release,
530
- data: (0, import_node_fs2.readFileSync)(f.path),
594
+ data: (0, import_node_fs3.readFileSync)(f.path),
531
595
  gzip: true
532
596
  }));
533
- if (options.obfuscationMap && !(0, import_node_fs2.existsSync)(options.obfuscationMap)) throw new Error(`${options.obfuscationMap} does not exist.`);
597
+ if (options.obfuscationMap && !(0, import_node_fs3.existsSync)(options.obfuscationMap)) throw new Error(`${options.obfuscationMap} does not exist.`);
534
598
  const mapFile = options.obfuscationMap ?? findObfuscationMap(path);
535
599
  if (mapFile) {
536
600
  const data = readObfuscationMap(mapFile);
537
601
  const buildIds = [...new Set(files.map((f) => f.buildId))];
538
- logger.info(`With obfuscation map ${(0, import_node_path2.basename)(mapFile)} for ${buildIds.length} build ID(s)`);
602
+ logger.info(`With obfuscation map ${(0, import_node_path3.basename)(mapFile)} for ${buildIds.length} build ID(s)`);
539
603
  for (const debugId of buildIds) {
540
- artifacts.push({ kind: "dart-obfuscation-map", debugId, name: (0, import_node_path2.basename)(mapFile), release: options.release, data, gzip: true });
604
+ artifacts.push({ kind: "dart-obfuscation-map", debugId, name: (0, import_node_path3.basename)(mapFile), release: options.release, data, gzip: true });
541
605
  }
542
606
  } else {
543
607
  logger.info("No obfuscation map found (build with --extra-gen-snapshot-options=--save-obfuscation-map=build/obfuscation.json, or pass --obfuscation-map): obfuscated names in error messages stay as they are.");
@@ -545,11 +609,11 @@ async function uploadDartSymbols(path, connection, options = {}) {
545
609
  return send(connection, artifacts, options);
546
610
  }
547
611
  function findDsymImages(path) {
548
- const root = (0, import_node_path2.resolve)(path);
549
- const candidates = (0, import_node_fs2.statSync)(root).isDirectory() ? walk(root).filter((f) => /[\\/]Contents[\\/]Resources[\\/]DWARF[\\/][^\\/]+$/.test(f)) : [root];
612
+ const root = (0, import_node_path3.resolve)(path);
613
+ const candidates = (0, import_node_fs3.statSync)(root).isDirectory() ? walk(root).filter((f) => /[\\/]Contents[\\/]Resources[\\/]DWARF[\\/][^\\/]+$/.test(f)) : [root];
550
614
  const out = [];
551
615
  for (const file of candidates) {
552
- const data = (0, import_node_fs2.readFileSync)(file);
616
+ const data = (0, import_node_fs3.readFileSync)(file);
553
617
  if (!isMachO(data)) continue;
554
618
  for (const image of readMachO(data)) out.push({ path: file, ...image });
555
619
  }
@@ -563,11 +627,11 @@ async function uploadDsyms(path, connection, options = {}) {
563
627
  const cache = /* @__PURE__ */ new Map();
564
628
  const artifacts = images.map((image) => {
565
629
  let data = cache.get(image.path);
566
- if (!data) cache.set(image.path, data = (0, import_node_fs2.readFileSync)(image.path));
630
+ if (!data) cache.set(image.path, data = (0, import_node_fs3.readFileSync)(image.path));
567
631
  return {
568
632
  kind: "dsym",
569
633
  debugId: image.uuid,
570
- name: (0, import_node_path2.basename)(image.path),
634
+ name: (0, import_node_path3.basename)(image.path),
571
635
  arch: image.arch,
572
636
  release: options.release,
573
637
  // One architecture's slice of a universal file is a complete Mach-O file on its own.
@@ -579,13 +643,13 @@ async function uploadDsyms(path, connection, options = {}) {
579
643
  }
580
644
 
581
645
  // src/sourcemaps.ts
582
- var import_node_fs3 = require("fs");
583
- var import_node_path3 = require("path");
646
+ var import_node_fs4 = require("fs");
647
+ var import_node_path4 = require("path");
584
648
  var JS_FILE = /\.(?:js|mjs|cjs)$/;
585
649
  function mapFor(file, code) {
586
650
  const sibling = `${file}.map`;
587
- if ((0, import_node_fs3.existsSync)(sibling)) return sibling;
588
- const url = sourceMappingUrl(code ?? (0, import_node_fs3.readFileSync)(file, "utf8"));
651
+ if ((0, import_node_fs4.existsSync)(sibling)) return sibling;
652
+ const url = sourceMappingUrl(code ?? (0, import_node_fs4.readFileSync)(file, "utf8"));
589
653
  if (!url || /^[a-z][a-z0-9+.-]*:/i.test(url)) return void 0;
590
654
  let path;
591
655
  try {
@@ -593,16 +657,16 @@ function mapFor(file, code) {
593
657
  } catch {
594
658
  return void 0;
595
659
  }
596
- const full = (0, import_node_path3.isAbsolute)(path) ? path : (0, import_node_path3.resolve)((0, import_node_path3.dirname)(file), path);
597
- return (0, import_node_fs3.existsSync)(full) ? full : void 0;
660
+ const full = (0, import_node_path4.isAbsolute)(path) ? path : (0, import_node_path4.resolve)((0, import_node_path4.dirname)(file), path);
661
+ return (0, import_node_fs4.existsSync)(full) ? full : void 0;
598
662
  }
599
663
  function findSourceFiles(dir, options = {}) {
600
- const root = (0, import_node_path3.resolve)(dir);
664
+ const root = (0, import_node_path4.resolve)(dir);
601
665
  const keep = pathFilter(options.include, options.exclude);
602
- const candidates = options.files ? options.files.map((f) => (0, import_node_path3.resolve)(f)) : walk(root);
666
+ const candidates = options.files ? options.files.map((f) => (0, import_node_path4.resolve)(f)) : walk(root);
603
667
  const out = [];
604
668
  for (const file of candidates) {
605
- if (!JS_FILE.test(file) || !(0, import_node_fs3.existsSync)(file)) continue;
669
+ if (!JS_FILE.test(file) || !(0, import_node_fs4.existsSync)(file)) continue;
606
670
  const name = relativePath(root, file);
607
671
  if (!keep(name)) continue;
608
672
  const map = mapFor(file);
@@ -616,18 +680,18 @@ function injectDirectory(dir, options = {}) {
616
680
  const out = [];
617
681
  const noSourcesContent = [];
618
682
  for (const source of files) {
619
- const code = (0, import_node_fs3.readFileSync)(source.file, "utf8");
683
+ const code = (0, import_node_fs4.readFileSync)(source.file, "utf8");
620
684
  const injected = injectCode(code, { stripSourceMappingUrl: options.stripSourceMappingUrl });
621
685
  let map;
622
686
  try {
623
- map = injectMap((0, import_node_fs3.readFileSync)(source.map, "utf8"), injected.debugId);
687
+ map = injectMap((0, import_node_fs4.readFileSync)(source.map, "utf8"), injected.debugId, source.map);
624
688
  } catch (error) {
625
689
  logger.warn(`Skipped ${source.name}: ${source.mapName} is not a readable source map (${error.message}).`);
626
690
  continue;
627
691
  }
628
692
  if (!options.dryRun) {
629
- if (injected.changed) (0, import_node_fs3.writeFileSync)(source.file, injected.code);
630
- if (map.changed) (0, import_node_fs3.writeFileSync)(source.map, map.text);
693
+ if (injected.changed) (0, import_node_fs4.writeFileSync)(source.file, injected.code);
694
+ if (map.changed) (0, import_node_fs4.writeFileSync)(source.map, map.text);
631
695
  }
632
696
  if (!map.hasSourcesContent) noSourcesContent.push(source.mapName);
633
697
  const changed = injected.changed || map.changed;
@@ -648,13 +712,13 @@ async function uploadSourcemaps(dir, connection, options = {}) {
648
712
  const result = { files: 0, artifacts: 0, bytes: 0, deleted: [], skipped: [] };
649
713
  const work = [];
650
714
  for (const source of files) {
651
- const code = (0, import_node_fs3.readFileSync)(source.file);
715
+ const code = (0, import_node_fs4.readFileSync)(source.file);
652
716
  const debugId = existingDebugId(code.toString("utf8"));
653
717
  if (!debugId) {
654
718
  result.skipped.push(source.name);
655
719
  continue;
656
720
  }
657
- work.push({ source, debugId, code, map: (0, import_node_fs3.readFileSync)(source.map) });
721
+ work.push({ source, debugId, code, map: (0, import_node_fs4.readFileSync)(source.map) });
658
722
  }
659
723
  if (result.skipped.length) {
660
724
  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" : ""}`);
@@ -691,13 +755,13 @@ async function uploadSourcemaps(dir, connection, options = {}) {
691
755
  function deleteSourcemaps(files, options = {}) {
692
756
  const deleted = [];
693
757
  for (const source of files) {
694
- if (options.stripSourceMappingUrl && (0, import_node_fs3.existsSync)(source.file)) {
695
- const code = (0, import_node_fs3.readFileSync)(source.file, "utf8");
758
+ if (options.stripSourceMappingUrl && (0, import_node_fs4.existsSync)(source.file)) {
759
+ const code = (0, import_node_fs4.readFileSync)(source.file, "utf8");
696
760
  const stripped = withoutSourceMappingUrl(code);
697
- if (stripped !== code) (0, import_node_fs3.writeFileSync)(source.file, stripped);
761
+ if (stripped !== code) (0, import_node_fs4.writeFileSync)(source.file, stripped);
698
762
  }
699
- if ((0, import_node_fs3.existsSync)(source.map)) {
700
- (0, import_node_fs3.unlinkSync)(source.map);
763
+ if ((0, import_node_fs4.existsSync)(source.map)) {
764
+ (0, import_node_fs4.unlinkSync)(source.map);
701
765
  deleted.push(source.mapName);
702
766
  }
703
767
  }
@@ -705,7 +769,7 @@ function deleteSourcemaps(files, options = {}) {
705
769
  }
706
770
 
707
771
  // src/version.ts
708
- var VERSION = "0.1.0";
772
+ var VERSION = "0.2.1";
709
773
 
710
774
  // src/build.ts
711
775
  function defaultRelease(options) {