[79a0317] | 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").JsonObject} JsonObject */
|
---|
| 12 | /** @typedef {import("./Resolver").JsonValue} JsonValue */
|
---|
| 13 | /** @typedef {import("./Resolver").ResolveContext} ResolveContext */
|
---|
| 14 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @typedef {Object} DescriptionFileInfo
|
---|
| 18 | * @property {JsonObject=} content
|
---|
| 19 | * @property {string} path
|
---|
| 20 | * @property {string} directory
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @callback ErrorFirstCallback
|
---|
| 25 | * @param {Error|null=} error
|
---|
| 26 | * @param {DescriptionFileInfo=} result
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @typedef {Object} Result
|
---|
| 31 | * @property {string} path path to description file
|
---|
| 32 | * @property {string} directory directory of description file
|
---|
| 33 | * @property {JsonObject} content content of description file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * @param {Resolver} resolver resolver
|
---|
| 38 | * @param {string} directory directory
|
---|
| 39 | * @param {string[]} filenames filenames
|
---|
| 40 | * @param {DescriptionFileInfo|undefined} oldInfo oldInfo
|
---|
| 41 | * @param {ResolveContext} resolveContext resolveContext
|
---|
| 42 | * @param {ErrorFirstCallback} callback callback
|
---|
| 43 | */
|
---|
| 44 | function loadDescriptionFile(
|
---|
| 45 | resolver,
|
---|
| 46 | directory,
|
---|
| 47 | filenames,
|
---|
| 48 | oldInfo,
|
---|
| 49 | resolveContext,
|
---|
| 50 | callback
|
---|
| 51 | ) {
|
---|
| 52 | (function findDescriptionFile() {
|
---|
| 53 | if (oldInfo && oldInfo.directory === directory) {
|
---|
| 54 | // We already have info for this directory and can reuse it
|
---|
| 55 | return callback(null, oldInfo);
|
---|
| 56 | }
|
---|
| 57 | forEachBail(
|
---|
| 58 | filenames,
|
---|
| 59 | /**
|
---|
| 60 | * @param {string} filename filename
|
---|
| 61 | * @param {(err?: null|Error, result?: null|Result) => void} callback callback
|
---|
| 62 | * @returns {void}
|
---|
| 63 | */
|
---|
| 64 | (filename, callback) => {
|
---|
| 65 | const descriptionFilePath = resolver.join(directory, filename);
|
---|
| 66 | if (resolver.fileSystem.readJson) {
|
---|
| 67 | resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
|
---|
| 68 | if (err) {
|
---|
| 69 | if (
|
---|
| 70 | typeof (/** @type {NodeJS.ErrnoException} */ (err).code) !==
|
---|
| 71 | "undefined"
|
---|
| 72 | ) {
|
---|
| 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 | return onJson(err);
|
---|
| 82 | }
|
---|
| 83 | if (resolveContext.fileDependencies) {
|
---|
| 84 | resolveContext.fileDependencies.add(descriptionFilePath);
|
---|
| 85 | }
|
---|
| 86 | onJson(null, content);
|
---|
| 87 | });
|
---|
| 88 | } else {
|
---|
| 89 | resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {
|
---|
| 90 | if (err) {
|
---|
| 91 | if (resolveContext.missingDependencies) {
|
---|
| 92 | resolveContext.missingDependencies.add(descriptionFilePath);
|
---|
| 93 | }
|
---|
| 94 | return callback();
|
---|
| 95 | }
|
---|
| 96 | if (resolveContext.fileDependencies) {
|
---|
| 97 | resolveContext.fileDependencies.add(descriptionFilePath);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /** @type {JsonObject | undefined} */
|
---|
| 101 | let json;
|
---|
| 102 |
|
---|
| 103 | if (content) {
|
---|
| 104 | try {
|
---|
| 105 | json = JSON.parse(content.toString());
|
---|
| 106 | } catch (/** @type {unknown} */ e) {
|
---|
| 107 | return onJson(/** @type {Error} */ (e));
|
---|
| 108 | }
|
---|
| 109 | } else {
|
---|
| 110 | return onJson(new Error("No content in file"));
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | onJson(null, json);
|
---|
| 114 | });
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * @param {null|Error} [err] error
|
---|
| 119 | * @param {JsonObject} [content] content
|
---|
| 120 | * @returns {void}
|
---|
| 121 | */
|
---|
| 122 | function onJson(err, content) {
|
---|
| 123 | if (err) {
|
---|
| 124 | if (resolveContext.log)
|
---|
| 125 | resolveContext.log(
|
---|
| 126 | descriptionFilePath + " (directory description file): " + err
|
---|
| 127 | );
|
---|
| 128 | else
|
---|
| 129 | err.message =
|
---|
| 130 | descriptionFilePath + " (directory description file): " + err;
|
---|
| 131 | return callback(err);
|
---|
| 132 | }
|
---|
| 133 | callback(null, {
|
---|
| 134 | content: /** @type {JsonObject} */ (content),
|
---|
| 135 | directory,
|
---|
| 136 | path: descriptionFilePath
|
---|
| 137 | });
|
---|
| 138 | }
|
---|
| 139 | },
|
---|
| 140 | /**
|
---|
| 141 | * @param {null|Error} [err] error
|
---|
| 142 | * @param {null|Result} [result] result
|
---|
| 143 | * @returns {void}
|
---|
| 144 | */
|
---|
| 145 | (err, result) => {
|
---|
| 146 | if (err) return callback(err);
|
---|
| 147 | if (result) {
|
---|
| 148 | return callback(null, result);
|
---|
| 149 | } else {
|
---|
| 150 | const dir = cdUp(directory);
|
---|
| 151 | if (!dir) {
|
---|
| 152 | return callback();
|
---|
| 153 | } else {
|
---|
| 154 | directory = dir;
|
---|
| 155 | return findDescriptionFile();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | );
|
---|
| 160 | })();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * @param {JsonObject} content content
|
---|
| 165 | * @param {string|string[]} field field
|
---|
| 166 | * @returns {JsonValue | undefined} field data
|
---|
| 167 | */
|
---|
| 168 | function getField(content, field) {
|
---|
| 169 | if (!content) return undefined;
|
---|
| 170 | if (Array.isArray(field)) {
|
---|
| 171 | /** @type {JsonValue} */
|
---|
| 172 | let current = content;
|
---|
| 173 | for (let j = 0; j < field.length; j++) {
|
---|
| 174 | if (current === null || typeof current !== "object") {
|
---|
| 175 | current = null;
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 | current = /** @type {JsonObject} */ (current)[field[j]];
|
---|
| 179 | }
|
---|
| 180 | return current;
|
---|
| 181 | } else {
|
---|
| 182 | return content[field];
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * @param {string} directory directory
|
---|
| 188 | * @returns {string|null} parent directory or null
|
---|
| 189 | */
|
---|
| 190 | function cdUp(directory) {
|
---|
| 191 | if (directory === "/") return null;
|
---|
| 192 | const i = directory.lastIndexOf("/"),
|
---|
| 193 | j = directory.lastIndexOf("\\");
|
---|
| 194 | const p = i < 0 ? j : j < 0 ? i : i < j ? j : i;
|
---|
| 195 | if (p < 0) return null;
|
---|
| 196 | return directory.slice(0, p || 1);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | exports.loadDescriptionFile = loadDescriptionFile;
|
---|
| 200 | exports.getField = getField;
|
---|
| 201 | exports.cdUp = cdUp;
|
---|