wallet-core/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-05-03 15:35:00 +02:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-09-20 02:56:13 +02:00
"use strict";
2017-05-03 15:35:00 +02:00
2018-09-20 02:56:13 +02:00
const forEachBail = require("./forEachBail");
function loadDescriptionFile(resolver, directory, filenames, resolveContext, callback) {
2017-05-03 15:35:00 +02:00
(function findDescriptionFile() {
2018-09-20 02:56:13 +02:00
forEachBail(filenames, (filename, callback) => {
const descriptionFilePath = resolver.join(directory, filename);
2017-05-03 15:35:00 +02:00
if(resolver.fileSystem.readJson) {
2018-09-20 02:56:13 +02:00
resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
2017-05-03 15:35:00 +02:00
if(err) {
if(typeof err.code !== "undefined") return callback();
return onJson(err);
}
onJson(null, content);
});
} else {
2018-09-20 02:56:13 +02:00
resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {
2017-05-03 15:35:00 +02:00
if(err) return callback();
2018-09-20 02:56:13 +02:00
let json;
2017-05-03 15:35:00 +02:00
try {
2018-09-20 02:56:13 +02:00
json = JSON.parse(content);
2017-05-03 15:35:00 +02:00
} catch(e) {
onJson(e);
}
onJson(null, json);
});
}
function onJson(err, content) {
if(err) {
2018-09-20 02:56:13 +02:00
if(resolveContext.log)
resolveContext.log(descriptionFilePath + " (directory description file): " + err);
2017-05-03 15:35:00 +02:00
else
err.message = descriptionFilePath + " (directory description file): " + err;
return callback(err);
}
callback(null, {
content: content,
directory: directory,
path: descriptionFilePath
});
}
2018-09-20 02:56:13 +02:00
}, (err, result) => {
2017-05-03 15:35:00 +02:00
if(err) return callback(err);
if(result) {
return callback(null, result);
} else {
directory = cdUp(directory);
if(!directory) {
return callback();
} else {
return findDescriptionFile();
}
}
});
}());
}
function getField(content, field) {
if(!content) return undefined;
if(Array.isArray(field)) {
2018-09-20 02:56:13 +02:00
let current = content;
for(let j = 0; j < field.length; j++) {
2017-05-03 15:35:00 +02:00
if(current === null || typeof current !== "object") {
current = null;
break;
}
current = current[field[j]];
}
if(typeof current === "object") {
return current;
}
} else {
if(typeof content[field] === "object") {
return content[field];
}
}
}
function cdUp(directory) {
if(directory === "/") return null;
2018-09-20 02:56:13 +02:00
const i = directory.lastIndexOf("/"),
2017-05-03 15:35:00 +02:00
j = directory.lastIndexOf("\\");
2018-09-20 02:56:13 +02:00
const p = i < 0 ? j : j < 0 ? i : i < j ? j : i;
2017-05-03 15:35:00 +02:00
if(p < 0) return null;
return directory.substr(0, p || 1);
}
exports.loadDescriptionFile = loadDescriptionFile;
exports.getField = getField;
exports.cdUp = cdUp;