This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.js
executable file
·135 lines (114 loc) · 4.12 KB
/
cli.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
// Copyright 2020 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.
#!/usr/bin/env node
'use strict'
const fetcher = require('./');
const minimist = require('minimist');
const extend = require('extend');
const fs = require('fs');
const os = require('os');
const async = require('async');
const mkdirp = require('mkdirp');
/**
* makes the targetDir (recursively) and runs a cb
*/
function createTargetDir( targetDir, cb) {
mkdirp.sync(targetDir, function (err) {
if (err) return cb(err);
});
}
let opts = minimist(process.argv.slice(2))
if (opts['strict-ssl'] === false) {
opts.strictSSL = false
}
function flatten_configuration(cfg) {
const node_env = process.env.NODE_ENV || "dev";
let conf = extend(true, {}, cfg);
let env = conf.ENV;
if (env) {
Object.keys(env).forEach((envValue) => {
Object.keys(env[envValue]).forEach((fileName) => {
Object.keys(env[envValue][fileName]).forEach((prop) => {
if (prop === "override" && !env[envValue][fileName][prop]) {
// mix in platform specifics
if (env[os.platform()]) {
env[envValue][fileName] = extend(true, env[envValue][fileName], env[os.platform()][fileName])
}
// then apply environment setup
if (env[os.platform()] && env[os.platform()].ENV && env[os.platform()].ENV[node_env]) {
env[envValue][fileName] = extend(true, env[envValue][fileName], env[os.platform()].ENV[node_env][fileName]);
}
if (env[node_env]) {
env[envValue][fileName] = extend(true, env[envValue][fileName], env[node_env][fileName]);
}
env[envValue][`${fileName}_${envValue}`] = extend(true, {}, conf[fileName], env[envValue][fileName]);
delete env[envValue][fileName];
}
});
});
});
// mix in platform specifics
if (env[os.platform()]) {
conf = extend(true, conf, env[os.platform()])
}
// then apply environment setup
if (env[os.platform()] && env[os.platform()].ENV && env[os.platform()].ENV[node_env]) {
conf = extend(true, conf, env[os.platform()].ENV[node_env]);
}
if (env[node_env]) {
conf = extend(true, conf, env[node_env]);
}
delete conf.ENV;
Object.keys(conf).forEach((name) => {
Object.keys(conf[name]).forEach((prop) => {
if (prop === "disabled" && conf[name][prop]) {
delete conf[name];
}
});
});
}
return conf
}
function make_downloader(pkgs, name) {
const opts = pkgs[name];
const targetDir = opts.targetDir;
if (!opts.filename) {
opts.filename = name
}
// make targetDir if it doesnt exist.
if (!fs.existsSync(targetDir)) {
createTargetDir(targetDir);
}
return (cb) => fetcher(opts, (targetFiles, opts) => {
console.log('Downloaded and unzip of ' + opts.filename +
' complete: \n - ' + targetDir + '/'
+ targetFiles.join(',\n - ' + targetDir + '/') + "\n\n\n");
cb(0, targetFiles)
})
}
if (opts.package) {
const packages = JSON.parse(fs.readFileSync(opts.package, 'utf8'));
if (!packages.download_deps) {
console.log("No 'download_deps' found in configuration " + opts.package);
process.exit(1)
}
const pkgs = flatten_configuration(packages.download_deps);
async.parallelLimit(
Object.getOwnPropertyNames(pkgs)
.map((name) => make_downloader(pkgs, name)), 4)
} else {
if (!opts.filename) {
console.log("You must specify the --version and --filename")
}
fetcher(opts, (targetFiles, opts) => {
console.log('Downloaded and unzip of ' + opts.filename + ' complete: \n - ' + opts.targetDir + '/'
+ targetFiles.join(',\n - ' + opts.targetDir + '/'))
process.exit(0)
})
}