Skip to content

Commit

Permalink
Refactor clasp open (#537)
Browse files Browse the repository at this point in the history
Signed-off-by: campionfellin <campionfellin@gmail.com>
  • Loading branch information
campionfellin committed Feb 17, 2019
1 parent c7486cd commit 36c9491
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 71 deletions.
70 changes: 0 additions & 70 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,76 +734,6 @@ export const version = async (description: string) => {
console.log(LOG.VERSION_CREATED(versions.data.versionNumber || -1));
};

/**
* Opens an Apps Script project's script.google.com editor.
* @param scriptId {string} The Apps Script project to open.
* @param cmd.webapp {boolean} If true, the command will open the webapps URL.
* @param cmd.creds {boolean} If true, the command will open the credentials URL.
*/
export const openCmd = async (scriptId: any, cmd: {
webapp: boolean,
creds: boolean,
}) => {
await checkIfOnline();
const projectSettings = await getProjectSettings();
if (!scriptId) scriptId = projectSettings.scriptId;
if (scriptId.length < 30) {
return logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
// We've specified to open creds.
if (cmd.creds) {
const projectId = projectSettings.projectId;
if (!projectId) {
return logError(null, ERROR.NO_GCLOUD_PROJECT);
}
console.log(LOG.OPEN_CREDS(projectId));
return open(URL.CREDS(projectId), { wait: false });
}

// If we're not a web app, open the script URL.
if (!cmd.webapp) {
console.log(LOG.OPEN_PROJECT(scriptId));
return open(URL.SCRIPT(scriptId), { wait: false });
}

// Web app: Otherwise, open the latest deployment.
await loadAPICredentials();
const deploymentsList = await script.projects.deployments.list({
scriptId,
});
if (deploymentsList.status !== 200) {
return logError(deploymentsList.statusText);
}
const deployments = deploymentsList.data.deployments || [];
if (!deployments.length) {
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
const choices = deployments
.sort((d1: any, d2: any) => d1.updateTime.localeCompare(d2.updateTime))
.map((deployment: any) => {
const DESC_PAD_SIZE = 30;
const id = deployment.deploymentId;
const description = deployment.deploymentConfig.description;
const versionNumber = deployment.deploymentConfig.versionNumber;
return {
name:
padEnd(ellipsize(description || '', DESC_PAD_SIZE), DESC_PAD_SIZE) +
`@${padEnd(versionNumber || 'HEAD', 4)} - ${id}`,
value: deployment,
};
});
const answers = await prompt([
{
type: 'list',
name: 'deployment',
message: 'Open which deployment?',
choices,
},
]);
console.log(LOG.OPEN_WEBAPP(answers.deployment.deploymentId));
open(getWebApplicationURL(answers.deployment), { wait: false });
};

/**
* Acts as a router to apis subcommands
* Calls functions for list, enable, or disable
Expand Down
94 changes: 94 additions & 0 deletions src/commands/openCmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
loadAPICredentials,
script,
} from './../auth';

import { URL } from './../urls';

import {
checkIfOnline,
ERROR,
getProjectSettings,
getWebApplicationURL,
LOG,
logError,
} from './../utils';

const ellipsize = require('ellipsize');
const open = require('opn');
const padEnd = require('string.prototype.padend');

// setup inquirer
const inquirer = require('inquirer');
const prompt = inquirer.prompt;
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

/**
* Opens an Apps Script project's script.google.com editor.
* @param scriptId {string} The Apps Script project to open.
* @param cmd.webapp {boolean} If true, the command will open the webapps URL.
* @param cmd.creds {boolean} If true, the command will open the credentials URL.
*/
export default async (scriptId: any, cmd: {
webapp: boolean,
creds: boolean,
}) => {
await checkIfOnline();
const projectSettings = await getProjectSettings();
if (!scriptId) scriptId = projectSettings.scriptId;
if (scriptId.length < 30) {
return logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
// We've specified to open creds.
if (cmd.creds) {
const projectId = projectSettings.projectId;
if (!projectId) {
return logError(null, ERROR.NO_GCLOUD_PROJECT);
}
console.log(LOG.OPEN_CREDS(projectId));
return open(URL.CREDS(projectId), { wait: false });
}

// If we're not a web app, open the script URL.
if (!cmd.webapp) {
console.log(LOG.OPEN_PROJECT(scriptId));
return open(URL.SCRIPT(scriptId), { wait: false });
}

// Web app: Otherwise, open the latest deployment.
await loadAPICredentials();
const deploymentsList = await script.projects.deployments.list({
scriptId,
});
if (deploymentsList.status !== 200) {
return logError(deploymentsList.statusText);
}
const deployments = deploymentsList.data.deployments || [];
if (!deployments.length) {
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
const choices = deployments
.sort((d1: any, d2: any) => d1.updateTime.localeCompare(d2.updateTime))
.map((deployment: any) => {
const DESC_PAD_SIZE = 30;
const id = deployment.deploymentId;
const description = deployment.deploymentConfig.description;
const versionNumber = deployment.deploymentConfig.versionNumber;
return {
name:
padEnd(ellipsize(description || '', DESC_PAD_SIZE), DESC_PAD_SIZE) +
`@${padEnd(versionNumber || 'HEAD', 4)} - ${id}`,
value: deployment,
};
});
const answers = await prompt([
{
type: 'list',
name: 'deployment',
message: 'Open which deployment?',
choices,
},
]);
console.log(LOG.OPEN_WEBAPP(answers.deployment.deploymentId));
open(getWebApplicationURL(answers.deployment), { wait: false });
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
deployments,
login,
logs,
openCmd,
run,
setting,
undeploy,
Expand All @@ -47,6 +46,7 @@ import logout from './commands/logout';
import versions from './commands/versions';
import defaultCmd from './commands/defaultCmd';
import help from './commands/help';
import openCmd from './commands/openCmd';

// const commander = require('commander');

Expand Down

0 comments on commit 36c9491

Please sign in to comment.