-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
149 lines (141 loc) · 2.94 KB
/
Copy pathutils.js
File metadata and controls
149 lines (141 loc) · 2.94 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import fs from 'node:fs';
import path from 'node:path';
import { echo, startColor, stopColor } from './ansiUtils';
import { curDir, patchDir } from './variables';
/**
*
* @param version {String}
* @returns {string}
*/
export function removeBuildMetadataFromVersion(version)
{
const plusPos = version.indexOf('+');
if (plusPos === -1)
{
return version;
}
return version.substring(0, plusPos);
}
/**
* Removes the author's prefix from the package name (if any)
* @param name {String}
* @returns {string}
*/
export function getScopelessName(name)
{
if (name[0] !== '@')
{
return name;
}
return name.split('/')[1];
}
/**
* Checks if the folder "patches" inside the project exists. If not - throws an error
* @returns {boolean}
*/
export function hasPatches()
{
if (!fs.existsSync(patchDir))
{
echo(
startColor('yellowBright'),
'WARNING: ',
stopColor(),
'Missing ',
startColor('whiteBright'),
'patches',
stopColor(),
' folder - nothing to do'
);
process.exit(2);
}
return true;
}
/**
* returns FALSE on error, Package.JSON of the requested package on success
* @param pkgName {String}
* @returns {Object|boolean}
*/
export function getConfig(pkgName)
{
const folder = path.join(curDir, 'node_modules', pkgName);
const cfgName = path.join(folder, 'package.json');
if(!fs.existsSync(folder))
{
echo(
startColor('redBright'),
'ERROR: ',
stopColor(),
'Missing folder "',
startColor('whiteBright'),
'./node_modules/',
stopColor(),
startColor('greenBright'),
pkgName,
stopColor(),
'"'
);
return false;
}
try
{
fs.accessSync(cfgName, fs.constants.R_OK);
}
catch (e)
{
echo(
startColor('redBright'),
'ERROR: ',
stopColor(),
'Can not read ',
startColor('whiteBright'),
'"package.json"',
stopColor(),
' for ',
startColor('greenBright'),
pkgName,
stopColor(),
);
return false;
}
const pkgConfig = fs.readFileSync(cfgName,'utf8');
let cfg = {};
try
{
cfg = JSON.parse(pkgConfig);
}
catch(e)
{
echo(
startColor('redBright'),
'ERROR: ',
stopColor(),
'Could not parse ',
startColor('whiteBright'),
'"package.json"',
stopColor(),
' - ',
startColor('redBright'),
e.message,
stopColor()
);
return false;
}
return cfg;
}
/**
* return FALSE if packageSemVer is lower than patchSemVer
* @param patchSemVer {String}
* @param packageSemVer {String}
* @returns {boolean}
*/
export function isVersionSuitable(patchSemVer, packageSemVer)
{
const oldVer = patchSemVer.split('.');
const newVer = packageSemVer.split('.');
if (+oldVer[0] < +newVer[0]) return true;
if (+oldVer[0] > +newVer[0]) return false;
if (+oldVer[1] < +newVer[1]) return true;
if (+oldVer[1] > +newVer[1]) return false;
return +oldVer[2] <= +newVer[2];
}