-
Notifications
You must be signed in to change notification settings - Fork 7
/
batch.ts
79 lines (74 loc) · 2.27 KB
/
batch.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
import {
AwsOptions,
CommonOptions,
faastAws,
faastLocal,
FaastModuleProxy,
LocalOptions,
log,
} from "faastjs";
import mergeOptions from "merge-options";
import semver from "semver";
import { assertNonNullable } from "./assert";
import * as compilerFaastFunctions from "./compiler-core";
import { DuckConfig } from "./duckconfig";
import { logger } from "./logger";
// change to stdout
log.info.log = console.log.bind(console);
export async function getFaastCompiler(
config: DuckConfig
): Promise<FaastModuleProxy<typeof compilerFaastFunctions, CommonOptions, any>> {
logger.info("Initializing batch mode");
const batch = assertNonNullable(config.batch);
const batchOptions = getBatchOptions(config);
const m =
batch === "aws"
? await faastAws(compilerFaastFunctions, batchOptions as AwsOptions)
: batch === "local"
? await faastLocal(compilerFaastFunctions, batchOptions as LocalOptions)
: null;
if (!m) {
throw new TypeError(`Unsupported batch mode: ${batch}`);
}
return m;
}
function getBatchOptions(config: DuckConfig): AwsOptions | LocalOptions {
const { batchOptions = {} } = config;
return mergeOptions.call({ concatArrays: true }, defaultBatchOptions(config), batchOptions);
}
function defaultBatchOptions(config: DuckConfig): AwsOptions {
const closureVersion = require("google-closure-compiler/package.json").version;
const major = semver.major(closureVersion);
return {
packageJson: {
// To suppress npm warnings
private: true,
dependencies: {
[`google-closure-compiler-${getOsForNativeImage(config)}`]: `^${major}.0.0`,
},
},
webpackOptions: {
externals: [
/^aws-sdk\/?/,
"google-closure-compiler-js",
"google-closure-compiler-linux",
"google-closure-compiler-osx",
// used in google-closure-compiler/lib/(grunt|gulp)
"chalk",
// used in google-closure-compiler/lib/gulp
/^gulp($|-)/,
/^vinyl($|-)/,
],
},
};
}
function getOsForNativeImage(config: DuckConfig) {
const { platform } = process;
if (config.batch === "aws" || platform === "linux") {
return "linux";
} else if (platform === "darwin") {
return "osx";
} else {
throw new Error(`Unsuported Platform: ${platform}`);
}
}