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

[cli] Read auth credentials from menu-bar MMKV storage #137

Merged
merged 1 commit into from
Jan 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"commander": "^10.0.1",
"common-types": "1.0.0",
"eas-shared": "*",
"nodejs-mmkv": "^0.1.1",
"strip-ansi": "^6.0.0"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions apps/cli/src/mmkv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { StorageUtils } from 'common-types';
import os from 'os';

const MMKVModule = require('nodejs-mmkv');
const storage = new MMKVModule({
rootDir: StorageUtils.getExpoOrbitDirectory(os.homedir()),
id: StorageUtils.MMKVInstanceId,
});

export function getSessionSecret() {
return storage.getString('sessionSecret');
}
1 change: 1 addition & 0 deletions apps/menu-bar/macos/ExpoMenuBar-macOS/MenuBarModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (NSDictionary *)constantsToExport
@"height": @([[NSScreen mainScreen] frame].size.height),
@"width": @([[NSScreen mainScreen] frame].size.width)
},
@"homedir": NSHomeDirectory(),
};
}

Expand Down
1 change: 1 addition & 0 deletions apps/menu-bar/src/modules/MenuBarModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MenuBarModuleConstants = {
appVersion: string;
buildVersion: string;
initialScreenSize: { height: number; width: number };
homedir: string;
};
const constants: MenuBarModuleConstants = NativeModules.MenuBarModule.getConstants();

Expand Down
31 changes: 30 additions & 1 deletion apps/menu-bar/src/modules/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { StorageUtils } from 'common-types';
import { MMKV } from 'react-native-mmkv';

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

export const userPreferencesStorageKey = 'user-preferences';
Expand Down Expand Up @@ -59,7 +61,34 @@ export const resetStorage = () => {
storage.clearAll();
};

export const storage = new MMKV();
const oldStorage = new MMKV();
export const storage = new MMKV({
id: StorageUtils.MMKVInstanceId,
path: StorageUtils.getExpoOrbitDirectory(MenuBarModule.constants.homedir),
});

const migratedStorageKey = 'migratedFromOldPath';
// Migrate MMKV storage to new path so that it's accessible from the CLI
function migrateMMKVFromOldStoragePath() {
const keys = oldStorage.getAllKeys();

for (const key of keys) {
const value =
oldStorage.getString(key) ??
oldStorage.getBoolean(key) ??
oldStorage.getNumber(key) ??
oldStorage.getBuffer(key);

if (value != null) {
storage.set(key, value);
}
}

storage.set(migratedStorageKey, true);
}
if (!storage.getBoolean(migratedStorageKey)) {
migrateMMKVFromOldStoragePath();
}

export function saveSessionSecret(sessionSecret: string) {
storage.set(sessionSecretStorageKey, sessionSecret);
Expand Down
3 changes: 2 additions & 1 deletion packages/common-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Devices from './devices';
import * as CliCommands from './cli-commands';
import * as StorageUtils from './storage';
import { Platform } from './cli-commands';
import InternalError, { InternalErrorCode } from './InternalError';

export { Devices, CliCommands, InternalError, InternalErrorCode, Platform };
export { Devices, CliCommands, InternalError, InternalErrorCode, Platform, StorageUtils };
5 changes: 5 additions & 0 deletions packages/common-types/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const MMKVInstanceId = 'mmkv.default';

export function getExpoOrbitDirectory(homedir: string) {
return `${homedir}/.expo/orbit`;
}
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4644,6 +4644,13 @@ big-integer@1.6.x, big-integer@^1.6.44:
resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.51.tgz#0df92a5d9880560d3ff2d5fd20245c889d130686"
integrity sha512-GPEid2Y9QU1Exl1rpO9B2IPJGHPSupF5GnVIP0blYvNOMer2bTvSWs1jGOUg04hTmu67nmLsQ9TBo1puaotBHg==

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 @@ -6476,6 +6483,11 @@ 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 @@ -9362,6 +9374,11 @@ 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-dir@^0.1.17:
version "0.1.17"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5"
Expand Down Expand Up @@ -9437,6 +9454,14 @@ node-stream-zip@^1.9.1:
resolved "https://registry.yarnpkg.com/node-stream-zip/-/node-stream-zip-1.15.0.tgz#158adb88ed8004c6c49a396b50a6a5de3bca33ea"
integrity sha512-LN4fydt9TqhZhThkZIVQnF9cwjU3qmUH9h78Mx/K7d3VvfRqqwthLwJEUOEL0QPZ0XQmNN7be5Ggit5+4dq3Bw==

nodejs-mmkv@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/nodejs-mmkv/-/nodejs-mmkv-0.1.1.tgz#3804fe41e21804e66baa94fbabf24a040500561e"
integrity sha512-P8AXlfpe9IHeGUvRt5W0auahbjvTbj+DcqmlRZKXpstfWTZf2KyJqvLishyX1vKzJzk4M7H71LXmM9ld5C/MXA==
dependencies:
bindings "^1.5.0"
node-addon-api "^5.0.0"

nopt@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-6.0.0.tgz#245801d8ebf409c6df22ab9d95b65e1309cdb16d"
Expand Down