-
Notifications
You must be signed in to change notification settings - Fork 35
/
config.ts
207 lines (177 loc) · 4.99 KB
/
config.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as path from "path";
import * as pkgDir from "pkg-dir";
import * as findUp from "find-up";
import { logger } from "just-task-logger";
import {
CacheStorageConfig,
NpmCacheStorageConfig,
AzureBlobCacheStorageConfig
} from "backfill-cache";
export type WatchGlobs = {
folders: {
include: string[];
exclude: string[];
};
files: {
include: string[];
exclude?: string[];
};
};
export type Config = {
name: string;
packageRoot: string;
cacheStorageConfig: CacheStorageConfig;
folderToCache: string;
outputPerformanceLogs: boolean;
localCacheFolder: string;
hashFileFolder: string;
logFolder: string;
performanceReportName?: string;
verboseLogs: boolean;
watchGlobs: WatchGlobs;
};
export type ConfigEnv = {
cacheStorageConfig?: CacheStorageConfig;
outputPerformanceLogs?: boolean;
localCacheFolder?: string;
logFolder?: string;
performanceReportName?: string;
};
export function getName(packageRoot: string) {
return (
require(path.join(packageRoot, "package.json")).name ||
path.basename(path.dirname(packageRoot))
);
}
function getNpmConfigFromSerializedOptions(
options: string
): NpmCacheStorageConfig {
try {
const parsedOptions = JSON.parse(options);
if (
typeof parsedOptions.npmPackageName !== "string" ||
typeof parsedOptions.registryUrl !== "string"
) {
throw new Error("Incorrect npm storage configuration");
}
return {
provider: "npm",
options: { ...parsedOptions }
};
} catch (e) {
logger.error(e.message);
throw new Error("Invalid npm storage options");
}
}
function getAzureBlobConfigFromSerializedOptions(
options: string
): AzureBlobCacheStorageConfig {
try {
const parsedOptions = JSON.parse(options);
if (
typeof parsedOptions.connectionString !== "string" ||
typeof parsedOptions.container !== "string"
) {
throw new Error("Incorrect blob storage configuration");
}
return {
provider: "azure-blob",
options: { ...parsedOptions }
};
} catch (e) {
logger.error(e.message);
throw new Error("Invalid blob storage options");
}
}
export function getEnvConfig() {
const config: ConfigEnv = {};
const logFolder = process.env["BACKFILL_LOG_FOLDER"];
if (logFolder) {
config["logFolder"] = logFolder;
}
const outputPerformanceLogs = process.env["BACKFILL_OUTPUT_PERFORMANCE_LOGS"];
if (outputPerformanceLogs) {
config["outputPerformanceLogs"] = Boolean(outputPerformanceLogs === "true");
}
const localCacheFolder = process.env["BACKFILL_LOCAL_CACHE_FOLDER"];
if (localCacheFolder) {
config["localCacheFolder"] = localCacheFolder;
}
const cacheProvider = process.env["BACKFILL_CACHE_PROVIDER"];
const serializedCacheProviderOptions =
process.env["BACKFILL_CACHE_PROVIDER_OPTIONS"];
if (cacheProvider === "azure-blob" && serializedCacheProviderOptions) {
config["cacheStorageConfig"] = getAzureBlobConfigFromSerializedOptions(
serializedCacheProviderOptions
);
} else if (cacheProvider === "npm" && serializedCacheProviderOptions) {
config["cacheStorageConfig"] = getNpmConfigFromSerializedOptions(
serializedCacheProviderOptions
);
} else if (cacheProvider === "local") {
// local cache has no config at the moment
}
const performanceReportName = process.env["BACKFILL_PERFORMANCE_REPORT_NAME"];
if (performanceReportName) {
config["performanceReportName"] = performanceReportName;
}
return config;
}
export function getSearchPaths() {
const searchPaths = [];
let nextPath: string | undefined = process.cwd();
while (nextPath) {
const configLocation = findUp.sync("backfill.config.js", { cwd: nextPath });
if (configLocation) {
searchPaths.push(configLocation);
nextPath = path.join(path.dirname(configLocation), "..");
} else {
nextPath = undefined;
}
}
return searchPaths.reverse();
}
export function createDefaultConfig(): Config {
const packageRoot = pkgDir.sync(process.cwd()) || process.cwd();
const defaultCacheFolder = path.join("node_modules", ".cache", "backfill");
return {
packageRoot,
name: getName(packageRoot),
cacheStorageConfig: {
provider: "local"
},
folderToCache: "lib",
outputPerformanceLogs: false,
localCacheFolder: defaultCacheFolder,
hashFileFolder: defaultCacheFolder,
logFolder: defaultCacheFolder,
verboseLogs: false,
get watchGlobs(): WatchGlobs {
return {
folders: {
exclude: [this.folderToCache, "node_modules"],
include: ["*"]
},
files: {
include: ["*"]
}
};
}
};
}
export function createConfig(): Config {
const defaultConfig = createDefaultConfig();
const fileBasedConfig = getSearchPaths().reduce((acc, configPath) => {
const config = require(configPath);
return {
...acc,
...config
};
}, {});
const envBasedConfig = getEnvConfig();
return {
...defaultConfig,
...fileBasedConfig,
...envBasedConfig
};
}