From 66086abe9cacfd57a25826048cfceb1409d154e2 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 30 May 2018 11:39:35 -0700 Subject: [PATCH] Move pull and push to commands. Lint project. --- README.md | 5 +++ index.ts | 84 +++++++++++++++---------------------------------- package.json | 7 +++++ src/auth.ts | 46 +++++++++++++-------------- src/commands.ts | 73 +++++++++++++++++++++++++++++++++++++----- src/files.ts | 6 ++-- src/utils.ts | 4 +-- tests/test.ts | 14 ++++----- 8 files changed, 136 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index 53326ef2..7ff3bd7c 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,11 @@ sudo npm run build; npm run test ``` +#### Lint + +- Use `npm run lint` to find common style errors. +- Download [sort-imports](https://marketplace.visualstudio.com/items?itemName=amatiasq.sort-imports) for VSC to automatically sort imports. + #### Publishing `clasp` to npm (admin) 1. Build `index.js` locally. `.gitignore`/`.npmignore` will hide js/ts files appropriately. diff --git a/index.ts b/index.ts index 1955d5bc..4be8d05b 100755 --- a/index.ts +++ b/index.ts @@ -19,67 +19,33 @@ /** * clasp – The Apps Script CLI */ + import 'connect'; -const open = require('open'); -const path = require('path'); -const commander = require('commander'); -import * as pluralize from 'pluralize'; -import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME, - ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL, - getProjectSettings, getAPIFileType, checkIfOnline, - saveProjectId, manifestExists } from './src/utils'; -import { drive, script, logger, getAPICredentials, login } from './src/auth'; -import { LOG, help, defaultCmd, logs, run, logout, create, clone, deploy, - undeploy, redeploy, version, versions, list, deployments, openCmd, status} from './src/commands'; -import {getProjectFiles, fetchProject, getFileType, hasProject} from './src/files'; -// Functions (not yet moved out of this file) -const pull = async () => { - await checkIfOnline(); - const { scriptId, rootDir } = await getProjectSettings(); - if (scriptId) { - spinner.setSpinnerTitle(LOG.PULLING); - fetchProject(scriptId, rootDir); - } -}; -const push = async () => { - await checkIfOnline(); - spinner.setSpinnerTitle(LOG.PUSHING).start(); - getAPICredentials(async () => { - const { scriptId, rootDir } = await getProjectSettings(); - if (!scriptId) return; - getProjectFiles(rootDir, (err, projectFiles, files) => { - if(err) { - console.log(err); - spinner.stop(true); - } else if (projectFiles) { - const [nonIgnoredFilePaths] = projectFiles; - script.projects.updateContent({ - scriptId, - resource: { files }, - }, {}, (error: any) => { - spinner.stop(true); - if (error) { - console.error(LOG.PUSH_FAILURE); - error.errors.map((err: any) => { - console.error(err.message); - }); - console.error(LOG.FILES_TO_PUSH); - nonIgnoredFilePaths.map((filePath: string) => { - console.error(`└─ ${filePath}`); - }); - process.exit(1); - } else { - nonIgnoredFilePaths.map((filePath: string) => { - console.log(`└─ ${filePath}`); - }); - console.log(LOG.PUSH_SUCCESS(nonIgnoredFilePaths.length)); - } - }); - } - }); - }); -}; +import { login } from './src/auth'; +import { + clone, + create, + defaultCmd, + deploy, + deployments, + help, + list, + logout, + logs, + openCmd, + pull, + push, + redeploy, + run, + status, + undeploy, + version, + versions, +} from './src/commands'; +import { PROJECT_NAME } from './src/utils'; + +const commander = require('commander'); // CLI diff --git a/package.json b/package.json index 1487a077..906328df 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,13 @@ "extension", "add-on" ], + "importSort": { + ".ts": { + "parser": "typescript", + "style": "module-compact" + }, + "on-save": false + }, "author": "Grant Timmerman", "license": "Apache-2.0", "dependencies": { diff --git a/src/auth.ts b/src/auth.ts index cedd2af6..5fb7aa98 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,14 +1,14 @@ -import { OAuth2Client } from 'google-auth-library'; -import { checkIfOnline, ClaspSettings, DOTFILE, ERROR } from './utils'; import * as http from 'http'; -import * as url from 'url'; import { AddressInfo } from 'net'; -import open = require('open'); -import readline = require('readline'); -import { LOG } from './commands'; +import * as url from 'url'; +import { OAuth2Client } from 'google-auth-library'; import { google } from 'googleapis'; -import { Logging } from 'googleapis/build/src/apis/logging/v2'; import { Drive } from 'googleapis/build/src/apis/drive/v3'; +import { Logging } from 'googleapis/build/src/apis/logging/v2'; +import { LOG } from './commands'; +import { ClaspSettings, DOTFILE, ERROR, checkIfOnline } from './utils'; +import open = require('open'); +import readline = require('readline'); // API settings // @see https://developers.google.com/oauthplayground/ @@ -45,6 +45,22 @@ export const drive = google.drive({ auth: oauth2Client, }) as Drive; +/** + * Requests authorization to manage Apps Script projects. + * @param {boolean} useLocalhost True if a local HTTP server should be run + * to handle the auth response. False if manual entry used. + */ +async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) { + try { + const token = await (useLocalhost ? authorizeWithLocalhost() : authorizeWithoutLocalhost()); + await (writeToOwnKey ? DOTFILE.RC_LOCAL.write(token) : DOTFILE.RC.write(token)); + console.log(LOG.AUTH_SUCCESSFUL); + process.exit(0); // gracefully exit after successful login + } catch(err) { + console.error(ERROR.ACCESS_TOKEN + err); + } +} + /** * Loads the Apps Script API credentials for the CLI. * Required before every API call. @@ -126,22 +142,6 @@ async function authorizeWithoutLocalhost() { return (await client.getToken(authCode)).tokens; } -/** - * Requests authorization to manage Apps Script projects. - * @param {boolean} useLocalhost True if a local HTTP server should be run - * to handle the auth response. False if manual entry used. - */ -export async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) { - try { - const token = await (useLocalhost ? authorizeWithLocalhost() : authorizeWithoutLocalhost()); - await (writeToOwnKey ? DOTFILE.RC_LOCAL.write(token) : DOTFILE.RC.write(token)); - console.log(LOG.AUTH_SUCCESSFUL); - process.exit(0); // gracefully exit after successful login - } catch(err) { - 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 diff --git a/src/commands.ts b/src/commands.ts index 130f47d8..3821bacd 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,13 +1,23 @@ -import { DOT, PROJECT_NAME, getScriptURL, - logError, ClaspSettings, DOTFILE, ERROR, - checkIfOnline, spinner, saveProjectId, manifestExists, - getProjectSettings, ProjectSettings, PROJECT_MANIFEST_BASENAME } from './utils'; -const open = require('open'); -import {hasProject, fetchProject, getProjectFiles} from './files'; -import { authorize, getAPICredentials, drive, script, logger} from './auth'; +import * as del from 'del'; import * as pluralize from 'pluralize'; +import { drive, getAPICredentials, logger, script } from './auth'; +import {fetchProject, getProjectFiles, hasProject} from './files'; +import { + DOT, + ERROR, + PROJECT_MANIFEST_BASENAME, + PROJECT_NAME, + ProjectSettings, + checkIfOnline, + getProjectSettings, + getScriptURL, + logError, + manifestExists, + saveProjectId, + spinner, +} from './utils'; +const open = require('open'); const commander = require('commander'); -import * as del from 'del'; const chalk = require('chalk'); const { prompt } = require('inquirer'); @@ -48,6 +58,53 @@ export const LOG = { VERSION_NUM: (numVersions: number) => `~ ${numVersions} ${pluralize('Version', numVersions)} ~`, }; +export const pull = async () => { + await checkIfOnline(); + const { scriptId, rootDir } = await getProjectSettings(); + if (scriptId) { + spinner.setSpinnerTitle(LOG.PULLING); + fetchProject(scriptId, rootDir); + } +}; +export const push = async () => { + await checkIfOnline(); + spinner.setSpinnerTitle(LOG.PUSHING).start(); + getAPICredentials(async () => { + const { scriptId, rootDir } = await getProjectSettings(); + if (!scriptId) return; + getProjectFiles(rootDir, (err, projectFiles, files) => { + if(err) { + console.log(err); + spinner.stop(true); + } else if (projectFiles) { + const [nonIgnoredFilePaths] = projectFiles; + script.projects.updateContent({ + scriptId, + resource: { files }, + }, {}, (error: any) => { + spinner.stop(true); + if (error) { + console.error(LOG.PUSH_FAILURE); + error.errors.map((err: any) => { + console.error(err.message); + }); + console.error(LOG.FILES_TO_PUSH); + nonIgnoredFilePaths.map((filePath: string) => { + console.error(`└─ ${filePath}`); + }); + process.exit(1); + } else { + nonIgnoredFilePaths.map((filePath: string) => { + console.log(`└─ ${filePath}`); + }); + console.log(LOG.PUSH_SUCCESS(nonIgnoredFilePaths.length)); + } + }); + } + }); + }); +}; + export const help = () => { commander.outputHelp(); }; diff --git a/src/files.ts b/src/files.ts index 09fa6c7f..eb34b190 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,11 +1,11 @@ +import * as fs from 'fs'; import * as anymatch from 'anymatch'; import * as mkdirp from 'mkdirp'; +import * as recursive from 'recursive-readdir'; import {getAPICredentials, script} from './auth'; import {LOG} from './commands'; -import {checkIfOnline, DOTFILE, getAPIFileType, spinner, logError, DOT, ERROR} from './utils'; -import * as fs from 'fs'; +import {DOT, DOTFILE, ERROR, checkIfOnline, getAPIFileType, logError, spinner} from './utils'; const path = require('path'); -import * as recursive from 'recursive-readdir'; const readMultipleFiles = require('read-multiple-files'); // An Apps Script API File diff --git a/src/utils.ts b/src/utils.ts index 14b14d90..13c3ae7a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,11 @@ +import * as fs from 'fs'; import * as os from 'os'; +import { Spinner } from 'cli-spinner'; const path = require('path'); const findParentDir = require('find-parent-dir'); const splitLines = require('split-lines'); -import * as fs from 'fs'; const dotf = require('dotf'); const read = require('read-file'); -import { Spinner } from 'cli-spinner'; const isOnline = require('is-online'); // Names / Paths diff --git a/tests/test.ts b/tests/test.ts index 3edb5134..e5f0ccce 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -1,14 +1,12 @@ -import { describe, it } from 'mocha'; -import { expect } from 'chai'; -import * as fs from 'fs-extra'; import * as os from 'os'; - -const { spawnSync } = require('child_process'); -import { getScriptURL, getAPIFileType, saveProjectId } from './../src/utils.js'; -import { getFileType } from './../src/files'; import * as path from 'path'; +import { expect } from 'chai'; +import * as fs from 'fs-extra'; +import { describe, it } from 'mocha'; import * as tmp from 'tmp'; - +import { getFileType } from './../src/files'; +import { getAPIFileType, getScriptURL, saveProjectId } from './../src/utils.js'; +const { spawnSync } = require('child_process'); const TEST_CODE_JS = 'function test() { Logger.log(\'test\'); }'; const TEST_JSON = '{"timeZone": "America/New_York"}';