-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot-refresh.js
142 lines (97 loc) · 2.62 KB
/
snapshot-refresh.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
'use strict';
import Jsonfile from 'jsonfile';
import Prompt from 'prompt';
import rp from 'request-promise';
import colors from 'colors';
const paths = {
userConfig: './src/config/user-config.json',
appConfig: './src/config/app-config.json'
};
const UserConfig = Jsonfile.readFileSync(paths.userConfig, {
throws: false
});
const AppConfig = Jsonfile.readFileSync(paths.appConfig);
const UserConfigRequiredFields = ['site_name', 'site_domain', 'api_key', 'callback_url'];
const MODE = _getMode();
function _userConfigValidator(config) {
return !!config && UserConfigRequiredFields.every(field => !!config[field]);
}
function _getMode() {
if (process.argv.indexOf('--update-config') > -1) {
console.log('--update-config');
return 'CONFIG_CREATE';
} else if (process.argv.indexOf('--delete-config') > -1 || process.argv.indexOf('--reset-config') > -1) {
console.log('--delete-reset-config');
return 'CONFIG_RESET';
} else {
let isUserConfigValid = _userConfigValidator(UserConfig);
if (isUserConfigValid) {
console.log(`Cool ! Found some valid config.`.green);
return 'SNAPSHOT_REFRESH';
} else {
console.log(`Ups ! Didn't found valid config. Let's create one.`.yellow);
return 'CONFIG_CREATE';
}
}
}
function _runSnapshotRefresh() {
console.log(`Please enter slug of your page, which you'd like to refresh in brobmone.`);
Prompt
.start()
.get(['slug'], function(err, result) {
_callBrombone({
site_name: UserConfig.site_name,
url: UserConfig.site_domain + '/' + result.slug,
api_key: UserConfig.api_key,
callback_url: UserConfig.callback_url
});
});
}
function _runConfigCreator() {
Prompt
.start()
.get(UserConfigRequiredFields, function(err, result) {
Jsonfile.writeFile(paths.userConfig, result, function(err) {
console.error(err);
});
});
}
function _resetConfig() {
Jsonfile.writeFile(paths.userConfig, {});
}
function _callBrombone(body) {
console.log('Trying to call brombone...');
let logingDotsInterval = setInterval(function() {
console.log('...');
}, 1000);
let options = {
method: 'POST',
uri: AppConfig.bromboneApiUrl,
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json'
},
body: body,
json: true
};
rp(options)
.then(function(res) {
console.log(res.green.bold);
clearInterval(logingDotsInterval);
})
.catch(function(err) {
console.log(err.red.bold);
clearInterval(logingDotsInterval);
});
}
switch (MODE) {
case 'CONFIG_CREATE':
_runConfigCreator();
break;
case 'CONFIG_RESSET':
_resetConfig();
break;
case 'SNAPSHOT_REFRESH':
_runSnapshotRefresh();
break;
}