From 6a52dde8a4db2574c9afa7b308cce9bd5317d294 Mon Sep 17 00:00:00 2001 From: Campion Fellin Date: Sat, 28 Apr 2018 09:43:19 -0700 Subject: [PATCH] Move more items to utils and fix clasp open (#153) Clasp open was trying to open an object, rather than the scriptID Moved over manifestExists, saveProjectId, checkIfOnline, and getAPIFileType Signed-off-by: campionfellin --- index.ts | 43 ++++--------------------------------------- src/utils.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/index.ts b/index.ts index 1d66f85c..795b7b38 100755 --- a/index.ts +++ b/index.ts @@ -25,7 +25,6 @@ import * as del from 'del'; import * as fs from 'fs'; import { google } from 'googleapis'; import * as http from 'http'; -const isOnline = require('is-online'); import * as mkdirp from 'mkdirp'; import { OAuth2Client } from 'google-auth-library'; const open = require('open'); @@ -41,7 +40,8 @@ const chalk = require('chalk'); const { prompt } = require('inquirer'); import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME, ClaspSettings, ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL, - getProjectSettings, getFileType } from './src/utils.js'; + getProjectSettings, getFileType, getAPIFileType, checkIfOnline, + saveProjectId, manifestExists } from './src/utils.js'; // An Apps Script API File interface AppsScriptFile { @@ -221,42 +221,6 @@ function authorizeWithoutLocalhost(opts: any): Promise { }); } -/** - * Gets the API FileType. Assumes the path is valid. - * @param {string} path The file path - * @return {string} The API's FileType enum (uppercase), null if not valid. - */ -function getAPIFileType(path: string): string { - const extension: string = path.substr(path.lastIndexOf('.') + 1).toUpperCase(); - return (extension === 'GS' || extension === 'JS') ? 'SERVER_JS' : extension.toUpperCase(); -} - -/** - * Checks if the network is available. Gracefully exits if not. - */ -async function checkIfOnline() { - if (!(await isOnline())) { - logError(null, ERROR.OFFLINE); - process.exit(1); - } -} - -/** - * Saves the script ID in the project dotfile. - * @param {string} scriptId The script ID - */ -function saveProjectId(scriptId: string): void { - DOTFILE.PROJECT().write({ scriptId }); // Save the script id -} - -/** - * Checks if the current directory appears to be a valid project. - * @return {boolean} True if valid project, false otherwise - */ -function manifestExists(): boolean { - return fs.existsSync(`${PROJECT_MANIFEST_BASENAME}.json`); -} - /** * Recursively finds all files that are part of the current project, and those that are ignored * by .claspignore and calls the passed callback function with the file lists. @@ -633,7 +597,8 @@ commander .description('Open a script') .action(async (scriptId: any) => { if (!scriptId) { - scriptId = await getProjectSettings(); + const settings = await getProjectSettings(); + scriptId = settings.scriptId; } if (scriptId.length < 30) { logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId)); diff --git a/src/utils.ts b/src/utils.ts index 663857e7..05c4d77b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,6 +6,7 @@ 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 export const PROJECT_NAME = 'clasp'; @@ -192,3 +193,39 @@ export function getProjectSettings(failSilently?: boolean): Promise