Skip to content

Commit

Permalink
Load credentials is now async (google#202)
Browse files Browse the repository at this point in the history
Though it looks like a lot of changes, note that most of it is un-indenting the callbacks that were originally given to `getAPICredentials()`. It is now an `async` function, and any command that uses it has been changed to use `await getAPICredentials()` instead. 

Signed-off-by: campionfellin <[email protected]>

Fixes google#201 
- [x] `npm run test` succeeds.
Results:
```
  19 passing (55s)
  4 pending

--------------|----------|----------|----------|----------|-------------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files     |    56.37 |    43.12 |    60.95 |    62.89 |                   |
 clasp        |    96.55 |       50 |      100 |    96.55 |                   |
  index.js    |    96.55 |       50 |      100 |    96.55 |               187 |
 clasp/src    |     53.9 |    47.11 |    59.53 |    59.03 |                   |
  auth.js     |    28.07 |    31.91 |    27.27 |    31.82 |... 39,240,241,245 |
  commands.js |    56.74 |    49.11 |    60.22 |    59.08 |... 05,706,707,733 |
  files.js    |    67.11 |    55.14 |     93.1 |    78.57 |... 82,192,194,197 |
  utils.js    |    60.61 |    48.21 |    67.35 |    70.16 |... 43,268,269,294 |
 clasp/tests  |    61.25 |     9.38 |     66.1 |    72.64 |                   |
  test.js     |    61.25 |     9.38 |     66.1 |    72.64 |... 55,256,257,258 |
--------------|----------|----------|----------|----------|-------------------|
```
- [x] `npm run lint` succeeds.
- [ ] Appropriate changes to README are included in PR.
  • Loading branch information
campionfellin authored and grant committed May 31, 2018
1 parent 573cd4e commit 2d8ba8e
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 300 deletions.
22 changes: 9 additions & 13 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,19 @@ async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
/**
* Loads the Apps Script API credentials for the CLI.
* Required before every API call.
* @param {Function} cb The callback
* @param {boolean} isLocal If we should load local API credentials for this clasp project.
*/
export function getAPICredentials(cb: (rc: ClaspSettings | void) => void) {
DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => {
export async function loadAPICredentials() {
return DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => {
oauth2Client.setCredentials(rc);
}).catch((err: any) => {
return DOTFILE.RC.read().then((rc: ClaspSettings) => {
oauth2Client.setCredentials(rc);
cb(rc);
}).catch((err: any) => {
DOTFILE.RC.read().then((rc: ClaspSettings) => {
oauth2Client.setCredentials(rc);
cb(rc);
}).catch((err: any) => {
console.error('Could not read API credentials. Error:');
console.error(err);
process.exit(-1);
});
console.error('Could not read API credentials. Error:');
console.error(err);
process.exit(1);
});
});
}

/**
Expand Down
Loading

0 comments on commit 2d8ba8e

Please sign in to comment.