@sanity/pkg-utils 7.2.8-canary.0 → 7.2.8-canary.2

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 (66) hide show
  1. package/bin/pkg-utils.cjs +0 -1
  2. package/dist/_chunks-cjs/build.cjs +4 -4
  3. package/dist/_chunks-cjs/build.cjs.map +1 -1
  4. package/dist/_chunks-cjs/check.cjs +14 -14
  5. package/dist/_chunks-cjs/check.cjs.map +1 -1
  6. package/dist/_chunks-cjs/consoleSpy.cjs +153 -83
  7. package/dist/_chunks-cjs/consoleSpy.cjs.map +1 -1
  8. package/dist/_chunks-cjs/index.cjs +0 -1
  9. package/dist/_chunks-cjs/index.cjs.map +1 -1
  10. package/dist/_chunks-cjs/init.cjs +11 -11
  11. package/dist/_chunks-cjs/init.cjs.map +1 -1
  12. package/dist/_chunks-cjs/isRecord.cjs +39 -0
  13. package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
  14. package/dist/_chunks-cjs/watch.cjs +8 -8
  15. package/dist/_chunks-cjs/watch.cjs.map +1 -1
  16. package/dist/cli.cjs +1 -1
  17. package/dist/index.cjs +3 -3
  18. package/dist/index.d.cts +2 -2
  19. package/dist/index.d.ts +2 -2
  20. package/dist/index.js +130 -127
  21. package/dist/index.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/node/build.ts +4 -2
  24. package/src/node/check.ts +2 -3
  25. package/src/node/core/config/findConfigFile.ts +0 -2
  26. package/src/node/core/config/loadConfig.ts +0 -2
  27. package/src/node/core/config/types.ts +0 -4
  28. package/src/node/core/contexts/buildContext.ts +2 -3
  29. package/src/node/core/pkg/loadPkg.ts +0 -2
  30. package/src/node/core/pkg/loadPkgWithReporting.ts +0 -1
  31. package/src/node/core/pkg/parseExports.ts +1 -2
  32. package/src/node/core/pkg/validateExports.ts +1 -1
  33. package/src/node/core/pkg/validatePkg.ts +0 -1
  34. package/src/node/core/template/createFromTemplate.ts +0 -2
  35. package/src/node/getFilesize.ts +0 -1
  36. package/src/node/globFiles.ts +0 -1
  37. package/src/node/index.ts +64 -8
  38. package/src/node/init.ts +2 -4
  39. package/src/node/printExtractMessages.ts +1 -3
  40. package/src/node/printPackageTree.ts +2 -3
  41. package/src/node/resolveBuildContext.ts +11 -14
  42. package/src/node/resolveBuildTasks.ts +4 -3
  43. package/src/node/resolveWatchTasks.ts +4 -3
  44. package/src/node/strict.test-d.ts +0 -1
  45. package/src/node/tasks/dts/buildTypes.ts +0 -1
  46. package/src/node/tasks/dts/createApiExtractorConfig.ts +0 -1
  47. package/src/node/tasks/dts/createTSDocConfig.ts +0 -1
  48. package/src/node/tasks/dts/doExtract.ts +1 -1
  49. package/src/node/tasks/dts/dtsTask.ts +0 -1
  50. package/src/node/tasks/dts/extractTypes.ts +2 -1
  51. package/src/node/tasks/dts/getExtractMessagesConfig.ts +1 -2
  52. package/src/node/tasks/dts/getTargetPaths.ts +2 -1
  53. package/src/node/tasks/dts/printDiagnostic.ts +0 -2
  54. package/src/node/tasks/index.ts +4 -6
  55. package/src/node/tasks/rollup/resolveRollupConfig.ts +3 -1
  56. package/src/node/tasks/rollup/rollupTask.ts +1 -3
  57. package/src/node/tasks/rollup/rollupWatchTask.ts +1 -3
  58. package/src/node/tasks/types.ts +4 -4
  59. package/src/node/templates/default/template.ts +4 -7
  60. package/src/node/watch.ts +4 -4
  61. package/src/node/watchConfigFiles.ts +0 -2
  62. package/dist/_chunks-cjs/logger.cjs +0 -107
  63. package/dist/_chunks-cjs/logger.cjs.map +0 -1
  64. package/src/node/core/index.ts +0 -7
  65. package/src/node/tasks/dts/index.ts +0 -4
  66. package/src/node/tasks/rollup/index.ts +0 -2
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ import { register } from "esbuild-register/dist/node";
5
5
  import pkgUp from "pkg-up";
6
6
  import findConfig from "find-config";
7
7
  import { statSync, existsSync } from "node:fs";
8
- import { z, ZodError } from "zod";
9
8
  import chalk from "chalk";
10
- import fs, { writeFile, readFile, readdir, lstat } from "node:fs/promises";
9
+ import { z, ZodError } from "zod";
10
+ import fs, { readFile, writeFile, readdir, lstat } from "node:fs/promises";
11
11
  import browserslist from "browserslist";
12
12
  import config$1 from "@sanity/browserslist-config";
13
13
  import ts from "typescript";
@@ -42,9 +42,6 @@ import { outdent } from "outdent";
42
42
  import { scan, startWith, distinctUntilChanged } from "rxjs/operators";
43
43
  import globby from "globby";
44
44
  import * as chokidar from "chokidar";
45
- function defineConfig(configOptions) {
46
- return configOptions;
47
- }
48
45
  function fileExists(filePath) {
49
46
  try {
50
47
  return statSync(filePath), !0;
@@ -78,12 +75,13 @@ async function loadConfig(options) {
78
75
  } } : register(esbuildOptions), mod = require(configFile);
79
76
  return unregister(), mod?.default || mod || void 0;
80
77
  }
81
- function resolveConfigProperty(prop, initialValue) {
82
- return prop ? typeof prop == "function" ? prop(initialValue) : prop : initialValue;
78
+ function assertLast(a, arr) {
79
+ const aIdx = arr.indexOf(a);
80
+ return aIdx === -1 ? !0 : aIdx === arr.length - 1;
83
81
  }
84
- const DEFAULT_BROWSERSLIST_QUERY = config$1;
85
- function isRecord(value) {
86
- return !!value && !Array.isArray(value) && typeof value == "object";
82
+ function assertOrder(a, b, arr) {
83
+ const aIdx = arr.indexOf(a), bIdx = arr.indexOf(b);
84
+ return aIdx === -1 || bIdx === -1 ? !0 : aIdx < bIdx;
87
85
  }
88
86
  const pkgSchema = z.object({
89
87
  type: z.optional(z.enum(["commonjs", "module"])),
@@ -158,14 +156,6 @@ async function loadPkg(options) {
158
156
  const buf = await fs.readFile(pkgPath), raw = JSON.parse(buf.toString());
159
157
  return validatePkg(raw), raw;
160
158
  }
161
- function assertLast(a, arr) {
162
- const aIdx = arr.indexOf(a);
163
- return aIdx === -1 ? !0 : aIdx === arr.length - 1;
164
- }
165
- function assertOrder(a, b, arr) {
166
- const aIdx = arr.indexOf(a), bIdx = arr.indexOf(b);
167
- return aIdx === -1 || bIdx === -1 ? !0 : aIdx < bIdx;
168
- }
169
159
  async function loadPkgWithReporting(options) {
170
160
  const { cwd, logger, strict } = options;
171
161
  try {
@@ -224,6 +214,74 @@ async function loadPkgWithReporting(options) {
224
214
  function formatPath(segments) {
225
215
  return segments.map((s, idx) => idx === 0 ? s : typeof s == "number" ? `[${s}]` : s.startsWith(".") ? `["${s}"]` : `.${s}`).join("");
226
216
  }
217
+ function createLogger() {
218
+ return {
219
+ log: (...args) => {
220
+ console.log(...args);
221
+ },
222
+ info: (...args) => {
223
+ console.log(chalk.blue("[info]"), ...args);
224
+ },
225
+ warn: (...args) => {
226
+ console.log(chalk.yellow("[warning]"), ...args);
227
+ },
228
+ error: (...args) => {
229
+ console.log(chalk.red("[error]"), ...args);
230
+ },
231
+ success: (...args) => {
232
+ console.log(chalk.green("[success]"), ...args);
233
+ }
234
+ };
235
+ }
236
+ function browserslistToEsbuild(browserslistConfig, options = {}) {
237
+ if (!browserslistConfig) {
238
+ const path2 = process.cwd();
239
+ browserslistConfig = browserslist.loadConfig({ path: path2, ...options });
240
+ }
241
+ const SUPPORTED_ESBUILD_TARGETS = [
242
+ "es",
243
+ "chrome",
244
+ "edge",
245
+ "firefox",
246
+ "ios",
247
+ "node",
248
+ "safari",
249
+ "opera",
250
+ "ie"
251
+ ], UNSUPPORTED = ["android 4"], replaces = {
252
+ ios_saf: "ios",
253
+ android: "chrome"
254
+ }, separator = " ";
255
+ return browserslist(browserslistConfig, options).filter((b) => !UNSUPPORTED.some((u) => b.startsWith(u))).map((b) => b === "safari TP" ? browserslist("last 1 safari version")[0] : b).map((b) => b.split(separator)).map((b) => (replaces[b[0]] && (b[0] = replaces[b[0]]), b)).map((b) => (b[1].includes("-") && (b[1] = b[1].slice(0, b[1].indexOf("-"))), b)).map((b) => (b[1].endsWith(".0") && (b[1] = b[1].slice(0, -2)), b)).filter((b) => /^\d+(\.\d+)*$/.test(b[1])).filter((b) => SUPPORTED_ESBUILD_TARGETS.includes(b[0])).reduce((acc, b) => {
256
+ const existingIndex = acc.findIndex((br) => br[0] === b[0]);
257
+ return existingIndex !== -1 ? acc[existingIndex][1] = b[1] : acc.push(b), acc;
258
+ }, []).map((b) => b.join(""));
259
+ }
260
+ function resolveConfigProperty(prop, initialValue) {
261
+ return prop ? typeof prop == "function" ? prop(initialValue) : prop : initialValue;
262
+ }
263
+ const DEFAULT_BROWSERSLIST_QUERY = config$1;
264
+ function pathContains(containerPath, itemPath) {
265
+ return !path.relative(containerPath, itemPath).startsWith("..");
266
+ }
267
+ function findCommonDirPath(filePaths) {
268
+ let ret;
269
+ for (const filePath of filePaths) {
270
+ let dirPath = path.dirname(filePath);
271
+ if (!ret) {
272
+ ret = dirPath;
273
+ continue;
274
+ }
275
+ for (; dirPath !== ret && (dirPath = path.dirname(dirPath), dirPath !== ret); ) {
276
+ if (pathContains(dirPath, ret)) {
277
+ ret = dirPath;
278
+ break;
279
+ }
280
+ if (dirPath === ".") return;
281
+ }
282
+ }
283
+ return ret;
284
+ }
227
285
  const fileEnding = /\.[mc]?js$/, dtsEnding = ".d.ts", defaultEnding = ".js", mjsEnding = ".mjs", cjsEnding = ".cjs", mtsEnding = ".d.mts", ctsEnding = ".d.cts";
228
286
  function getTargetPaths(_type, expOrBundle) {
229
287
  const type = _type === "module" ? "module" : "commonjs", set = /* @__PURE__ */ new Set();
@@ -243,6 +301,9 @@ function getTargetPaths(_type, expOrBundle) {
243
301
  function isPkgExport$1(exp) {
244
302
  return exp?.browser || exp?.node || exp?.default;
245
303
  }
304
+ function isRecord(value) {
305
+ return !!value && !Array.isArray(value) && typeof value == "object";
306
+ }
246
307
  const pkgExtMap = {
247
308
  // pkg.type: "commonjs"
248
309
  commonjs: {
@@ -371,50 +432,6 @@ function parseExports(options) {
371
432
  function isPkgExport(value) {
372
433
  return isRecord(value) && "source" in value && typeof value.source == "string";
373
434
  }
374
- const promptsTypes = {
375
- string: "text"
376
- };
377
- async function createFromTemplate(options) {
378
- const { cwd, logger, packagePath, template: templateOrResolver } = options, template = typeof templateOrResolver == "function" ? await templateOrResolver({ cwd, logger, packagePath }) : templateOrResolver;
379
- logger.log("create new package at", relative(cwd, packagePath));
380
- const templateOptions = {};
381
- for (const templateOption of template.options) {
382
- const templateValidate = templateOption.validate, res = await prompts(
383
- {
384
- type: promptsTypes[templateOption.type],
385
- name: templateOption.name,
386
- message: templateOption.description,
387
- validate: templateValidate ? (prev) => templateValidate(prev) : void 0,
388
- initial: typeof templateOption.initial == "function" ? templateOption.initial(templateOptions) : templateOption.initial
389
- },
390
- { onCancel: () => process.exit(0) }
391
- );
392
- templateOptions[templateOption.name] = templateOption.parse ? templateOption.parse(res[templateOption.name]) : res[templateOption.name];
393
- }
394
- const features = {};
395
- for (const templateFeature of template.features) {
396
- const res = templateFeature.optional ? await prompts(
397
- {
398
- type: "confirm",
399
- name: "confirm",
400
- message: `use ${templateFeature.name}?`,
401
- initial: templateFeature.initial
402
- },
403
- { onCancel: () => process.exit(0) }
404
- ) : void 0;
405
- features[templateFeature.name] = res?.confirm || !templateFeature.optional;
406
- }
407
- const files = await template.getFiles(templateOptions, features);
408
- files.sort((a, b) => a.name.localeCompare(b.name));
409
- for (const file of files) {
410
- const filePath = resolve(packagePath, file.name);
411
- await mkdirp(dirname(filePath)), await writeFile(filePath, file.contents.trim() + `
412
- `), logger.success(`wrote ${relative(cwd, filePath)}`);
413
- }
414
- }
415
- function defineTemplateOption(option) {
416
- return option;
417
- }
418
435
  async function loadTSConfig(options) {
419
436
  const { cwd, tsconfigPath } = options, configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
420
437
  if (!configPath)
@@ -422,70 +439,6 @@ async function loadTSConfig(options) {
422
439
  const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
423
440
  return ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd);
424
441
  }
425
- function createLogger() {
426
- return {
427
- log: (...args) => {
428
- console.log(...args);
429
- },
430
- info: (...args) => {
431
- console.log(chalk.blue("[info]"), ...args);
432
- },
433
- warn: (...args) => {
434
- console.log(chalk.yellow("[warning]"), ...args);
435
- },
436
- error: (...args) => {
437
- console.log(chalk.red("[error]"), ...args);
438
- },
439
- success: (...args) => {
440
- console.log(chalk.green("[success]"), ...args);
441
- }
442
- };
443
- }
444
- function browserslistToEsbuild(browserslistConfig, options = {}) {
445
- if (!browserslistConfig) {
446
- const path2 = process.cwd();
447
- browserslistConfig = browserslist.loadConfig({ path: path2, ...options });
448
- }
449
- const SUPPORTED_ESBUILD_TARGETS = [
450
- "es",
451
- "chrome",
452
- "edge",
453
- "firefox",
454
- "ios",
455
- "node",
456
- "safari",
457
- "opera",
458
- "ie"
459
- ], UNSUPPORTED = ["android 4"], replaces = {
460
- ios_saf: "ios",
461
- android: "chrome"
462
- }, separator = " ";
463
- return browserslist(browserslistConfig, options).filter((b) => !UNSUPPORTED.some((u) => b.startsWith(u))).map((b) => b === "safari TP" ? browserslist("last 1 safari version")[0] : b).map((b) => b.split(separator)).map((b) => (replaces[b[0]] && (b[0] = replaces[b[0]]), b)).map((b) => (b[1].includes("-") && (b[1] = b[1].slice(0, b[1].indexOf("-"))), b)).map((b) => (b[1].endsWith(".0") && (b[1] = b[1].slice(0, -2)), b)).filter((b) => /^\d+(\.\d+)*$/.test(b[1])).filter((b) => SUPPORTED_ESBUILD_TARGETS.includes(b[0])).reduce((acc, b) => {
464
- const existingIndex = acc.findIndex((br) => br[0] === b[0]);
465
- return existingIndex !== -1 ? acc[existingIndex][1] = b[1] : acc.push(b), acc;
466
- }, []).map((b) => b.join(""));
467
- }
468
- function pathContains(containerPath, itemPath) {
469
- return !path.relative(containerPath, itemPath).startsWith("..");
470
- }
471
- function findCommonDirPath(filePaths) {
472
- let ret;
473
- for (const filePath of filePaths) {
474
- let dirPath = path.dirname(filePath);
475
- if (!ret) {
476
- ret = dirPath;
477
- continue;
478
- }
479
- for (; dirPath !== ret && (dirPath = path.dirname(dirPath), dirPath !== ret); ) {
480
- if (pathContains(dirPath, ret)) {
481
- ret = dirPath;
482
- break;
483
- }
484
- if (dirPath === ".") return;
485
- }
486
- }
487
- return ret;
488
- }
489
442
  function resolveBrowserTarget(versions) {
490
443
  const target = versions.filter(
491
444
  (version) => version.startsWith("chrome") || version.startsWith("edge") || version.startsWith("firefox") || version.startsWith("ios") || version.startsWith("safari") || version.startsWith("opera")
@@ -556,7 +509,10 @@ async function resolveBuildContext(options) {
556
509
  strict,
557
510
  strictOptions: strictOptions2,
558
511
  logger
559
- }).reduce((acc, { _path: exportPath, ...exportEntry }) => Object.assign(acc, { [exportPath]: exportEntry }), {}), exports = resolveConfigProperty(config2?.exports, parsedExports), parsedExternal = [
512
+ }).reduce(
513
+ (acc, { _path: exportPath, ...exportEntry }) => Object.assign(acc, { [exportPath]: exportEntry }),
514
+ {}
515
+ ), exports = resolveConfigProperty(config2?.exports, parsedExports), parsedExternal = [
560
516
  ...pkg.dependencies ? Object.keys(pkg.dependencies) : [],
561
517
  ...pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []
562
518
  ], external = config2 && Array.isArray(config2.external) ? [...parsedExternal, ...config2.external] : resolveConfigProperty(config2?.external, parsedExternal), externalWithTypes = /* @__PURE__ */ new Set([pkg.name, ...external, ...external.map(transformPackageName)]), bundledDependencies = (pkg.devDependencies ? Object.keys(pkg.devDependencies) : []).filter(
@@ -1589,6 +1545,53 @@ function isEsbuildFailure(err) {
1589
1545
  function isEsbuildMessage(msg) {
1590
1546
  return typeof msg == "object" && msg !== null && "text" in msg && typeof msg.text == "string" && "location" in msg && (msg.location === null || typeof msg.location == "object");
1591
1547
  }
1548
+ function defineConfig(configOptions) {
1549
+ return configOptions;
1550
+ }
1551
+ const promptsTypes = {
1552
+ string: "text"
1553
+ };
1554
+ async function createFromTemplate(options) {
1555
+ const { cwd, logger, packagePath, template: templateOrResolver } = options, template = typeof templateOrResolver == "function" ? await templateOrResolver({ cwd, logger, packagePath }) : templateOrResolver;
1556
+ logger.log("create new package at", relative(cwd, packagePath));
1557
+ const templateOptions = {};
1558
+ for (const templateOption of template.options) {
1559
+ const templateValidate = templateOption.validate, res = await prompts(
1560
+ {
1561
+ type: promptsTypes[templateOption.type],
1562
+ name: templateOption.name,
1563
+ message: templateOption.description,
1564
+ validate: templateValidate ? (prev) => templateValidate(prev) : void 0,
1565
+ initial: typeof templateOption.initial == "function" ? templateOption.initial(templateOptions) : templateOption.initial
1566
+ },
1567
+ { onCancel: () => process.exit(0) }
1568
+ );
1569
+ templateOptions[templateOption.name] = templateOption.parse ? templateOption.parse(res[templateOption.name]) : res[templateOption.name];
1570
+ }
1571
+ const features = {};
1572
+ for (const templateFeature of template.features) {
1573
+ const res = templateFeature.optional ? await prompts(
1574
+ {
1575
+ type: "confirm",
1576
+ name: "confirm",
1577
+ message: `use ${templateFeature.name}?`,
1578
+ initial: templateFeature.initial
1579
+ },
1580
+ { onCancel: () => process.exit(0) }
1581
+ ) : void 0;
1582
+ features[templateFeature.name] = res?.confirm || !templateFeature.optional;
1583
+ }
1584
+ const files = await template.getFiles(templateOptions, features);
1585
+ files.sort((a, b) => a.name.localeCompare(b.name));
1586
+ for (const file of files) {
1587
+ const filePath = resolve(packagePath, file.name);
1588
+ await mkdirp(dirname(filePath)), await writeFile(filePath, file.contents.trim() + `
1589
+ `), logger.success(`wrote ${relative(cwd, filePath)}`);
1590
+ }
1591
+ }
1592
+ function defineTemplateOption(option) {
1593
+ return option;
1594
+ }
1592
1595
  async function isEmptyDirectory(dirPath) {
1593
1596
  return (await readdir(dirPath)).length === 0;
1594
1597
  }