-
Notifications
You must be signed in to change notification settings - Fork 0
/
twapi.js
executable file
·73 lines (64 loc) · 1.97 KB
/
twapi.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
#!/usr/bin/env node
var argv = require('yargs').boolean('stage').argv;
var api = require('./lib/api');
var theme = require('./lib/theme');
var fs = require('fs');
if (argv._.length < 1) {
console.log('Usage: twapi-cli -w <webshop> [-a <auth>] [-l <lang>] [-u <api-url>] <method> [<param1> <param2> ...]');
console.log(' twapi-cli -w <webshop> [-a <auth>] [-l <lang>] [-u <api-url>] theme-update <theme-id> <patch>');
console.log(' twapi-cli -w <webshop> [-a <auth>] [-l <lang>] [-u <api-url>] theme-set-js <theme-id> <file>');
process.exit();
}
var options = {
apiUrl: argv.u || process.env.TWAPI_URL || argv.stage ? 'http://jenkinsstage.textalk.se/backend/jsonrpc/v1' : 'http://shop.textalk.se/backend/jsonrpc/v1',
webshop: argv.w || process.env.TWAPI_WEBSHOP || 22222
};
if (argv.l) { options.language = argv.l; }
if (argv.a) { options.auth = argv.a; }
if (argv._[0] === 'theme-update') {
// Special case. Theme blobb updating
var patch;
var contents;
try {
contents = fs.readFileSync(argv._[2]);
} catch (e) {
contents = argv._[2];
}
try {
patch = JSON.parse(contents);
} catch (e) {
console.log('Could not parse patch');
process.exit(1);
}
theme.patch(argv._[1], patch, options);
} else if (argv._[0] === 'theme-set-js') {
var file = fs.readFileSync(argv._[2]);
theme.patch(argv._[1], {
params: {
settings: {
custom_javascript: {
script: file.toString()
}
}
}
}, options);
} else {
var method = argv._[0];
// Ordinary request
var params = argv._.slice(1).map(function(arg) {
try {
return JSON.parse(arg);
} catch (e) {
console.log('Failed to parse argument ' + arg);
process.exit(1);
}
});
api.call(argv._[0], params, options).then(function(result) {
console.log(JSON.stringify(result, undefined, 2));
process.exit();
}, function(error) {
console.log('Failed to do request');
console.log(error);
process.exit(1);
});
}