forked from wincent/wincent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
123 lines (115 loc) · 3.42 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
113
114
115
116
117
118
119
120
121
122
123
import {attributes, path, run, 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 'codespaces':
return variable('hostHandle') === 'codespaces';
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 'wincent':
return variable('identity') === 'wincent';
default:
throw new Error(
`checkCondition(): Unknown condition label ${JSON.stringify(label)}`
);
}
}
export async function isDecrypted(pathish: string): Promise<boolean> {
const result = await run('bin/git-cipher', [
'is-encrypted',
'--exit-code',
path(pathish).expand,
]);
// 0 = encrypted, 1 = decrypted, anything else = error.
return result.status === 1;
}