@sanity/pkg-utils 1.4.1 → 1.5.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 (49) hide show
  1. package/dist/{__printPackageTree-793a302b.cjs → __printPackageTree-3ff3ecd3.cjs} +5 -4
  2. package/dist/__printPackageTree-3ff3ecd3.cjs.map +1 -0
  3. package/dist/{__resolveBuildContext-1b29cff2.cjs → __resolveBuildContext-85cfca8d.cjs} +2 -1
  4. package/dist/__resolveBuildContext-85cfca8d.cjs.map +1 -0
  5. package/dist/{_build-08236974.cjs → _build-683d9922.cjs} +6 -7
  6. package/dist/_build-683d9922.cjs.map +1 -0
  7. package/dist/{_buildAction-586dfb5f.cjs → _buildAction-f593323a.cjs} +6 -5
  8. package/dist/{_buildAction-586dfb5f.cjs.map → _buildAction-f593323a.cjs.map} +1 -1
  9. package/dist/_check-6843c26d.cjs +118 -0
  10. package/dist/_check-6843c26d.cjs.map +1 -0
  11. package/dist/{_statusAction-385b72dd.cjs → _checkAction-0bca6a14.cjs} +9 -7
  12. package/dist/_checkAction-0bca6a14.cjs.map +1 -0
  13. package/dist/{_index-c5bd1bc8.cjs → _index-5765fada.cjs} +34 -15
  14. package/dist/_index-5765fada.cjs.map +1 -0
  15. package/dist/{_watch-003a06b2.cjs → _watch-ec1b35d6.cjs} +5 -6
  16. package/dist/_watch-ec1b35d6.cjs.map +1 -0
  17. package/dist/{_watchAction-c7cf282d.cjs → _watchAction-a388fc00.cjs} +5 -4
  18. package/dist/{_watchAction-c7cf282d.cjs.map → _watchAction-a388fc00.cjs.map} +1 -1
  19. package/dist/cli.cjs +5 -5
  20. package/dist/cli.cjs.map +1 -1
  21. package/dist/index.cjs +8 -6
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.ts +9 -5
  24. package/dist/index.js +135 -24
  25. package/dist/index.js.map +1 -1
  26. package/package.json +14 -3
  27. package/src/cli/checkAction.ts +9 -0
  28. package/src/cli/index.ts +3 -2
  29. package/src/node/_core/pkg/_loadPkg.ts +1 -0
  30. package/src/node/_printPackageTree.ts +3 -3
  31. package/src/node/_resolveBuildTasks.ts +3 -3
  32. package/src/node/_resolveWatchTasks.ts +3 -3
  33. package/src/node/_tasks/dts/_buildTypes.ts +11 -1
  34. package/src/node/_tasks/dts/_doExtract.ts +16 -5
  35. package/src/node/_tasks/dts/_dtsTask.ts +4 -2
  36. package/src/node/_tasks/dts/_dtsWatchTask.ts +2 -2
  37. package/src/node/_tasks/dts/_extractTypes.ts +5 -2
  38. package/src/node/_tasks/dts/_types.ts +4 -4
  39. package/src/node/_tasks/rollup/_rollupTask.ts +6 -4
  40. package/src/node/check.ts +125 -3
  41. package/dist/__printPackageTree-793a302b.cjs.map +0 -1
  42. package/dist/__resolveBuildContext-1b29cff2.cjs.map +0 -1
  43. package/dist/_build-08236974.cjs.map +0 -1
  44. package/dist/_check-9d180462.cjs +0 -16
  45. package/dist/_check-9d180462.cjs.map +0 -1
  46. package/dist/_index-c5bd1bc8.cjs.map +0 -1
  47. package/dist/_statusAction-385b72dd.cjs.map +0 -1
  48. package/dist/_watch-003a06b2.cjs.map +0 -1
  49. package/src/cli/statusAction.ts +0 -8
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ import { Observable, switchMap } from 'rxjs';
11
11
  import { promisify } from 'util';
12
12
  import rimrafCallback from 'rimraf';
13
13
  import { ExtractorConfig, Extractor } from '@microsoft/api-extractor';
14
+ import mkdirp from 'mkdirp';
14
15
  import prettier from 'prettier';
15
16
  import { TSDocConfigFile } from '@microsoft/tsdoc-config';
16
17
  import { parse } from 'jsonc-parser';
@@ -25,6 +26,7 @@ import treeify from 'treeify';
25
26
  import prettyBytes from 'pretty-bytes';
26
27
  import browserslist from 'browserslist';
27
28
  import ora from 'ora';
29
+ import esbuild$1 from 'esbuild';
28
30
  import { scan, startWith, distinctUntilChanged } from 'rxjs/operators';
29
31
  import globby from 'globby';
30
32
  import chokidar from 'chokidar';
@@ -96,6 +98,7 @@ const pkgSchema = z.object({
96
98
  main: z.optional(z.string()),
97
99
  browser: z.optional(z.record(z.string())),
98
100
  module: z.optional(z.string()),
101
+ types: z.optional(z.string()),
99
102
  exports: z.optional(
100
103
  z.record(
101
104
  z.object({
@@ -228,8 +231,8 @@ function _resolveBuildTasks(ctx) {
228
231
  type: "build:dts",
229
232
  importId,
230
233
  exportPath: exp._path,
231
- source: exp.source,
232
- output: exp.types
234
+ sourcePath: exp.source,
235
+ targetPath: exp.types
233
236
  };
234
237
  }
235
238
  }
@@ -285,7 +288,6 @@ function _resolveBuildTasks(ctx) {
285
288
  [
286
289
  `// AUTO-GENERATED \u2013 DO NOT EDIT`,
287
290
  ``,
288
- `export {default} from '${relativeTargetPath}'`,
289
291
  `export * from '${relativeTargetPath}'`,
290
292
  ``
291
293
  ].join("\n")
@@ -355,6 +357,10 @@ async function _buildTypes(options) {
355
357
  noEmit: false,
356
358
  outDir
357
359
  };
360
+ const rootDir = path.relative(
361
+ cwd,
362
+ compilerOptions.rootDir ? path.resolve(cwd, compilerOptions.rootDir) : path.dirname(path.resolve(cwd, sourcePath))
363
+ ) || ".";
358
364
  const program = ts.createProgram(tsconfig.fileNames, compilerOptions);
359
365
  const sourceFiles = program.getSourceFiles();
360
366
  const sourceFile = sourceFiles.find((f) => f.fileName === path.resolve(cwd, sourcePath));
@@ -380,7 +386,7 @@ ${message}`);
380
386
  throw new Error("failed to compile");
381
387
  }
382
388
  }
383
- const typesPath = path.resolve(outDir, sourcePath.replace(/\.ts$/, ".d.ts"));
389
+ const typesPath = path.resolve(outDir, path.relative(rootDir, path.resolve(cwd, sourcePath))).replace(/\.[jt]sx?$/, ".d.ts");
384
390
  return { path: typesPath };
385
391
  }
386
392
 
@@ -523,9 +529,10 @@ async function _extractTypes(options) {
523
529
  message.handled = true;
524
530
  }
525
531
  });
526
- const typesPath = path.resolve(distPath, `${exportPath}.d.ts`);
532
+ const typesPath = path.resolve(distPath, filePath);
527
533
  const typesBuf = await fs.readFile(typesPath);
528
534
  const prettierConfig = await prettier.resolveConfig(typesPath);
535
+ await mkdirp(path.dirname(typesPath));
529
536
  await fs.writeFile(
530
537
  typesPath,
531
538
  prettier.format(typesBuf.toString(), {
@@ -542,27 +549,37 @@ async function _extractTypes(options) {
542
549
 
543
550
  const rimraf = promisify(rimrafCallback);
544
551
  async function _doExtract(ctx, task) {
545
- var _a, _b;
552
+ var _a;
546
553
  const { config, cwd, dist, files, ts } = ctx;
547
554
  if (!ts.config || !ts.configPath)
548
555
  return { type: "dts", messages: [] };
549
- const sourcePath = task.source;
556
+ const sourcePath = task.sourcePath;
550
557
  const exportPath = task.exportPath === "." ? "./index" : task.exportPath;
551
558
  const distPath = path.resolve(cwd, dist);
552
- const outDir = ((_a = ts.config) == null ? void 0 : _a.options.outDir) || path.resolve(cwd, dist);
553
- const types = await _buildTypes({ cwd, outDir, sourcePath, tsconfig: ts.config });
559
+ const filePath = path.relative(distPath, path.resolve(cwd, task.targetPath));
560
+ const tmpDirPath = path.resolve(distPath, "__dts__");
561
+ const tmpDir = {
562
+ path: tmpDirPath,
563
+ removeCallback: () => rimraf(tmpDirPath)
564
+ };
565
+ const types = await _buildTypes({
566
+ cwd,
567
+ outDir: tmpDir.path,
568
+ sourcePath,
569
+ tsconfig: ts.config
570
+ });
554
571
  const { messages } = await _extractTypes({
555
572
  cwd,
556
573
  exportPath,
557
574
  files,
558
- filePath: path.relative(distPath, path.resolve(cwd, task.output)),
575
+ filePath,
559
576
  projectPath: cwd,
560
- rules: (_b = config == null ? void 0 : config.extract) == null ? void 0 : _b.rules,
577
+ rules: (_a = config == null ? void 0 : config.extract) == null ? void 0 : _a.rules,
561
578
  sourcePath: types.path,
562
579
  tsconfigPath: path.resolve(cwd, ts.configPath),
563
580
  distPath
564
581
  });
565
- await rimraf(outDir);
582
+ await tmpDir.removeCallback();
566
583
  if (messages.some((msg) => msg.logLevel === "error")) {
567
584
  throw new _DtsError("encountered errors when extracting types", messages);
568
585
  }
@@ -572,14 +589,16 @@ async function _doExtract(ctx, task) {
572
589
  const _dtsTask = {
573
590
  name: (_ctx, task) => [
574
591
  `build type definitions`,
575
- ` ${chalk.blue(task.importId)}: ${task.source} -> ${task.output}`
592
+ ` ${chalk.blue(task.importId)}: ${task.sourcePath} -> ${task.targetPath}`
576
593
  ].join("\n"),
577
594
  exec: (ctx, task) => {
578
595
  return new Observable((observer) => {
579
596
  _doExtract(ctx, task).then((result) => {
580
597
  observer.next(result);
581
598
  observer.complete();
582
- }).catch(observer.error);
599
+ }).catch((err) => {
600
+ observer.error(err);
601
+ });
583
602
  });
584
603
  },
585
604
  complete: (ctx, _task, result) => {
@@ -598,14 +617,14 @@ const _dtsTask = {
598
617
  const _dtsWatchTask = {
599
618
  name: (_ctx, task) => [
600
619
  `build type definitions`,
601
- ` ${chalk.blue(task.importId)}: ${task.source} -> ${task.output}`
620
+ ` ${chalk.blue(task.importId)}: ${task.sourcePath} -> ${task.targetPath}`
602
621
  ].join("\n"),
603
622
  exec: (ctx, task) => {
604
623
  return new Observable((observer) => {
605
624
  _doExtract(ctx, task).then((result) => {
606
625
  observer.next(result);
607
626
  observer.complete();
608
- }).catch(observer.error);
627
+ }).catch((err) => observer.error(err));
609
628
  });
610
629
  },
611
630
  complete: (ctx, _task, result) => {
@@ -726,7 +745,7 @@ const _rollupTask = {
726
745
  _execPromise(ctx, task).then((result) => {
727
746
  observer.next(result);
728
747
  observer.complete();
729
- }, observer.error);
748
+ }).catch((err) => observer.error(err));
730
749
  });
731
750
  },
732
751
  complete: () => {
@@ -824,7 +843,7 @@ function _getFilesize(file) {
824
843
  return prettyBytes(stats.size);
825
844
  }
826
845
 
827
- function _getFileExists(file) {
846
+ function _fileExists(file) {
828
847
  try {
829
848
  fs$1.accessSync(file);
830
849
  return true;
@@ -834,7 +853,7 @@ function _getFileExists(file) {
834
853
  }
835
854
  function _getFileInfo(cwd, filePath) {
836
855
  const p = path.resolve(cwd, filePath);
837
- const exists = _getFileExists(p);
856
+ const exists = _fileExists(p);
838
857
  const size = exists ? _getFilesize(p) : void 0;
839
858
  return { exists, size };
840
859
  }
@@ -850,7 +869,7 @@ function _printPackageTree(ctx) {
850
869
  function _fileInfo(file) {
851
870
  const info = _getFileInfo(cwd, file);
852
871
  if (!info.size) {
853
- return chalk.gray(file);
872
+ return `${chalk.gray(file)} ${chalk.red("does not exist")}`;
854
873
  }
855
874
  return `${file} ${chalk.gray(info.size)}`;
856
875
  }
@@ -1066,11 +1085,104 @@ async function build(options) {
1066
1085
  }
1067
1086
 
1068
1087
  async function check(options) {
1069
- const { cwd, tsconfig = "tsconfig.json" } = options;
1088
+ const { cwd, strict, tsconfig = "tsconfig.json" } = options;
1070
1089
  const pkg = await _loadPkgWithReporting({ cwd });
1071
1090
  const config = await _loadConfig({ cwd });
1072
1091
  const ctx = await _resolveBuildContext({ config, cwd, pkg, tsconfig });
1073
1092
  _printPackageTree(ctx);
1093
+ if (strict) {
1094
+ const missingFiles = [];
1095
+ for (const [, exp] of Object.entries(ctx.exports || {})) {
1096
+ if (exp.source && !_fileExists(path.resolve(cwd, exp.source))) {
1097
+ missingFiles.push(exp.source);
1098
+ }
1099
+ if (exp.require && !_fileExists(path.resolve(cwd, exp.require))) {
1100
+ missingFiles.push(exp.require);
1101
+ }
1102
+ if (exp.import && !_fileExists(path.resolve(cwd, exp.import))) {
1103
+ missingFiles.push(exp.import);
1104
+ }
1105
+ if (exp.types && !_fileExists(path.resolve(cwd, exp.types))) {
1106
+ missingFiles.push(exp.types);
1107
+ }
1108
+ }
1109
+ if (missingFiles.length) {
1110
+ throw new Error("missing files");
1111
+ }
1112
+ const _paths = {
1113
+ require: [],
1114
+ import: []
1115
+ };
1116
+ for (const [exportPath, exp] of Object.entries(ctx.exports || {})) {
1117
+ if (!exp._exported)
1118
+ continue;
1119
+ if (exp.require)
1120
+ _paths.require.push(exportPath);
1121
+ if (exp.import)
1122
+ _paths.import.push(exportPath);
1123
+ }
1124
+ const external = Object.keys(pkg.dependencies || {}).concat(
1125
+ Object.keys(pkg.devDependencies || {})
1126
+ );
1127
+ if (_paths.import.length) {
1128
+ const code = _paths.import.map((id) => `import('${id}')`).join("\n");
1129
+ try {
1130
+ const esbuildResult = await esbuild$1.build({
1131
+ external,
1132
+ stdin: {
1133
+ contents: code,
1134
+ loader: "js",
1135
+ resolveDir: cwd
1136
+ },
1137
+ format: "esm",
1138
+ bundle: true,
1139
+ logLevel: "silent",
1140
+ outfile: "/dev/null",
1141
+ platform: "node"
1142
+ });
1143
+ if (esbuildResult.warnings.length > 0) {
1144
+ console.warn(...esbuildResult.warnings);
1145
+ }
1146
+ } catch (err) {
1147
+ const { errors } = err;
1148
+ for (const _err of errors) {
1149
+ if (_err.location) {
1150
+ console.log(chalk.red("error"), _err.detail || _err.text);
1151
+ console.log(`${_err.location.line} | ${_err.location.lineText}`);
1152
+ console.log(
1153
+ "in",
1154
+ `./${_err.location.file}:${_err.location.line}:${_err.location.column}`
1155
+ );
1156
+ } else {
1157
+ console.log(_err.detail || _err.text);
1158
+ }
1159
+ console.log();
1160
+ }
1161
+ process.exit(1);
1162
+ }
1163
+ }
1164
+ if (_paths.require.length) {
1165
+ const code = _paths.require.map((id) => `require('${id}')`).join("\n");
1166
+ const esbuildResult = await esbuild$1.build({
1167
+ external,
1168
+ stdin: {
1169
+ contents: code,
1170
+ loader: "js",
1171
+ resolveDir: cwd
1172
+ },
1173
+ format: "cjs",
1174
+ bundle: true,
1175
+ outfile: "/dev/null",
1176
+ platform: "node"
1177
+ });
1178
+ if (esbuildResult.errors.length > 0) {
1179
+ throw new Error(esbuildResult.errors.join(", "));
1180
+ }
1181
+ if (esbuildResult.warnings.length > 0) {
1182
+ console.warn(...esbuildResult.warnings);
1183
+ }
1184
+ }
1185
+ }
1074
1186
  }
1075
1187
 
1076
1188
  function _resolveWatchTasks(ctx) {
@@ -1105,8 +1217,8 @@ function _resolveWatchTasks(ctx) {
1105
1217
  type: "watch:dts",
1106
1218
  importId,
1107
1219
  exportPath: exp._path,
1108
- source: exp.source,
1109
- output: exp.types
1220
+ sourcePath: exp.source,
1221
+ targetPath: exp.types
1110
1222
  };
1111
1223
  }
1112
1224
  }
@@ -1162,7 +1274,6 @@ function _resolveWatchTasks(ctx) {
1162
1274
  [
1163
1275
  `// AUTO-GENERATED \u2013 DO NOT EDIT`,
1164
1276
  ``,
1165
- `export {default} from '${relativeTargetPath}'`,
1166
1277
  `export * from '${relativeTargetPath}'`,
1167
1278
  ``
1168
1279
  ].join("\n")