utils.js
2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { join, dirname, readJson } = require("../util/fs");
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
/**
* @param {string} str maybe required version
* @returns {boolean} true, if it looks like a version
*/
exports.isRequiredVersion = str => {
return /^([\d^=v<>~]|[*xX]$)/.test(str);
};
/**
*
* @param {InputFileSystem} fs file system
* @param {string} directory directory to start looking into
* @param {string[]} descriptionFiles possible description filenames
* @param {function((Error | null)=, {data: object, path: string}=): void} callback callback
*/
const getDescriptionFile = (fs, directory, descriptionFiles, callback) => {
let i = 0;
const tryLoadCurrent = () => {
if (i >= descriptionFiles.length) {
const parentDirectory = dirname(fs, directory);
if (!parentDirectory || parentDirectory === directory) return callback();
return getDescriptionFile(
fs,
parentDirectory,
descriptionFiles,
callback
);
}
const filePath = join(fs, directory, descriptionFiles[i]);
readJson(fs, filePath, (err, data) => {
if (err) {
if ("code" in err && err.code === "ENOENT") {
i++;
return tryLoadCurrent();
}
return callback(err);
}
if (!data || typeof data !== "object" || Array.isArray(data)) {
return callback(
new Error(`Description file ${filePath} is not an object`)
);
}
callback(null, { data, path: filePath });
});
};
tryLoadCurrent();
};
exports.getDescriptionFile = getDescriptionFile;
exports.getRequiredVersionFromDescriptionFile = (data, packageName) => {
if (
data.optionalDependencies &&
typeof data.optionalDependencies === "object" &&
packageName in data.optionalDependencies
) {
return data.optionalDependencies[packageName];
}
if (
data.dependencies &&
typeof data.dependencies === "object" &&
packageName in data.dependencies
) {
return data.dependencies[packageName];
}
if (
data.peerDependencies &&
typeof data.peerDependencies === "object" &&
packageName in data.peerDependencies
) {
return data.peerDependencies[packageName];
}
if (
data.devDependencies &&
typeof data.devDependencies === "object" &&
packageName in data.devDependencies
) {
return data.devDependencies[packageName];
}
};