Skip to content

Commit

Permalink
Move more items to utils and fix clasp open (google#153)
Browse files Browse the repository at this point in the history
Clasp open was trying to open an object, rather than the scriptID
Moved over manifestExists, saveProjectId, checkIfOnline, and getAPIFileType

Signed-off-by: campionfellin <[email protected]>
  • Loading branch information
campionfellin authored and grant committed Apr 28, 2018
1 parent e4366b8 commit 6a52dde
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
43 changes: 4 additions & 39 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 {
Expand Down Expand Up @@ -221,42 +221,6 @@ function authorizeWithoutLocalhost(opts: any): Promise<string> {
});
}

/**
* 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.
Expand Down Expand Up @@ -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));
Expand Down
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -192,3 +193,39 @@ export function getProjectSettings(failSilently?: boolean): Promise<ProjectSetti
export function getFileType(type: string): string {
return (type === 'SERVER_JS') ? 'js' : type.toLowerCase();
}

/**
* 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.
*/
export 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.
*/
export 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
*/
export 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
*/
export function manifestExists(): boolean {
return fs.existsSync(`${PROJECT_MANIFEST_BASENAME}.json`);
}

0 comments on commit 6a52dde

Please sign in to comment.