-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
129 lines (111 loc) · 3.44 KB
/
cli.ts
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
import * as fs from 'fs';
import * as ProgressBar from 'progress';
import * as chalk from 'chalk';
import { safeLoad } from 'js-yaml';
import { argv } from 'yargs';
import { prompt } from 'inquirer';
import { JiraExtractor } from './index';
import { convertYamlToJiraSettings } from './src/components/yaml-converter';
const defaultYamlPath = 'config.yaml';
const defaultOutputPath = `output${new Date().getTime()}.csv`;
const bar = new ProgressBar(chalk.cyan(' Extracting: [:bar] :percent | :eta seconds remaining'), {
complete: '=',
incomplete: ' ',
width: 20,
total: 100,
});
const getArgs = () => argv;
const clearConsole = (): boolean => process.stdout.write('\x1Bc');
const writeFile = (filePath: string, data: any) =>
new Promise<void>((accept, reject) => {
fs.writeFile(filePath, data, (err => {
if (err) {
console.log(`Error writing file to ${filePath}`);
return reject(err);
}
accept();
}));
});
const getPassword = async (): Promise<string> => {
const passwordQuestion = {
message: 'Enter your JIRA password: ',
type: 'password',
name: 'password'
};
const answers: {[index: string]:any} = await prompt(passwordQuestion);
const password: string = answers['password'] as string;
return password;
};
const run = async function (cliArgs: any): Promise<void> {
clearConsole();
console.log(chalk.underline('Jira data extractor tool'));
console.log('JIRA Extractor configuring...');
// Parse CLI settings
const jiraConfigPath: string = cliArgs.i ? cliArgs.i : defaultYamlPath;
const debugMode: boolean = cliArgs.d ? true : false;
const outputPath: string = cliArgs.o ? cliArgs.o : defaultOutputPath;
const cliUsername: string = cliArgs.u;
const cliPassword: string = cliArgs.p;
// Parse YAML settings
let settings: any = {};
try {
let yamlConfig = safeLoad(fs.readFileSync(jiraConfigPath, 'utf8'));
settings = yamlConfig;
} catch (e) {
console.log(`Error parsing settings ${e}`);
throw e;
}
if (cliUsername) {
settings.Connection.Username = cliUsername;
}
if (cliPassword) {
settings.Connection.Password = cliPassword;
}
console.log('');
if (debugMode) {
console.log(`Debug mode: ${chalk.green('ON') }`);
}
if (!settings.Connection.Password && !settings.Connection.Token) {
const password = await getPassword();
settings.Connection.Password = password;
console.log('');
}
// Import data
const jiraExtractorConfig = convertYamlToJiraSettings(settings);
const jiraExtractor = new JiraExtractor(jiraExtractorConfig);
console.log('Beginning extraction process');
// Progress bar setup
const updateProgressHook = (bar => {
bar.tick();
return (percentDone: number) => {
if (percentDone <= 100) {
bar.tick(percentDone);
}
};
})(bar);
try {
const workItems = await jiraExtractor.extractAll(updateProgressHook, debugMode);
// Export data
let data: string = '';
data = await jiraExtractor.toCSV(workItems);
try {
await writeFile(outputPath, data);
} catch (e) {
console.log(`Error writing jira data to ${outputPath}`);
}
console.log(chalk.green('Successful.'));
console.log(`Results written to ${outputPath}`);
return;
} catch (e) {
console.log(e?.stack);
throw e;
}
};
(async function (args: any): Promise<void> {
try {
await run(args);
} catch (e) {
console.log('');
console.log(chalk.red(e));
}
} (getArgs()));