@google/clasp
Version:
Develop Apps Script Projects locally
774 lines (773 loc) • 33.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Clasp command method bodies.
*/
var del = require("del");
var pluralize = require("pluralize");
var watch_1 = require("watch");
var auth_1 = require("./auth");
var files_1 = require("./files");
var utils_1 = require("./utils");
var open = require('opn');
var commander = require('commander');
var chalk = require('chalk');
var prompt = require('inquirer').prompt;
var padEnd = require('string.prototype.padend');
/**
* Force downloads all Apps Script project files into the local filesystem.
*/
exports.pull = function () { return __awaiter(_this, void 0, void 0, function () {
var _a, scriptId, rootDir;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_b.sent();
return [4 /*yield*/, utils_1.getProjectSettings()];
case 2:
_a = _b.sent(), scriptId = _a.scriptId, rootDir = _a.rootDir;
if (scriptId) {
utils_1.spinner.setSpinnerTitle(utils_1.LOG.PULLING);
files_1.fetchProject(scriptId, rootDir);
}
return [2 /*return*/];
}
});
}); };
/**
* Uploads all files into the script.google.com filesystem.
* TODO: Only push when a non-ignored file is changed.
* TODO: Only push the specific files that changed (rather than all files).
* @param cmd.watch {boolean} If true, runs `clasp push` when any local file changes. Exit with ^C.
*/
exports.push = function (cmd) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
if (cmd.watch) {
console.log(utils_1.LOG.PUSH_WATCH);
// @see https://www.npmjs.com/package/watch
watch_1.watchTree('.', function (f, curr, prev) {
if (typeof f === 'string') { // The first watch doesn't give a string for some reason.
console.log("\n" + utils_1.LOG.PUSH_WATCH_UPDATED(f) + "\n");
}
console.log(utils_1.LOG.PUSHING);
files_1.pushFiles();
});
}
else {
utils_1.spinner.setSpinnerTitle(utils_1.LOG.PUSHING).start();
files_1.pushFiles();
}
return [2 /*return*/];
}
});
}); };
/**
* Outputs the help command.
*/
exports.help = function () {
commander.outputHelp();
process.exit(1);
};
/**
* Displays a default message when an unknown command is typed.
* @param command {string} The command that was typed.
*/
exports.defaultCmd = function (command) {
utils_1.logError(null, utils_1.ERROR.COMMAND_DNE(command));
};
/**
* Creates a new Apps Script project.
* @param title {string} The title of the Apps Script project's file
* @param parentId {string} The Drive ID of the G Suite doc this script is bound to.
*/
exports.create = function (title, parentId) { return __awaiter(_this, void 0, void 0, function () {
var scriptId, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
if (!files_1.hasProject()) return [3 /*break*/, 2];
utils_1.logError(null, utils_1.ERROR.FOLDER_EXISTS);
return [3 /*break*/, 10];
case 2: return [4 /*yield*/, auth_1.loadAPICredentials()];
case 3:
_a.sent();
if (!!title) return [3 /*break*/, 5];
return [4 /*yield*/, prompt([{
type: 'input',
name: 'title',
message: 'Give a script title:',
default: utils_1.LOG.UNTITLED_SCRIPT_TITLE,
}]).then(function (answers) {
title = answers.title;
}).catch(function (err) {
console.log(err);
})];
case 4:
_a.sent();
_a.label = 5;
case 5:
utils_1.spinner.setSpinnerTitle(utils_1.LOG.CREATE_PROJECT_START(title)).start();
_a.label = 6;
case 6:
_a.trys.push([6, 8, , 9]);
return [4 /*yield*/, utils_1.getProjectSettings(true)];
case 7:
scriptId = (_a.sent()).scriptId;
if (scriptId) {
utils_1.logError(null, utils_1.ERROR.NO_NESTED_PROJECTS);
process.exit(1);
}
return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();
return [3 /*break*/, 9];
case 9:
auth_1.script.projects.create({ title: title, parentId: parentId }, {}).then(function (res) {
utils_1.spinner.stop(true);
var createdScriptId = res.data.scriptId;
console.log(utils_1.LOG.CREATE_PROJECT_FINISH(createdScriptId));
utils_1.saveProjectId(createdScriptId);
if (!utils_1.manifestExists()) {
files_1.fetchProject(createdScriptId); // fetches appsscript.json, o.w. `push` breaks
}
}).catch(function (error) {
utils_1.spinner.stop(true);
utils_1.logError(error, utils_1.ERROR.CREATE);
});
_a.label = 10;
case 10: return [2 /*return*/];
}
});
}); };
/**
* Fetches an Apps Script project.
* Prompts the user if no script ID is provided.
* @param scriptId {string} The Apps Script project ID to fetch.
* @param versionNumber {string} An optional version to pull the script from.
*/
exports.clone = function (scriptId, versionNumber) { return __awaiter(_this, void 0, void 0, function () {
var data, files, fileIds;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
if (!files_1.hasProject()) return [3 /*break*/, 2];
utils_1.logError(null, utils_1.ERROR.FOLDER_EXISTS);
return [3 /*break*/, 9];
case 2:
if (!!scriptId) return [3 /*break*/, 8];
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 3:
_a.sent();
return [4 /*yield*/, auth_1.drive.files.list({
pageSize: 10,
fields: 'files(id, name)',
q: 'mimeType="application/vnd.google-apps.script"',
})];
case 4:
data = (_a.sent()).data;
files = data.files;
if (!files.length) return [3 /*break*/, 6];
fileIds = files.map(function (file) {
return {
name: padEnd(file.name, 20) + " - (" + file.id + ")",
value: file.id,
};
});
return [4 /*yield*/, prompt([{
type: 'list',
name: 'scriptId',
message: 'Clone which script? ',
choices: fileIds,
}]).then(function (answers) {
utils_1.checkIfOnline();
utils_1.spinner.setSpinnerTitle(utils_1.LOG.CLONING);
utils_1.saveProjectId(answers.scriptId);
files_1.fetchProject(answers.scriptId, '', versionNumber);
}).catch(function (err) {
console.log(err);
})];
case 5:
_a.sent();
return [3 /*break*/, 7];
case 6:
console.log(utils_1.LOG.FINDING_SCRIPTS_DNE);
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
utils_1.spinner.setSpinnerTitle(utils_1.LOG.CLONING);
utils_1.saveProjectId(scriptId);
files_1.fetchProject(scriptId, '', versionNumber);
_a.label = 9;
case 9: return [2 /*return*/];
}
});
}); };
/**
* Logs out the user by deleting credentials.
*/
exports.logout = function () {
del(utils_1.DOT.RC.ABSOLUTE_PATH, { force: true }); // del doesn't work with a relative path (~)
del(utils_1.DOT.RC.ABSOLUTE_LOCAL_PATH, { force: true });
};
/**
* Prints StackDriver logs from this Apps Script project.
* @param cmd.json {boolean} If true, the command will output logs as json.
* @param cmd.open {boolean} If true, the command will open the StackDriver logs website.
*/
exports.logs = function (cmd) { return __awaiter(_this, void 0, void 0, function () {
function printLogs(entries) {
for (var i = 0; i < 50 && entries ? i < entries.length : i < 0; ++i) {
var _a = entries[i], severity = _a.severity, timestamp = _a.timestamp, resource = _a.resource, textPayload = _a.textPayload, protoPayload = _a.protoPayload, jsonPayload = _a.jsonPayload;
var functionName = resource.labels.function_name;
functionName = functionName ? padEnd(functionName, 15) : utils_1.ERROR.NO_FUNCTION_NAME;
var payloadData = '';
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
}
else {
var data_1 = {
textPayload: textPayload,
jsonPayload: jsonPayload ? jsonPayload.fields.message.stringValue : '',
protoPayload: protoPayload,
};
payloadData = data_1.textPayload || data_1.jsonPayload || data_1.protoPayload || utils_1.ERROR.PAYLOAD_UNKNOWN;
if (payloadData && payloadData['@type'] === 'type.googleapis.com/google.cloud.audit.AuditLog') {
payloadData = utils_1.LOG.STACKDRIVER_SETUP;
functionName = padEnd(protoPayload.methodName, 15);
}
if (payloadData && typeof (payloadData) === 'string') {
payloadData = padEnd(payloadData, 20);
}
}
var coloredStringMap = {
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity),
};
var coloredSeverity = coloredStringMap[severity] || severity;
coloredSeverity = padEnd(String(coloredSeverity), 20);
console.log(coloredSeverity + " " + timestamp + " " + functionName + " " + payloadData);
}
}
var projectId, url, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, utils_1.getProjectSettings()];
case 2:
projectId = (_a.sent()).projectId;
if (!projectId)
utils_1.logError(null, utils_1.ERROR.NO_GCLOUD_PROJECT);
if (cmd.open) {
url = 'https://console.cloud.google.com/logs/viewer?project=' +
(projectId + "&resource=app_script_function");
console.log("Opening logs: " + url);
open(url);
process.exit(0);
}
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 3:
_a.sent();
return [4 /*yield*/, auth_1.logger.entries.list({
resourceNames: [
"projects/" + projectId,
],
orderBy: 'timestamp desc',
})];
case 4:
data = (_a.sent()).data;
printLogs(data.entries);
return [2 /*return*/];
}
});
}); };
/**
* Executes an Apps Script function. Requires additional setup.
* @param functionName {string} The function name within the Apps Script project.
* @see https://developers.google.com/apps-script/api/how-tos/execute
*/
exports.run = function (functionName) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.getProjectSettings().then(function (_a) {
var scriptId = _a.scriptId;
var params = {
scriptId: scriptId,
function: functionName,
devMode: false,
};
auth_1.script.scripts.run(params).then(function (response) {
console.log(response.data);
}).catch(function (e) {
console.log(e);
});
});
return [2 /*return*/];
}
});
}); };
/**
* Deploys an Apps Script project.
* @param version {string} The project version to deploy at.
* @param description {string} The deployment's description.
*/
exports.deploy = function (version, description) { return __awaiter(_this, void 0, void 0, function () {
function createDeployment(versionNumber) {
utils_1.spinner.setSpinnerTitle(utils_1.LOG.DEPLOYMENT_CREATE);
auth_1.script.projects.deployments.create({
scriptId: scriptId,
resource: {
versionNumber: versionNumber,
manifestFileName: utils_1.PROJECT_MANIFEST_BASENAME,
description: description,
},
}, {}, function (err, response) {
utils_1.spinner.stop(true);
if (err) {
utils_1.logError(null, utils_1.ERROR.DEPLOYMENT_COUNT);
}
else if (response) {
console.log("- " + response.data.deploymentId + " @" + versionNumber + ".");
}
});
}
var scriptId, versionRequestBody;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
description = description || '';
return [4 /*yield*/, utils_1.getProjectSettings()];
case 3:
scriptId = (_a.sent()).scriptId;
if (!scriptId)
return [2 /*return*/];
utils_1.spinner.setSpinnerTitle(utils_1.LOG.DEPLOYMENT_START(scriptId)).start();
versionRequestBody = {
description: description,
};
if (version) {
createDeployment(version);
}
else { // if no version, create a new version and deploy that
auth_1.script.projects.versions.create({
scriptId: scriptId,
resource: versionRequestBody,
}, {}, function (err, _a) {
var data = _a.data;
utils_1.spinner.stop(true);
if (err) {
utils_1.logError(null, utils_1.ERROR.ONE_DEPLOYMENT_CREATE);
}
else {
console.log(utils_1.LOG.VERSION_CREATED(data.versionNumber));
createDeployment(data.versionNumber);
}
});
}
return [2 /*return*/];
}
});
}); };
/**
* Removes a deployment from the Apps Script project.
* @param deploymentId {string} The deployment's ID
*/
exports.undeploy = function (deploymentId) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.getProjectSettings().then(function (_a) {
var scriptId = _a.scriptId;
if (!scriptId)
return;
utils_1.spinner.setSpinnerTitle(utils_1.LOG.UNDEPLOYMENT_START(deploymentId)).start();
auth_1.script.projects.deployments.delete({
scriptId: scriptId,
deploymentId: deploymentId,
}, {}, function (err, res) {
utils_1.spinner.stop(true);
if (err) {
utils_1.logError(null, utils_1.ERROR.READ_ONLY_DELETE);
}
else {
console.log(utils_1.LOG.UNDEPLOYMENT_FINISH(deploymentId));
}
});
});
return [2 /*return*/];
}
});
}); };
/**
* Lists a user's Apps Script projects using Google Drive.
*/
exports.list = function () { return __awaiter(_this, void 0, void 0, function () {
var res, files;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.spinner.setSpinnerTitle(utils_1.LOG.FINDING_SCRIPTS).start();
return [4 /*yield*/, auth_1.drive.files.list({
pageSize: 50,
fields: 'nextPageToken, files(id, name)',
q: 'mimeType="application/vnd.google-apps.script"',
})];
case 3:
res = _a.sent();
utils_1.spinner.stop(true);
files = res.data.files;
if (files.length) {
files.map(function (file) {
console.log(padEnd(file.name, 20) + " \u2013 " + utils_1.getScriptURL(file.id));
});
}
else {
console.log(utils_1.LOG.FINDING_SCRIPTS_DNE);
}
return [2 /*return*/];
}
});
}); };
/**
* Redeploys an Apps Script deployment.
* @param deploymentId {string} The deployment ID to redeploy.
* @param version {string} The version to redeploy at.
* @param description {string} A description of the redeployment.
*/
exports.redeploy = function (deploymentId, version, description) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.getProjectSettings().then(function (_a) {
var scriptId = _a.scriptId;
auth_1.script.projects.deployments.update({
scriptId: scriptId,
deploymentId: deploymentId,
resource: {
deploymentConfig: {
versionNumber: version,
manifestFileName: utils_1.PROJECT_MANIFEST_BASENAME,
description: description,
},
},
}, {}, function (error, res) {
utils_1.spinner.stop(true);
if (error) {
utils_1.logError(null, error); // TODO prettier error
}
else {
console.log(utils_1.LOG.REDEPLOY_END);
}
});
});
return [2 /*return*/];
}
});
}); };
/**
* Lists a script's deployments.
*/
exports.deployments = function () { return __awaiter(_this, void 0, void 0, function () {
var scriptId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
return [4 /*yield*/, utils_1.getProjectSettings()];
case 3:
scriptId = (_a.sent()).scriptId;
if (!scriptId)
return [2 /*return*/];
utils_1.spinner.setSpinnerTitle(utils_1.LOG.DEPLOYMENT_LIST(scriptId)).start();
auth_1.script.projects.deployments.list({
scriptId: scriptId,
}, {}, function (error, _a) {
var data = _a.data;
utils_1.spinner.stop(true);
if (error) {
utils_1.logError(error);
}
else {
var deployments_1 = data.deployments;
var numDeployments = deployments_1.length;
var deploymentWord = pluralize('Deployment', numDeployments);
console.log(numDeployments + " " + deploymentWord + ".");
deployments_1.map(function (_a) {
var deploymentId = _a.deploymentId, deploymentConfig = _a.deploymentConfig;
var versionString = !!deploymentConfig.versionNumber ?
"@" + deploymentConfig.versionNumber : '@HEAD';
var description = deploymentConfig.description ?
'- ' + deploymentConfig.description : '';
console.log("- " + deploymentId + " " + versionString + " " + description);
});
}
});
return [2 /*return*/];
}
});
}); };
/**
* Lists versions of an Apps Script project.
*/
exports.versions = function () { return __awaiter(_this, void 0, void 0, function () {
var scriptId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.spinner.setSpinnerTitle('Grabbing versions...').start();
return [4 /*yield*/, utils_1.getProjectSettings()];
case 3:
scriptId = (_a.sent()).scriptId;
auth_1.script.projects.versions.list({
scriptId: scriptId,
pageSize: 500,
}, {}, function (error, _a) {
var data = _a.data;
utils_1.spinner.stop(true);
if (error) {
utils_1.logError(error);
}
else {
if (data && data.versions && data.versions.length) {
var numVersions = data.versions.length;
console.log(utils_1.LOG.VERSION_NUM(numVersions));
data.versions.reverse().map(function (version) {
console.log(utils_1.LOG.VERSION_DESCRIPTION(version));
});
}
else {
utils_1.logError(null, utils_1.LOG.DEPLOYMENT_DNE);
}
}
});
return [2 /*return*/];
}
});
}); };
/**
* Creates a new version of an Apps Script project.
*/
exports.version = function (description) { return __awaiter(_this, void 0, void 0, function () {
var scriptId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
return [4 /*yield*/, auth_1.loadAPICredentials()];
case 2:
_a.sent();
utils_1.spinner.setSpinnerTitle(utils_1.LOG.VERSION_CREATE).start();
return [4 /*yield*/, utils_1.getProjectSettings()];
case 3:
scriptId = (_a.sent()).scriptId;
auth_1.script.projects.versions.create({
scriptId: scriptId,
description: description,
}, {}, function (error, _a) {
var data = _a.data;
utils_1.spinner.stop(true);
if (error) {
utils_1.logError(error);
}
else {
console.log(utils_1.LOG.VERSION_CREATED(data.versionNumber));
}
});
return [2 /*return*/];
}
});
}); };
/**
* Displays the status of which Apps Script files are ignored from .claspignore
* @param cmd.json {boolean} Displays the status in json format.
*/
exports.status = function (cmd) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_a.sent();
utils_1.getProjectSettings().then(function (_a) {
var scriptId = _a.scriptId, rootDir = _a.rootDir;
if (!scriptId)
return;
files_1.getProjectFiles(rootDir, function (err, projectFiles) {
if (err)
return console.log(err);
else if (projectFiles) {
var filesToPush = projectFiles[0], untrackedFiles = projectFiles[1];
if (cmd.json) {
console.log(JSON.stringify({ filesToPush: filesToPush, untrackedFiles: untrackedFiles }));
}
else {
console.log(utils_1.LOG.STATUS_PUSH);
filesToPush.forEach(function (file) { return console.log("\u2514\u2500 " + file); });
console.log(utils_1.LOG.STATUS_IGNORE);
untrackedFiles.forEach(function (file) { return console.log("\u2514\u2500 " + file); });
}
}
});
});
return [2 /*return*/];
}
});
}); };
/**
* Opens an Apps Script project's script.google.com editor.
* @param scriptId {string} The Apps Script project to open.
*/
exports.openCmd = function (scriptId) { return __awaiter(_this, void 0, void 0, function () {
var settings;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!scriptId) return [3 /*break*/, 2];
return [4 /*yield*/, utils_1.getProjectSettings()];
case 1:
settings = _a.sent();
scriptId = settings.scriptId;
_a.label = 2;
case 2:
if (scriptId.length < 30) {
utils_1.logError(null, utils_1.ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
else {
console.log(utils_1.LOG.OPEN_PROJECT(scriptId));
open(utils_1.getScriptURL(scriptId));
process.exit(0);
}
return [2 /*return*/];
}
});
}); };
/**
* Acts as a router to apis subcommands
* Calls functions for list, enable, or disable
* Otherwise returns an error of command not supported
*/
exports.apis = function () { return __awaiter(_this, void 0, void 0, function () {
var list, subcommand, command;
var _this = this;
return __generator(this, function (_a) {
list = function () { return __awaiter(_this, void 0, void 0, function () {
var data, _i, _a, api;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, utils_1.checkIfOnline()];
case 1:
_b.sent();
return [4 /*yield*/, auth_1.discovery.apis.list({
preferred: true,
})];
case 2:
data = (_b.sent()).data;
for (_i = 0, _a = data.items; _i < _a.length; _i++) {
api = _a[_i];
console.log(padEnd(api.name, 25) + " - " + padEnd(api.id, 30));
}
return [2 /*return*/];
}
});
}); };
subcommand = process.argv[3];
command = {
list: list,
enable: function () { console.log('In development...'); },
disable: function () { console.log('In development...'); },
};
if (command[subcommand]) {
command[subcommand]();
}
else {
utils_1.logError(null, utils_1.ERROR.COMMAND_DNE('apis ' + subcommand));
}
return [2 /*return*/];
});
}); };