Skip to content

Commit

Permalink
Move login to auth, help and defaultCmd to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
grant committed May 30, 2018
1 parent e19fab5 commit f1b64a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
12 changes: 3 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -628,12 +628,6 @@ const run = (functionName:string) => {
});
});
};
const help = () => {
commander.outputHelp();
};
const defaultCmd = (command: string) => {
console.error(ERROR.COMMAND_DNE(command));
};

// CLI

Expand Down
19 changes: 18 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});
}
26 changes: 8 additions & 18 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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);
});
});
}
export const help = () => {
commander.outputHelp();
};
export const defaultCmd = (command: string) => {
console.error(ERROR.COMMAND_DNE(command));
};

0 comments on commit f1b64a6

Please sign in to comment.