From f1b64a6de597d56295780deca5d066aa86c14492 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 30 May 2018 10:25:16 -0700 Subject: [PATCH] Move login to auth, help and defaultCmd to commands --- index.ts | 12 +++--------- src/auth.ts | 19 ++++++++++++++++++- src/commands.ts | 26 ++++++++------------------ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/index.ts b/index.ts index 96b62793..7995a59f 100755 --- a/index.ts +++ b/index.ts @@ -38,9 +38,9 @@ import * as pluralize from 'pluralize'; import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME, ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL, getProjectSettings, getFileType, getAPIFileType, checkIfOnline, - saveProjectId, manifestExists } from './src/utils.js'; -import { oauth2Client, getAPICredentials } from './src/auth'; -import { LOG, login } from './src/commands.js'; + saveProjectId, manifestExists } from './src/utils'; +import { oauth2Client, getAPICredentials, login } from './src/auth'; +import { LOG, help, defaultCmd } from './src/commands'; // An Apps Script API File interface AppsScriptFile { @@ -628,12 +628,6 @@ const run = (functionName:string) => { }); }); }; -const help = () => { - commander.outputHelp(); -}; -const defaultCmd = (command: string) => { - console.error(ERROR.COMMAND_DNE(command)); -}; // CLI diff --git a/src/auth.ts b/src/auth.ts index 952122e7..bf862d5d 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,5 +1,5 @@ import { OAuth2Client } from 'google-auth-library'; -import { ClaspSettings, DOTFILE, ERROR } from './utils'; +import { checkIfOnline, ClaspSettings, DOTFILE, ERROR } from './utils'; import * as http from 'http'; import * as url from 'url'; import { AddressInfo } from 'net'; @@ -124,3 +124,20 @@ export async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) { console.error(ERROR.ACCESS_TOKEN + err); } } + +/** + * Logs the user in. Saves the client credentials to an rc file. + * @param options the localhost and ownkey options from commander + */ +export function login(options: { localhost: boolean, ownkey: boolean}) { + DOTFILE.RC.read().then((rc: ClaspSettings) => { + console.warn(ERROR.LOGGED_IN); + }).catch(async (err: string) => { + DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => { + console.warn(ERROR.LOGGED_IN); + }).catch(async (err: string) => { + await checkIfOnline(); + authorize(options.localhost, options.ownkey); + }); + }); +} \ No newline at end of file diff --git a/src/commands.ts b/src/commands.ts index 06009b02..748f172a 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,7 +1,7 @@ -import { DOT, PROJECT_NAME, getScriptURL, ClaspSettings, DOTFILE, ERROR, -checkIfOnline } from './utils.js'; +import { DOT, PROJECT_NAME, getScriptURL, ClaspSettings, DOTFILE, ERROR } from './utils.js'; import { authorize } from './auth.js'; import * as pluralize from 'pluralize'; +const commander = require('commander'); // Log messages (some logs take required params) export const LOG = { @@ -40,19 +40,9 @@ export const LOG = { VERSION_NUM: (numVersions: number) => `~ ${numVersions} ${pluralize('Version', numVersions)} ~`, }; -/** - * Logs the user in. Saves the client credentials to an rc file. - * @param options the localhost and ownkey options from commander - */ -export function login(options: { localhost: boolean, ownkey: boolean}) { - DOTFILE.RC.read().then((rc: ClaspSettings) => { - console.warn(ERROR.LOGGED_IN); - }).catch(async (err: string) => { - DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => { - console.warn(ERROR.LOGGED_IN); - }).catch(async (err: string) => { - await checkIfOnline(); - authorize(options.localhost, options.ownkey); - }); - }); -} \ No newline at end of file +export const help = () => { + commander.outputHelp(); +}; +export const defaultCmd = (command: string) => { + console.error(ERROR.COMMAND_DNE(command)); +}; \ No newline at end of file