-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
97 lines (91 loc) · 2.82 KB
/
index.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
import { MergedRollupOptions } from '../../src/rollup/types';
import { isWatchEnabled } from '../../src/utils/options/mergeOptions';
import { getAliasName } from '../../src/utils/relativeId';
import { loadFsEvents } from '../../src/watch/fsevents-importer';
import { handleError } from '../logging';
import { BatchWarnings } from './batchWarnings';
import build from './build';
import { getConfigPath } from './getConfigPath';
import loadAndParseConfigFile from './loadConfigFile';
import loadConfigFromCommand from './loadConfigFromCommand';
export default async function runRollup(command: Record<string, any>): Promise<void> {
let inputSource;
if (command._.length > 0) {
if (command.input) {
handleError({
code: 'DUPLICATE_IMPORT_OPTIONS',
message: 'Either use --input, or pass input path as argument'
});
}
inputSource = command._;
} else if (typeof command.input === 'string') {
inputSource = [command.input];
} else {
inputSource = command.input;
}
if (inputSource && inputSource.length > 0) {
if (inputSource.some((input: string) => input.indexOf('=') !== -1)) {
command.input = {};
inputSource.forEach((input: string) => {
const equalsIndex = input.indexOf('=');
const value = input.substr(equalsIndex + 1);
let key = input.substr(0, equalsIndex);
if (!key) key = getAliasName(input);
command.input[key] = value;
});
} else {
command.input = inputSource;
}
}
if (command.environment) {
const environment = Array.isArray(command.environment)
? command.environment
: [command.environment];
environment.forEach((arg: string) => {
arg.split(',').forEach((pair: string) => {
const [key, ...value] = pair.split(':');
if (value.length) {
process.env[key] = value.join(':');
} else {
process.env[key] = String(true);
}
});
});
}
if (isWatchEnabled(command.watch)) {
await loadFsEvents();
const { watch } = await import('./watch-cli');
watch(command);
} else {
try {
const { options, warnings } = await getConfigs(command);
try {
for (const inputOptions of options) {
await build(inputOptions, warnings, command.silent);
}
if (command.failAfterWarnings && warnings.warningOccurred) {
warnings.flush();
handleError({
code: 'FAIL_AFTER_WARNINGS',
message: 'Warnings occurred and --failAfterWarnings flag present'
});
}
} catch (err: any) {
warnings.flush();
handleError(err);
}
} catch (err: any) {
handleError(err);
}
}
}
async function getConfigs(
command: any
): Promise<{ options: MergedRollupOptions[]; warnings: BatchWarnings }> {
if (command.config) {
const configFile = getConfigPath(command.config);
const { options, warnings } = await loadAndParseConfigFile(configFile, command);
return { options, warnings };
}
return await loadConfigFromCommand(command);
}