MCPcopy Create free account
hub / github.com/charto/nbind / findCompiledModule

Function findCompiledModule

src/nbind.ts:153–209  ·  view source on GitHub ↗
(
	basePath: string,
	specList: FindModuleSpec[],
	callback: FindCallback
)

Source from the content-addressed store, hash-verified

151export type FindCallback = (err: any, result?: ModuleSpec) => any;
152
153function findCompiledModule<ResultType>(
154 basePath: string,
155 specList: FindModuleSpec[],
156 callback: FindCallback
157) {
158 const resolvedList: string[] = [];
159 const ext = path.extname(basePath);
160
161 /** If basePath has a known extension, check if it's a loadable module. */
162
163 for(let spec of specList) {
164 if(ext == spec.ext) {
165 try {
166 spec.path = require.resolve(basePath);
167
168 // Stop if a module was found.
169 callback(null, spec as ModuleSpec);
170 return(spec);
171 } catch(err) {
172 resolvedList.push(basePath);
173 }
174 }
175 }
176
177 /** Try all possible subdirectories of basePath. */
178
179 for(let spec of specList) {
180 // Check if any possible path contains a loadable module,
181 // and store unsuccessful attempts.
182
183 for(let pathParts of makeModulePathList(basePath, spec.name)) {
184 const resolvedPath = path.resolve.apply(path, pathParts);
185
186 try {
187 spec.path = require.resolve(resolvedPath);
188 } catch(err) {
189 resolvedList.push(resolvedPath);
190 continue;
191 }
192
193 // Stop if a module was found.
194
195 callback(null, spec as ModuleSpec);
196 return(spec);
197 }
198 }
199
200 const err = new Error(
201 'Could not locate the bindings file. Tried:\n' +
202 resolvedList.join('\n')
203 );
204
205 (err as any).tries = resolvedList;
206
207 callback(err);
208 return(null);
209}
210

Callers 1

findFunction · 0.85

Calls 2

makeModulePathListFunction · 0.85
resolveMethod · 0.80

Tested by

no test coverage detected