barrelize 1.4.0 → 1.4.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.
package/README.md CHANGED
@@ -109,9 +109,9 @@ Create a `.barrelize` file in your project root. The configuration file uses JSO
109
109
  // Export configuration for different file patterns
110
110
  "exports": {
111
111
  "**/*.ts": {
112
- // Include specific members (default: [])
112
+ // Include specific members using string or regex patterns (default: [])
113
113
  "includeMembers": ["MyClass", "/^.*Service$/"],
114
- // Exclude specific members (default: [])
114
+ // Exclude specific members using string or regex patterns (default: [])
115
115
  "excludeMembers": ["internal", "/^_.*$/"],
116
116
  // Map member patterns to export names
117
117
  "map": {
@@ -119,6 +119,8 @@ Create a `.barrelize` file in your project root. The configuration file uses JSO
119
119
  "default": "lib",
120
120
  "*": "services"
121
121
  },
122
+ // Skip mapping members if they don't exist in source (default: true)
123
+ "skipMapMembersIfNotExists": true,
122
124
  // Use * export when all members are exported (default: true)
123
125
  "asteriskIfAllExported": true,
124
126
  // Add 'type' prefix for type-only exports (default: true)
package/lib/index.js CHANGED
@@ -591,7 +591,7 @@ class CAC extends EventEmitter {
591
591
  }
592
592
  const cac = (name2 = "") => new CAC(name2);
593
593
  const name = "barrelize";
594
- const version = "1.3.1";
594
+ const version = "1.4.1";
595
595
  function cliInit() {
596
596
  const cli = cac(name);
597
597
  cli.command("[config path]", `Generate barrel files`).action(async (config) => {
@@ -943,6 +943,11 @@ function logWarning(message) {
943
943
  function logInfo(message) {
944
944
  console.log(colorize(message, TerminalColor.BLUE));
945
945
  }
946
+ function logDebug(message) {
947
+ if (process.argv.includes("--verbose")) {
948
+ console.log(colorize(message, TerminalColor.MAGENTA));
949
+ }
950
+ }
946
951
  const INITIAL_CONFIG = {
947
952
  $schema: "node_modules/barrelize/schema.json",
948
953
  barrels: [
@@ -996,8 +1001,8 @@ const validateConfig = (() => {
996
1001
  return true;
997
1002
  return "object" === typeof value && null !== value && false === Array.isArray(value) && _io4(value, _exceptionable);
998
1003
  });
999
- const _io4 = (input, _exceptionable = true) => (void 0 === input.includeMembers || Array.isArray(input.includeMembers) && input.includeMembers.every((elem, _index5) => "string" === typeof elem)) && (void 0 === input.excludeMembers || Array.isArray(input.excludeMembers) && input.excludeMembers.every((elem, _index6) => "string" === typeof elem)) && (void 0 === input.map || "object" === typeof input.map && null !== input.map && false === Array.isArray(input.map) && _io5(input.map, _exceptionable)) && (void 0 === input.asteriskIfAllExported || "boolean" === typeof input.asteriskIfAllExported) && (void 0 === input.typePrefixIfPossible || "boolean" === typeof input.typePrefixIfPossible) && (0 === Object.keys(input).length || Object.keys(input).every((key2) => {
1000
- if (["includeMembers", "excludeMembers", "map", "asteriskIfAllExported", "typePrefixIfPossible"].some((prop) => key2 === prop))
1004
+ const _io4 = (input, _exceptionable = true) => (void 0 === input.includeMembers || Array.isArray(input.includeMembers) && input.includeMembers.every((elem, _index5) => "string" === typeof elem)) && (void 0 === input.excludeMembers || Array.isArray(input.excludeMembers) && input.excludeMembers.every((elem, _index6) => "string" === typeof elem)) && (void 0 === input.map || "object" === typeof input.map && null !== input.map && false === Array.isArray(input.map) && _io5(input.map, _exceptionable)) && (void 0 === input.skipMapMembersIfNotExists || "boolean" === typeof input.skipMapMembersIfNotExists) && (void 0 === input.asteriskIfAllExported || "boolean" === typeof input.asteriskIfAllExported) && (void 0 === input.typePrefixIfPossible || "boolean" === typeof input.typePrefixIfPossible) && (0 === Object.keys(input).length || Object.keys(input).every((key2) => {
1005
+ if (["includeMembers", "excludeMembers", "map", "skipMapMembersIfNotExists", "asteriskIfAllExported", "typePrefixIfPossible"].some((prop) => key2 === prop))
1001
1006
  return true;
1002
1007
  const value = input[key2];
1003
1008
  if (void 0 === value)
@@ -1202,6 +1207,10 @@ const validateConfig = (() => {
1202
1207
  path: _path + ".map",
1203
1208
  expected: "(__type.o2 | undefined)",
1204
1209
  value: input.map
1210
+ }), void 0 === input.skipMapMembersIfNotExists || "boolean" === typeof input.skipMapMembersIfNotExists || _report(_exceptionable, {
1211
+ path: _path + ".skipMapMembersIfNotExists",
1212
+ expected: "(boolean | undefined)",
1213
+ value: input.skipMapMembersIfNotExists
1205
1214
  }), void 0 === input.asteriskIfAllExported || "boolean" === typeof input.asteriskIfAllExported || _report(_exceptionable, {
1206
1215
  path: _path + ".asteriskIfAllExported",
1207
1216
  expected: "(boolean | undefined)",
@@ -1211,7 +1220,7 @@ const validateConfig = (() => {
1211
1220
  expected: "(boolean | undefined)",
1212
1221
  value: input.typePrefixIfPossible
1213
1222
  }), 0 === Object.keys(input).length || (false === _exceptionable || Object.keys(input).map((key2) => {
1214
- if (["includeMembers", "excludeMembers", "map", "asteriskIfAllExported", "typePrefixIfPossible"].some((prop) => key2 === prop))
1223
+ if (["includeMembers", "excludeMembers", "map", "skipMapMembersIfNotExists", "asteriskIfAllExported", "typePrefixIfPossible"].some((prop) => key2 === prop))
1215
1224
  return true;
1216
1225
  const value = input[key2];
1217
1226
  if (void 0 === value)
@@ -16806,7 +16815,7 @@ function requireLib() {
16806
16815
  var libExports = requireLib();
16807
16816
  async function extractExports(sourcePath) {
16808
16817
  if (!existsSync(sourcePath)) {
16809
- logWarning(`Source path '${sourcePath}' does not exist`);
16818
+ logDebug(`Source path '${sourcePath}' does not exist`);
16810
16819
  return [];
16811
16820
  }
16812
16821
  const options = {
@@ -16821,7 +16830,9 @@ async function extractExportsFromFile(sourcePath, options) {
16821
16830
  const ast = parseFileContent(content);
16822
16831
  return extractExportsFromAst(ast, options);
16823
16832
  } catch (error) {
16824
- console.error(`Failed to process file '${sourcePath}':`, error);
16833
+ if (error instanceof Error) {
16834
+ logDebug(`Failed to process file '${sourcePath}': ${error.message}`);
16835
+ }
16825
16836
  return [];
16826
16837
  }
16827
16838
  }
@@ -16853,7 +16864,7 @@ async function extractExportsFromAst(ast, options) {
16853
16864
  if (options.recursive) {
16854
16865
  const normalizedPath = normalizeSourcePath(node.source.value, options);
16855
16866
  if (!normalizedPath) {
16856
- logWarning(`Exported module path '${normalizedPath}' does not exist`);
16867
+ logDebug(`Exported module path '${normalizedPath}' does not exist`);
16857
16868
  } else {
16858
16869
  const innerExports = await extractExportsFromFile(normalizedPath, options);
16859
16870
  exports.push(...innerExports);
@@ -23577,65 +23588,83 @@ async function fillExports(rootDir, exportPaths, config) {
23577
23588
  const mapRegexes = config.map ? Object.entries(config.map).map(([k, v]) => [tryParseRegex(k) ?? k, v]) : null;
23578
23589
  const asteriskIfAllExported = config.asteriskIfAllExported ?? true;
23579
23590
  const typePrefixIfPossible = config.typePrefixIfPossible ?? true;
23591
+ const skipMapMembersIfNotExists = config.skipMapMembersIfNotExists ?? true;
23580
23592
  for (const pathInfo of exportPaths) {
23581
23593
  const resolvedPath = resolve(rootDir, pathInfo.originalPath);
23582
- let allExportMembers = await getExportedMembers(resolvedPath);
23583
- if (!allExportMembers.length) {
23584
- continue;
23585
- }
23594
+ const allExportMembers = await getExportedMembers(resolvedPath);
23586
23595
  const exportMemberList = await filterExportMembers(allExportMembers, includeRegexes, excludeRegexes);
23587
- const exportMemberMap = getExportMemberMap(mapRegexes, allExportMembers);
23588
- if (exportMemberMap.has("*")) {
23596
+ const exportMemberMap = getExportMemberMap(allExportMembers, mapRegexes, skipMapMembersIfNotExists);
23597
+ const asteriskMapItem = exportMemberMap.get("*");
23598
+ if (asteriskMapItem) {
23599
+ pathInfo.exports = [
23600
+ formatExportMember(
23601
+ typePrefixIfPossible,
23602
+ asteriskMapItem.exportKind,
23603
+ asteriskMapItem.oldName,
23604
+ asteriskMapItem.newName
23605
+ )
23606
+ ];
23589
23607
  continue;
23590
23608
  }
23591
23609
  if (asteriskIfAllExported && !exportMemberMap.size && exportMemberList.length === allExportMembers.length) {
23592
23610
  continue;
23593
23611
  }
23594
23612
  pathInfo.exports = [];
23595
- for (const { exportInfo, newName } of exportMemberMap.values()) {
23596
- pathInfo.exports.push(formatExportMember(typePrefixIfPossible, exportInfo, newName));
23613
+ for (const { exportKind, oldName, newName } of exportMemberMap.values()) {
23614
+ pathInfo.exports.push(formatExportMember(typePrefixIfPossible, exportKind, oldName, newName));
23597
23615
  }
23598
23616
  for (const exportInfo of exportMemberList) {
23599
23617
  if (!exportMemberMap.has(exportInfo.name)) {
23600
- pathInfo.exports.push(formatExportMember(typePrefixIfPossible, exportInfo));
23618
+ pathInfo.exports.push(
23619
+ formatExportMember(typePrefixIfPossible, exportInfo.exportKind, exportInfo.name)
23620
+ );
23601
23621
  }
23602
23622
  }
23603
23623
  }
23604
23624
  }
23605
- function formatExportMember(typePrefixIfPossible, exportInfo, newName) {
23606
- const typeIfNeeded = typePrefixIfPossible && exportInfo.exportKind === "type" ? "type " : "";
23625
+ function formatExportMember(typePrefixIfPossible, exportKind, name2, newName) {
23626
+ const typeIfNeeded = typePrefixIfPossible && exportKind === "type" ? "type " : "";
23607
23627
  if (newName) {
23608
- return `${typeIfNeeded}${exportInfo.name} as ${newName}`;
23628
+ return `${typeIfNeeded}${name2} as ${newName}`;
23609
23629
  } else {
23610
- return `${typeIfNeeded}${exportInfo.name}`;
23630
+ return `${typeIfNeeded}${name2}`;
23611
23631
  }
23612
23632
  }
23613
- function getExportMemberMap(mapRegexes, exportMembers) {
23633
+ function getExportMemberMap(exportMembers, mapRegexes, skipMapMembersIfNotExists) {
23614
23634
  const exportMemberMap = /* @__PURE__ */ new Map();
23615
23635
  if (!mapRegexes?.length) {
23616
23636
  return exportMemberMap;
23617
23637
  }
23618
23638
  for (const [find, replacement] of mapRegexes) {
23619
23639
  if (typeof find === "string") {
23640
+ if (!skipMapMembersIfNotExists || find === "*") {
23641
+ exportMemberMap.set(find, { oldName: find, newName: replacement });
23642
+ continue;
23643
+ }
23620
23644
  if (find !== replacement) {
23621
23645
  const exportInfo = exportMembers.find((x) => x.name === find);
23622
23646
  if (exportInfo) {
23623
- exportMemberMap.set(find, { exportInfo, newName: replacement });
23624
- if (find === "*") {
23625
- return exportMemberMap;
23626
- }
23647
+ exportMemberMap.set(find, {
23648
+ exportKind: exportInfo.exportKind,
23649
+ oldName: exportInfo.name,
23650
+ newName: replacement
23651
+ });
23627
23652
  }
23628
23653
  }
23629
- } else {
23630
- exportMembers.forEach((exportInfo) => {
23631
- if (find.test(exportInfo.name)) {
23632
- const newName = exportInfo.name.replace(find, replacement);
23633
- if (exportInfo.name !== newName) {
23634
- exportMemberMap.set(exportInfo.name, { exportInfo, newName });
23635
- }
23636
- }
23637
- });
23654
+ continue;
23638
23655
  }
23656
+ exportMembers.forEach((exportInfo) => {
23657
+ if (find.test(exportInfo.name)) {
23658
+ const newName = exportInfo.name.replace(find, replacement);
23659
+ if (exportInfo.name !== newName) {
23660
+ exportMemberMap.set(exportInfo.name, {
23661
+ exportKind: exportInfo.exportKind,
23662
+ oldName: exportInfo.name,
23663
+ newName
23664
+ });
23665
+ }
23666
+ }
23667
+ });
23639
23668
  }
23640
23669
  return exportMemberMap;
23641
23670
  }
@@ -23731,6 +23760,7 @@ export {
23731
23760
  handlePathReplace,
23732
23761
  handlePaths,
23733
23762
  indexFileExportedPaths,
23763
+ logDebug,
23734
23764
  logError,
23735
23765
  logInfo,
23736
23766
  logValidationError,
@@ -121,6 +121,11 @@ export type ExportsConfig = {
121
121
  */
122
122
  [key: string]: string;
123
123
  };
124
+ /**
125
+ * Whether to skip mapping members if they don't export in the source file
126
+ * @default true
127
+ */
128
+ skipMapMembersIfNotExists?: boolean;
124
129
  /**
125
130
  * Use asterisk (*) export when all members from the file are exported
126
131
  * @default true
@@ -1,2 +1,7 @@
1
- import { BarrelConfig, ExportPathInfo } from '../../index.ts';
1
+ import { BarrelConfig, ExportInfo, ExportPathInfo } from '../../index.ts';
2
+ export type MapReplacement = {
3
+ exportKind?: ExportInfo['exportKind'];
4
+ oldName: string;
5
+ newName: string;
6
+ };
2
7
  export declare function handlePathExports(rootDir: string, barrelConfig: BarrelConfig, exportPaths: ExportPathInfo[]): Promise<void>;
@@ -1,3 +1,4 @@
1
1
  export declare function logError(message: string): void;
2
2
  export declare function logWarning(message: string): void;
3
3
  export declare function logInfo(message: string): void;
4
+ export declare function logDebug(message: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barrelize",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Automatically generating index (barrel) files",
5
5
  "scripts": {
6
6
  "build": "npm run schema && vite build",
package/schema.json CHANGED
@@ -189,6 +189,12 @@
189
189
  "*": "lib"
190
190
  }
191
191
  },
192
+ "skipMapMembersIfNotExists": {
193
+ "type": "boolean",
194
+ "description": "Whether to skip mapping members if they don't export in the source file",
195
+ "markdownDescription": "Whether to skip mapping members if they don't export in the source file",
196
+ "default": true
197
+ },
192
198
  "asteriskIfAllExported": {
193
199
  "type": "boolean",
194
200
  "description": "Use asterisk (*) export when all members from the file are exported",