Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move more items to utils and fix clasp open #153

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`);
}