Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / webpack / lib / sharing / utils.js
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 { join, dirname, readJson } = require("../util/fs");
9
10 /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
11
12 /**
13  * @param {string} str maybe required version
14  * @returns {boolean} true, if it looks like a version
15  */
16 exports.isRequiredVersion = str => {
17         return /^([\d^=v<>~]|[*xX]$)/.test(str);
18 };
19
20 /**
21  *
22  * @param {InputFileSystem} fs file system
23  * @param {string} directory directory to start looking into
24  * @param {string[]} descriptionFiles possible description filenames
25  * @param {function((Error | null)=, {data: object, path: string}=): void} callback callback
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 };