Skip to content

Commit

Permalink
[menu-bar][cli] Refactor session storage to no longer use mmkv on CLI (
Browse files Browse the repository at this point in the history
…#179)

* [cli] Add set-session command

* Remove mmkv-node-bindings dependency

* [menu-bar] Use set-session command
  • Loading branch information
gabrieldonadel authored Feb 16, 2024
1 parent 2842cdb commit cd682f8
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 53 deletions.
7 changes: 0 additions & 7 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"eas-shared": "*",
"graphql": "^16.8.0",
"graphql-request": "^6.1.0",
"mmkv-node-bindings": "0.2.1",
"snack-content": "2.0.0-preview.2",
"strip-ansi": "^6.0.0"
},
Expand All @@ -35,11 +34,5 @@
"@graphql-codegen/typescript-operations": "^4.0.1",
"pkg": "^5.8.1",
"typescript": "4.9.4"
},
"mmkv-node-bindings": {
"arch": [
"arm64",
"x64"
]
}
}
1 change: 0 additions & 1 deletion apps/cli/pkg.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"pkg": {
"assets": ["../../node_modules/mmkv-node-bindings/**/*.node"],
"targets": ["node18-macos-arm64", "node18-macos-x64"]
}
}
2 changes: 1 addition & 1 deletion apps/cli/src/api/GraphqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLClient } from 'graphql-request';
import { Config } from 'common-types';

import { getSdk } from '../graphql/generated/graphql';
import { getSessionSecret } from '../mmkv';
import { getSessionSecret } from '../storage';

const endpoint = `${Config.api.origin}/graphql`;
const sessionSecret = getSessionSecret();
Expand Down
5 changes: 5 additions & 0 deletions apps/cli/src/commands/SetSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { setSessionSecret } from '../storage';

export async function setSessionAsync(sessionSecret: string) {
await setSessionSecret(sessionSecret);
}
12 changes: 10 additions & 2 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { bootDeviceAsync } from './commands/BootDevice';
import { installAndLaunchAppAsync } from './commands/InstallAndLaunchApp';
import { launchSnackAsync } from './commands/LaunchSnack';
import { checkToolsAsync } from './commands/CheckTools';
import { launchUpdateAsync } from './commands/LaunchUpdate';
import { setSessionAsync } from './commands/SetSession';
import { returnLoggerMiddleware } from './utils';

const program = new Command();
Expand Down Expand Up @@ -55,7 +55,15 @@ program
.argument('<string>', 'Update URL')
.requiredOption('-p, --platform <string>', 'Selected platform')
.requiredOption('--device-id <string>', 'UDID or name of the device')
.action(returnLoggerMiddleware(launchUpdateAsync));
.action(async (...args) => {
const { launchUpdateAsync } = await import('./commands/LaunchUpdate');
returnLoggerMiddleware(launchUpdateAsync)(...args);
});

program
.command('set-session')
.argument('<string>', 'Session secret')
.action(returnLoggerMiddleware(setSessionAsync));

if (process.argv.length < 3) {
program.help();
Expand Down
14 changes: 0 additions & 14 deletions apps/cli/src/mmkv.ts

This file was deleted.

22 changes: 22 additions & 0 deletions apps/cli/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StorageUtils } from 'common-types';
import os from 'os';
import JsonFile from '@expo/json-file';

export function getSessionSecret(): string | undefined {
const userSettings = userSettingsJsonFile().read();
return userSettings.sessionSecret;
}

export async function setSessionSecret(sessionSecret: string): Promise<void> {
await userSettingsJsonFile().setAsync('sessionSecret', sessionSecret);
}

type UserData = {
sessionSecret?: string;
};
function userSettingsJsonFile(): JsonFile<UserData> {
return new JsonFile<UserData>(StorageUtils.userSettingsFile(os.homedir()), {
jsonParseErrorDefault: {},
cantReadFileDefault: {},
});
}
5 changes: 5 additions & 0 deletions apps/menu-bar/src/commands/setSessionAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MenuBarModule from '../modules/MenuBarModule';

export const setSessionAsync = async (sessionSecret: string) => {
await MenuBarModule.runCli('set-session', [sessionSecret], console.log);
};
16 changes: 14 additions & 2 deletions apps/menu-bar/src/modules/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Platform } from 'react-native';
import { MMKV } from 'react-native-mmkv';

import { apolloClient } from '../api/ApolloClient';
import { setSessionAsync } from '../commands/setSessionAsync';
import MenuBarModule from '../modules/MenuBarModule';

export const userPreferencesStorageKey = 'user-preferences';
Expand Down Expand Up @@ -92,8 +93,19 @@ if (!storage.getBoolean(migratedStorageKey) && Platform.OS !== 'web') {
migrateMMKVFromOldStoragePath();
}

export function saveSessionSecret(sessionSecret: string) {
storage.set(sessionSecretStorageKey, sessionSecret);
const hasSetSessionFile = 'hasSetSessionFile';
if (!storage.getBoolean(hasSetSessionFile) && Platform.OS !== 'web') {
setSessionAsync(storage.getString(sessionSecretStorageKey) ?? '');
storage.set(hasSetSessionFile, true);
}

export function saveSessionSecret(sessionSecret: string | undefined) {
if (sessionSecret === undefined) {
storage.delete(sessionSecretStorageKey);
} else {
storage.set(sessionSecretStorageKey, sessionSecret);
}
setSessionAsync(sessionSecret ?? '');
}

export function resetApolloStore() {
Expand Down
2 changes: 1 addition & 1 deletion apps/menu-bar/src/windows/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const Settings = () => {
};

const handleLogout = () => {
storage.delete(sessionSecretStorageKey);
saveSessionSecret(undefined);
resetApolloStore();
};

Expand Down
5 changes: 5 additions & 0 deletions packages/common-types/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export const MMKVInstanceId = 'mmkv.default';
const AUTH_FILE_NAME = 'auth.json';

export function getExpoOrbitDirectory(homedir: string) {
return `${homedir}/.expo/orbit`;
}

export function userSettingsFile(homedir: string): string {
return `${getExpoOrbitDirectory(homedir)}/${AUTH_FILE_NAME}`;
}
25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7680,13 +7680,6 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"

bl@^4.0.3, bl@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/bl/-/bl-4.1.0.tgz#451535264182bec2fbbc83a62ab98cf11d9f7b3a"
Expand Down Expand Up @@ -10319,11 +10312,6 @@ file-entry-cache@^6.0.1:
dependencies:
flat-cache "^3.0.4"

file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==

filelist@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.4.tgz#f78978a1e944775ff9e62e744424f215e58352b5"
Expand Down Expand Up @@ -14043,14 +14031,6 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mmkv-node-bindings@0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/mmkv-node-bindings/-/mmkv-node-bindings-0.2.1.tgz#a1d8d7698936a5f6afd8dd8092d372d2d2e2b46c"
integrity sha512-cpGEcqN0tEdajxD+4R5Hp/XDh5skCyD1MnkNtQOtnM0Y3ye5EWkJclCBP2vBBvA5lTyJ6lFfJJX5JdWj7/5mEQ==
dependencies:
bindings "^1.5.0"
node-addon-api "^5.0.0"

modify-values@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down Expand Up @@ -14225,11 +14205,6 @@ node-addon-api@^3.2.1:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==

node-addon-api@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-5.1.0.tgz#49da1ca055e109a23d537e9de43c09cca21eb762"
integrity sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==

node-api-version@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/node-api-version/-/node-api-version-0.2.0.tgz#5177441da2b1046a4d4547ab9e0972eed7b1ac1d"
Expand Down

0 comments on commit cd682f8

Please sign in to comment.