diff --git a/package.json b/package.json index 45daa218..24168001 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dependencies": { "autoprefixer": "6.7.2", "babili": "0.0.10", + "bonjour": "^3.5.0", "chalk": "1.1.3", "chokidar": "1.6.1", "clean-css": "3.4.24", diff --git a/src/dev-server/bonjour.ts b/src/dev-server/bonjour.ts new file mode 100644 index 00000000..e30f4f59 --- /dev/null +++ b/src/dev-server/bonjour.ts @@ -0,0 +1,30 @@ +import { Logger } from '../logger/logger'; +import { getProjectJson } from '../util/ionic-project'; +import { ServeConfig } from './serve-config'; + +export function createBonjourService(config: ServeConfig) { + if (!config.bonjour) { + return; + } + getProjectJson() + .then(project => project.name) + .catch(() => 'ionic-app-scripts') + .then(projectName => { + Logger.info(`publishing bonjour service`); + + const bonjour = require('bonjour')(); + bonjour.publish({ + name: projectName, + type: 'ionicdev', + port: config.httpPort + }); + + const unpublish = function () { + bonjour.unpublishAll(); + bonjour.destroy(); + }; + + process.on('exit', unpublish); + process.on('SIGINT', unpublish); + }); +} diff --git a/src/dev-server/serve-config.ts b/src/dev-server/serve-config.ts index 2dd8b49f..4b467a0a 100644 --- a/src/dev-server/serve-config.ts +++ b/src/dev-server/serve-config.ts @@ -17,6 +17,7 @@ export interface ServeConfig { useServerLogs: boolean; notifyOnConsoleLog: boolean; useProxy: boolean; + bonjour: boolean; } export const LOGGER_DIR = '__ion-dev-server'; export const IONIC_LAB_URL = '/ionic-lab'; diff --git a/src/serve.spec.ts b/src/serve.spec.ts index de568436..da8f3505 100644 --- a/src/serve.spec.ts +++ b/src/serve.spec.ts @@ -37,7 +37,8 @@ describe('test serve', () => { notificationPort: 53703, useServerLogs: false, useProxy: true, - notifyOnConsoleLog: false + notifyOnConsoleLog: false, + bonjour: false }; spyOn(network, 'findClosestOpenPorts').and.callFake((host: string, ports: number[]) => Promise.resolve(ports)); spyOn(notificationServer, 'createNotificationServer'); diff --git a/src/serve.ts b/src/serve.ts index eaeba310..4b36d602 100644 --- a/src/serve.ts +++ b/src/serve.ts @@ -8,6 +8,7 @@ import open from './util/open'; import { createNotificationServer } from './dev-server/notification-server'; import { createHttpServer } from './dev-server/http-server'; import { createLiveReloadServer } from './dev-server/live-reload'; +import { createBonjourService } from './dev-server/bonjour'; import { ServeConfig, IONIC_LAB_URL } from './dev-server/serve-config'; import { findClosestOpenPorts } from './util/network'; @@ -45,7 +46,8 @@ export function serve(context: BuildContext) { notificationPort: notificationPortFound, useServerLogs: useServerLogs(context), useProxy: useProxy(context), - notifyOnConsoleLog: sendClientConsoleLogs(context) + notifyOnConsoleLog: sendClientConsoleLogs(context), + bonjour: useBonjour(context) }; createNotificationServer(config); @@ -55,6 +57,7 @@ export function serve(context: BuildContext) { return watch(context); }) .then(() => { + createBonjourService(config); onReady(config, context); return config; }, (err: BuildError) => { @@ -139,6 +142,10 @@ function browserOption(context: BuildContext): string { return getConfigValue(context, '--browseroption', '-o', 'IONIC_BROWSEROPTION', 'ionic_browseroption', null); } +function useBonjour(context: BuildContext): boolean { + return hasConfigValue(context, '--bonjour', '-B', 'bonjour', false); +} + function launchLab(context: BuildContext): boolean { return hasConfigValue(context, '--lab', '-l', 'ionic_lab', false); }