Skip to content

Commit efeb673

Browse files
committed
Big fix
1 parent d7f27cf commit efeb673

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

core/Run.ts

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,43 +69,38 @@ namespace Moduless
6969
}
7070

7171
/**
72-
* Finds cover functions tucked away in TypeScript files that can
73-
* only be found by traversing the TypeScript project structure.
74-
* Returns null in the case when no functions were discovered.
72+
* Scans the entire project graph, calls require() on each
73+
* outFile defined in each project, and returns an array of
74+
* objects containing the the Project objects and the export
75+
* value acquired from the require() call.
7576
*/
76-
function tryLoadCoversFromDependencies(projectPath: string)
77+
function getGraphFromDependencies(projectPath: string)
7778
{
7879
const graph = new ProjectGraph(projectPath);
79-
const scriptFilePaths: string[] = [];
80+
const out: { project: Project; export: object; }[] = [];
8081

8182
for (const project of graph.eachProject())
82-
if (project.outFile !== "")
83-
scriptFilePaths.push(project.outFile);
84-
85-
const exports: object[] = [];
86-
87-
for (const scriptFilePath of scriptFilePaths)
8883
{
89-
if (!Fs.existsSync(scriptFilePath))
84+
if (!Fs.existsSync(project.outFile))
9085
{
91-
Util.error("File not found: " + scriptFilePath);
86+
Util.error("File not found: " + project.outFile);
9287
continue;
9388
}
9489

9590
try
9691
{
97-
const exp = require(scriptFilePath);
92+
const exp = require(project.outFile);
9893

9994
if (exp && typeof exp === "object" && !Array.isArray(exp))
100-
exports.push(exp);
95+
out.push({ project, export: exp });
10196
}
10297
catch (e)
10398
{
10499
console.log(e);
105100
}
106101
}
107102

108-
return exports;
103+
return out;
109104
}
110105

111106
/**
@@ -114,51 +109,50 @@ namespace Moduless
114109
*/
115110
async function runCovers(target: IRunMeta)
116111
{
117-
const dependencies = tryLoadCoversFromDependencies(target.projectPath);
118-
const exports = [...dependencies, globalThis];
112+
const ns = target.functionNamespace.join(".");
113+
const graph = getGraphFromDependencies(target.projectPath);
114+
const startingProject = graph.at(-1);
115+
116+
if (!startingProject)
117+
throw new Error("No projects found at location: " + target.projectPath);
119118

120-
const resolvedNamespace = (() =>
119+
const tryResolveNamepace = (root: object) =>
121120
{
122-
nextExport: for (const exp of exports)
121+
let current: any = root;
122+
123+
for (const identifier of target.functionNamespace)
123124
{
124-
let current: any = exp;
125+
if (!(identifier in current))
126+
return null;
125127

126-
for (const identifier of target.functionNamespace)
127-
{
128-
if (!(identifier in current))
129-
continue nextExport;
130-
131-
current = current[identifier];
132-
}
133-
134-
if (!current || typeof current !== "object")
135-
continue nextExport;
136-
137-
return current as Record<string, any>;
128+
current = current[identifier];
138129
}
139130

140-
const ns = target.functionNamespace.join(".");
141-
throw new Error(
142-
`Could not resolve: ${ns}\nNot found or not an object.`);
143-
})();
131+
return current;
132+
};
133+
134+
const namespaceObject =
135+
tryResolveNamepace(startingProject.export) ||
136+
globalThis;
144137

145138
const covers = (() =>
146139
{
147140
const out: [string, CoverFn][] = [];
148141

149142
if (target.functionName)
150143
{
151-
const fn = resolvedNamespace[target.functionName];
144+
const fn = namespaceObject[target.functionName];
145+
152146
if (typeof fn !== "function")
153-
throw new Error(target.functionName + " is not a function.");
147+
throw new Error(`${ns}.${target.functionName} is not a function.`);
154148

155149
out.push([target.functionName, fn]);
156150
}
157151
else if (target.prefix)
158-
for (const [functionName, maybeFunction] of Object.entries(resolvedNamespace))
152+
for (const [functionName, maybeFunction] of Object.entries(namespaceObject))
159153
if (typeof maybeFunction === "function")
160154
if (functionName.startsWith(target.functionName))
161-
out.push([functionName, maybeFunction]);
155+
out.push([functionName, maybeFunction as any]);
162156

163157
return out;
164158
})();

0 commit comments

Comments
 (0)