-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathcheckValidTargetFilters.ts
72 lines (66 loc) · 2.27 KB
/
checkValidTargetFilters.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
import { VALID_DEPLOY_TARGETS } from "./commands/deploy";
import { FirebaseError } from "./error";
import { Options } from "./options";
/** Returns targets from `only` only for the specified deploy types. */
function targetsForTypes(only: string[], ...types: string[]): string[] {
return only.filter((t) => {
if (t.includes(":")) {
return types.includes(t.split(":")[0]);
} else {
return types.includes(t);
}
});
}
/** Returns true if any target has a filter (:). */
function targetsHaveFilters(...targets: string[]): boolean {
return targets.some((t) => t.includes(":"));
}
/** Returns true if any target doesn't include a filter (:). */
function targetsHaveNoFilters(...targets: string[]): boolean {
return targets.some((t) => !t.includes(":"));
}
const FILTERABLE_TARGETS = new Set([
"hosting",
"functions",
"firestore",
"storage",
"database",
"dataconnect",
]);
/**
* Validates that the target filters in options.only are valid.
* Throws an error (rejects) if it is invalid.
*/
export async function checkValidTargetFilters(options: Options): Promise<void> {
const only = !options.only ? [] : options.only.split(",");
return new Promise<void>((resolve, reject) => {
if (!only.length) {
return resolve();
}
if (options.except) {
return reject(new FirebaseError("Cannot specify both --only and --except"));
}
const nonFilteredTypes = VALID_DEPLOY_TARGETS.filter((t) => !FILTERABLE_TARGETS.has(t));
const targetsForNonFilteredTypes = targetsForTypes(only, ...nonFilteredTypes);
if (targetsForNonFilteredTypes.length && targetsHaveFilters(...targetsForNonFilteredTypes)) {
return reject(
new FirebaseError(
"Filters specified with colons (e.g. --only functions:func1,functions:func2) are only supported for functions, hosting, storage, and firestore",
),
);
}
const targetsForFunctions = targetsForTypes(only, "functions");
if (
targetsForFunctions.length &&
targetsHaveFilters(...targetsForFunctions) &&
targetsHaveNoFilters(...targetsForFunctions)
) {
return reject(
new FirebaseError(
'Cannot specify "--only functions" and "--only functions:<filter>" at the same time',
),
);
}
return resolve();
});
}