@google/clasp
Version:
Develop Apps Script Projects locally
62 lines (61 loc) • 2.94 kB
JavaScript
import chalk from 'chalk';
import { Command } from 'commander';
import fuzzy from 'fuzzy';
import autocomplete from 'inquirer-autocomplete-standalone';
import { intl } from '../intl.js';
import { isInteractive, withSpinner } from './utils.js';
export const command = new Command('run-function')
.alias('run')
.description('Run a function in your Apps Scripts project')
.argument('[functionName]', 'The name of the function to run')
.option('--nondev', 'Run script function in non-devMode')
.option('-p, --params <value>', 'Parameters to pass to the function, as a JSON-encoded array')
.action(async function (functionName, options) {
var _a, _b;
const clasp = this.opts().clasp;
const devMode = !options.nondev; // Defaults to true
let params = [];
if (options.params) {
params = JSON.parse(options.params);
}
if (!functionName && isInteractive()) {
const allFunctions = await clasp.functions.getFunctionNames();
const source = async (input = '') => fuzzy.filter(input, allFunctions).map(element => ({
value: element.original,
}));
const prompt = intl.formatMessage({ id: "Y8u3Vb", defaultMessage: [{ type: 0, value: "Selection a function name" }] });
functionName = await autocomplete({
message: prompt,
source,
});
}
try {
const { error, response } = await withSpinner(`Running function: ${functionName}`, async () => {
return await clasp.functions.runFunction(functionName, params, devMode);
});
if (error && error.details) {
const { errorMessage, scriptStackTraceElements } = error.details[0];
const msg = intl.formatMessage({ id: "1l462L", defaultMessage: [{ type: 0, value: "Exception:" }] });
console.error(`${chalk.red(msg)}`, errorMessage, scriptStackTraceElements || []);
return;
}
if (response && response.result !== undefined) {
console.log(response.result);
}
else {
const msg = intl.formatMessage({ id: "S/0IKk", defaultMessage: [{ type: 0, value: "No response." }] });
console.log(chalk.red(msg));
}
}
catch (error) {
if (((_a = error.cause) === null || _a === void 0 ? void 0 : _a.code) === 'NOT_AUTHORIZED') {
const msg = intl.formatMessage({ id: "HZmND2", defaultMessage: [{ type: 0, value: "Unable to run script function. Please make sure you have permission to run the script function." }] });
this.error(msg);
}
if (((_b = error.cause) === null || _b === void 0 ? void 0 : _b.code) === 'NOT_FOUND') {
const msg = intl.formatMessage({ id: "4wxpit", defaultMessage: [{ type: 0, value: "Script function not found. Please make sure script is deployed as API executable." }] });
this.error(msg);
}
throw error;
}
});