Skip to content

Commit

Permalink
Add Windows hosts file detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lithrel authored and comzeradd committed May 8, 2024
1 parent a1b1eba commit 5be4b76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules/*
.wp-env.*.json
junit.xml
hosts.backup
hosts.windows.backup
31 changes: 30 additions & 1 deletion scripts/lib/hosts.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
const os = require('node:os');
const {readFileSync} = require('fs');
const {run} = require('./run');

function isWsl() {
return os.release().toLowerCase().includes('microsoft');
}

function configureWindowsHosts() {
const hostsFile = '/mnt/c/Windows/System32/drivers/etc/hosts';
try {
const data = readFileSync(hostsFile);

if (data.includes('www.planet4.test')) {
console.log('Windows hosts file is already configured.');
}
else {
run(`cp ${hostsFile} hosts.windows.backup`);

console.log('Your Windows hosts file has been backed up to hosts.windows.backup');
console.log(`May require sudo password to configure the ${hostsFile} file.`);

run(`echo \"\n# Planet 4 local development environment\n127.0.0.1\twww.planet4.test\" | sudo tee -a ${hostsFile}`);
}
} catch (e) {
console.log('\nWSL is detected, but Windows hosts file is not accessible, please be sure to add Planet4 configuration to it:');
console.log('127.0.0.1\twww.planet4.test');
}
}

function configureHosts () {
const data = readFileSync('/etc/hosts');

if (data.includes('www.planet4.test')) {
console.log('Hosts file is already configured.');
process.exit(0);
} else {
run('cp /etc/hosts hosts.backup');

Expand All @@ -16,6 +41,10 @@ function configureHosts () {

run('echo \"\n# Planet 4 local development environment\n127.0.0.1\twww.planet4.test\" | sudo tee -a /etc/hosts');
}

if (isWsl()) {
configureWindowsHosts();
}
}

module.exports = {configureHosts};

0 comments on commit 5be4b76

Please sign in to comment.