forked from rakutentech/docpub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docpub.js
86 lines (75 loc) · 2.67 KB
/
docpub.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
const Promise = require('bluebird');
const path = require('path');
const _ = require('lodash');
const Config = require('./config');
const Category = require('./category');
const CategoryUploader = require('./zendesk-uploader/category-uploader');
const ZendeskDeleter = require('./zendesk-uploader/deleter');
const logger = require('./logger');
const chalk = require('chalk');
/**
* Docpub represents interface to utility. It is used as entry point for all operations.
* It's interface methods represent a command utility support.
* Must be initialised with args received from user.
*/
module.exports = class Docpub {
/**
* Creates DocpubPipeline instance
* Options, received from user, must be passed to this constructor
* If path passed, it would be resolved relatively to process.cwd()
*
* @param {Object} opts - options received from user.
*/
constructor(opts) {
opts = _.defaults(opts || {}, {
path: process.cwd(),
verbose: false
});
logger.setup(opts);
this._path = path.resolve(opts.path);
this._config = this._loadConfig(opts);
}
/**
* Performs upload to Zendesk action for all available documents
* First reads the directory for all availblae items.
* If read was correct, performs upload of this items.
* If read or upload were rejected, rejects promise.
* If everything was successful, resolves the promise
*
* @returns Promise - promise to be resolved when uploading finished
*/
uploadCategory() {
logger.info(`Start uploading.`);
logger.info(`Category path: ${this._path}`);
const category = new Category(this._path, this._config);
return category.read()
.then(() => {
const categoryUploader = new CategoryUploader(category, this._config);
const zendeskDeleter = new ZendeskDeleter(category, this._config);
return categoryUploader.upload()
.then(() => zendeskDeleter.delete());
})
.then(() => {
logger.info(
chalk.green(`Successfully uploaded all entities for category ${this._path}`)
);
})
.catch(e => {
this._logError(e);
return Promise.reject(e);
});
}
_loadConfig(opts) {
try {
return new Config(opts.configPath, this._path);
} catch (e) {
this._logError(e);
throw e;
}
}
_logError(error) {
logger.error(`Upload failed!`);
logger.error(error.message);
logger.error(error.stack);
}
};