From 98c40e4864dd24462e92191d93019e0338cab4c1 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 30 May 2018 11:07:23 -0700 Subject: [PATCH] Move status, open, deployments to commands.ts --- index.ts | 65 ++----------------------------------------------- src/commands.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 64 deletions(-) diff --git a/index.ts b/index.ts index 5e87408b..1955d5bc 100755 --- a/index.ts +++ b/index.ts @@ -29,8 +29,8 @@ import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME, 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} from './src/commands'; +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) @@ -81,67 +81,6 @@ const push = async () => { }); }; -const status = async (cmd: { json: boolean }) => { - await checkIfOnline(); - getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => { - if (!scriptId) return; - getProjectFiles(rootDir, (err, projectFiles) => { - if(err) return console.log(err); - else if (projectFiles) { - const [filesToPush, untrackedFiles] = projectFiles; - if (cmd.json) { - console.log(JSON.stringify({ filesToPush, untrackedFiles })); - } else { - console.log(LOG.STATUS_PUSH); - filesToPush.forEach((file) => console.log(`└─ ${file}`)); - console.log(LOG.STATUS_IGNORE); - untrackedFiles.forEach((file) => console.log(`└─ ${file}`)); - } - } - }); - }); -}; -const openCmd = async (scriptId: any) => { - if (!scriptId) { - const settings = await getProjectSettings(); - scriptId = settings.scriptId; - } - if (scriptId.length < 30) { - logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId)); - } else { - console.log(LOG.OPEN_PROJECT(scriptId)); - open(getScriptURL(scriptId)); - } -}; -const deployments = async () => { - await checkIfOnline(); - getAPICredentials(async () => { - const { scriptId } = await getProjectSettings(); - if (!scriptId) return; - spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start(); - script.projects.deployments.list({ - scriptId, - }, {}, (error: any, { data }: any) => { - spinner.stop(true); - if (error) { - logError(error); - } else { - const deployments = data.deployments; - const numDeployments = deployments.length; - const deploymentWord = pluralize('Deployment', numDeployments); - console.log(`${numDeployments} ${deploymentWord}.`); - deployments.map(({ deploymentId, deploymentConfig }: any) => { - const versionString = !!deploymentConfig.versionNumber ? - `@${deploymentConfig.versionNumber}` : '@HEAD'; - const description = deploymentConfig.description ? - '- ' + deploymentConfig.description : ''; - console.log(`- ${deploymentId} ${versionString} ${description}`); - }); - } - }); - }); -}; - // CLI /** diff --git a/src/commands.ts b/src/commands.ts index b6952492..130f47d8 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -3,7 +3,7 @@ import { DOT, PROJECT_NAME, getScriptURL, checkIfOnline, spinner, saveProjectId, manifestExists, getProjectSettings, ProjectSettings, PROJECT_MANIFEST_BASENAME } from './utils'; const open = require('open'); -import {hasProject, fetchProject} from './files'; +import {hasProject, fetchProject, getProjectFiles} from './files'; import { authorize, getAPICredentials, drive, script, logger} from './auth'; import * as pluralize from 'pluralize'; const commander = require('commander'); @@ -375,4 +375,64 @@ export const redeploy = async (deploymentId: string, version: string, descriptio }); }); }); +}; +export const status = async (cmd: { json: boolean }) => { + await checkIfOnline(); + getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => { + if (!scriptId) return; + getProjectFiles(rootDir, (err, projectFiles) => { + if(err) return console.log(err); + else if (projectFiles) { + const [filesToPush, untrackedFiles] = projectFiles; + if (cmd.json) { + console.log(JSON.stringify({ filesToPush, untrackedFiles })); + } else { + console.log(LOG.STATUS_PUSH); + filesToPush.forEach((file) => console.log(`└─ ${file}`)); + console.log(LOG.STATUS_IGNORE); + untrackedFiles.forEach((file) => console.log(`└─ ${file}`)); + } + } + }); + }); +}; +export const openCmd = async (scriptId: any) => { + if (!scriptId) { + const settings = await getProjectSettings(); + scriptId = settings.scriptId; + } + if (scriptId.length < 30) { + logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId)); + } else { + console.log(LOG.OPEN_PROJECT(scriptId)); + open(getScriptURL(scriptId)); + } +}; +export const deployments = async () => { + await checkIfOnline(); + getAPICredentials(async () => { + const { scriptId } = await getProjectSettings(); + if (!scriptId) return; + spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start(); + script.projects.deployments.list({ + scriptId, + }, {}, (error: any, { data }: any) => { + spinner.stop(true); + if (error) { + logError(error); + } else { + const deployments = data.deployments; + const numDeployments = deployments.length; + const deploymentWord = pluralize('Deployment', numDeployments); + console.log(`${numDeployments} ${deploymentWord}.`); + deployments.map(({ deploymentId, deploymentConfig }: any) => { + const versionString = !!deploymentConfig.versionNumber ? + `@${deploymentConfig.versionNumber}` : '@HEAD'; + const description = deploymentConfig.description ? + '- ' + deploymentConfig.description : ''; + console.log(`- ${deploymentId} ${versionString} ${description}`); + }); + } + }); + }); }; \ No newline at end of file