1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { join, dirname, readJson } = require("../util/fs");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | exports.isRequiredVersion = str => {
|
17 | return /^([\d^=v<>~]|[*xX]$)/.test(str);
|
18 | };
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const getDescriptionFile = (fs, directory, descriptionFiles, callback) => {
|
28 | let i = 0;
|
29 | const tryLoadCurrent = () => {
|
30 | if (i >= descriptionFiles.length) {
|
31 | const parentDirectory = dirname(fs, directory);
|
32 | if (!parentDirectory || parentDirectory === directory) return callback();
|
33 | return getDescriptionFile(
|
34 | fs,
|
35 | parentDirectory,
|
36 | descriptionFiles,
|
37 | callback
|
38 | );
|
39 | }
|
40 | const filePath = join(fs, directory, descriptionFiles[i]);
|
41 | readJson(fs, filePath, (err, data) => {
|
42 | if (err) {
|
43 | if ("code" in err && err.code === "ENOENT") {
|
44 | i++;
|
45 | return tryLoadCurrent();
|
46 | }
|
47 | return callback(err);
|
48 | }
|
49 | if (!data || typeof data !== "object" || Array.isArray(data)) {
|
50 | return callback(
|
51 | new Error(`Description file ${filePath} is not an object`)
|
52 | );
|
53 | }
|
54 | callback(null, { data, path: filePath });
|
55 | });
|
56 | };
|
57 | tryLoadCurrent();
|
58 | };
|
59 | exports.getDescriptionFile = getDescriptionFile;
|
60 |
|
61 | exports.getRequiredVersionFromDescriptionFile = (data, packageName) => {
|
62 | if (
|
63 | data.optionalDependencies &&
|
64 | typeof data.optionalDependencies === "object" &&
|
65 | packageName in data.optionalDependencies
|
66 | ) {
|
67 | return data.optionalDependencies[packageName];
|
68 | }
|
69 | if (
|
70 | data.dependencies &&
|
71 | typeof data.dependencies === "object" &&
|
72 | packageName in data.dependencies
|
73 | ) {
|
74 | return data.dependencies[packageName];
|
75 | }
|
76 | if (
|
77 | data.peerDependencies &&
|
78 | typeof data.peerDependencies === "object" &&
|
79 | packageName in data.peerDependencies
|
80 | ) {
|
81 | return data.peerDependencies[packageName];
|
82 | }
|
83 | if (
|
84 | data.devDependencies &&
|
85 | typeof data.devDependencies === "object" &&
|
86 | packageName in data.devDependencies
|
87 | ) {
|
88 | return data.devDependencies[packageName];
|
89 | }
|
90 | };
|