forked from luisdemarchi/serverless-esm-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (74 loc) · 2.32 KB
/
index.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
86
87
88
'use strict';
const os = require('os');
const fs = require('fs');
const path = require('path');
const zl = require('zip-lib');
const lnk = require('lnk');
const archiver = require('archiver');
class EsmLayer {
constructor(serverless, cliOptions, { log }) {
this.serverless = serverless;
this.log = log;
this.hooks = {
'after:package:createDeploymentArtifacts': async () => {
await this.packageFinalize();
},
};
}
async packageFinalize() {
this.log.info('serverless-esm-layer: started');
const dirPath = path.relative('./', '.serverless');
const files = await fs.promises.readdir(dirPath);
const zipFiles = files.filter(el => path.extname(el) === '.zip');
const tempName = `ServerlessEsmLayer-${Math.random().toString(36).slice(2, 7)}`;
await fs.promises.mkdtemp(path.join(os.tmpdir(), tempName));
this.tmpDir = path.join(os.tmpdir(), tempName);
await Promise.all(
zipFiles.map(async (fileName) => {
try {
this.log.info(`serverless-esm-layer: editing the file ${fileName}`);
await this.unzip(fileName);
await this.symlink(fileName);
return this.zip(fileName);
} catch(error) {
this.serverless.classes.Error(`serverless-esm-layer - error: ${error}`);
}
})
);
this.log.info('serverless-esm-layer: finished');
}
fullPath(filename) {
return path.join(this.tmpDir, filename.slice(0, -4));
}
async unzip(filename) {
await zl.extract(`./.serverless/${filename}`, this.fullPath(filename), {
overwrite: true,
symlinkAsFileOnWindows: false
});
}
async symlink(filename) {
await lnk('/opt/nodejs/node_modules', this.fullPath(filename), { rename: 'node_modules', type: 'symbolic', force: true });
}
async zip(filename) {
const output = fs.createWriteStream(`./.serverless/${filename}`);
const archive = archiver('zip', {
zlib: { level: 9 }
});
archive.pipe(output);
archive.glob('**/*',
{
cwd: this.fullPath(filename),
root: false,
nodir: false,
nosort: false,
dot: false,
follow: false
}
);
await archive.finalize();
}
async deleteDirTemp() {
return fs.promises.rm(this.tmpDir, { recursive: true, force: true });
}
}
module.exports = EsmLayer;