1 | const sh = require( 'shelljs' );
|
2 | const fs = require( 'fs' );
|
3 | const commandExists = require( 'command-exists' );
|
4 | const app = require( './app' );
|
5 |
|
6 | const dnsmasq = {
|
7 |
|
8 | |
9 |
|
10 |
|
11 | async setup() {
|
12 |
|
13 | try {
|
14 | await commandExists( 'brew' );
|
15 | } catch ( err ) {
|
16 | sh.exec( '/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"' );
|
17 | }
|
18 |
|
19 |
|
20 | const dnsmasqExists = sh.exec( 'if [ $(brew list | grep -c "dnsmasq") -eq 1 ]; then printf true; else printf false; fi', { silent: true }).stdout;
|
21 | if ( dnsmasqExists === 'false' ) {
|
22 | sh.exec( 'brew install dnsmasq' );
|
23 | }
|
24 |
|
25 |
|
26 | sh.exec( 'sudo mkdir -p /etc/resolver' );
|
27 | sh.exec( `sudo bash -c 'echo "nameserver 127.0.0.1" > ${app.resolver}'` );
|
28 |
|
29 |
|
30 | const dnsmasqconf = sh.exec( 'printf $(brew --prefix)/etc/dnsmasq.conf', { silent: true }).stdout;
|
31 | const reference = `conf-file=${app.dnsmasqconf}`;
|
32 | fs.writeFileSync( app.dnsmasqconf, 'listen-address=127.0.0.1\naddress=/.slab/127.0.0.1' );
|
33 |
|
34 | const content = fs.readFileSync( dnsmasqconf, 'utf8' );
|
35 |
|
36 |
|
37 | if ( content.indexOf( app.dnsmasqconf ) > -1 ) {
|
38 |
|
39 | } else {
|
40 | console.log( `Adding ${reference} to ${dnsmasqconf}` );
|
41 | fs.appendFileSync( dnsmasqconf, reference );
|
42 | }
|
43 | },
|
44 |
|
45 | };
|
46 |
|
47 | module.exports = dnsmasq;
|