-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpackage.js
More file actions
91 lines (79 loc) · 2.25 KB
/
Copy pathpackage.js
File metadata and controls
91 lines (79 loc) · 2.25 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
91
/**
* External dependencies
*/
const { realpathSync } = require('fs');
const path = require('path');
const readPkgUp = require('read-pkg-up');
const readPkg = require('read-pkg');
/**
* Internal dependencies
*/
const { getCurrentWorkingDirectory } = require('./process');
let pkg;
let pkgPath;
const packageData = readPkgUp.sync({
cwd: realpathSync(getCurrentWorkingDirectory()),
});
if (packageData && packageData.packageJson && packageData.path) {
pkg = packageData.packageJson;
pkgPath = packageData.path;
}
const getPackage = () => pkg;
const getPackagePath = () => pkgPath;
const hasPackageProp = (prop) => pkg && Object.prototype.hasOwnProperty.call(pkg, prop);
/**
* Returns the 10up scripts version directly from package.json
*
* @returns {number}
*/
const getPackageVersion = async () => {
const pkg = await readPkg({ cwd: path.dirname(__dirname) });
return pkg.version;
};
/**
* Checks whether the passed package name is installed in the project.
*
* @param {string} packageName The name of npm package.
* @returns {boolean} Returns true when the package is installed or false otherwise.
*/
const isPackageInstalled = (packageName) => {
try {
if (require.resolve(packageName)) {
return true;
}
} catch (error) {
// do nothing
}
return false;
};
/**
* Compares two semver-like version strings (e.g. "5.2.2", "1.0.0-beta.1").
* Only the numeric segments are compared; non-numeric segments are treated as 0.
*
* @param {string} actual - The resolved version (e.g. from a package).
* @param {string} min - The minimum required version.
* @returns {boolean} True if actual >= min, false otherwise.
*/
const isMinimumPackageVersion = (actual, min) => {
const actualVersions = actual.split('.').map(Number);
const minimumVersions = min.split('.').map(Number);
for (let i = 0; i < Math.max(actualVersions.length, minimumVersions.length); i++) {
const actualVersionLevel = actualVersions[i] || 0;
const minimumVersionLevel = minimumVersions[i] || 0;
if (actualVersionLevel > minimumVersionLevel) {
return true;
}
if (actualVersionLevel < minimumVersionLevel) {
return false;
}
}
return true;
};
module.exports = {
isMinimumPackageVersion,
isPackageInstalled,
getPackagePath,
hasPackageProp,
getPackage,
getPackageVersion,
};