bw-cli
Version:
The Brightwork (bw) command line deployment tool. Build & deploy serverless RESTful APIs in minutes.
102 lines (74 loc) • 2.17 kB
JavaScript
;
var targz = require('tar.gz');
var path = require('path');
var fs = require('fs');
var tempFolder = path.join(path.resolve('./'), '.tmp');
var Promise = require('bluebird');
var code_folder = path.join(tempFolder, 'compressed');
var compressed_path = path.join(tempFolder, 'compressed.tar.gz');
var stack = [];
var bots = [];
var streamToPromise = function(stream, successEvent) {
return new Promise(function(resolve, reject){
stream.on('error', reject);
stream.on(successEvent, function(){
resolve(stream);
});
});
};
module.exports = {
copy: function(dir) {
return Promise.fromCallback(function(cb){
var list = fs.readdirSync(dir);
for(var i = 0; i < list.length; i++){
if(list[i].match(/^\w+\.(js|yml|yaml)$/gm)) {
stack.push(list[i]);
if (list[i].match(/^\w+\.(js)$/gm)) {
bots.push(list[i]);
}
}
}
if (!fs.existsSync(tempFolder)) {
fs.mkdirSync(tempFolder);
}
if (!fs.existsSync(code_folder)) {
fs.mkdirSync(code_folder);
}
stack.forEach(function(file) {
console.log('packaging file ' + file);
fs.writeFileSync(
path.join(code_folder, file),
fs.readFileSync(path.join(dir, file)));
});
if (bots.length > 0) {
let indexRequires = [];
bots.forEach(function(file){
let finalPath = path.basename(file);
indexRequires.push(`require('./${finalPath}')`);
});
fs.writeFileSync(
path.join(code_folder, 'index.js'),
`
module.exports = {
bots: [${indexRequires.join(',')}]
}
`
);
stack.push('index.js'); //tag for cleanup
}
cb();
});
},
zip: function() {
return targz().compress(code_folder, compressed_path);
},
clean: function() {
return new Promise(function(resolve, reject){
while(stack.length){
fs.unlinkSync(path.join(code_folder, stack.pop()));
}
fs.rmdirSync(code_folder);
resolve();
});
}
}