@forsakringskassan/vite-lib-config 5.2.1 → 5.3.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.
@@ -485,8 +485,112 @@ var init_find_up = __esm({
485
485
  });
486
486
 
487
487
  // src/write-config.ts
488
+ import { existsSync } from "node:fs";
488
489
  import fs3 from "node:fs/promises";
489
490
  import path3 from "node:path";
491
+ import { styleText } from "node:util";
492
+
493
+ // node_modules/detect-indent/index.js
494
+ var INDENT_REGEX = /^(?:( )+|\t+)/;
495
+ var INDENT_TYPE_SPACE = "space";
496
+ var INDENT_TYPE_TAB = "tab";
497
+ function shouldIgnoreSingleSpace(ignoreSingleSpaces, indentType, value) {
498
+ return ignoreSingleSpaces && indentType === INDENT_TYPE_SPACE && value === 1;
499
+ }
500
+ function makeIndentsMap(string, ignoreSingleSpaces) {
501
+ const indents = /* @__PURE__ */ new Map();
502
+ let previousSize = 0;
503
+ let previousIndentType;
504
+ let key;
505
+ for (const line of string.split(/\n/g)) {
506
+ if (!line) {
507
+ continue;
508
+ }
509
+ const matches = line.match(INDENT_REGEX);
510
+ if (matches === null) {
511
+ previousSize = 0;
512
+ previousIndentType = "";
513
+ } else {
514
+ const indent = matches[0].length;
515
+ const indentType = matches[1] ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB;
516
+ if (shouldIgnoreSingleSpace(ignoreSingleSpaces, indentType, indent)) {
517
+ continue;
518
+ }
519
+ if (indentType !== previousIndentType) {
520
+ previousSize = 0;
521
+ }
522
+ previousIndentType = indentType;
523
+ let use = 1;
524
+ let weight = 0;
525
+ const indentDifference = indent - previousSize;
526
+ previousSize = indent;
527
+ if (indentDifference === 0) {
528
+ use = 0;
529
+ weight = 1;
530
+ } else {
531
+ const absoluteIndentDifference = Math.abs(indentDifference);
532
+ if (shouldIgnoreSingleSpace(ignoreSingleSpaces, indentType, absoluteIndentDifference)) {
533
+ continue;
534
+ }
535
+ key = encodeIndentsKey(indentType, absoluteIndentDifference);
536
+ }
537
+ const entry = indents.get(key);
538
+ indents.set(key, entry === void 0 ? [1, 0] : [entry[0] + use, entry[1] + weight]);
539
+ }
540
+ }
541
+ return indents;
542
+ }
543
+ function encodeIndentsKey(indentType, indentAmount) {
544
+ const typeCharacter = indentType === INDENT_TYPE_SPACE ? "s" : "t";
545
+ return typeCharacter + String(indentAmount);
546
+ }
547
+ function decodeIndentsKey(indentsKey) {
548
+ const keyHasTypeSpace = indentsKey[0] === "s";
549
+ const type = keyHasTypeSpace ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB;
550
+ const amount = Number(indentsKey.slice(1));
551
+ return { type, amount };
552
+ }
553
+ function getMostUsedKey(indents) {
554
+ let result;
555
+ let maxUsed = 0;
556
+ let maxWeight = 0;
557
+ for (const [key, [usedCount, weight]] of indents) {
558
+ if (usedCount > maxUsed || usedCount === maxUsed && weight > maxWeight) {
559
+ maxUsed = usedCount;
560
+ maxWeight = weight;
561
+ result = key;
562
+ }
563
+ }
564
+ return result;
565
+ }
566
+ function makeIndentString(type, amount) {
567
+ const indentCharacter = type === INDENT_TYPE_SPACE ? " " : " ";
568
+ return indentCharacter.repeat(amount);
569
+ }
570
+ function detectIndent(string) {
571
+ if (typeof string !== "string") {
572
+ throw new TypeError("Expected a string");
573
+ }
574
+ let indents = makeIndentsMap(string, true);
575
+ if (indents.size === 0) {
576
+ indents = makeIndentsMap(string, false);
577
+ }
578
+ const keyOfMostUsedIndent = getMostUsedKey(indents);
579
+ let type;
580
+ let amount = 0;
581
+ let indent = "";
582
+ if (keyOfMostUsedIndent !== void 0) {
583
+ ({ type, amount } = decodeIndentsKey(keyOfMostUsedIndent));
584
+ indent = makeIndentString(type, amount);
585
+ }
586
+ return {
587
+ amount,
588
+ type,
589
+ indent
590
+ };
591
+ }
592
+
593
+ // src/write-config.ts
490
594
  var boilerplate = `/* This file was generated by @forsakringskassan/vite-lib-config. Changes may be overwritten! */`;
491
595
  function detectTestRunner(flags) {
492
596
  if (flags.has("--with-jest")) {
@@ -497,6 +601,11 @@ function detectTestRunner(flags) {
497
601
  }
498
602
  return "vitest";
499
603
  }
604
+ function detectApiExtractor(cwd) {
605
+ const haveBaseConfig = existsSync(path3.join(cwd, "api-extractor.json"));
606
+ const haveLibConfig = existsSync(path3.join(cwd, "api-extractor.lib.json"));
607
+ return haveBaseConfig || haveLibConfig;
608
+ }
500
609
  async function serializeJson(data) {
501
610
  const { format, resolveConfig } = await import("prettier");
502
611
  const content = JSON.stringify(data, null, 4);
@@ -590,15 +699,44 @@ async function generateTsconfigPageobjects(options) {
590
699
  };
591
700
  return [boilerplate, await serializeJson(config)].join("\n\n");
592
701
  }
593
- async function readJsonFile(filePath) {
702
+ async function readJsonFile(cwd, filename) {
703
+ const filePath = path3.join(cwd, filename);
594
704
  const content = await fs3.readFile(filePath, "utf8");
595
- return JSON.parse(content);
705
+ const data = JSON.parse(content);
706
+ const { indent } = detectIndent(content);
707
+ return { data, indent };
596
708
  }
597
709
  async function writeJsonFile(cwd, filename, content) {
598
710
  const filePath = path3.join(cwd, filename);
599
711
  await fs3.writeFile(filePath, content, "utf8");
600
712
  console.log(filename, "written");
601
713
  }
714
+ function updateBuildScripts(pkg, options) {
715
+ const result = { ...pkg, scripts: { ...pkg.scripts } };
716
+ result.scripts.build = [
717
+ "run-s",
718
+ "build:lib",
719
+ options.enableSelectors ? "build:selectors" : null,
720
+ "build:dts",
721
+ options.enableApiExtractor ? "build:api" : null
722
+ ].filter(Boolean).join(" ");
723
+ result.scripts["build:api"] = options.enableApiExtractor ? "fk-api-extractor api-extractor.*.json" : void 0;
724
+ result.scripts["build:dts"] = "vue-tsc -b";
725
+ result.scripts["build:lib"] = "fk-build-vue-lib";
726
+ result.scripts["build:selectors"] = options.enableSelectors ? "fk-build-selectors" : void 0;
727
+ delete result.scripts["build:pageobject"];
728
+ return result;
729
+ }
730
+ async function writeBuildScripts(cwd, options) {
731
+ const result = await readJsonFile(cwd, "package.json");
732
+ const { data: pkg, indent } = result;
733
+ const updated = JSON.stringify(
734
+ updateBuildScripts(pkg, options),
735
+ void 0,
736
+ indent
737
+ );
738
+ await writeJsonFile(cwd, "package.json", updated);
739
+ }
602
740
  async function findCypressConfigPath(cwd) {
603
741
  const { findUp: findUp2 } = await Promise.resolve().then(() => (init_find_up(), find_up_exports));
604
742
  const absolutePath = await findUp2("cypress/tsconfig.json", { cwd });
@@ -610,6 +748,9 @@ async function findCypressConfigPath(cwd) {
610
748
  function isTSConfigFile(filename) {
611
749
  return filename.startsWith("tsconfig.") && filename.endsWith(".json");
612
750
  }
751
+ function flagEnabled(value) {
752
+ return value ? styleText("green", "enabled") : styleText("red", "disabled");
753
+ }
613
754
  async function run(cwd, argv) {
614
755
  const flags = new Set(argv.filter((it) => it.startsWith("--")));
615
756
  if (flags.has("--help")) {
@@ -621,7 +762,7 @@ async function run(cwd, argv) {
621
762
  `);
622
763
  return;
623
764
  }
624
- const pkg = await readJsonFile(path3.join(cwd, "package.json"));
765
+ const { data: pkg } = await readJsonFile(cwd, "package.json");
625
766
  if (!pkg.devDependencies?.["@forsakringskassan/vite-lib-config"]) {
626
767
  console.error(
627
768
  `The "${pkg.name}" does not use "@forsakringskassan/vite-lib-config".`
@@ -631,10 +772,12 @@ async function run(cwd, argv) {
631
772
  }
632
773
  const cypressConfigPath = await findCypressConfigPath(cwd);
633
774
  const testRunner = detectTestRunner(flags);
775
+ const enableApiExtractor = detectApiExtractor(cwd);
634
776
  const options = {
635
777
  name: pkg.name,
636
778
  cypressConfigPath,
637
779
  testRunner,
780
+ enableApiExtractor,
638
781
  enablePageobjects: Boolean(pkg.exports?.["./cypress"]),
639
782
  enableSelectors: Boolean(pkg.exports?.["./selectors"])
640
783
  };
@@ -646,7 +789,7 @@ async function run(cwd, argv) {
646
789
  ["tsconfig.pageobjects.json", generateTsconfigPageobjects(options)]
647
790
  ]);
648
791
  console.group("Writing TypeScript configuration");
649
- console.log("Test runner:", testRunner);
792
+ console.log("Test runner:", styleText("cyan", testRunner));
650
793
  console.log();
651
794
  const written = /* @__PURE__ */ new Set();
652
795
  for (const [filename, promise] of generated) {
@@ -665,6 +808,14 @@ async function run(cwd, argv) {
665
808
  console.log(entry, "removed");
666
809
  }
667
810
  console.groupEnd();
811
+ console.log();
812
+ console.group("Writing package.json scripts");
813
+ console.log("API Extractor:", flagEnabled(options.enableApiExtractor));
814
+ console.log("Selector objects:", flagEnabled(options.enableSelectors));
815
+ console.log();
816
+ await writeBuildScripts(cwd, options);
817
+ console.groupEnd();
818
+ console.log();
668
819
  }
669
820
  export {
670
821
  generateTsconfig,
@@ -672,5 +823,6 @@ export {
672
823
  generateTsconfigLib,
673
824
  generateTsconfigPageobjects,
674
825
  generateTsconfigSelectors,
675
- run
826
+ run,
827
+ updateBuildScripts
676
828
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forsakringskassan/vite-lib-config",
3
- "version": "5.2.1",
3
+ "version": "5.3.0",
4
4
  "description": "Försäkringskassan toolchain to build libraries with Vite",
5
5
  "keywords": [
6
6
  "vite"