[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const forEachBail = require("./forEachBail");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 11 | /** @typedef {import("./Resolver").ResolveContext} ResolveContext */
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * @typedef {Object} DescriptionFileInfo
|
---|
| 15 | * @property {any=} content
|
---|
| 16 | * @property {string} path
|
---|
| 17 | * @property {string} directory
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * @callback ErrorFirstCallback
|
---|
| 22 | * @param {Error|null=} error
|
---|
| 23 | * @param {DescriptionFileInfo=} result
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * @param {Resolver} resolver resolver
|
---|
| 28 | * @param {string} directory directory
|
---|
| 29 | * @param {string[]} filenames filenames
|
---|
| 30 | * @param {DescriptionFileInfo|undefined} oldInfo oldInfo
|
---|
| 31 | * @param {ResolveContext} resolveContext resolveContext
|
---|
| 32 | * @param {ErrorFirstCallback} callback callback
|
---|
| 33 | */
|
---|
| 34 | function loadDescriptionFile(
|
---|
| 35 | resolver,
|
---|
| 36 | directory,
|
---|
| 37 | filenames,
|
---|
| 38 | oldInfo,
|
---|
| 39 | resolveContext,
|
---|
| 40 | callback
|
---|
| 41 | ) {
|
---|
| 42 | (function findDescriptionFile() {
|
---|
| 43 | if (oldInfo && oldInfo.directory === directory) {
|
---|
| 44 | // We already have info for this directory and can reuse it
|
---|
| 45 | return callback(null, oldInfo);
|
---|
| 46 | }
|
---|
| 47 | forEachBail(
|
---|
| 48 | filenames,
|
---|
| 49 | (filename, callback) => {
|
---|
| 50 | const descriptionFilePath = resolver.join(directory, filename);
|
---|
| 51 | if (resolver.fileSystem.readJson) {
|
---|
| 52 | resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
|
---|
| 53 | if (err) {
|
---|
| 54 | if (typeof err.code !== "undefined") {
|
---|
| 55 | if (resolveContext.missingDependencies) {
|
---|
| 56 | resolveContext.missingDependencies.add(descriptionFilePath);
|
---|
| 57 | }
|
---|
| 58 | return callback();
|
---|
| 59 | }
|
---|
| 60 | if (resolveContext.fileDependencies) {
|
---|
| 61 | resolveContext.fileDependencies.add(descriptionFilePath);
|
---|
| 62 | }
|
---|
| 63 | return onJson(err);
|
---|
| 64 | }
|
---|
| 65 | if (resolveContext.fileDependencies) {
|
---|
| 66 | resolveContext.fileDependencies.add(descriptionFilePath);
|
---|
| 67 | }
|
---|
| 68 | onJson(null, content);
|
---|
| 69 | });
|
---|
| 70 | } else {
|
---|
| 71 | resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {
|
---|
| 72 | if (err) {
|
---|
| 73 | if (resolveContext.missingDependencies) {
|
---|
| 74 | resolveContext.missingDependencies.add(descriptionFilePath);
|
---|
| 75 | }
|
---|
| 76 | return callback();
|
---|
| 77 | }
|
---|
| 78 | if (resolveContext.fileDependencies) {
|
---|
| 79 | resolveContext.fileDependencies.add(descriptionFilePath);
|
---|
| 80 | }
|
---|
| 81 | let json;
|
---|
| 82 |
|
---|
| 83 | if (content) {
|
---|
| 84 | try {
|
---|
| 85 | json = JSON.parse(content.toString());
|
---|
| 86 | } catch (e) {
|
---|
| 87 | return onJson(e);
|
---|
| 88 | }
|
---|
| 89 | } else {
|
---|
| 90 | return onJson(new Error("No content in file"));
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | onJson(null, json);
|
---|
| 94 | });
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | function onJson(err, content) {
|
---|
| 98 | if (err) {
|
---|
| 99 | if (resolveContext.log)
|
---|
| 100 | resolveContext.log(
|
---|
| 101 | descriptionFilePath + " (directory description file): " + err
|
---|
| 102 | );
|
---|
| 103 | else
|
---|
| 104 | err.message =
|
---|
| 105 | descriptionFilePath + " (directory description file): " + err;
|
---|
| 106 | return callback(err);
|
---|
| 107 | }
|
---|
| 108 | callback(null, {
|
---|
| 109 | content,
|
---|
| 110 | directory,
|
---|
| 111 | path: descriptionFilePath
|
---|
| 112 | });
|
---|
| 113 | }
|
---|
| 114 | },
|
---|
| 115 | (err, result) => {
|
---|
| 116 | if (err) return callback(err);
|
---|
| 117 | if (result) {
|
---|
| 118 | return callback(null, result);
|
---|
| 119 | } else {
|
---|
| 120 | const dir = cdUp(directory);
|
---|
| 121 | if (!dir) {
|
---|
| 122 | return callback();
|
---|
| 123 | } else {
|
---|
| 124 | directory = dir;
|
---|
| 125 | return findDescriptionFile();
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | );
|
---|
| 130 | })();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @param {any} content content
|
---|
| 135 | * @param {string|string[]} field field
|
---|
| 136 | * @returns {object|string|number|boolean|undefined} field data
|
---|
| 137 | */
|
---|
| 138 | function getField(content, field) {
|
---|
| 139 | if (!content) return undefined;
|
---|
| 140 | if (Array.isArray(field)) {
|
---|
| 141 | let current = content;
|
---|
| 142 | for (let j = 0; j < field.length; j++) {
|
---|
| 143 | if (current === null || typeof current !== "object") {
|
---|
| 144 | current = null;
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | current = current[field[j]];
|
---|
| 148 | }
|
---|
| 149 | return current;
|
---|
| 150 | } else {
|
---|
| 151 | return content[field];
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * @param {string} directory directory
|
---|
| 157 | * @returns {string|null} parent directory or null
|
---|
| 158 | */
|
---|
| 159 | function cdUp(directory) {
|
---|
| 160 | if (directory === "/") return null;
|
---|
| 161 | const i = directory.lastIndexOf("/"),
|
---|
| 162 | j = directory.lastIndexOf("\\");
|
---|
| 163 | const p = i < 0 ? j : j < 0 ? i : i < j ? j : i;
|
---|
| 164 | if (p < 0) return null;
|
---|
| 165 | return directory.substr(0, p || 1);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | exports.loadDescriptionFile = loadDescriptionFile;
|
---|
| 169 | exports.getField = getField;
|
---|
| 170 | exports.cdUp = cdUp;
|
---|