Skip to content

Commit 68fa8f9

Browse files
committed
Add function to discover modules from installed Python packages
This update introduces a new function, `discoverModulesFromInstalled`, which retrieves module names from the installed `spoon_ai` package using Python's pkgutil. The function handles exceptions and ensures a fallback to a default module name if parsing fails. Additionally, the main `run` function is modified to call this new discovery method when the package root does not exist, enhancing module detection capabilities.
1 parent 05b5b03 commit 68fa8f9

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

scripts/generate-api-docs.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ function discoverModules(packageRoot, packageName) {
3535
return Array.from(modules).sort();
3636
}
3737

38+
function discoverModulesFromInstalled(pythonBin, env) {
39+
const script = `
40+
import json, pkgutil
41+
try:
42+
import spoon_ai
43+
except Exception:
44+
print("[]")
45+
raise SystemExit(0)
46+
mods = {"spoon_ai"}
47+
for m in pkgutil.walk_packages(spoon_ai.__path__, spoon_ai.__name__ + "."):
48+
mods.add(m.name)
49+
print(json.dumps(sorted(mods)))
50+
`;
51+
const res = spawnSync(pythonBin, ['-c', script], { env, encoding: 'utf8' });
52+
if (res.status !== 0) return ['spoon_ai'];
53+
try {
54+
const parsed = JSON.parse(res.stdout.trim() || '[]');
55+
return Array.isArray(parsed) && parsed.length ? parsed : ['spoon_ai'];
56+
} catch (err) {
57+
return ['spoon_ai'];
58+
}
59+
}
60+
3861
function renderModule({ pythonBin, env, cookbookRoot, searchPath, moduleName, outputDir }) {
3962
const moduleParts = moduleName.split('.');
4063
const isPackage = modulesGlobal.some((m) => m.startsWith(moduleName + '.'));
@@ -80,7 +103,7 @@ renderer:
80103
const relPath = outputFile
81104
.replace(path.join(cookbookRoot, 'docs', 'api-reference') + path.sep, '')
82105
.replace(/\\/g, '/');
83-
const slugBase = `/api-reference/${relPath.replace(/index\\.md$/, '').replace(/\\.md$/, '')}`;
106+
const slugBase = `/api-reference/${relPath.replace(/index\.md$/, '').replace(/\.md$/, '')}`;
84107

85108
const content = readFileSync(outputFile, 'utf8').split('\n');
86109
let inCode = false;
@@ -141,6 +164,8 @@ function run() {
141164
if (existsSync(packageRoot)) {
142165
searchPath.unshift(coreDir);
143166
modules = discoverModules(packageRoot, 'spoon_ai');
167+
} else {
168+
modules = discoverModulesFromInstalled(pythonBin, env);
144169
}
145170
modulesGlobal = modules; // for render helper
146171

0 commit comments

Comments
 (0)