-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfunctions.js
96 lines (85 loc) · 2.65 KB
/
functions.js
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
/**
* Match script to the list of available scripts, or check for aliases
*
* The order for checking matches should be direct-platform and then aliases
*
* @param {string} script - name of the script to be matched paired to the platform or alias
* @param {string} platform - name of the platform to be paired with the script
* @param {array} scripts - list of available scripts defined in package.json
*/
exports.matchScript = function matchScript(script, platform, scripts) {
/**
* Save the result so we can determine if there was a match
* First check for a basic match before we have to go through each script with a regex
*/
let result = (`${script}:${platform}` in scripts) ? `${script}:${platform}` : false;
if (result) return result;
/**
* Regular expresion match
* it helps when the "in" operator can't determine if there's a real match or not,
* due to the properties changing
*/
let regex = new RegExp(`^(${script}):([a-zA-Z0-9-]*:)*(${platform})(:[a-zA-Z0-9-]*)*$`, "g");
for (let command in scripts) {
if (command.match(regex)) return command;
}
/**
* Alias match, allows for a more verbose description of the platform
* it also helps to group similar platforms on a single execution
*/
switch (platform) {
case 'win32':
result = (`${script}:windows` in scripts) ? `${script}:windows` : false;
break;
case 'aix':
case 'linux':
case 'sunos':
case 'openbsd':
case 'freebsd':
case 'android':
result = (`${script}:nix` in scripts) ? `${script}:nix` : false;
break;
case 'darwin':
case 'macos':
/**
* macOS specific scripts (e.g. brew)
*/
result = (`${script}:macos` in scripts) ? `${script}:macos` : false;
/**
* nix compatible scripts (cp, rm...)
*/
if (!result) result = (`${script}:nix` in scripts) ? `${script}:nix` : false;
break;
default: result = false;
}
/**
* Successful finding of a given script by platform, present it.
*/
if (result) return result;
/**
* Fall to default if it's given, otherwise fail
*/
return (`${script}:default` in scripts) ? `${script}:default` : false;
};
/**
* Expand the shorthand description for npm commands
*
* i.e. npm i -> npm install
*
* @param String shorthand Shorthand command to be expanded
* @return String Actual command
*/
exports.expandShorthand = function expandShorthand(shorthand) {
switch(shorthand) {
case 'i':
return 'install';
case 't':
case 'tst':
return 'test';
/**
* Expansion is not possible
* @type {[type]}
*/
default: return shorthand;
}
}