-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsflog.js
executable file
·136 lines (113 loc) · 3.97 KB
/
sflog.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
#!/usr/bin/env node
'use strict';
/**
* SFlog
*/
var app = require('commander');
var _ = require('lodash');
var force = require('./lib/force');
var log = require('./lib/util/log');
var SFLog = require('./lib/sflog');
var path = require('path');
app
.version('0.0.1')
.option('-c, --config [path]', 'Custom config file')
.option('-o, --org [org]', 'Pro/Dev/Sandbox Org')
.option('-u, --username [username]', 'Username')
.option('-p, --password [password]', 'Password concat with token')
.option('-i, --instance-url [instanceUrl]', 'jsforce\'s instanceUrl see: https://jsforce.github.io/document/#access-token')
.option('-a, --access-token [accessToken]', 'jsforce\'s accessToken see: https://jsforce.github.io/document/#access-token')
.option('--forcecode [path]', 'Using ForceCode config file')
.option('--pulling-interval [interval]', 'Pulling interval in milliseconds should >= 1000', parseInt)
.option('--silly', 'Set log level to silly')
.option('--verbose', 'Set log level to verbose')
.option('--silent', 'Set log level to silent')
.parse(process.argv);
log.silly( 'app: ', app );
var org = app.org || 'dev';
var isValidAuth = false;
var config = {};
var usableConfig = {};
if( typeof app.config === 'string' ) {
try {
var configPath = path.resolve(app.config);
log.silly( 'configPath: ', configPath );
config = _.assign(config, JSON.parse( require("fs").readFileSync( configPath ) ) );
} catch( e ) {
log.error( 'Not found/Invalid config file in ' + app.config );
}
}
config[org] = ( config[org] || {} );
// Default options
usableConfig = _.assign({
logPrefix: 'sflog ',
pullingInterval: 5000
}, config);
// 1s
if( typeof app.pullingInterval === 'number' && app.pullingInterval >= 1000 ) {
usableConfig.pullingInterval = app.pullingInterval;
}
// ForceCode
var forcecodeConfig = {};
var forcecodeConfigPath = null;
if( typeof app.forcecode === 'boolean' ) {
forcecodeConfigPath = path.resolve('./force.json');
} else if ( typeof app.forcecode === 'string' ) {
forcecodeConfigPath = path.resolve(app.forcecode);
}
log.silly( 'app.forcecode: ', app.forcecode );
if( app.forcecode ) {
try {
var fs = require("fs");
var forcecodeConfigJson = JSON.parse( fs.readFileSync( forcecodeConfigPath ) );
forcecodeConfig = {
loginUrl: forcecodeConfigJson.url,
username: forcecodeConfigJson.username,
password: forcecodeConfigJson.password
};
log.silly( 'forcecodeConfig: ', forcecodeConfig );
} catch( e ) {
// Ignore if not exist or error
log.error( e );
// log.error( 'Not found/Invalid MavensMate session file in ' + forcecodeConfigPath );
}
}
// END ForceCode
usableConfig[org] = {
loginUrl: forcecodeConfig['loginUrl'] || null,
username: app.username || config[org]['username'] || forcecodeConfig['username'] || null,
password: app.password || config[org]['password'] || forcecodeConfig['password'] || null,
instanceUrl: app.instanceUrl || config[org]['instanceUrl'] || null,
accessToken: app.accessToken || config[org]['accessToken'] || null
};
log.silly( 'org: usableConfig: ', org, usableConfig );
// TODO: Check for valid format
if( !_.isEmpty( usableConfig[org].username ) && !_.isEmpty( usableConfig[org].password ) ) {
isValidAuth = true;
}
log.silly( 'isValidAuth: ', isValidAuth );
// TODO: Check for a valid format of instanceUrl & accessToken
if( !_.isEmpty( usableConfig[org].instanceUrl ) && !_.isEmpty( usableConfig[org].accessToken ) ) {
isValidAuth = true;
}
if( !isValidAuth ) {
app.outputHelp();
process.exit(1);
}
log.info('Authenticating ...');
force.auth.login({
loginUrl: usableConfig[org].loginUrl,
username: usableConfig[org].username,
passwordToken: usableConfig[org].password,
instanceUrl: usableConfig[org].instanceUrl,
accessToken: usableConfig[org].accessToken
}).then(function( identity ) {
log.silly( 'identity: ', identity );
log.info('Authenticated as ' + identity.username );
return new SFLog({
connection: force.auth.connection,
pullingInterval: usableConfig.pullingInterval
}).tail();
}).catch(function( e ) {
log.error( e );
});