Skip to content

Commit

Permalink
Merge branch 'staging' into feature/reopen-closed-tabs-999
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 2, 2019
2 parents f934795 + 25c4e9e commit 65c9c9d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "altair",
"productName": "Altair GraphQL Client",
"version": "2.3.7",
"license": "MIT",
"author": "Samuel Imolorhe <samuelimolo4real@gmail.com> (https://sirmuel.design/)",
Expand Down Expand Up @@ -116,7 +115,7 @@
"@types/chrome": "^0.0.90",
"@types/electron": "^1.6.10",
"@types/graphql": "^14.2.2",
"@types/jasmine": "3.4.1",
"@types/jasmine": "3.4.5",
"@types/mousetrap": "^1.6.3",
"@types/node": "^12.6.8",
"ajv-cli": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/altair-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "altair",
"productName": "Altair GraphQL Client",
"version": "2.3.7",
"description": "Electron wrapper for Altair",
"main": "index.js",
Expand Down
4 changes: 3 additions & 1 deletion packages/altair-electron/src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { app, protocol } = require('electron');
const { readFileSync, readFile } = require('fs');
const { readFile } = require('fs');
const isDev = require('electron-is-dev');
const { setupAutoUpdates } = require('./updates');
const { createWindow, getInstance } = require('./window');
const { getStore } = require('./store');
const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions');

// require('electron-debug')();
// try {
Expand Down Expand Up @@ -31,6 +32,7 @@ app.on('ready', () => {
if (!isDev) {
setupAutoUpdates();
}
// checkMultipleDataVersions();
});

// Quit when all windows are closed.
Expand Down
67 changes: 67 additions & 0 deletions packages/altair-electron/src/utils/check-multi-data-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { app, dialog } = require('electron');
const { resolve } = require('path');
const { renameSync } = require('fs');
const { getDirectoriesInDirectory, deleteFolderRecursive } = require('./index');

const userDataParentPath = resolve(app.getPath('userData'), '..');

const DIR_2_3_7 = 'altair';
const DIR_2_3_6 = 'altair-electron';
const DIR_ALL = 'Altair GraphQL Client';
const userDataVersions = [
DIR_2_3_6, //v2.3.6
DIR_2_3_7, //v2.3.7
DIR_ALL, // all others
];

const checkMultipleDataVersions = (win) => {
getDirectoriesInDirectory(userDataParentPath).then((directories) => {
const foundVersions = directories.filter(dir => userDataVersions.includes(dir));
if (foundVersions.length > 1) {
console.log(foundVersions);
dialog.showMessageBox(win, {
type: 'info',
buttons: [ 'This is the correct version', 'Clear this version', 'Remind me later' ],
defaultId: 0,
cancelId: 2,
title: 'Multiple versions found',
message: 'We found multiple versions of your Altair data',
detail: `Due to some misconfiguration that happened in some of the recent versions of Altair, there are several versions of your data. We want to help you keep the right data.\n\nTo do that, check if the current data is correct, and we will clear out the rest.`
}, (response) => {
switch(response) {
case 0: // Correct version
// Clear other data
foundVersions
.filter(v => v !== DIR_ALL)
.forEach(version => {
console.log('removing version', version);
// Delete DIR_2_3_6 and DIR_2_3_7
deleteFolderRecursive(resolve(userDataParentPath, version));
});
app.relaunch();
app.exit(0);
return;
case 1: // clear THIS version
// Clear THIS data
const nextVersion = foundVersions.find(v => v !== DIR_ALL);
if (nextVersion) {
console.log('next version', nextVersion);
// Delete DIR_ALL
deleteFolderRecursive(resolve(userDataParentPath, DIR_ALL));
// Move the next version (DIR_2_3_6 or DIR_2_3_7) to DIR_ALL
renameSync(resolve(userDataParentPath, nextVersion), resolve(userDataParentPath, DIR_ALL));
app.relaunch();
app.exit(0);
}
return;
default: // do nothing
console.log('Doing nothing.');
}
});
}
});
};

module.exports = {
checkMultipleDataVersions,
};
31 changes: 31 additions & 0 deletions packages/altair-electron/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { readdir } = require('fs');
const fs = require('fs');
const { join } = require('path');

const getDirectoriesInDirectory = (path) => {
return new Promise((resolve, reject) => {
readdir(
path,
{ withFileTypes: true },
(err, dirents) => err ? reject(err) : resolve(dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name)));
});
};

const deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file, index) => {
const curPath = join(path, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};

module.exports = {
getDirectoriesInDirectory,
deleteFolderRecursive,
};
4 changes: 3 additions & 1 deletion packages/altair-electron/src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { getStore } = require('./store');
const { createMenu } = require('./menu');
const { createTouchBar } = require('./touchbar');
const { checkForUpdates } = require('./updates');
const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions');

/**
* @type {BrowserWindow}
Expand Down Expand Up @@ -92,7 +93,7 @@ const createWindow = () => {

// Load the data from the file into a buffer and pass it to the callback
// Using the mime package to get the mime type for the file, based on the file name
callback({ mimeType: mime.lookup(filePath), data: new Buffer(data) });
callback({ mimeType: mime.lookup(filePath), data: Buffer.from(data) });
});
});
}, (error) => {
Expand Down Expand Up @@ -173,6 +174,7 @@ const createWindow = () => {
instance.on('ready-to-show', () => {
instance.show();
instance.focus();
checkMultipleDataVersions(instance);
});

// Doesn't seem to be called. Might be because of buffer protocol.
Expand Down
53 changes: 36 additions & 17 deletions src/app/effects/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,32 @@ export class QueryEffects {
withLatestFrom(this.store, (action: queryActions.Action, state: fromRoot.State) => {
return { data: state.windows[action.windowId], windowId: action.windowId, action };
}),
switchMap(response => {
return this.getPrerequesstTransformedData$(response);
}),
switchMap(res => {
if (!res) {
return observableEmpty();
}
const { response, transformedData } = res;
let connectionParams = undefined;
const subscriptionUrl = this.environmentService.hydrate(res.data.query.subscriptionUrl);
const query = this.environmentService.hydrate(res.data.query.query);
const variables = this.environmentService.hydrate(res.data.variables.variables);
let subscriptionUrl = this.environmentService.hydrate(response.data.query.subscriptionUrl);
let query = this.environmentService.hydrate(response.data.query.query);
let variables = this.environmentService.hydrate(response.data.variables.variables);
let variablesObj = undefined;
let selectedOperation = res.data.query.selectedOperation;
let selectedOperation = response.data.query.selectedOperation;

if (transformedData) {
subscriptionUrl = this.environmentService.hydrate(response.data.query.subscriptionUrl, {
activeEnvironment: transformedData.environment
});
query = this.environmentService.hydrate(response.data.query.query, {
activeEnvironment: transformedData.environment
});
variables = this.environmentService.hydrate(response.data.variables.variables, {
activeEnvironment: transformedData.environment
});
}

const subscriptionErrorHandler = (err, errMsg?) => {
if (Array.isArray(err)) {
Expand All @@ -435,7 +454,7 @@ export class QueryEffects {
An error occurred in subscription.<br>
Error: ${errMsg}
`);
this.store.dispatch(new queryActions.StopSubscriptionAction(res.windowId));
this.store.dispatch(new queryActions.StopSubscriptionAction(response.windowId));
return observableEmpty();
};

Expand All @@ -451,32 +470,32 @@ export class QueryEffects {
const operationData = this.gqlService.getSelectedOperationData({
query,
selectedOperation,
queryCursorIndex: res.data.query.queryEditorState &&
res.data.query.queryEditorState.isFocused &&
res.data.query.queryEditorState.cursorIndex,
queryCursorIndex: response.data.query.queryEditorState &&
response.data.query.queryEditorState.isFocused &&
response.data.query.queryEditorState.cursorIndex,
selectIfOneOperation: true,
});

this.store.dispatch(new queryActions.SetQueryOperationsAction(res.windowId, { operations: operationData.operations }));
this.store.dispatch(new queryActions.SetQueryOperationsAction(response.windowId, { operations: operationData.operations }));
selectedOperation = operationData.selectedOperation;
} catch (err) {
this.store.dispatch(new queryActions.SetSelectedOperationAction(res.windowId, { selectedOperation: '' }));
this.store.dispatch(new queryActions.SetSelectedOperationAction(response.windowId, { selectedOperation: '' }));
this.notifyService.warning(err.message);
return observableEmpty();
}

try {
// Stop any currently active subscription
this.gqlService.closeSubscriptionClient(res.data.query.subscriptionClient);
this.gqlService.closeSubscriptionClient(response.data.query.subscriptionClient);


try {
const subscriptionConnectionParams = this.environmentService.hydrate(res.data.query.subscriptionConnectionParams);
const subscriptionConnectionParams = this.environmentService.hydrate(response.data.query.subscriptionConnectionParams);

connectionParams =
subscriptionConnectionParams ? JSON.parse(subscriptionConnectionParams) : undefined;
} catch (err) {
this.store.dispatch(new dialogsActions.ToggleSubscriptionUrlDialogAction(res.windowId));
this.store.dispatch(new dialogsActions.ToggleSubscriptionUrlDialogAction(response.windowId));
return subscriptionErrorHandler(err, 'Your connection parameters is not a valid JSON object.');
}

Expand Down Expand Up @@ -504,15 +523,15 @@ export class QueryEffects {
strData = 'ERROR: Invalid subscription response format.';
}

this.store.dispatch(new queryActions.AddSubscriptionResponseAction(res.windowId, {
this.store.dispatch(new queryActions.AddSubscriptionResponseAction(response.windowId, {
response: strData,
responseTime: (new Date()).getTime() // store responseTime in ms
}));

// Send notification in electron app
this.notifyService.pushNotify(strData, res.data.layout.title, {
this.notifyService.pushNotify(strData, response.data.layout.title, {
onclick: () => {
this.store.dispatch(new windowsMetaActions.SetActiveWindowIdAction({ windowId: res.windowId }));
this.store.dispatch(new windowsMetaActions.SetActiveWindowIdAction({ windowId: response.windowId }));
}
});

Expand All @@ -529,7 +548,7 @@ export class QueryEffects {
}
});

return observableOf(new queryActions.SetSubscriptionClientAction(res.windowId, { subscriptionClient }));
return observableOf(new queryActions.SetSubscriptionClientAction(response.windowId, { subscriptionClient }));
} catch (err) {
debug.error('An error occurred starting the subscription.', err);
return subscriptionErrorHandler(err);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1423,10 +1423,10 @@
resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-14.2.2.tgz#10f197e6f8559c11b16d630c5e9c10c3c8e61c5e"
integrity sha512-okXbUmdZFMO3AYBEJCcpJFPFDkKmIiZZBqWD5TmPtAv+GHfjD2qLZEI0PvZ8IWMU4ozoK2HV2lDxWjw4LbVlnw==

"@types/jasmine@3.4.1":
version "3.4.1"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-3.4.1.tgz#214496efdd7870f133051a95d1692f9cc0730ee7"
integrity sha512-kA2/srq6lb/kiczw+l/yW7lKdAN9Ae5A/pp1bhu8RBzhYY/ybc1hwav3sWrPyAwSRcAS95NVB0uZdM62qT75EA==
"@types/jasmine@3.4.5":
version "3.4.5"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-3.4.5.tgz#589cd518a725ceaf7605997e441a09d7f530483c"
integrity sha512-ljf19razYUgsBv5ofh6oqsd5KMM2Q7A/s2yKI+89v6PFr9jrTGLIIr1P4aR7g3J79s89fC61TX+bjqq+4jxFdQ==

"@types/js-beautify@0.0.28":
version "0.0.28"
Expand Down

0 comments on commit 65c9c9d

Please sign in to comment.