-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-functions.js
85 lines (71 loc) · 2.31 KB
/
build-functions.js
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
const esbuild = require('esbuild');
const { join, parse, resolve } = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const manifest = require('./contentful-app-manifest.json');
const argv = yargs(hideBin(process.argv)).argv;
const validateFunctions = () => {
const requiredProperties = ['id', 'path', 'entryFile', 'accepts'];
const uniqueValues = new Set();
manifest.functions.forEach((contentfulFunction) => {
requiredProperties.forEach((property) => {
if (!contentfulFunction.hasOwnProperty(property)) {
throw new Error(
`Function with name: '${contentfulFunction.name}' is missing the '${property}' property`
);
}
});
const { id, path, entryFile, accepts } = contentfulFunction;
if (uniqueValues.has(id)) {
throw new Error(`Duplicate function id: '${id}'`);
}
if (uniqueValues.has(path)) {
throw new Error(`Duplicate function path: '${path}'`);
}
if (uniqueValues.has(entryFile)) {
throw new Error(`Duplicate entryFile path: '${entryFile}'`);
}
if (uniqueValues.has(accepts)) {
throw new Error(`Duplicate accepts: '${accepts}'`);
}
uniqueValues.add(entryFile);
uniqueValues.add(path);
uniqueValues.add(id);
uniqueValues.add(accepts);
});
};
const getEntryPoints = () => {
return manifest.functions.reduce((result, contentfulFunction) => {
const fileProperties = parse(contentfulFunction.path);
const fileName = join(fileProperties.dir, fileProperties.name);
result[fileName] = resolve(__dirname, contentfulFunction.entryFile);
return result;
}, {});
};
const main = async (watch = false) => {
try {
console.log('Building functions');
validateFunctions();
const config = {
entryPoints: getEntryPoints(),
bundle: true,
minify: true,
platform: 'node',
outdir: 'build',
logLevel: 'info',
format: 'esm',
target: 'es6',
external: ['node:*'],
};
if (watch) {
const context = await esbuild.context(config);
await context.watch();
} else {
await esbuild.build(config);
}
} catch (e) {
console.error('Error building functions');
throw Error(e);
}
};
main(argv._.includes('watch')).then(() => console.log('functions built successfully'));