1 | 'use strict';
|
2 |
|
3 | const fs = require('fs-extra');
|
4 | const archiver = require('archiver');
|
5 | const yaml = require('js-yaml');
|
6 | const yamlfiles = require('yaml-files');
|
7 | const merge = require('lodash.merge');
|
8 | const dotenv = require('dotenv');
|
9 | const AWS = require('aws-sdk');
|
10 | const execSync = require('child_process').execSync;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function zip(zipFile, srcList, dstPath) {
|
21 | if (!dstPath) {
|
22 | dstPath = false;
|
23 | }
|
24 | const output = fs.createWriteStream(zipFile);
|
25 | const archive = archiver('zip', {
|
26 | zlib: { level: 9 }
|
27 | });
|
28 |
|
29 | return new Promise((resolve, reject) => {
|
30 | output.on('close', function() {
|
31 | return resolve();
|
32 | });
|
33 |
|
34 | archive.on('warning', function(err) {
|
35 | if (err.code === 'ENOENT') {
|
36 | console.log(err);
|
37 | }
|
38 | else {
|
39 | return reject(err);
|
40 | }
|
41 | });
|
42 |
|
43 | archive.on('error', function(err) {
|
44 | return reject(err);
|
45 | });
|
46 |
|
47 | archive.pipe(output);
|
48 |
|
49 | srcList.forEach((src) => {
|
50 | const stat = fs.lstatSync(src);
|
51 |
|
52 | if (stat.isFile()) {
|
53 | archive.file(src);
|
54 | }
|
55 | else if (stat.isDirectory()) {
|
56 | archive.directory(src, dstPath);
|
57 | }
|
58 | else {
|
59 | return reject(new Error('Invalid path'));
|
60 | }
|
61 | });
|
62 |
|
63 | archive.finalize();
|
64 | });
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function exec(cmd, verbose) {
|
75 | verbose = verbose === false ? verbose : true;
|
76 |
|
77 | const stdout = execSync(cmd);
|
78 | if (verbose) {
|
79 | console.log(stdout.toString());
|
80 | }
|
81 | return stdout;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function configureAws(region, profile, role) {
|
92 | if (region) {
|
93 | AWS.config.update({ region });
|
94 | }
|
95 |
|
96 | if (profile) {
|
97 | AWS.config.credentials = new AWS.SharedIniFileCredentials({
|
98 | profile
|
99 | });
|
100 | }
|
101 |
|
102 | if (role) {
|
103 | AWS.config.credentials = new AWS.TemporaryCredentials({
|
104 | RoleArn: role
|
105 | });
|
106 | }
|
107 | }
|
108 |
|
109 | function loadLocalEnvs(envFile) {
|
110 | let _dotenv;
|
111 | try {
|
112 | _dotenv = dotenv.parse(fs.readFileSync(envFile));
|
113 | }
|
114 | catch (e) {
|
115 | if (!e.message.includes('ENOENT')) {
|
116 | throw e;
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 | return Object.assign(process.env, _dotenv);
|
122 | }
|
123 |
|
124 | function getZipName(handler) {
|
125 | return handler.split('.')[0];
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function fileToString(file) {
|
138 | try {
|
139 | const stat = fs.lstatSync(file);
|
140 |
|
141 | if (stat.isFile()) {
|
142 | const content = fs.readFileSync(file, 'utf8');
|
143 | return content.toString();
|
144 | }
|
145 | }
|
146 | catch (e) {
|
147 | if (!e.message.includes('ENOENT') && !e.message.includes('name too long, lstat')) {
|
148 | throw e;
|
149 | }
|
150 | }
|
151 | return file;
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | function mergeYamls(file1, file2) {
|
165 | const obj1 = yaml.safeLoad(fileToString(file1), { schema: yamlfiles.YAML_FILES_SCHEMA });
|
166 | const obj2 = yaml.safeLoad(fileToString(file2), { schema: yamlfiles.YAML_FILES_SCHEMA });
|
167 |
|
168 | return yaml.safeDump(merge({}, obj1, obj2));
|
169 | }
|
170 |
|
171 | module.exports = {
|
172 | exec,
|
173 | mergeYamls,
|
174 | fileToString,
|
175 | getZipName,
|
176 | configureAws,
|
177 | loadLocalEnvs,
|
178 | zip
|
179 | };
|