1 | 'use strict';
|
2 |
|
3 | var tar = require('tar'),
|
4 | zlib = require('zlib'),
|
5 | path = require('path'),
|
6 | http = require('http'),
|
7 | fs = require('fs'),
|
8 | ProgressBar = require('progress'),
|
9 | utils = require('./utils');
|
10 |
|
11 | var download = function (downloadUrl, installPath, callback) {
|
12 | console.log("Started downloading Dynamodb-local. Process may take few minutes.");
|
13 | http.get(downloadUrl, function (response) {
|
14 | var len = parseInt(response.headers['content-length'], 10),
|
15 | bar = new ProgressBar('Downloading dynamodb-local [:bar] :percent :etas', {
|
16 | complete: '=',
|
17 | incomplete: ' ',
|
18 | width: 40,
|
19 | total: len
|
20 | });
|
21 |
|
22 | if (200 != response.statusCode) {
|
23 | throw new Error('Error getting DynamoDb local latest tar.gz location ' + response.headers.location + ': ' + response.statusCode);
|
24 | }
|
25 |
|
26 | response
|
27 | .pipe(zlib.Unzip())
|
28 | .pipe(tar.Extract({
|
29 | path: installPath
|
30 | }))
|
31 | .on('data', function (chunk) {
|
32 | bar.tick(chunk.length);
|
33 | })
|
34 | .on('end', function () {
|
35 | callback("\n Installation complete!");
|
36 | })
|
37 | .on('error', function (err) {
|
38 | throw new Error("Error in downloading Dynamodb local " + err);
|
39 | });
|
40 | })
|
41 | .on('error', function (err) {
|
42 | throw new Error("Error in downloading Dynamodb local " + err);
|
43 | });
|
44 | };
|
45 |
|
46 | var install = function (config, callback) {
|
47 | var install_path = utils.absPath(config.setup.install_path),
|
48 | jar = config.setup.jar,
|
49 | download_url = config.setup.download_url;
|
50 |
|
51 | try {
|
52 | if (fs.existsSync(path.join(install_path, jar))) {
|
53 | callback("Dynamodb is already installed on path!");
|
54 | } else {
|
55 | utils.createDir(config.setup.install_path);
|
56 | download(download_url, install_path, callback);
|
57 | }
|
58 | } catch (e) {}
|
59 | };
|
60 | module.exports.install = install;
|