Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
feat(bonjour): adds service auto-discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat authored and adamdbradley committed May 4, 2017
1 parent d7a4d1e commit c17e6df
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions src/dev-server/bonjour.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
1 change: 1 addition & 0 deletions src/dev-server/serve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 8 additions & 1 deletion src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand All @@ -55,6 +57,7 @@ export function serve(context: BuildContext) {
return watch(context);
})
.then(() => {
createBonjourService(config);
onReady(config, context);
return config;
}, (err: BuildError) => {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c17e6df

Please sign in to comment.