@8ms/helpers 2.3.3 → 2.3.4

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.
@@ -1,20 +1,15 @@
1
+
1
2
  import fs from 'fs';
2
3
  import path from 'path';
3
4
  import {fileURLToPath} from 'url';
4
5
 
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = path.dirname(__filename);
7
-
8
- const projectRoot = path.join(__dirname, '..');
9
- const packageJsonPath = path.join(projectRoot, 'package.json');
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const projectRoot = path.join(__dirname, '..');
8
+ const srcRoot = path.join(projectRoot, 'src');
9
+ const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'));
10
10
 
11
- // Read package.json
12
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
11
+ const excludeDirs = new Set(['node_modules', 'scripts', 'jest', 'src', '.git', '.github', '.idea']);
13
12
 
14
- // Directories to exclude from scanning
15
- const excludeDirs = ['node_modules', 'scripts', 'jest', 'src', '.git', '.github', '.idea'];
16
-
17
- // Build exports object
18
13
  const exports = {
19
14
  '.': {
20
15
  types: './index.d.ts',
@@ -24,55 +19,51 @@ const exports = {
24
19
  }
25
20
  };
26
21
 
27
- // Recursively find all directories with index.js files
28
- function findIndexDirs(baseDir, relativePath = '')
29
- {
30
- const entries = fs.readdirSync(baseDir, {withFileTypes: true});
22
+ const importExportRegex = /((?:import|export)\s+(?:type\s+)?(?:[\s\S]*?from\s+)?['"])(\.{1,2}\/[^'"]+?)(['"])/g;
31
23
 
32
- for(const entry of entries)
24
+ function scan(baseDir, relativePath = '')
25
+ {
26
+ for(const entry of fs.readdirSync(baseDir, {withFileTypes: true}))
33
27
  {
34
- if(!entry.isDirectory() || excludeDirs.includes(entry.name) || entry.name.startsWith('.'))
35
- {
36
- continue;
37
- }
38
-
39
- const fullPath = path.join(baseDir, entry.name);
28
+ const fullPath = path.join(baseDir, entry.name);
40
29
  const newRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
41
- const indexJsPath = path.join(fullPath, 'index.js');
42
30
 
43
- // If this directory has an index.js, add it to exports
44
- if(fs.existsSync(indexJsPath))
31
+ if(entry.isDirectory())
45
32
  {
46
- const exportConfig = {
47
- types: `./${newRelativePath}/index.d.ts`,
48
- import: `./${newRelativePath}/index.js`,
49
- require: `./${newRelativePath}/index.js`,
50
- default: `./${newRelativePath}/index.js`
51
- };
33
+ if(excludeDirs.has(entry.name) || entry.name.startsWith('.')) continue;
52
34
 
53
- // Add both the directory path and explicit index.js path
54
- exports[`./${newRelativePath}`] = exportConfig;
55
- exports[`./${newRelativePath}/index.js`] = exportConfig;
35
+ if(fs.existsSync(path.join(fullPath, 'index.js')))
36
+ {
37
+ const jsPath = `./${newRelativePath}/index.js`;
38
+ const config = {types: `./${newRelativePath}/index.d.ts`, import: jsPath, require: jsPath, default: jsPath};
56
39
 
57
- // Also add .d.ts mapping for TypeScript
58
- exports[`./${newRelativePath}/index.d.ts`] = {
59
- types: `./${newRelativePath}/index.d.ts`,
60
- default: `./${newRelativePath}/index.d.ts`
61
- };
62
- }
40
+ exports[`./${newRelativePath}`] = config;
41
+ exports[`./${newRelativePath}/index.js`] = config;
42
+ exports[`./${newRelativePath}/index.d.ts`] = {types: config.types, default: config.types};
43
+ }
63
44
 
64
- // Recursively scan subdirectories
65
- findIndexDirs(fullPath, newRelativePath);
45
+ scan(fullPath, newRelativePath);
46
+ }
47
+ else if(entry.isFile() && !entry.name.endsWith('.d.ts') && (entry.name.endsWith('.ts') || entry.name.endsWith('.tsx')))
48
+ {
49
+ const original = fs.readFileSync(fullPath, 'utf8');
50
+ const updated = original.replace(importExportRegex, (match, prefix, specifier, suffix) =>
51
+ path.extname(specifier) ? match : `${prefix}${specifier}.js${suffix}`
52
+ );
53
+
54
+ if(updated !== original)
55
+ {
56
+ fs.writeFileSync(fullPath, updated, 'utf8');
57
+ console.log(`Fixed extensions in: ${path.relative(projectRoot, fullPath)}`);
58
+ }
59
+ }
66
60
  }
67
61
  }
68
62
 
69
- // Start scanning from project root
70
- findIndexDirs(projectRoot);
63
+ scan(projectRoot);
64
+ scan(srcRoot);
71
65
 
72
- // Update package.json with new exports
73
66
  packageJson.exports = exports;
74
-
75
- // Write back to package.json
76
- fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, '\t') + '\n', 'utf8');
67
+ fs.writeFileSync(path.join(projectRoot, 'package.json'), JSON.stringify(packageJson, null, '\t') + '\n', 'utf8');
77
68
 
78
69
  console.log(`Generated exports for ${Object.keys(exports).length} entries`);
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "2.3.3",
4
+ "version": "2.3.4",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"