@8ms/helpers 2.3.4 → 2.3.5

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.
@@ -3,13 +3,18 @@ import fs from 'fs';
3
3
  import path from 'path';
4
4
  import {fileURLToPath} from 'url';
5
5
 
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'));
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
10
8
 
11
- const excludeDirs = new Set(['node_modules', 'scripts', 'jest', 'src', '.git', '.github', '.idea']);
9
+ const projectRoot = path.join(__dirname, '..');
10
+ const packageJsonPath = path.join(projectRoot, 'package.json');
11
+ const srcRoot = path.join(projectRoot, 'src');
12
12
 
13
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
14
+
15
+ const excludeDirs = ['node_modules', 'scripts', 'jest', '.git', '.github', '.idea', 'tests', 'test'];
16
+
17
+ // Build exports object
13
18
  const exports = {
14
19
  '.': {
15
20
  types: './index.d.ts',
@@ -19,51 +24,88 @@ const exports = {
19
24
  }
20
25
  };
21
26
 
22
- const importExportRegex = /((?:import|export)\s+(?:type\s+)?(?:[\s\S]*?from\s+)?['"])(\.{1,2}\/[^'"]+?)(['"])/g;
23
-
24
- function scan(baseDir, relativePath = '')
27
+ // Fix ESM exports in a src index.ts file by appending .js to bare relative imports
28
+ function fixIndexFile(filePath)
25
29
  {
26
- for(const entry of fs.readdirSync(baseDir, {withFileTypes: true}))
30
+ const content = fs.readFileSync(filePath, 'utf8');
31
+
32
+ const updated = content.replace(
33
+ /^(export \* from ["'])(\.\/[^"'.]+)(["'];?)$/gm,
34
+ (_, prefix, specifier, suffix) => `${prefix}${specifier}.js${suffix}`
35
+ );
36
+
37
+ if(updated !== content)
27
38
  {
28
- const fullPath = path.join(baseDir, entry.name);
29
- const newRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
39
+ fs.writeFileSync(filePath, updated, 'utf8');
40
+ console.log(`Fixed: ${path.relative(projectRoot, filePath)}`);
41
+ }
42
+ }
30
43
 
44
+ // Recursively scan src for index.ts files to fix
45
+ function fixSrcIndexFiles(dir)
46
+ {
47
+ const entries = fs.readdirSync(dir, {withFileTypes: true});
48
+
49
+ for(const entry of entries)
50
+ {
31
51
  if(entry.isDirectory())
32
52
  {
33
- if(excludeDirs.has(entry.name) || entry.name.startsWith('.')) continue;
34
-
35
- if(fs.existsSync(path.join(fullPath, 'index.js')))
53
+ if(!excludeDirs.includes(entry.name) && !entry.name.startsWith('.'))
36
54
  {
37
- const jsPath = `./${newRelativePath}/index.js`;
38
- const config = {types: `./${newRelativePath}/index.d.ts`, import: jsPath, require: jsPath, default: jsPath};
39
-
40
- exports[`./${newRelativePath}`] = config;
41
- exports[`./${newRelativePath}/index.js`] = config;
42
- exports[`./${newRelativePath}/index.d.ts`] = {types: config.types, default: config.types};
55
+ fixSrcIndexFiles(path.join(dir, entry.name));
43
56
  }
57
+ }
58
+ else if(entry.name === 'index.ts')
59
+ {
60
+ fixIndexFile(path.join(dir, entry.name));
61
+ }
62
+ }
63
+ }
64
+
65
+ // Recursively find all compiled directories with index.js files (outside src)
66
+ function findIndexDirs(baseDir, relativePath = '')
67
+ {
68
+ const entries = fs.readdirSync(baseDir, {withFileTypes: true});
44
69
 
45
- scan(fullPath, newRelativePath);
70
+ for(const entry of entries)
71
+ {
72
+ if(!entry.isDirectory() || excludeDirs.includes(entry.name) || entry.name.startsWith('.'))
73
+ {
74
+ continue;
46
75
  }
47
- else if(entry.isFile() && !entry.name.endsWith('.d.ts') && (entry.name.endsWith('.ts') || entry.name.endsWith('.tsx')))
76
+
77
+ const fullPath = path.join(baseDir, entry.name);
78
+ const newRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
79
+ const indexJsPath = path.join(fullPath, 'index.js');
80
+
81
+ if(fs.existsSync(indexJsPath))
48
82
  {
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
- );
83
+ const exportConfig = {
84
+ types: `./${newRelativePath}/index.d.ts`,
85
+ import: `./${newRelativePath}/index.js`,
86
+ require: `./${newRelativePath}/index.js`,
87
+ default: `./${newRelativePath}/index.js`
88
+ };
53
89
 
54
- if(updated !== original)
55
- {
56
- fs.writeFileSync(fullPath, updated, 'utf8');
57
- console.log(`Fixed extensions in: ${path.relative(projectRoot, fullPath)}`);
58
- }
90
+ exports[`./${newRelativePath}`] = exportConfig;
91
+ exports[`./${newRelativePath}/index.js`] = exportConfig;
92
+
93
+ exports[`./${newRelativePath}/index.d.ts`] = {
94
+ types: `./${newRelativePath}/index.d.ts`,
95
+ default: `./${newRelativePath}/index.d.ts`
96
+ };
59
97
  }
98
+
99
+ findIndexDirs(fullPath, newRelativePath);
60
100
  }
61
101
  }
62
102
 
63
- scan(projectRoot);
64
- scan(srcRoot);
103
+ fixSrcIndexFiles(srcRoot);
104
+
105
+ findIndexDirs(projectRoot);
65
106
 
66
107
  packageJson.exports = exports;
67
- fs.writeFileSync(path.join(projectRoot, 'package.json'), JSON.stringify(packageJson, null, '\t') + '\n', 'utf8');
108
+
109
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, '\t') + '\n', 'utf8');
68
110
 
69
111
  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.4",
4
+ "version": "2.3.5",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"