1 | var async = require('async');
|
2 | var _ = require('lodash');
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 | var crypto = require('crypto');
|
6 | var debug = require('debug')('4front-cli:helper');
|
7 |
|
8 | module.exports.takeFirstExistsPath = function(cwd, candidates, callback) {
|
9 | var baseDir;
|
10 | var i = 0;
|
11 | async.whilst(function() {
|
12 | return _.isUndefined(baseDir) && i < candidates.length;
|
13 | }, function(cb) {
|
14 | var checkDir = path.join(cwd, candidates[i]);
|
15 | fs.exists(checkDir, function(exists) {
|
16 | if (exists === true)
|
17 | baseDir = checkDir;
|
18 |
|
19 | i++;
|
20 | cb();
|
21 | });
|
22 | }, function() {
|
23 | if (_.isUndefined(baseDir))
|
24 | baseDir = cwd;
|
25 |
|
26 | callback(null, baseDir);
|
27 | });
|
28 | };
|
29 |
|
30 | module.exports.fileHash = function(filePath, callback) {
|
31 | var hash = crypto.createHash('sha1');
|
32 | hash.setEncoding('hex');
|
33 |
|
34 | var stream;
|
35 | if (_.isString(filePath))
|
36 | stream = fs.createReadStream(filePath);
|
37 | else
|
38 | stream = filePath;
|
39 |
|
40 | var errorEmitted;
|
41 | stream.on('end', function() {
|
42 | if (errorEmitted === true)
|
43 | return;
|
44 |
|
45 | hash.end();
|
46 | callback(null, hash.read());
|
47 | });
|
48 |
|
49 | stream.on('error', function(err) {
|
50 | errorEmitted = true;
|
51 | callback(err);
|
52 | });
|
53 |
|
54 | stream.pipe(hash);
|
55 | };
|
56 |
|
57 | module.exports.pickOrgQuestion = function(orgs, message) {
|
58 | var choices = _.map(orgs, function(org) {
|
59 | return {
|
60 | name: org.name,
|
61 | value: org.orgId
|
62 | };
|
63 | });
|
64 |
|
65 |
|
66 | return {
|
67 | type: 'list',
|
68 | name: 'orgId',
|
69 | choices: choices,
|
70 | message: message
|
71 | };
|
72 | };
|