-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcli.ts
153 lines (134 loc) · 4.46 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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.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((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 = await prompt(passwordQuestion);
const password: string = answers['password'];
return password;
};
const run = async function (cliArgs: any): Promise<void> {
clearConsole();
console.log(chalk.underline('ActionableAgile Extraction Tool'));
console.log('JIRA Extractor configuring...');
// Parse CLI settings
const jiraConfigPath: string = cliArgs.i ? cliArgs.i : defaultYamlPath;
const isLegacyYaml: boolean = (cliArgs.l || cliArgs.legacy) ? true : false;
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;
const outputType: string = outputPath.split('.')[1].toUpperCase();
if (outputType !== 'CSV' && outputType !== 'JSON') {
throw new Error('Only CSV and JSON is currently supported for file output.');
}
// Parse YAML settings
let settings: any = {};
try {
let yamlConfig = safeLoad(fs.readFileSync(jiraConfigPath, 'utf8'));
settings = yamlConfig;
settings.legacy = isLegacyYaml;
} 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['Feature Flags']) {
console.log('Feature Flags detected:');
for (let featureName in settings['Feature Flags']) {
console.log(` ${featureName}: ${settings['Feature Flags'][featureName] ? chalk.green('ON') : chalk.red('OFF')}`);
}
console.log('');
}
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('Authenticating...');
const isAuthenticated = await jiraExtractor.testConnection();
if (!isAuthenticated) {
throw new Error('Unable to authenticate. Please check your provided credentials.');
}
console.log(chalk.green('Authentication successful.\n'));
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 = '';
if (outputType === 'CSV') {
data = await jiraExtractor.toCSV(workItems);
} else if (outputType === 'JSON') {
console.error('JSON not currently supported');
}
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) {
throw e;
}
};
(async function (args: any): Promise<void> {
try {
await run(args);
} catch (e) {
console.log('');
console.log(chalk.red(`Error running ActionableAgile command line tool. Please see error below.`));
console.log(chalk.red(e));
}
} (getArgs()));