UNPKG

1.46 kBJavaScriptView Raw
1const sh = require( 'shelljs' );
2const fs = require( 'fs' );
3const commandExists = require( 'command-exists' );
4const app = require( './app' );
5
6const dnsmasq = {
7
8 /**
9 * Setup dnsmasq.
10 */
11 async setup() {
12 // Install brew if it does not exist.
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 // Install dnsmasq if it does not exist.
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 // Add slab's resolver.
26 sh.exec( 'sudo mkdir -p /etc/resolver' );
27 sh.exec( `sudo bash -c 'echo "nameserver 127.0.0.1" > ${app.resolver}'` );
28
29 // Create slab's dnsmasq.conf.
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 // Add reference to slab's dnsmasq.conf to dnsmasq.
37 if ( content.indexOf( app.dnsmasqconf ) > -1 ) {
38 // OK.
39 } else {
40 console.log( `Adding ${reference} to ${dnsmasqconf}` );
41 fs.appendFileSync( dnsmasqconf, reference );
42 }
43 },
44
45};
46
47module.exports = dnsmasq;