-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathleo-cli-build.js
273 lines (244 loc) · 8.35 KB
/
leo-cli-build.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
var path = require('path');
// var fs = require('fs');
var program = require('commander');
var colors = require('colors');
// var archiver = require('archiver');
// var aws = require("aws-sdk");
// var glob = require("glob");
// var CopyWebpackPlugin = require('copy-webpack-plugin');
// var babelify = require("babelify");
// var spawn = require('child_process').spawn;
// var execsync = require('child_process').execSync;
// var through = require('through2');
// var async = require("async");
// var moment = require("moment");
// var leo = require("leo-sdk");
// var configure;
// //Build Stuff
// var browserify = require('browserify');
// var gulp = require("gulp");
// var source = require('vinyl-source-stream');
// var buffer = require('vinyl-buffer');
// var gutil = require('gulp-util');
// var uglify = require('gulp-uglify');
// var rename = require('gulp-rename');
// var ejs = require("gulp-ejs");
// var buildConfig = require("./lib/build-config").build;
// //webpack
// var webpack = require('webpack');
// var ExtractTextPlugin = require("extract-text-webpack-plugin");
var cmds = require("./lib/build.js")
program
.version('0.0.1')
.option("-e, --env [env]", "Environment")
.option("-r, --region [region]", "AWS Region")
.option("-p, --profile [profile]", "AWS Profile")
.arguments('<dir> [alias] [region]')
.usage('<dir> [alias] [region] [options]')
.action(function (dir, alias, region) {
var rootDir = path.resolve(process.cwd(), "./" + dir);
cmds.build(program, rootDir, {
alias,
region
}, (err) => {
});
// alias = program.env || alias;
// region = program.region || region
// console.log("Build Alias:", alias || "dev");
// process.env.LEO_ENV = alias || "dev";
// process.env.LEO_REGION = region;
// var rootDir = path.resolve(process.cwd(), "./" + dir);
// configure = buildConfig(rootDir);
// configure.aws = configure.aws || {};
// region = configure._meta.region;
// if (program.profile) {
// console.log("Using cli profile", program.profile)
// configure.aws.profile = program.profile;
// }
// if (configure.aws.profile) {
// console.log("Setting profile to", configure.aws.profile);
// var credentials = new aws.SharedIniFileCredentials({
// profile: configure.aws.profile
// });
// aws.config.credentials = credentials;
// process.env.AWS_DEFAULT_PROFILE = configure.aws.profile;
// }
// console.log("Setting region to", region);
// var deployAlias = alias ? alias.toLowerCase() : 'dev';
// var pkg = require(path.resolve(rootDir, "package.json"));
// var config = configure;
// if (config.type == "bot" || config.type == "cron" || config.type == "resource" || config.type == "apigateway") {
// buildLambdaDirectory(rootDir, {}, function (err, data) {
// console.log(err, data);
// });
// } else if (config.type == "package") {
// let cloudformation = fs.readFileSync(path.resolve(rootDir, "cloudformation.json"), {
// encoding: "utf-8"
// });
// let lambdas = config.lambdas || [];
// let dir = `${config.name}-${moment.now()}`;
// async.mapLimit(lambdas, 5, (lambdaDir, done) => {
// buildLambdaDirectory(path.resolve(rootDir, lambdaDir), {
// dir: dir
// }, (err, data) => {
// //setTimeout(() => done(err, data), 500)
// done(err, data)
// });
// }, err => {
// console.log("All lambda zip files completed", err || undefined);
// console.log("Create cloudformation");
// let cfPath = path.resolve(rootDir, "cloudformation.json");
// if (fs.existsSync(cfPath)) {
// let cf = require(cfPath);
// console.log(config)
// cf.Resources.LEO_VARS = {
// Type: "Custom::LeoVariables",
// Version: "1.0",
// Properties: {
// "ServiceToken": {
// "Fn::Join": [
// "", ["arn:aws:lambda:", {
// "Ref": "AWS::Region"
// }, ":", {
// "Ref": "AWS::AccountId"
// }, ":leo:vars"]
// ]
// },
// bucket: leo.configuration.bus.s3,
// version: moment.now().toString()
// }
// };
// fs.writeFileSync(`/tmp/${dir}/cloudformation.js`, JSON.stringify(cf, null, 2));
// //console.log(`Uploading static files ${staticDir} to ${config.staticS3}/${newVersion}`);
// //spawn('aws', ['s3', 'sync', staticDir, `${config.staticS3}/${newVersion}`]);
// }
// });
// } else {
// console.log("Unknown config.leo.type, not in (bot, cron, resource, microservice)");
// }
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}
// function buildLambdaDirectory(rootDir, config, callback) {
// var folder = config.dir ? `tmp/${config.dir}` : "tmp";
// if (!fs.existsSync(`/${folder}`)) {
// fs.mkdirSync(`/${folder}`);
// }
// var config = buildConfig(rootDir);
// console.log("Run build on", rootDir)
// execsync("npm install", {
// cwd: rootDir
// });
// console.log(`Zipping Lambda Function ${config.name}`);
// var archive = archiver('zip');
// var zipFilename = `/${folder}/${config.name}.zip`;
// var indexFilename = `${config.name}-index-${moment.now()}.js`;
// var zip = fs.createWriteStream(zipFilename);
// archive.pipe(zip);
// var b = browserify({
// standalone: 'lambda',
// bare: true,
// entries: [__dirname + "/lib/leowrap.js"],
// browserField: false,
// builtins: false,
// commondir: false,
// detectGlobals: true,
// insertGlobalVars: {
// process: function () {
// return;
// }
// },
// debug: true
// });
// b.transform(babelify, {
// presets: ["es2015"],
// sourceMaps: false
// });
// b.external("aws-sdk");
// if (config.build && config.build.include) {
// for (var i = 0; i < config.build.include.length; i++) {
// var inc = config.build.include[i];
// var src = inc.src || inc;
// var dest = inc.dest || "node_modules/";
// src = path.resolve(rootDir, src);
// b.external(path.basename(src));
// if (fs.lstatSync(src).isDirectory()) {
// execsync("npm install", {
// cwd: src
// });
// }
// archive.directory(path.normalize(src), path.join(dest, path.basename(src)));
// }
// }
// b.transform(function (file) {
// if (file.match("leowrap")) {
// return through(function (buf, enc, next) {
// next(null, "");
// }, function (cb) {
// var type = config.type;
// if (config.cron && typeof config.cron == "object" && config.cron.autoType !== false) {
// type = "cron";
// }
// var wrapperFile = __dirname + "/lib/wrappers/" + type + ".js";
// if (!fs.existsSync(wrapperFile)) {
// wrapperFile = __dirname + "/lib/wrappers/base.js";
// }
// var contents = fs.readFileSync(wrapperFile, 'utf-8')
// .replace("____FILE____", path.normalize(path.resolve(rootDir, "index.js")).replace(/\\/g, "\\\\"));
// this.push(contents);
// cb();
// });
// } else if (file.match("leoConfigure.js")) {
// return through(function (buf, enc, next) {
// next(null, "");
// }, function (cb) {
// this.push("module.exports = " + JSON.stringify(config));
// cb();
// });
// } else if (file.match("leo-sdk-config.js")) {
// return through(function (buf, enc, next) {
// next(null, "");
// }, function (cb) {
// var sdkConfigData = "{}";
// var sdkConfigPath = path.resolve(`${require('os').homedir()}/.leo`, "config.json")
// if (fs.existsSync(sdkConfigPath)) {
// sdkConfigData = JSON.parse(fs.readFileSync(sdkConfigPath) || sdkConfigData);
// }
// // Can't Change aws profile in lambda so remove the profile key
// Object.keys(sdkConfigData).map(k => delete sdkConfigData[k].profile);
// this.push("module.exports = " + JSON.stringify(sdkConfigData));
// cb();
// });
// } else {
// return through();
// }
// }, {
// global: true
// });
// b.bundle().pipe(source(indexFilename)).pipe(buffer())
// .pipe(gulp.dest(`/${folder}/`)).on("end", () => {
// console.log("done building");
// archive.file(`/${folder}/${indexFilename}`, {
// name: "index.js"
// });
// if (config.files) {
// for (var file in config.files) {
// archive.file(config.files[file], {
// name: file
// });
// }
// }
// zip.on("close", function () {
// console.log("Created zip");
// fs.unlinkSync(`/${folder}/${indexFilename}`);
// callback(null, {
// config: config,
// path: zipFilename
// });
// });
// archive.finalize();
// });
// }