-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-layers.js
68 lines (54 loc) · 2.44 KB
/
build-layers.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
"use strict"
// build-layers copies layers/src folder contents into layer/build, then runs
// the npm install and prune commands
// additionally, copies in linux builds for packages (eg. bcrypt) which must
// be compiled for aws targets
const fs = require("fs");
const fse = require("fs-extra");
const childProcess = require("child_process");
console.log('building layers...')
// ensure layers directory created
fs.mkdirSync('layers/src', {recursive: true});
console.log(`deleting previous build directories...`);
fs.rmdirSync('layers/build', { recursive: true })
// get layers' src directories
let srcdirs = fs.readdirSync('layers/src', { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
for (let i in srcdirs) {
let layer = srcdirs[i];
console.log(`\nprocessing layer ${layer}...`);
let layerSrcPath = `layers/src/${layer}`
let layerBuildPath = `layers/build/${layer}/nodejs`
console.log(`(re)creating build directory...`);
fs.mkdirSync(layerBuildPath, { recursive: true });
// copy everything except the package-lock file and node_modules
let srcContents = fs.readdirSync(layerSrcPath, { withFileTypes: true })
.filter(dirent => {
return !(
dirent.name == "node_modules" ||
dirent.name == "package-lock.json"
)
})
.map(dirent => dirent.name)
for (let i in srcContents) {
let file = srcContents[i];
fse.copySync(`${layerSrcPath}/${file}`, `${layerBuildPath}/${file}`);
}
console.log("installing npm dependencies...");
childProcess.execSync('npm install', { cwd: layerBuildPath });
console.log("pruning unused npm modules...");
childProcess.execSync('npm prune', { cwd: layerBuildPath });
// if "bcrypt" in the package file
let packageJsonContents = fs.readFileSync(`${layerBuildPath}/package.json`, "utf8");
if (packageJsonContents.includes('"bcrypt"')) {
console.log("bcrypt found, replacing with linux build");
// overwrite with the node_modules subfolder from linux-builds
fs.rmdirSync(`${layerBuildPath}/node_modules/bcrypt`, { recursive: true })
fse.copySync(`linux-builds/bcrypt`, `${layerBuildPath}/node_modules/bcrypt`);
}
console.log("removing package-lock.json...");
fs.unlinkSync(`${layerBuildPath}/package-lock.json`);
console.log(`${layer} folder build complete`);
}
console.log('layer builds completed.')