This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimporter.js
executable file
·87 lines (69 loc) · 2.13 KB
/
importer.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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var config = require('config');
var program = require('commander');
var meta = require('./lib/meta');
var index = require('./lib/index');
var moment = require('moment-timezone');
var template = function () {
throw new Error('Template not set');
};
program
.version(meta.VERSION)
.usage('[options] -f <swagger file> -o <output>')
.option('-f, --file [file]', 'Path to json data file', 'data.json')
.option('-t, --template [template]', 'template file name', './lib/templates/template01.js')
.option('-n, --name [name]', 'festival name', 'festival-name')
.option('-tz, --timezone [timezone]', 'timezone for dates import', 'Europe/Warsaw')
.option('-tp, --type [type]', 'type of import', 'festival')
.option('-u, --user [user]', 'user name')
.option('-p, --password [password]', 'user name')
.parse(process.argv);
if (!program.file) {
throw new Error('Invalid json file path: ' + program.file);
}
if (!program.template) {
throw new Error('Invalid template path: ' + program.template);
}
if (!program.name) {
throw new Error('Invalid festival name: ' + program.name);
}
if (!program.user || !program.password) {
throw new Error('user and password are required to import festival');
}
if (!config.token) {
throw new Error('Missing config.token value');
}
template = require(program.template);
moment.tz.setDefault(program.timezone);
fs.readFile(program.file, function (err, data) {
if (err) {
throw err;
}
if (!data) {
throw new Error('Invalid json data file: ' + program.file);
}
var json = JSON.parse(data);
//console.dir(json, {depth: null});
var func = function () {
};
switch (program.type) {
case 'festival':
func = index.importFestival;
break;
case 'news':
func = index.importNews;
break;
default:
throw new Error('Unsupported program.type: ' + program.type);
}
func(program.name, template, json, config.token, function (funcErr, result) {
if (funcErr) {
console.log('err', funcErr);
throw funcErr;
}
console.log('result');
console.dir(result, {depth: null});
});
});