Projekt

Obecné

Profil

Stáhnout (2.43 KB) Statistiky
| Větev: | Revize:
1
/*
2
	MIT License http://www.opensource.org/licenses/mit-license.php
3
	Author Tobias Koppers @sokra
4
*/
5
"use strict";
6

    
7
const forEachBail = require("./forEachBail");
8

    
9
function loadDescriptionFile(resolver, directory, filenames, resolveContext, callback) {
10
	(function findDescriptionFile() {
11
		forEachBail(filenames, (filename, callback) => {
12
			const descriptionFilePath = resolver.join(directory, filename);
13
			if(resolver.fileSystem.readJson) {
14
				resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
15
					if(err) {
16
						if(typeof err.code !== "undefined") return callback();
17
						return onJson(err);
18
					}
19
					onJson(null, content);
20
				});
21
			} else {
22
				resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {
23
					if(err) return callback();
24
					let json;
25
					try {
26
						json = JSON.parse(content);
27
					} catch(e) {
28
						onJson(e);
29
					}
30
					onJson(null, json);
31
				});
32
			}
33

    
34
			function onJson(err, content) {
35
				if(err) {
36
					if(resolveContext.log)
37
						resolveContext.log(descriptionFilePath + " (directory description file): " + err);
38
					else
39
						err.message = descriptionFilePath + " (directory description file): " + err;
40
					return callback(err);
41
				}
42
				callback(null, {
43
					content: content,
44
					directory: directory,
45
					path: descriptionFilePath
46
				});
47
			}
48
		}, (err, result) => {
49
			if(err) return callback(err);
50
			if(result) {
51
				return callback(null, result);
52
			} else {
53
				directory = cdUp(directory);
54
				if(!directory) {
55
					return callback();
56
				} else {
57
					return findDescriptionFile();
58
				}
59
			}
60
		});
61
	}());
62
}
63

    
64
function getField(content, field) {
65
	if(!content) return undefined;
66
	if(Array.isArray(field)) {
67
		let current = content;
68
		for(let j = 0; j < field.length; j++) {
69
			if(current === null || typeof current !== "object") {
70
				current = null;
71
				break;
72
			}
73
			current = current[field[j]];
74
		}
75
		if(typeof current === "object") {
76
			return current;
77
		}
78
	} else {
79
		if(typeof content[field] === "object") {
80
			return content[field];
81
		}
82
	}
83
}
84

    
85
function cdUp(directory) {
86
	if(directory === "/") return null;
87
	const i = directory.lastIndexOf("/"),
88
		j = directory.lastIndexOf("\\");
89
	const p = i < 0 ? j : j < 0 ? i : i < j ? j : i;
90
	if(p < 0) return null;
91
	return directory.substr(0, p || 1);
92
}
93

    
94
exports.loadDescriptionFile = loadDescriptionFile;
95
exports.getField = getField;
96
exports.cdUp = cdUp;
(8-8/37)