Skip to content

Commit 2bcc1e7

Browse files
author
Myron van Velsen
committed
Merge branch '202-voeg-automatisch-playwright-regels-toe-aan-gitignore-bij-installatie-van-de-testing-suite' into 'main'
Resolve "Voeg automatisch Playwright-regels toe aan .gitignore bij installatie van de testing suite" See merge request elgentos/magento2-playwright!30
2 parents 76075f3 + 64d5d1f commit 2bcc1e7

File tree

2 files changed

+136
-57
lines changed

2 files changed

+136
-57
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ node_modules/
1111
#files
1212
/*.config.ts
1313
/.env
14-
/tsconfig.json
14+
/tsconfig.json

install.js

Lines changed: 135 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,145 @@ const path = require('path');
55
const readline = require('readline');
66
const { execSync } = require('child_process');
77

8-
const rl = readline.createInterface({
9-
input: process.stdin,
10-
output: process.stdout
11-
});
12-
13-
const question = (query) => new Promise((resolve) => rl.question(query, resolve));
14-
15-
async function install() {
16-
17-
// Get current user
18-
const currentUser = execSync('whoami').toString().trim();
19-
20-
// Environment variables with defaults
21-
const envVars = {
22-
'PLAYWRIGHT_BASE_URL': { default: 'https://hyva-demo.elgentos.io/' },
23-
'PLAYWRIGHT_PRODUCTION_URL': { default: 'https://hyva-demo.elgentos.io/' },
24-
'PLAYWRIGHT_REVIEW_URL': { default: 'https://hyva-demo.elgentos.io/' },
25-
26-
'MAGENTO_ADMIN_SLUG': { default: 'admin' },
27-
'MAGENTO_ADMIN_USERNAME': { default: currentUser },
28-
'MAGENTO_ADMIN_PASSWORD': { default: 'Test1234!' },
29-
'MAGENTO_THEME_LOCALE': { default: 'nl_NL' },
30-
31-
'MAGENTO_NEW_ACCOUNT_PASSWORD': { default: 'NewTest1234!' },
32-
'MAGENTO_EXISTING_ACCOUNT_EMAIL_CHROMIUM': { default: '[email protected]' },
33-
'MAGENTO_EXISTING_ACCOUNT_EMAIL_FIREFOX': { default: '[email protected]' },
34-
'MAGENTO_EXISTING_ACCOUNT_EMAIL_WEBKIT': { default: '[email protected]' },
35-
'MAGENTO_EXISTING_ACCOUNT_PASSWORD': { default: 'Test1234!' },
36-
'MAGENTO_EXISTING_ACCOUNT_CHANGED_PASSWORD': { default: 'AanpassenKan@0212' },
37-
38-
'MAGENTO_COUPON_CODE_CHROMIUM': { default: 'CHROMIUM321' },
39-
'MAGENTO_COUPON_CODE_FIREFOX': { default: 'FIREFOX321' },
40-
'MAGENTO_COUPON_CODE_WEBKIT': { default: 'WEBKIT321' }
41-
};
42-
43-
// Read and update .env file
44-
const envPath = path.join('.env');
45-
let envContent = '';
46-
47-
// for (const [key, value] of Object.entries(envVars)) {
48-
// const userInput = await question(`Enter ${ key } (default: ${ value.default }): `);
49-
// envContent += `${ key }=${ userInput || value.default }\n`;
50-
// }
51-
52-
const isCI = process.env.CI === 'true';
53-
for (const [key, value] of Object.entries(envVars)) {
54-
let userInput = '';
55-
if (!isCI) {
56-
userInput = await question(`Enter ${ key } (default: ${ value.default }): `);
8+
class Install {
9+
10+
rl = '';
11+
currentUser = '';
12+
isCi = false;
13+
useDefaults = false;
14+
pathToMagentoRoot = '../../../../../../../../../../'; // default: when installed via npm
15+
envVars = {};
16+
17+
rulesToAddToIgnore = [
18+
'# playwright',
19+
'/app/design/frontend/<vendor>/<theme>/web/playwright/*',
20+
'!/app/design/frontend/<vendor>/<theme>/web/playwright/tests/',
21+
'!/app/design/frontend/<vendor>/<theme>/web/playwright/package.json',
22+
'!/app/design/frontend/<vendor>/<theme>/web/playwright/package-lock.json'
23+
]
24+
25+
constructor() {
26+
this.useDefaults = true
27+
this.isCi = process.env.CI === 'true';
28+
this.currentUser = execSync('whoami').toString().trim();
29+
const isLocalDev = fs.existsSync(path.resolve(__dirname, '.git'));
30+
31+
if (isLocalDev) {
32+
this.pathToMagentoRoot = './'; // we're in the root of the dev repo
5733
}
58-
envContent += `${ key }=${ userInput || value.default }\n`;
34+
35+
this.envVars = {
36+
'PLAYWRIGHT_BASE_URL': { default: 'https://hyva-demo.elgentos.io/' },
37+
'PLAYWRIGHT_PRODUCTION_URL': { default: 'https://hyva-demo.elgentos.io/' },
38+
'PLAYWRIGHT_REVIEW_URL': { default: 'https://hyva-demo.elgentos.io/' },
39+
'MAGENTO_ADMIN_SLUG': { default: 'admin' },
40+
'MAGENTO_ADMIN_USERNAME': { default: this.currentUser },
41+
'MAGENTO_ADMIN_PASSWORD': { default: 'Test1234!' },
42+
'MAGENTO_THEME_LOCALE': { default: 'nl_NL' },
43+
'MAGENTO_NEW_ACCOUNT_PASSWORD': { default: 'NewTest1234!' },
44+
'MAGENTO_EXISTING_ACCOUNT_EMAIL_CHROMIUM': { default: '[email protected]' },
45+
'MAGENTO_EXISTING_ACCOUNT_EMAIL_FIREFOX': { default: '[email protected]' },
46+
'MAGENTO_EXISTING_ACCOUNT_EMAIL_WEBKIT': { default: '[email protected]' },
47+
'MAGENTO_EXISTING_ACCOUNT_PASSWORD': { default: 'Test1234!' },
48+
'MAGENTO_EXISTING_ACCOUNT_CHANGED_PASSWORD': { default: 'AanpassenKan@0212' },
49+
'MAGENTO_COUPON_CODE_CHROMIUM': { default: 'CHROMIUM321' },
50+
'MAGENTO_COUPON_CODE_FIREFOX': { default: 'FIREFOX321' },
51+
'MAGENTO_COUPON_CODE_WEBKIT': { default: 'WEBKIT321' }
52+
}
53+
54+
55+
this.rl = readline.createInterface({
56+
input: process.stdin,
57+
output: process.stdout
58+
});
59+
60+
this.init();
61+
}
62+
63+
async init() {
64+
65+
console.log(this.envVars);
66+
67+
68+
// Check if user
69+
if (!this.isCi) {
70+
const initialAnswer = await this.askQuestion('Do you want to customize environment variables? (y/N): ');
71+
this.useDefaults = initialAnswer.trim().toLowerCase() !== 'y';
72+
}
73+
74+
await this.appendToGitIgnore();
75+
await this.setEnvVariables();
5976
}
6077

61-
fs.writeFileSync(envPath, envContent);
78+
async askQuestion(query) {
79+
return new Promise((resolve) => this.rl.question(query, resolve))
80+
}
81+
82+
async setEnvVariables() {
83+
// Read and update .env file
84+
const envPath = path.join('.env');
85+
let envContent = '';
86+
87+
for (const [key, value] of Object.entries(this.envVars)) {
88+
let userInput = '';
89+
if (!this.isCi && !this.useDefaults) {
90+
userInput = await this.askQuestion(`Enter ${ key } (default: ${ value.default }): `);
91+
}
92+
envContent += `${ key }=${ userInput || value.default }\n`;
93+
}
94+
95+
fs.writeFileSync(envPath, envContent);
6296

63-
console.log('\nInstallation completed successfully!');
64-
console.log('\nFor more information, please visit:');
65-
console.log('https://wiki.elgentos.nl/doc/stappenplan-testing-suite-implementeren-voor-klanten-hCGe4hVQvN');
97+
console.log('\nInstallation completed successfully!');
98+
console.log('\nFor more information, please visit:');
99+
console.log('https://wiki.elgentos.nl/doc/stappenplan-testing-suite-implementeren-voor-klanten-hCGe4hVQvN');
66100

67-
rl.close();
101+
this.rl.close();
102+
}
103+
104+
async appendToGitIgnore() {
105+
console.log('Checking .gitignore and adding lines if necessary...');
106+
107+
const gitignorePath = path.join(this.pathToMagentoRoot, '.gitignore');
108+
109+
// Read existing content if file exists
110+
let existingLines = [];
111+
if (fs.existsSync(gitignorePath)) {
112+
const content = fs.readFileSync(gitignorePath, 'utf-8');
113+
existingLines = content.split(/\r?\n/);
114+
}
115+
116+
// Get vendor and theme
117+
const { vendor, theme } = await this.setVendorAndTheme(__dirname);
118+
119+
// Append missing lines
120+
let updated = false;
121+
for (let line of this.rulesToAddToIgnore) {
122+
// Replace placeholders with actual values
123+
line = line.replace('<vendor>', vendor).replace('<theme>', theme);
124+
125+
if (!existingLines.includes(line)) {
126+
existingLines.push(line);
127+
updated = true;
128+
}
129+
}
130+
131+
// Write back if updated
132+
if (updated) {
133+
fs.writeFileSync(gitignorePath, existingLines.join('\n'), 'utf-8');
134+
console.log('.gitignore updated.');
135+
} else {
136+
console.log('.gitignore already contains all required lines.');
137+
}
138+
}
139+
140+
async setVendorAndTheme() {
141+
// Ask user for input if path structure is invalid
142+
const vendor = await this.askQuestion('Enter the vendor name: ');
143+
const theme = await this.askQuestion('Enter the theme name: ');
144+
145+
return { vendor, theme };
146+
}
68147
}
69148

70-
install();
149+
new Install();

0 commit comments

Comments
 (0)