UNPKG

4.34 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs-extra');
4const archiver = require('archiver');
5const yaml = require('js-yaml');
6const yamlfiles = require('yaml-files');
7const merge = require('lodash.merge');
8const dotenv = require('dotenv');
9const AWS = require('aws-sdk');
10const execSync = require('child_process').execSync;
11
12/**
13 * Zips a list of files or directories
14 * @param {string} zipFile filename and path where the zip file is stored
15 * @param {array} srcList array of files and directories paths
16 * @param {type} dstPath for directories whether to put the directories at
17 * root of the zip file or relative to your path on the local machine
18 * @return {Promise}
19 */
20function 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 } // Sets the compression level.
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 * Executes shell commands synchronously and logs the
69 * stdout to console.
70 * @param {String} cmd Bash command
71 * @param {Boolean} [verbose=true] whether to post stdout to console
72 * @return {Buffer} The command's stdout in for of Buffer
73 */
74function 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 * Updates region of an AWS configuration and point to the correct
86 * of profile on ~/.aws/credentials file if necessary
87 *
88 * @param {String} [region='us-east-1'] AWS region
89 * @param {String} [profile=null] aws credentials profile name
90 */
91function 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
109function 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 // load all env variables to an object
121 return Object.assign(process.env, _dotenv);
122}
123
124function getZipName(handler) {
125 return handler.split('.')[0];
126}
127
128/**
129 * Checks if the input is a file, if it is a file,
130 * it reads it and return the content, otherwise just pass
131 * the input as an output
132 *
133 * @param {String} file A file path or a string
134 * @returns {String} String content of a given file
135 */
136
137function 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 * Merges two yaml files. The merge is done using lodash.merge
156 * and it happens recursively. Meaning that values of file2 will
157 * replace values of file 1 if they have the same key.
158 *
159 * @param {String} file1 Yaml path to file 1 or file 1 string
160 * @param {String} file2 Yaml path to file 2 or file 2 string
161 * @returns {String} Merged Yaml file in string format
162 */
163
164function 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
171module.exports = {
172 exec,
173 mergeYamls,
174 fileToString,
175 getZipName,
176 configureAws,
177 loadLocalEnvs,
178 zip
179};