findPath method
Implementation
String? findPath(String basename, String module) {
if (isEmpty) return null;
String filepath;
if (module[0] == "/") {
filepath = module;
} else {
filepath = path.join(basename, '..', module);
}
if (filepath[0] != "/") filepath = "/" + filepath;
filepath = path.normalize(filepath);
String ext = path.extension(filepath);
if (ext.isEmpty) {
for (var fileSystem in this) {
if (fileSystem.mount != null) {
String mountPath = "${fileSystem.mount!}/";
if (filepath.startsWith(mountPath)) {
filepath = filepath.replaceFirst(fileSystem.mount!, "");
} else {
continue;
}
}
String? testFile(String filepath) {
String newPath = "$filepath.js";
if (fileSystem.exist(newPath)) return newPath;
newPath = "$filepath.json";
if (fileSystem.exist(newPath)) return newPath;
newPath = "$filepath/package.json";
if (fileSystem.exist(newPath)) {
var code = fileSystem.read(newPath);
var json = jsonDecode(code!);
if (json is Map) {
var main = json["main"];
if (main != null) {
return "$filepath/$main";
}
}
}
return null;
}
String? newPath = testFile(filepath);
if (newPath != null)
return newPath;
String basename = path.dirname(filepath);
while (true) {
if (basename.isEmpty) break;
String modulePath = path.join(basename, 'node_modules', module);
var newPath = testFile(modulePath);
if (newPath != null) return newPath;
basename = path.dirname(basename);
}
print("$module is not found!");
}
} else {
for (var fileSystem in this) {
if (fileSystem.exist(filepath)) return filepath;
}
}
}