Skip to content

Commit 24b89ff

Browse files
petermsouzajrPeter Souza
and
Peter Souza
authored
Main new postinstall (#86)
* Sync changes from main branch to demo (merged from ) * Sync changes from main branch to demo (merged from main branch to demo (merged ) * Sync changes from main branch to demo (merged from main branch to demo (merged ) * Update package.json (#80) * Update package.json * Update package.json * Sync changes from main branch to demo (merged from ) * Sync changes from main branch to demo (merged from main branch to demo (merged ) * added manual init post command * added manual init post command * added new files * updated version * updated version * Delete cypress-example/package-lock.json * Delete cypress-example/package.json * removed unused files * added file path * update install scripts * updated filepath * Delete package-lock.json --------- Co-authored-by: Peter Souza <peter@petermsouzajr.com>
1 parent 62917dd commit 24b89ff

File tree

6 files changed

+178
-8081
lines changed

6 files changed

+178
-8081
lines changed

cli.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { main } from './src/index.js';
55
import { handleDailyReport } from './src/sharedMethods/dailyReportHandler.js';
66
import { handleSummary } from './src/sharedMethods/summaryHandler.js';
77
import { spawn } from 'child_process';
8+
import { GOOGLE_KEYFILE_PATH, GOOGLE_SHEET_ID } from './constants.js';
89

910
/**
1011
* Main execution function for the command-line interface.
@@ -74,15 +75,36 @@ async function run() {
7475
process.exit(0);
7576
}
7677

78+
if (GOOGLE_KEYFILE_PATH() === false || GOOGLE_SHEET_ID() === false) {
79+
// If the Google Sheets configuration is missing, default to CSV
80+
console.info(
81+
chalk.yellow(
82+
"You haven't set up a Google Sheets config yet. Use the command"
83+
),
84+
chalk.green('qasr-setup'),
85+
chalk.yellow(
86+
'to create a config file. We can create a CSV report instead.'
87+
)
88+
);
89+
optionsPayload.csv = true;
90+
}
91+
7792
if (openWizard || !isConfigured) {
7893
// Execute the postinstall.js script
79-
const child = spawn(
80-
'node',
81-
['./node_modules/qa-shadow-report/scripts/postinstall.js'],
82-
{
83-
stdio: 'inherit', // inherit stdio to allow interactive input/output
84-
}
85-
);
94+
const child = spawn('node', ['./scripts/postinstall.js'], {
95+
stdio: 'inherit', // inherit stdio to allow interactive input/output
96+
});
97+
98+
child.on('close', (code) => {
99+
console.log(`postinstall.js process exited with code ${code}`);
100+
process.exit(code);
101+
});
102+
103+
// Ensure the parent script does not continue
104+
child.on('error', (err) => {
105+
console.error('Failed to start postinstall.js:', err);
106+
process.exit(1);
107+
});
86108
} else if (!framework) {
87109
console.error(
88110
chalk.yellow(

constants.js

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
import chalk from 'chalk';
22
import fs from 'fs';
3-
import path, { dirname } from 'path';
4-
import { fileURLToPath, pathToFileURL } from 'url';
3+
import path from 'path';
4+
import { pathToFileURL } from 'url';
55

66
// Since __dirname is not available in ES modules, we need to derive it
7-
const __filename = fileURLToPath(import.meta.url);
8-
const __dirname = dirname(__filename);
7+
const __filename = new URL(import.meta.url).pathname;
8+
const __dirname = path.dirname(__filename);
99

10-
// Start the search from the parent directory of the current __dirname
11-
const parentDir = dirname(dirname(__dirname));
10+
// Function to find the project root directory
11+
const findProjectRoot = (startPath) => {
12+
let currentDir = startPath;
13+
14+
while (currentDir !== path.parse(currentDir).root) {
15+
const packageJsonPath = path.join(currentDir, 'package.json');
16+
17+
if (fs.existsSync(packageJsonPath)) {
18+
// Check if the current directory is inside a node_modules directory
19+
if (!currentDir.includes(path.sep + 'node_modules' + path.sep)) {
20+
return currentDir;
21+
}
22+
}
23+
24+
currentDir = path.dirname(currentDir);
25+
}
26+
27+
return null;
28+
};
1229

1330
// Function to find the config file
1431
const findConfigFile = (startPath, baseFileName) => {
@@ -35,25 +52,37 @@ const getConfigPathFromArgs = () => {
3552
return configIndex > 0 ? args[configIndex] : null;
3653
};
3754

55+
// Determine the project root path
56+
const projectRootPath = findProjectRoot(__dirname);
57+
58+
if (!projectRootPath) {
59+
console.error('Error: Could not determine the project root path.');
60+
process.exit(1);
61+
}
62+
3863
const defaultConfigPath = path.join(__dirname, 'shadowReportConfig.js');
3964
const absoluteDefaultConfigPath = pathToFileURL(defaultConfigPath).href;
4065

4166
// Main logic to determine config path
4267
const configPath =
4368
getConfigPathFromArgs() ||
44-
findConfigFile(parentDir, 'shadowReportConfig') ||
69+
findConfigFile(projectRootPath, 'shadowReportConfig') ||
4570
absoluteDefaultConfigPath;
4671

47-
if (!configPath) {
72+
let shadowConfigDetails = {};
73+
74+
try {
75+
if (fs.existsSync(configPath)) {
76+
const shadowConfig = await import(pathToFileURL(configPath).href);
77+
shadowConfigDetails = shadowConfig.default;
78+
}
79+
} catch (error) {
4880
console.error(
49-
'Please specify the path to the shadowReportConfig.js file or ensure it exists in the project root.'
81+
chalk.red(`Error loading configuration file at path: ${configPath}`)
5082
);
51-
process.exit(1);
83+
console.error(chalk.red(error));
5284
}
5385

54-
const shadowConfig = await import(configPath);
55-
const shadowConfigDetails = shadowConfig.default;
56-
5786
// Define the formula keys for daily report header metrics.
5887
export const FORMULA_KEYS = [
5988
'formula tests passed',
@@ -63,13 +92,26 @@ export const FORMULA_KEYS = [
6392
];
6493

6594
export const GOOGLE_SHEET_ID = () => {
66-
const sheetId =
67-
shadowConfigDetails && shadowConfigDetails.googleSpreadsheetId
68-
? shadowConfigDetails.googleSpreadsheetId
69-
: '6d567d67576fgd76dfg76dghfg';
95+
let sheetId;
96+
97+
if (shadowConfigDetails && shadowConfigDetails.googleSpreadsheetId) {
98+
sheetId = shadowConfigDetails.googleSpreadsheetId;
99+
} else {
100+
sheetId = false;
101+
}
70102
return sheetId;
71103
};
72104

105+
export const GOOGLE_KEYFILE_PATH = () => {
106+
let keyFilePath;
107+
if (shadowConfigDetails && shadowConfigDetails.googleKeyFilePath) {
108+
keyFilePath = shadowConfigDetails.googleKeyFilePath;
109+
} else {
110+
keyFilePath = false;
111+
}
112+
return keyFilePath;
113+
};
114+
73115
export const TEST_DATA = (cypress) => {
74116
const testData =
75117
shadowConfigDetails && shadowConfigDetails.testData

0 commit comments

Comments
 (0)