-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·165 lines (147 loc) · 5.53 KB
/
cli.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
#!/usr/bin/env node
import chalk from 'chalk';
import { isProjectConfigured } from './scripts/configuredStatus.js';
import { main } from './src/index.js';
import { handleDailyReport } from './src/sharedMethods/dailyReportHandler.js';
import { handleSummary } from './src/sharedMethods/summaryHandler.js';
import { spawn } from 'child_process';
import { GOOGLE_KEYFILE_PATH, GOOGLE_SHEET_ID } from './constants.js';
import path from 'path';
import { fileURLToPath } from 'url';
/**
* Resolves the path to the postInstall.js script.
* @returns {string} The resolved path to the postInstall.js script.
*/
const resolvePostInstallScript = () => {
try {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const postInstallPath = path.join(__dirname, 'scripts', 'postInstall.js');
return postInstallPath;
} catch (error) {
console.error('Error: Unable to resolve the path to postInstall.js.');
console.error(error);
process.exit(1);
}
};
/**
* Main execution function for the command-line interface.
* Processes command line arguments to determine the desired operation and configuration.
* Supports generating daily and monthly reports with optional CSV output and duplication.
*/
async function run() {
const args = process.argv.slice(2);
const commands = args.filter((arg) =>
['todays-report', 'monthly-summary'].includes(arg)
);
const command = commands[0] || ''; // Select the first command if multiple, though typically there should only be one
const isCSV = args.includes('--csv'); // Checks if the CSV output is requested
const isDuplicate = args.includes('--duplicate'); // Checks if duplication is requested
const frameworkArg = args.find((arg) =>
['cypress', 'playwright', 'cy', 'pw'].includes(arg)
);
const isConfigured = isProjectConfigured();
const framework = frameworkArg || ''; // Ensure the framework is correctly specified
const isCypress = framework === 'cypress' || framework === 'cy';
const isPlaywright = framework === 'playwright' || framework === 'pw';
const unsupportedCsvForMonthly = isCSV && command.includes('monthly-summary');
const unsupportedDuplicateCsvForMonthly =
isCSV && isDuplicate && command.includes('monthly-summary');
const optionsPayload = {
csv: isCSV,
duplicate: isDuplicate,
cypress: isCypress,
playwright: isPlaywright,
};
if (args.includes('--help')) {
console.log(`
Usage:
qa-shadow-report <framework> [command] [options]
Mandatory:
<framework> Specify the testing framework: cypress, playwright.
Commands (Optional):
todays-report Only generate today's report.
monthly-summary Only generate previous months summary.
Options (Optional):
--csv Output the report in CSV format.
--duplicate Create a duplicate report.
Setup:
qasr-setup Initiate the configuration.
Examples:
Generate today's report for Playwright in CSV format:
qa-shadow-report playwright todays-report --csv
Generate a daily report and monthly summary when necessary, in google sheets, for cypress:
qa-shadow-report cypress
Shortcuts:
qasr cy Equivalent to qa-shadow-report cypress
qasr pw Equivalent to qa-shadow-report playwright
For more details, visit our documentation: https://github.com/petermsouzajr/qa-shadow-report
`);
process.exit(0);
}
if ((!GOOGLE_KEYFILE_PATH() || GOOGLE_SHEET_ID() === false) && !isCSV) {
if (process.env.CI) {
console.info(chalk.yellow('CI environment detected.'));
process.exit(1); // Exit with failure in CI mode if the config is missing
} else {
// If the Google Sheets configuration is missing, default to CSV
console.info(
chalk.yellow(
"You haven't set up a Google Sheets config yet. Use the command"
),
chalk.green('qasr-setup'),
chalk.yellow(
'to create a config file. We can create a CSV report instead.'
)
);
optionsPayload.csv = true;
}
}
if (!isConfigured && !process.env.CI) {
const postInstallScriptPath = resolvePostInstallScript();
// Execute the postInstall.js script
const child = spawn('node', [postInstallScriptPath], {
stdio: 'inherit', // inherit stdio to allow interactive input/output
});
child.on('close', (code) => {
process.exit(code);
});
// Ensure the parent script does not continue
child.on('error', (err) => {
console.info('Failed to start postInstall.js:', err);
process.exit(1);
});
} else if (!framework) {
console.info(
chalk.yellow('Sheet not created. Please specify a framework:'),
chalk.green('cypress'),
chalk.yellow('or'),
chalk.green('playwright')
);
process.exit(1);
} else if (unsupportedCsvForMonthly || unsupportedDuplicateCsvForMonthly) {
console.info(
chalk.yellow(
'Error: CSV output for "monthly-summary" with or without duplication is not supported.'
)
);
process.exit(1);
} else {
try {
switch (command) {
case 'todays-report':
await handleDailyReport({ ...optionsPayload });
break;
case 'monthly-summary':
await handleSummary({ ...optionsPayload });
break;
default:
await main({ ...optionsPayload });
break;
}
} catch (error) {
console.error('Error executing command:', error);
}
}
}
run();