This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
config.ts
395 lines (310 loc) · 11.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { accessSync, readJSONSync, statSync } from 'fs-extra';
import { BuildContext, TaskInfo } from './interfaces';
import { join, resolve } from 'path';
import { objectAssign } from './helpers';
import { FileCache } from './file-cache';
/**
* Create a context object which is used by all the build tasks.
* Filling the config data uses the following hierarchy, which will
* keep going down the list until it, or if it, finds data.
*
* 1) Get from the passed in context variable
* 2) Get from the config file set using the command-line args
* 3) Get from environment variable
* 4) Get from package.json config property
* 5) Get environment variables
*
* Lastly, Ionic's default configs will always fill in any data
* which is missing from the user's data.
*/
export function generateContext(context?: BuildContext): BuildContext {
if (!context) {
context = {};
context.fileCache = new FileCache();
}
context.rootDir = resolve(context.rootDir || getConfigValue(context, '--rootDir', null, ENV_VAR_ROOT_DIR, ENV_VAR_ROOT_DIR.toLowerCase(), processCwd));
setProcessEnvVar(ENV_VAR_ROOT_DIR, context.rootDir);
context.tmpDir = resolve(context.tmpDir || getConfigValue(context, '--tmpDir', null, ENV_VAR_TMP_DIR, ENV_VAR_TMP_DIR.toLowerCase(), join(context.rootDir, TMP_DIR)));
setProcessEnvVar(ENV_VAR_TMP_DIR, context.tmpDir);
context.srcDir = resolve(context.srcDir || getConfigValue(context, '--srcDir', null, ENV_VAR_SRC_DIR, ENV_VAR_SRC_DIR.toLowerCase(), join(context.rootDir, SRC_DIR)));
setProcessEnvVar(ENV_VAR_SRC_DIR, context.srcDir);
context.wwwDir = resolve(context.wwwDir || getConfigValue(context, '--wwwDir', null, ENV_VAR_WWW_DIR, ENV_VAR_WWW_DIR.toLowerCase(), join(context.rootDir, WWW_DIR)));
setProcessEnvVar(ENV_VAR_WWW_DIR, context.wwwDir);
context.wwwIndex = join(context.wwwDir, WWW_INDEX_FILENAME);
context.buildDir = resolve(context.buildDir || getConfigValue(context, '--buildDir', null, ENV_VAR_BUILD_DIR, ENV_VAR_BUILD_DIR.toLowerCase(), join(context.wwwDir, BUILD_DIR)));
setProcessEnvVar(ENV_VAR_BUILD_DIR, context.buildDir);
setProcessEnvVar(ENV_VAR_APP_SCRIPTS_DIR, join(__dirname, '..', '..'));
const sourceMapValue = getConfigValue(context, '--sourceMap', null, ENV_VAR_SOURCE_MAP, ENV_VAR_SOURCE_MAP.toLowerCase(), 'eval');
setProcessEnvVar(ENV_VAR_SOURCE_MAP, sourceMapValue);
if (!isValidBundler(context.bundler)) {
context.bundler = bundlerStrategy(context);
}
context.isProd = getIsProd(context);
setIonicEnvironment(context.isProd);
if (typeof context.isWatch !== 'boolean') {
context.isWatch = hasArg('--watch', '-w');
}
context.inlineTemplates = true;
checkDebugMode();
return context;
}
export function getIsProd(context: BuildContext) {
// only check if isProd hasn't already been manually set
if (typeof context.isProd === 'boolean') {
return context.isProd;
}
if (hasArg('--dev', '-d')) {
// not production: has a --dev or -d cmd line arg
return false;
}
let val = getPackageJsonConfig(context, ENV_VAR_IONIC_DEV.toLowerCase());
if (typeof val === 'boolean') {
return !val;
}
val = getProcessEnvVar(ENV_VAR_IONIC_DEV);
if (typeof val === 'boolean') {
return !val;
}
return true;
}
export function getUserConfigFile(context: BuildContext, task: TaskInfo, userConfigFile: string) {
if (userConfigFile) {
return resolve(userConfigFile);
}
const defaultConfig = getConfigValue(context, task.fullArg, task.shortArg, task.envVar, task.packageConfig, null);
if (defaultConfig) {
return join(context.rootDir, defaultConfig);
}
return null;
}
export function fillConfigDefaults(userConfigFile: string, defaultConfigFile: string): any {
let userConfig: any = null;
if (userConfigFile) {
try {
// check if exists first, so we can print a more specific error message
// since required config could also throw MODULE_NOT_FOUND
statSync(userConfigFile);
// create a fresh copy of the config each time
userConfig = require(userConfigFile);
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`Config file "${userConfigFile}" not found. Using defaults instead.`);
} else {
console.error(`There was an error in config file "${userConfigFile}". Using defaults instead.`);
console.error(e);
}
}
}
const defaultConfig = require(join('..', '..', 'config', defaultConfigFile));
// create a fresh copy of the config each time
// always assign any default values which were not already supplied by the user
return objectAssign({}, defaultConfig, userConfig);
}
export function bundlerStrategy(context: BuildContext): string {
// 1) User provided a rollup config via cmd line args
let val: any = getArgValue('--rollup', '-r');
if (val) {
return BUNDLER_ROLLUP;
}
// 2) User provided both a rollup config and webpack config in package.json config
val = getPackageJsonConfig(context, 'ionic_rollup');
const webpackVal = getPackageJsonConfig(context, 'ionic_webpack');
if (val && webpackVal) {
let bundler = getPackageJsonConfig(context, 'ionic_bundler');
if (isValidBundler(bundler)) {
return bundler;
}
}
// 3) User provided a rollup config env var
val = getProcessEnvVar('ionic_rollup');
if (val) {
return BUNDLER_ROLLUP;
}
// 4) User provided a rollup config in package.json config
val = getPackageJsonConfig(context, 'ionic_rollup');
if (val) {
return BUNDLER_ROLLUP;
}
// 5) User set bundler through full arg
val = getArgValue('--bundler', null);
if (isValidBundler(val)) {
return val;
}
// 6) User set bundler through package.json config
val = getPackageJsonConfig(context, 'ionic_bundler');
if (isValidBundler(val)) {
return val;
}
// 7) User set to use rollup at the bundler
val = getProcessEnvVar('ionic_bundler');
if (isValidBundler(val)) {
return val;
}
// 8) Default to use webpack
return BUNDLER_WEBPACK;
}
function isValidBundler(bundler: any) {
return (bundler === BUNDLER_ROLLUP || bundler === BUNDLER_WEBPACK);
}
export function getConfigValue(context: BuildContext, argFullName: string, argShortName: string, envVarName: string, packageConfigProp: string, defaultValue: string) {
// first see if the value was set in the command-line args
const argVal = getArgValue(argFullName, argShortName);
if (argVal !== null) {
return argVal;
}
// next see if it was set in the environment variables
// which also checks if it was set in the package.json config property
const envVar = getProcessEnvVar(envVarName);
if (envVar !== null) {
return envVar;
}
const packageConfig = getPackageJsonConfig(context, packageConfigProp);
if (packageConfig !== null) {
return packageConfig;
}
// return the default if nothing above was found
return defaultValue;
}
function getArgValue(fullName: string, shortName: string): string {
for (var i = 2; i < processArgv.length; i++) {
var arg = processArgv[i];
if (arg === fullName || (shortName && arg === shortName)) {
var val = processArgv[i + 1];
if (val !== undefined && val !== '') {
return val;
}
}
}
return null;
}
export function hasConfigValue(context: BuildContext, argFullName: string, argShortName: string, envVarName: string, defaultValue: boolean) {
if (hasArg(argFullName, argShortName)) {
return true;
}
// next see if it was set in the environment variables
// which also checks if it was set in the package.json config property
const envVar = getProcessEnvVar(envVarName);
if (envVar !== null) {
return true;
}
const packageConfig = getPackageJsonConfig(context, envVarName);
if (packageConfig !== null) {
return true;
}
// return the default if nothing above was found
return defaultValue;
}
export function hasArg(fullName: string, shortName: string = null): boolean {
return !!(processArgv.some(a => a === fullName) || (shortName !== null && processArgv.some(a => a === shortName)));
}
export function replacePathVars(context: BuildContext, filePath: string) {
return filePath.replace('{{SRC}}', context.srcDir)
.replace('{{WWW}}', context.wwwDir)
.replace('{{TMP}}', context.tmpDir)
.replace('{{ROOT}}', context.rootDir)
.replace('{{BUILD}}', context.buildDir);
}
export function getNodeBinExecutable(context: BuildContext, cmd: string) {
let cmdPath = join(context.rootDir, 'node_modules', '.bin', cmd);
try {
accessSync(cmdPath);
} catch (e) {
cmdPath = null;
}
return cmdPath;
}
let checkedDebug = false;
function checkDebugMode() {
if (!checkedDebug) {
if (hasArg('--debug') || getProcessEnvVar('ionic_debug_mode') === 'true') {
processEnv.ionic_debug_mode = 'true';
}
checkedDebug = true;
}
}
export function isDebugMode() {
return (processEnv.ionic_debug_mode === 'true');
}
export function setIonicEnvironment(isProd: boolean) {
setProcessEnvVar(ENV_VAR_IONIC_ENV, (isProd ? ENV_VAR_PROD : ENV_VAR_DEV));
}
let processArgv: string[];
export function setProcessArgs(argv: string[]) {
processArgv = argv;
}
setProcessArgs(process.argv);
export function addArgv(value: string) {
processArgv.push(value);
}
let processEnv: any;
export function setProcessEnv(env: any) {
processEnv = env;
}
setProcessEnv(process.env);
export function setProcessEnvVar(key: string, value: any) {
processEnv[key] = value;
}
export function getProcessEnvVar(key: string): any {
const val = processEnv[key];
if (val !== undefined) {
if (val === 'true') {
return true;
}
if (val === 'false') {
return false;
}
return val;
}
return null;
}
let processCwd: string;
export function setCwd(cwd: string) {
processCwd = cwd;
}
setCwd(process.cwd());
export function getPackageJsonConfig(context: BuildContext, key: string): any {
const packageJsonData = getAppPackageJsonData(context);
if (packageJsonData && packageJsonData.config) {
const val = packageJsonData.config[key];
if (val !== undefined) {
if (val === 'true') {
return true;
}
if (val === 'false') {
return false;
}
return val;
}
}
return null;
}
let appPackageJsonData: any = null;
export function setAppPackageJsonData(data: any) {
appPackageJsonData = data;
}
function getAppPackageJsonData(context: BuildContext) {
if (!appPackageJsonData) {
try {
appPackageJsonData = readJSONSync(join(context.rootDir, 'package.json'));
} catch (e) {}
}
return appPackageJsonData;
}
const BUILD_DIR = 'build';
const SRC_DIR = 'src';
const TMP_DIR = '.tmp';
const WWW_DIR = 'www';
const WWW_INDEX_FILENAME = 'index.html';
const ENV_VAR_PROD = 'prod';
const ENV_VAR_DEV = 'dev';
const ENV_VAR_IONIC_ENV = 'IONIC_ENV';
const ENV_VAR_IONIC_DEV = 'IONIC_DEV';
const ENV_VAR_ROOT_DIR = 'IONIC_ROOT_DIR';
const ENV_VAR_TMP_DIR = 'IONIC_TMP_DIR';
const ENV_VAR_SRC_DIR = 'IONIC_SRC_DIR';
const ENV_VAR_WWW_DIR = 'IONIC_WWW_DIR';
const ENV_VAR_BUILD_DIR = 'IONIC_BUILD_DIR';
const ENV_VAR_APP_SCRIPTS_DIR = 'IONIC_APP_SCRIPTS_DIR';
const ENV_VAR_SOURCE_MAP = 'IONIC_SOURCE_MAP';
export const BUNDLER_ROLLUP = 'rollup';
export const BUNDLER_WEBPACK = 'webpack';