From 034f6910fb6244ff55573a38bdb495cd96f437a4 Mon Sep 17 00:00:00 2001 From: brunosimon Date: Sun, 24 Dec 2017 13:18:40 +0100 Subject: [PATCH] :art: Move opener to the watcher class --- lib/index.js | 27 +-------------------------- lib/watcher.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/lib/index.js b/lib/index.js index bc263a2..dad2933 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,7 +6,6 @@ const Projects = require('./projects.js') const socketIo = require('socket.io') const util = require('util') const colors = require('colors') -const opener = require('opener') const fs = require('fs') const path = require('path') const packageJson = require('../package.json') @@ -34,12 +33,6 @@ class App this.setServer() } - // If host is specified, show URL open in browser - if(this.config.host) - { - this.showUrlAndOpen() - } - // If not started as a server // Start watching files if(!this.config.server) @@ -239,11 +232,7 @@ class App console.log(util.inspect(project.files.describe(), { depth: null, colors: true })) } - // If not server, show URL open in browser - if(!this.config.server) - { - this.showUrlAndOpen() - } + socket.emit('started') }) // Update file @@ -327,20 +316,6 @@ class App { this.watcher = new Watcher(this.config) } - - /** - * Show URL and open it if should - */ - showUrlAndOpen() - { - const url = this.config.domain - console.log('app'.green.bold + ' - ' + url.yellow) - - if(this.config.open) - { - opener(url) - } - } } module.exports = App diff --git a/lib/watcher.js b/lib/watcher.js index 308bd6d..a5791c4 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -3,6 +3,7 @@ const chokidar = require('chokidar') const fs = require('fs') const mime = require('mime') const socketIoClient = require('socket.io-client') +const opener = require('opener') /** * Watcher class @@ -51,6 +52,18 @@ class Watcher console.log('watcher > socket'.green.bold + ' - ' + 'disconnect'.cyan) } }) + + // Started event + this.socket.on('started', () => + { + const url = this.config.domain + console.log('app'.green.bold + ' - ' + url.yellow) + + if(this.config.open) + { + opener(url) + } + }) } setExcludeRegex()