-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (80 loc) · 2.61 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
89
90
91
92
93
94
95
// A build script to get the files onto AWS
// We copy the working directory,
// Remove any unwated files, create a zip, and push to AWS.
// TODO uglify and minify the zip output.
// TODO perhaps this could be reduced to one single file.
const fs = require('fs');
const path = require('path');
const { exec, execSync } = require('child_process');
// const UglifyJS = require("uglify-js");
// Files to remove from output
var removeFiles = ['README.md', '.gitignore', 'package.json'];
var removeFolders = ['.git'];
// Create new copy of folder
var copyRecursiveSync = function(src, dest) {
// we do not include the test folder
if (src.toString() === 'app/test') return;
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (exists && isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
// TODO
// fs.readFile(src, function read(err, data) {
// if (err) {
// throw err;
// }
// content = data;
// // Invoke the next step here however you like
// console.log(content); // Put all of the code here (not the best solution)
// processFile(); // Or put the next step in a function and invoke it
// });
// Move from src to dest.
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
};
// From and To folder
copyRecursiveSync('./app/', './dist');
// Remove files from new dist folder
removeFiles.map((file) => {
fs.unlink('./dist/' + file, function (){});
});
// Delete
var deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index){
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
// For now its one folder we are targeting.
removeFolders.map((folder) => {
deleteFolderRecursive('./dist/' + folder);
});
// zip up the dist folder
execSync(`zip -r './../dist' *`, {
cwd: 'dist'
});
// delete the newly created dist folder
deleteFolderRecursive('./dist');
// Send to AWS
exec('aws lambda update-function-code --function-name ask-custom-Hello_World-cli-user --zip-file fileb://dist.zip --publish', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});