@emeryld/rrroutes-export 1.0.6 → 1.0.8

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/dist/index.mjs CHANGED
@@ -1800,10 +1800,112 @@ async function loadChangelogInput(modulePath, exportName) {
1800
1800
  }
1801
1801
  return value;
1802
1802
  }
1803
- async function createSnapshot(repoRoot, moduleRel, exportName, commit, tempRoot, tsconfigRel, exportOptions) {
1803
+ async function ensureWorktreeDependencies(repoRoot, worktreeDir) {
1804
+ const rootNodeModules = path4.join(repoRoot, "node_modules");
1805
+ const worktreeNodeModules = path4.join(worktreeDir, "node_modules");
1806
+ try {
1807
+ await fs2.access(rootNodeModules);
1808
+ } catch {
1809
+ throw new Error(
1810
+ `Missing dependencies at ${rootNodeModules}. Run install in the repository root before changelog export.`
1811
+ );
1812
+ }
1813
+ try {
1814
+ await fs2.lstat(worktreeNodeModules);
1815
+ return;
1816
+ } catch {
1817
+ }
1818
+ await fs2.symlink(rootNodeModules, worktreeNodeModules, "junction");
1819
+ }
1820
+ async function pathExists(filePath) {
1821
+ try {
1822
+ await fs2.access(filePath);
1823
+ return true;
1824
+ } catch {
1825
+ return false;
1826
+ }
1827
+ }
1828
+ async function createMissingJsImportShims(worktreeDir) {
1829
+ const shimWrites = [];
1830
+ const filesToScan = [];
1831
+ const skipNames = /* @__PURE__ */ new Set([".git", "node_modules", "dist", "build"]);
1832
+ const walk = async (dir) => {
1833
+ const entries = await fs2.readdir(dir, { withFileTypes: true });
1834
+ for (const entry of entries) {
1835
+ if (entry.isDirectory()) {
1836
+ if (skipNames.has(entry.name)) continue;
1837
+ await walk(path4.join(dir, entry.name));
1838
+ continue;
1839
+ }
1840
+ if (!entry.isFile()) continue;
1841
+ if (!/\\.(ts|tsx|mts)$/.test(entry.name)) continue;
1842
+ filesToScan.push(path4.join(dir, entry.name));
1843
+ }
1844
+ };
1845
+ await walk(worktreeDir);
1846
+ const importRegexes = [
1847
+ /\\bfrom\\s+['\"](\\.[^'\"]+\\.js)['\"]/g,
1848
+ /\\bimport\\(\\s*['\"](\\.[^'\"]+\\.js)['\"]\\s*\\)/g,
1849
+ /\\brequire\\(\\s*['\"](\\.[^'\"]+\\.js)['\"]\\s*\\)/g
1850
+ ];
1851
+ const seen = /* @__PURE__ */ new Set();
1852
+ for (const filePath of filesToScan) {
1853
+ const content = await fs2.readFile(filePath, "utf8");
1854
+ for (const regex of importRegexes) {
1855
+ regex.lastIndex = 0;
1856
+ let match = regex.exec(content);
1857
+ while (match) {
1858
+ const specifier = match[1];
1859
+ const resolvedJs = path4.resolve(path4.dirname(filePath), specifier);
1860
+ if (seen.has(resolvedJs)) {
1861
+ match = regex.exec(content);
1862
+ continue;
1863
+ }
1864
+ seen.add(resolvedJs);
1865
+ if (await pathExists(resolvedJs)) {
1866
+ match = regex.exec(content);
1867
+ continue;
1868
+ }
1869
+ const tsCandidates = [
1870
+ resolvedJs.replace(/\\.js$/, ".ts"),
1871
+ resolvedJs.replace(/\\.js$/, ".mts"),
1872
+ resolvedJs.replace(/\\.js$/, ".tsx")
1873
+ ];
1874
+ let targetTs;
1875
+ for (const candidate of tsCandidates) {
1876
+ if (await pathExists(candidate)) {
1877
+ targetTs = candidate;
1878
+ break;
1879
+ }
1880
+ }
1881
+ if (targetTs) {
1882
+ const relImport = normalizePath(path4.relative(path4.dirname(resolvedJs), targetTs));
1883
+ const importPath = relImport.startsWith(".") ? relImport : `./${relImport}`;
1884
+ const shim = `export * from '${importPath}'
1885
+ import * as __all from '${importPath}'
1886
+ export default ('default' in __all ? __all.default : __all)
1887
+ `;
1888
+ await fs2.mkdir(path4.dirname(resolvedJs), { recursive: true });
1889
+ await fs2.writeFile(resolvedJs, shim, "utf8");
1890
+ shimWrites.push(resolvedJs);
1891
+ }
1892
+ match = regex.exec(content);
1893
+ }
1894
+ }
1895
+ }
1896
+ return shimWrites;
1897
+ }
1898
+ async function createSnapshot(repoRoot, moduleRel, exportName, commit, tempRoot, tsconfigRel, exportOptions, log) {
1804
1899
  const worktreeDir = path4.join(tempRoot, `wt-${commit.sha.slice(0, 12)}`);
1805
1900
  await runGit(repoRoot, ["worktree", "add", "--detach", worktreeDir, commit.sha]);
1806
1901
  try {
1902
+ await ensureWorktreeDependencies(repoRoot, worktreeDir);
1903
+ const shimmedFiles = await createMissingJsImportShims(worktreeDir);
1904
+ if (shimmedFiles.length > 0) {
1905
+ log?.(
1906
+ `[changelog] created ${shimmedFiles.length} missing .js import shim(s) in ${commit.sha.slice(0, 12)}`
1907
+ );
1908
+ }
1807
1909
  const modulePath = path4.resolve(worktreeDir, moduleRel);
1808
1910
  const tsconfigPath = tsconfigRel ? path4.resolve(worktreeDir, tsconfigRel) : void 0;
1809
1911
  const input = await loadChangelogInput(modulePath, exportName);
@@ -2082,7 +2184,8 @@ async function exportFinalizedLeavesChangelog(options) {
2082
2184
  commit,
2083
2185
  tempRoot,
2084
2186
  tsconfigRelative,
2085
- options
2187
+ options,
2188
+ log
2086
2189
  )
2087
2190
  );
2088
2191
  }