Skip to content

Commit

Permalink
Make retrieving git config async
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland committed Jul 30, 2019
1 parent c08a984 commit 2e45a7b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/decorateMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export default (
): MenuItemConstructorOptions[] => {
const checkAndCallback = (
callback: (gitConfig: ConfigAndCommands) => void,
): NonNullable<MenuItemConstructorOptions['click']> => (): void => {
const commandsAndConfig = checkForMissingSettings();
): NonNullable<MenuItemConstructorOptions['click']> => async (): Promise<
void
> => {
const commandsAndConfig = await checkForMissingSettings();
if (commandsAndConfig === null) return;
callback(commandsAndConfig);
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/checkForMissingSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const hyperApp: App & {
config?: { getConfig?: () => { syncSettings?: SyncSettings } };
} = app;

export default (): null | ConfigAndCommands => {
export default async (): Promise<null | ConfigAndCommands> => {
const notifyErr = (message: string): void | string =>
notify({
title: ERROR_TITLE,
Expand All @@ -28,7 +28,7 @@ export default (): null | ConfigAndCommands => {
);
}

const config = getGitConfig();
const config = await getGitConfig();
const { personalAccessToken, gistId } = config;
const hyperConfig = hyperApp.config.getConfig().syncSettings || {
quiet: false,
Expand Down
5 changes: 2 additions & 3 deletions src/lib/getCommands/restore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GitProcess } from 'dugite';
import { ensureFile, outputFile, readFile } from 'fs-extra';
import { ensureFile, copyFile } from 'fs-extra';
import { FILE_RESTORE, FILE_BACKUP, DIR_REPO } from '../../constants';
import { GitConfig } from '../getGitConfig';

Expand All @@ -8,6 +8,5 @@ export default async ({ repoPromise }: GitConfig): Promise<void> => {
await GitProcess.exec(['fetch'], DIR_REPO);
await GitProcess.exec(['merge', 'origin/master', 'master'], DIR_REPO);
await ensureFile(FILE_RESTORE);
const file = await readFile(FILE_BACKUP);
await outputFile(FILE_RESTORE, file, { flag: 'r+' });
await copyFile(FILE_BACKUP, FILE_RESTORE);
};
14 changes: 7 additions & 7 deletions src/lib/getGitConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GitProcess } from 'dugite';
import { copySync, ensureDir, readJsonSync, pathExistsSync } from 'fs-extra';
import { copyFile, ensureDir, readJson, pathExists } from 'fs-extra';
import {
FILE_CONFIG,
FILE_CONFIG_TEMPLATE,
Expand All @@ -20,7 +20,7 @@ export interface GitConfig extends IdAndToken {
repoPromise: Promise<void>;
}

const getIdAndToken = (): IdAndToken => {
const getIdAndToken = async (): Promise<IdAndToken> => {
let config: IdAndToken = {
personalAccessToken: '',
gistId: '',
Expand All @@ -36,16 +36,16 @@ const getIdAndToken = (): IdAndToken => {
!HYPER_SYNC_SETTINGS_GIST_ID
) {
try {
if (!pathExistsSync(FILE_CONFIG)) {
if (!(await pathExists(FILE_CONFIG))) {
notify({
title: ERROR_TITLE,
body: `no config file found in \`${FILE_CONFIG}\`, creating one`,
level: 'error',
});
copySync(FILE_CONFIG_TEMPLATE, FILE_CONFIG);
await copyFile(FILE_CONFIG_TEMPLATE, FILE_CONFIG);
} else {
try {
config = readJsonSync(FILE_CONFIG);
config = await readJson(FILE_CONFIG);
} catch (err) {
notify({
title: ERROR_TITLE,
Expand Down Expand Up @@ -76,9 +76,9 @@ const getIdAndToken = (): IdAndToken => {

getIdAndToken();

export default (): GitConfig => {
export default async (): Promise<GitConfig> => {
const config: GitConfig = {
...getIdAndToken(),
...(await getIdAndToken()),
url: '',
remoteUrl: '',
repoPromise: Promise.resolve(),
Expand Down

0 comments on commit 2e45a7b

Please sign in to comment.