-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
112 lines (106 loc) · 3.13 KB
/
helpers.ts
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
import {attributes, variable} from 'fig';
/**
* @file
*
* Project-local helpers.
*/
/**
* Returns `true` if `conditions` apply.
*
* Note that although it is possible to specify multiple conditions with AND
* and OR semantics (as described in `when()` below), the `is()` function is
* probably best reserved for simple (non-compound) cases only; eg:
*
* if (is('darwin')) {
* // Do something on Darwin only...
* }
*
* because using it for compound conditionals:
*
* if (is(['codespaces', 'work'])) {
* // ...
* }
*
* is likely to be _less_ readable than spelling out the underlying expressions
* with explicit `||` and `&&` operators; eg:
*
* if (profile === 'codespaces' || profile === 'work') {
* // ...
* }
*
*/
export function is(...conditions: Array<Array<string> | string>): boolean {
return when(...conditions)() === true;
}
/**
* Returns a function that will return `true` if `conditions` apply, or a string
* explaining why they do not apply. Mainly intended as a convenience for
* defining conditional tasks:
*
* task('do this thing', when('darwin'), async () => {
* // Only on Darwin... When not on Darwin, task will be skipped
* // with the message "unsatisfied condition: (darwin)".
* });
*
* `conditions` is an array, and its entries must be either strings or nested
* arrays of strings. Top-level conditions must be true using AND semantics.
* Nested conditions employ OR semantics.
*
* That is, if `conditions` is `[['arch', 'debian'], 'wincent']`, the semantics
* are equivalent to "(arch OR debian) AND (wincent)", which is incidentally
* also in the string that is returned if the conditions are not met:
*
* unsatisfied condition: (arch OR debian) AND (wincent)
*/
export function when(
...conditions: Array<Array<string> | string>
): () => true | string {
return () => {
if (
conditions.every((condition) =>
Array.isArray(condition)
? condition.some(checkCondition)
: checkCondition(condition)
)
) {
return true;
}
const description = conditions
.map((condition) => {
return `(${
Array.isArray(condition) ? condition.join(' OR ') : condition
})`;
})
.join(' AND ');
return `unsatisfied condition: ${description}`;
};
}
/**
* Provides a uniform interface for checking conditionals identified by a label.
*
* @internal
*/
function checkCondition(label: string): boolean {
switch (label) {
case 'arch':
return attributes.distribution === 'arch';
case 'arm64':
return attributes.arch === 'arm64';
case 'darwin':
return attributes.platform === 'darwin';
case 'debian':
return attributes.distribution === 'debian';
case 'linux':
return attributes.platform === 'linux';
case 'personal':
return variable('profile') === 'personal';
case 'work':
return variable('profile') === 'work';
case 'therealcisse':
return variable('identity') === 'therealcisse';
default:
throw new Error(
`checkCondition(): Unknown condition label ${JSON.stringify(label)}`
);
}
}