Skip to content

Commit 5d77a33

Browse files
committed
fix: patch bitsocial-react-hooks esm imports on install
Add a `postinstall` repair step for the git-based `@bitsocialhq/bitsocial-react-hooks` package so test and build environments stop tripping over extensionless relative imports in dist. This keeps GitHub Actions aligned with local installs without changing app logic.
1 parent 723a2de commit 5d77a33

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"scripts": {
4949
"generate:assets": "node scripts/generate-asset-manifest.js",
5050
"sync:directories": "node scripts/sync-directories.js",
51-
"postinstall": "node scripts/patch-capacitor-cli-tar.cjs",
51+
"postinstall": "node scripts/patch-capacitor-cli-tar.cjs && node scripts/patch-bitsocial-react-hooks-esm.cjs",
5252
"prebuild": "yarn sync:directories && yarn generate:assets",
5353
"prestart": "yarn sync:directories && yarn generate:assets",
5454
"start": "node scripts/start-dev.js",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const packageDistPath = path.join(__dirname, '..', 'node_modules', '@bitsocialhq', 'bitsocial-react-hooks', 'dist');
7+
const logPrefix = '[patch-bitsocial-react-hooks-esm]';
8+
9+
if (!fs.existsSync(packageDistPath)) {
10+
console.log(`${logPrefix} Skip: @bitsocialhq/bitsocial-react-hooks dist not found.`);
11+
process.exit(0);
12+
}
13+
14+
const relativeImportPattern = /(from\s+|import\s+)(['"])(\.\.?\/[^'"]+)\2/g;
15+
let touchedFiles = 0;
16+
let rewrittenImports = 0;
17+
18+
const splitSpecifier = (specifier) => {
19+
const suffixStart = specifier.search(/[?#]/);
20+
21+
if (suffixStart === -1) {
22+
return { bareSpecifier: specifier, suffix: '' };
23+
}
24+
25+
return {
26+
bareSpecifier: specifier.slice(0, suffixStart),
27+
suffix: specifier.slice(suffixStart),
28+
};
29+
};
30+
31+
const resolveSpecifier = (filePath, specifier) => {
32+
const { bareSpecifier, suffix } = splitSpecifier(specifier);
33+
34+
if (path.extname(bareSpecifier)) {
35+
return null;
36+
}
37+
38+
const absoluteSpecifierPath = path.resolve(path.dirname(filePath), bareSpecifier);
39+
40+
if (fs.existsSync(`${absoluteSpecifierPath}.js`)) {
41+
return `${bareSpecifier}.js${suffix}`;
42+
}
43+
44+
if (fs.existsSync(path.join(absoluteSpecifierPath, 'index.js'))) {
45+
return `${bareSpecifier}/index.js${suffix}`;
46+
}
47+
48+
return null;
49+
};
50+
51+
const patchFile = (filePath) => {
52+
const source = fs.readFileSync(filePath, 'utf8');
53+
let fileImportCount = 0;
54+
55+
const updated = source.replace(relativeImportPattern, (match, prefix, quote, specifier) => {
56+
const resolvedSpecifier = resolveSpecifier(filePath, specifier);
57+
58+
if (!resolvedSpecifier || resolvedSpecifier === specifier) {
59+
return match;
60+
}
61+
62+
fileImportCount += 1;
63+
return `${prefix}${quote}${resolvedSpecifier}${quote}`;
64+
});
65+
66+
if (!fileImportCount) {
67+
return;
68+
}
69+
70+
fs.writeFileSync(filePath, updated, 'utf8');
71+
touchedFiles += 1;
72+
rewrittenImports += fileImportCount;
73+
};
74+
75+
const walk = (currentPath) => {
76+
for (const entry of fs.readdirSync(currentPath, { withFileTypes: true })) {
77+
const entryPath = path.join(currentPath, entry.name);
78+
79+
if (entry.isDirectory()) {
80+
walk(entryPath);
81+
continue;
82+
}
83+
84+
if (entry.isFile() && entry.name.endsWith('.js')) {
85+
patchFile(entryPath);
86+
}
87+
}
88+
};
89+
90+
walk(packageDistPath);
91+
92+
if (!touchedFiles) {
93+
console.log(`${logPrefix} No relative ESM imports needed patching.`);
94+
process.exit(0);
95+
}
96+
97+
console.log(`${logPrefix} Patched ${rewrittenImports} imports across ${touchedFiles} files.`);

0 commit comments

Comments
 (0)