Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#947 - Fixed defect when opening a bot file on a mac #970

Merged
merged 2 commits into from Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/app/main/package.json
Expand Up @@ -65,7 +65,6 @@
"@types/formidable": "^1.0.30",
"@types/fs-extra": "^5.0.4",
"@types/jest": "^22.2.3",
"@types/jsonpath": "^0.2.0",
"@types/mkdirp": "^0.5.2",
"@types/node": "8.9.3",
"@types/request": "^2.47.0",
Expand Down Expand Up @@ -126,7 +125,6 @@
"got": "^7.1.0",
"http-status-codes": "^1.3.0",
"jest-fetch-mock": "^1.6.2",
"jsonpath": "^1.0.0",
"jsonwebtoken": "^8.3.0",
"lock": "^0.1.2",
"mkdirp": "^0.5.1",
Expand Down
18 changes: 9 additions & 9 deletions packages/app/main/src/appMenuBuilder.ts
Expand Up @@ -36,7 +36,6 @@ import * as Electron from 'electron';
import { mainWindow } from './main';
import { AppUpdater, UpdateStatus } from './appUpdater';
import { BotInfo, SharedConstants } from '@bfemulator/app-shared';
import * as jsonpath from 'jsonpath';
import { ConversationService } from './services/conversationService';
import { getStore as getSettingsStore } from './settingsData/store';
import { rememberTheme } from './settingsData/actions/windowStateActions';
Expand Down Expand Up @@ -121,12 +120,12 @@ export const AppMenuBuilder = new class AppMenuBuilderImpl implements AppMenuBui
click: () => {
mainWindow.commandService.remoteCall(Bot.OpenBrowse);
}
}];
} ];
if (recentBots && recentBots.length) {
const recentBotsList = this.createRecentBotsList(recentBots);
subMenu.push({
label: 'Open Recent...',
submenu: [...recentBotsList]
submenu: [ ...recentBotsList ]
});
} else {
subMenu.push({
Expand Down Expand Up @@ -364,8 +363,8 @@ export const AppMenuBuilder = new class AppMenuBuilderImpl implements AppMenuBui
const getConversationId = async () => {
const state = await getState();
const { editors, activeEditor } = state.editor;
const { activeDocumentId } = editors[activeEditor];
return state.chat.chats[activeDocumentId].conversationId;
const { activeDocumentId } = editors[ activeEditor ];
return state.chat.chats[ activeDocumentId ].conversationId;
};

const getServiceUrl = () => emulator.framework.serverUrl.replace('[::]', 'localhost');
Expand Down Expand Up @@ -421,16 +420,17 @@ export const AppMenuBuilder = new class AppMenuBuilderImpl implements AppMenuBui
*/
setFileMenu(fileMenuTemplate: MenuOpts, appMenuTemplate: MenuOpts[]): MenuOpts[] {
if (process.platform === 'darwin') {
appMenuTemplate[1] = fileMenuTemplate;
appMenuTemplate[ 1 ] = fileMenuTemplate;
} else {
appMenuTemplate[0] = fileMenuTemplate;
appMenuTemplate[ 0 ] = fileMenuTemplate;
}
return appMenuTemplate;
}

refreshAppUpdateMenu() {
jsonpath.value(this.menuTemplate,
'$..[?(@.role == "help")].submenu[?(@.id == "auto-update")]', this.getUpdateMenuItem());
const helpMenu = this.menuTemplate.find(menuItem => menuItem.role === 'help');
const autoUpdateMenuItem = (helpMenu.submenu as Array<any>).find(menuItem => menuItem.id === 'auto-update');
Object.assign(autoUpdateMenuItem, this.getUpdateMenuItem());
Electron.Menu.setApplicationMenu(Electron.Menu.buildFromTemplate(this.menuTemplate));
}
};
31 changes: 5 additions & 26 deletions packages/app/main/src/commands/clientInitCommands.ts
Expand Up @@ -38,13 +38,13 @@ import { ExtensionManagerImpl } from '../extensions';
import { Protocol } from '../constants';
import { ProtocolHandler } from '../protocolHandler';
import { getStore } from '../botData/store';
import { getBotsFromDisk, readFileSync } from '../utils';
import * as Path from 'path';
import { getBotsFromDisk } from '../utils';
import { CommandRegistryImpl } from '@bfemulator/sdk-shared';
import { ClientAwareSettings, Settings, SharedConstants } from '@bfemulator/app-shared';
import { Migrator } from '../migrator';
import { getStore as getSettingsStore } from '../settingsData/store';
import { Store } from 'redux';
import { openFileFromCommandLine } from '../utils/openFileFromCommandLine';

/** Registers client initialization commands */
export function registerCommands(commandRegistry: CommandRegistryImpl) {
Expand Down Expand Up @@ -97,30 +97,9 @@ export function registerCommands(commandRegistry: CommandRegistryImpl) {
}

// Parse command line args to see if we are opening a .bot or .transcript file
if (args.some(arg => /(\.transcript)|(\.bot)$/.test(arg))) {
const fileToBeOpened = args.find(arg => /(\.transcript)|(\.bot)$/.test(arg));
if (Path.extname(fileToBeOpened) === '.bot') {
try {
const bot = await mainWindow.commandService.call(Commands.Bot.Open, fileToBeOpened);
await mainWindow.commandService.call(Commands.Bot.SetActive, bot);
await mainWindow.commandService.remoteCall(Commands.Bot.Load, bot);
} catch (e) {
throw new Error(`Error while trying to open a .bot file via double click at: ${fileToBeOpened}`);
}
} else if (Path.extname(fileToBeOpened) === '.transcript') {
const transcript = readFileSync(fileToBeOpened);
const conversationActivities = JSON.parse(transcript);
if (!Array.isArray(conversationActivities)) {
throw new Error('Invalid transcript file contents; should be an array of conversation activities.');
}

// open a transcript on the client side and pass in
// some extra info to differentiate it from a transcript on disk
await mainWindow.commandService.remoteCall(Commands.Emulator.OpenTranscript, 'deepLinkedTranscript', {
activities: conversationActivities,
inMemory: true
});
}
const fileToBeOpened = args.find(arg => /(\.transcript)|(\.bot)$/.test(arg));
if (fileToBeOpened) {
await openFileFromCommandLine(fileToBeOpened, mainWindow.commandService);
}
});
}
23 changes: 19 additions & 4 deletions packages/app/main/src/main.ts
Expand Up @@ -57,6 +57,7 @@ import { azureLoggedInUserChanged } from './settingsData/actions/azureAuthAction
import { ngrokEmitter } from './ngrok';
import { sendNotificationToClient } from './utils/sendNotificationToClient';
import Users from '@bfemulator/emulator-core/lib/facility/users';
import { openFileFromCommandLine } from './utils/openFileFromCommandLine';

export let mainWindow: Window;
export let windowManager: WindowManager;
Expand Down Expand Up @@ -89,7 +90,7 @@ AppUpdater.on('update-available', (update: UpdateInfo) => {
mainWindow.commandService.call(SharedConstants.Commands.Electron.ShowMessageBox, true, {
title: app.getName(),
message: `An update is available. Download it now?`,
buttons: ['Cancel', 'OK'],
buttons: [ 'Cancel', 'OK' ],
defaultId: 1,
cancelId: 0
}).then(result => {
Expand All @@ -107,7 +108,7 @@ AppUpdater.on('update-downloaded', (update: UpdateInfo) => {
mainWindow.commandService.call(SharedConstants.Commands.Electron.ShowMessageBox, true, {
title: app.getName(),
message: 'Finished downloading update. Restart and install now?',
buttons: ['Cancel', 'OK'],
buttons: [ 'Cancel', 'OK' ],
defaultId: 1,
cancelId: 0
}).then(result => {
Expand Down Expand Up @@ -202,6 +203,15 @@ Electron.app.on('will-finish-launching', () => {
Electron.app.on('open-url', onOpenUrl);
});

let fileToOpen: string;
Electron.app.on('open-file', async (event: Event, file: string) => {
if (!mainWindow || !mainWindow.commandService) {
fileToOpen = file;
} else {
await openFileFromCommandLine(file, mainWindow.commandService);
}
});

const windowIsOffScreen = function (windowBounds: Electron.Rectangle): boolean {
const nearestDisplay = Electron.screen.getDisplayMatching(windowBounds).workArea;
return (
Expand Down Expand Up @@ -372,6 +382,11 @@ const createMainWindow = async () => {
await mainWindow.commandService.call(SharedConstants.Commands.Electron.UpdateFileMenu);
}
}

if (fileToOpen) {
await openFileFromCommandLine(fileToOpen, mainWindow.commandService);
fileToOpen = null;
}
});

mainWindow.browserWindow.on('close', async function (event: Event) {
Expand All @@ -387,9 +402,9 @@ const createMainWindow = async () => {

function loadMainPage() {
let queryString = '';
if (process.argv[1] && process.argv[1].indexOf('botemulator') !== -1) {
if (process.argv[ 1 ] && process.argv[ 1 ].indexOf('botemulator') !== -1) {
// add a query string with the botemulator protocol handler content
queryString = '?' + process.argv[1];
queryString = '?' + process.argv[ 1 ];
}

let page = process.env.ELECTRON_TARGET_URL || url.format({
Expand Down
31 changes: 31 additions & 0 deletions packages/app/main/src/utils/openFileFromCommandLine.ts
@@ -0,0 +1,31 @@
import { readFileSync } from './readFileSync';
import { CommandService } from '@bfemulator/sdk-shared';
import * as path from 'path';
import { SharedConstants } from '@bfemulator/app-shared';

export async function openFileFromCommandLine(fileToBeOpened: string, commandService: CommandService): Promise<void> {
const { Bot, Emulator } = SharedConstants.Commands;
if (path.extname(fileToBeOpened) === '.bot') {
try {

const bot = await commandService.call(Bot.Open, fileToBeOpened);
await commandService.call(Bot.SetActive, bot);
await commandService.remoteCall(Bot.Load, bot);
} catch (e) {
throw new Error(`Error while trying to open a .bot file via double click at: ${fileToBeOpened}`);
}
} else if (path.extname(fileToBeOpened) === '.transcript') {
const transcript = readFileSync(fileToBeOpened);
const conversationActivities = JSON.parse(transcript);
if (!Array.isArray(conversationActivities)) {
throw new Error('Invalid transcript file contents; should be an array of conversation activities.');
}

// open a transcript on the client side and pass in
// some extra info to differentiate it from a transcript on disk
await commandService.remoteCall(Emulator.OpenTranscript, 'deepLinkedTranscript', {
activities: conversationActivities,
inMemory: true
});
}
}