-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.js
218 lines (182 loc) · 7.56 KB
/
app.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const path = require('path');
const Spinner = require('cli-spinner').Spinner;
const colors = require('colors');
const cliProgress = require('cli-progress');
const JSZip = require('jszip');
const fs = require('fs');
const moment = require('moment');
//PinoLogger implements LoggerAdapter-Interface
//It is possible to use another logger instead of PinoLogger
const PinoLogger = require('./lib/common/PinoLogger');
const QuipProcessor = require('./lib/QuipProcessor');
const QuipService = require('./lib/QuipService');
const utils = require('./lib/common/utils');
const CliArguments = require('./lib/cli/CliArguments');
//EJS template for html documents
const documentTemplate = utils.readTextFile(path.join(__dirname, './lib/templates/document.ejs'));
//CSS style for html documents
const documentCSS = utils.readTextFile(path.join(__dirname, './lib/templates/document.css'));
class App {
constructor() {
this.Logger = {};
this.desinationFolder;
this.cliArguments;
this.zip;
this.quipProcessor;
this.spinnerIndicator = new Spinner(' %s read 0 folder(s) | 0 thread(s)');
this.progressIndicator = new cliProgress.Bar({
format: ' |{bar}| {percentage}% | {value}/{total} threads | ETA: {eta_formatted}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: false
});
this.phase;
}
/*
callback-function for file saving
*/
fileSaver(data, fileName, type, filePath) {
if(type === 'BLOB') {
if(this.cliArguments.zip) {
this.zip.folder(filePath).file(fileName, data.arrayBuffer());
} else {
utils.writeBlobFile(path.join(this.desinationFolder, "quip-export", filePath, fileName), data);
}
} else {
if(this.cliArguments.zip) {
this.zip.folder(filePath).file(fileName, data);
} else {
utils.writeTextFile(path.join(this.desinationFolder, "quip-export", filePath, fileName), data);
}
}
}
/*
callback-function for progress indication
*/
progressFunc(progress) {
if(this.phase === 'ANALYSIS') {
this.spinnerIndicator.text = ` %s read ${progress.readFolders} folder(s) | ${progress.readThreads} thread(s)`;
}
else if(this.phase === 'EXPORT') {
this.progressIndicator.update(progress.threadsProcessed);
}
}
/*
callback-function for export life cycle phases
available phases:
START - start of process
STOP - end of process
ANALYSIS - folder/threads structure analysis
EXPORT - export
*/
phaseFunc(phase, prevPhase) {
this.phase = phase;
if(phase === 'START') {
process.stdout.write(colors.gray(`Quip API: ${this.quipProcessor.quipService.apiURL}`));
process.stdout.write('\n');
}
if (phase === 'ANALYSIS'){
process.stdout.write('\n');
process.stdout.write(colors.cyan('Analysing folders...'));
process.stdout.write('\n');
this.spinnerIndicator.setSpinnerDelay(80);
this.spinnerIndicator.setSpinnerString("|/-\\");
this.spinnerIndicator.start();
}
if(prevPhase === 'ANALYSIS') {
this.spinnerIndicator.onTick(` read ${this.quipProcessor.foldersTotal} folder(s) | ${this.quipProcessor.threadsTotal} thread(s)`);
this.spinnerIndicator.stop();
process.stdout.write('\n');
}
if(phase === 'EXPORT') {
process.stdout.write('\n');
process.stdout.write(colors.cyan('Exporting...'));
process.stdout.write('\n');
this.progressIndicator.start(this.quipProcessor.threadsTotal, 0);
}
if(prevPhase === 'EXPORT') {
this.progressIndicator.stop();
process.stdout.write('\n');
}
}
//main entry point
async main() {
console.log();
const versionInfo = await utils.getVersionInfo();
//cli arguments parsing and validation
try {
this.cliArguments = CliArguments();
} catch (message) {
console.log(message);
return;
}
//current folder as destination, if not set
this.desinationFolder = (this.cliArguments.destination || process.cwd());
if(this.cliArguments.debug) {
this.Logger = new PinoLogger(PinoLogger.LEVELS.DEBUG, `${this.desinationFolder}/export.log`);
} else {
this.Logger = new PinoLogger(PinoLogger.LEVELS.INFO, `${this.desinationFolder}/export.log`);
}
console.log(`Quip-Export v${versionInfo.localVersion}`);
if(versionInfo.localOutdate) {
utils.cliBox(`!!!! A new version of Quip-Export (v${versionInfo.remoteVersion}) is available.`);
}
if(this.cliArguments['comments'] && this.cliArguments['docx']) {
console.log('Docx export: comments option will be ignored.');
}
//Token verification
const quipService = new QuipService(this.cliArguments.token);
quipService.setLogger(this.Logger);
if(!await quipService.checkUser()) {
console.log(colors.red('ERROR: Token is wrong or expired.'));
return;
}
console.log(`Destination folder: ${this.desinationFolder}`);
//activate zip
if(this.cliArguments.zip) {
this.zip = new JSZip();
}
this.quipProcessor = new QuipProcessor(this.cliArguments.token, this.fileSaver.bind(this), this.progressFunc.bind(this), this.phaseFunc.bind(this),
{
documentTemplate,
documentCSS: this.cliArguments['embedded-styles']? documentCSS : '',
embeddedImages: this.cliArguments['embedded-images'],
comments: this.cliArguments['comments'],
docx: this.cliArguments['docx']
});
this.quipProcessor.setLogger(this.Logger);
if(!this.cliArguments['embedded-styles'] && !this.cliArguments['docx']) {
if(this.cliArguments.zip) {
this.zip.file('document.css', documentCSS);
} else {
utils.writeTextFile(path.join(this.desinationFolder, "quip-export", 'document.css'), documentCSS);
}
}
let foldersToExport = [
//'FOLDER-1'
//'FOLDER-2'
//'EVZAOAW2e6U',
//'UPWAOAAEpFn', //Test
//'bGGAOAKTL4Y' //Test/folder1
//'EJCAOAdY90Y', // Design patterns
//'NBaAOAhFXJJ' //React
];
if(this.cliArguments['folders']) {
foldersToExport = this.cliArguments['folders'];
}
const startTime = new Date().getTime();
await this.quipProcessor.startExport(foldersToExport);
const durationStr = moment.utc(new Date().getTime() - startTime).format("HH:mm:ss");
this.Logger.debug(this.quipProcessor.quipService.stats);
this.Logger.debug(`Export duration: ${durationStr}`);
console.log(`Export duration: ${durationStr}`);
if(this.cliArguments.zip) {
//save zip file
const content = await this.zip.generateAsync({type: "nodebuffer", compression: "DEFLATE"});
await fs.writeFile(path.join(this.desinationFolder, 'quip-export.zip'), content, () => {
console.log("Zip-file has been saved: ", path.join(this.desinationFolder, 'quip-export.zip'));
});
}
}
}
module.exports = {App, documentTemplate, documentCSS};