import moment from 'moment'; import ora from 'ora'; import path from 'path'; import { pick } from 'lodash'; import postLog from '../service/post.log'; import logger from '../utils/logger'; import registerFunctionRunCommand from './function'; import cloudSpaceList from '../service/cloud/space.list'; import cloudFunctionList from '../service/cloud/function.list'; import deployCloudFunction from '../service/cloud/deploy.function'; import downloadCloudFunction from '../service/cloud/download.function'; import updateFunctionHttpTrigger from '../service/cloud/httptrigger.function'; import { Command } from 'commander'; async function cloudSpaceListAction(options) { const appId = options.appId; const action = 'CLOUD_SPACE_LIST'; try { postLog({ action, appId, }); const list = await cloudSpaceList(appId); if (!list.length) { console.warn('暂无云开发服务空间'); return; } console.log(`该账号下的云开发服务空间列表:`); list.forEach((item) => { console.log(`* 空间名称: ${item.name}; 空间ID: ${item.appWorkspaceId}`); }); } catch (e) { logger.error(e, { action, }); } } async function cloudFunctionListAction(options) { const params = pick(options, ['appId', 'spaceId']); const action = 'CLOUD_FUNCTION_LIST'; try { postLog({ action, appId: params.appId, }); const list = await cloudFunctionList(params); if (!list.length) { console.log('该服务空间下暂无云函数。'); return; } console.log(`空间ID:${params.spaceId} 下的云函数列表:`); list.forEach((value) => { console.log( `* %s 最新修改时间`, value.name, moment(value.modifiedAt).format('YYYY-MM-DD HH:mm:ss') ); }); } catch (e) { logger.error(e, { action, }); } } async function deployCloudFunctionAction(options) { const params = pick(options, ['appId', 'spaceId']); const spinner = ora(); const action = 'CLOUD_FUNCTION_DEPLOY'; const project = path.resolve(options.project); const names = options.name; try { postLog({ action, appId: params.appId, }); for (let i = 0; i < names.length; i++) { const name = names[i]; spinner.start(`开始部署云函数: ${name}`); const res = await deployCloudFunction({ project, name, onProgressUpdate(info) { const { status, data } = info; if (status === 'LOCAL_PACKAGE') { spinner.succeed(`本地打包完成: ${data}`); spinner.start('开始上传云函数代码...'); return; } if (status === 'UPLOAD_SUCCESS') { spinner.succeed('云函数代码上传完成'); spinner.start('开始部署云函数...'); return; } }, ...params, }); spinner.succeed(`空间${params.spaceId}下的${res.name}云函数部署成功!`); } } catch (e) { spinner.stop(); logger.error(e, { action, }); } } async function downloadCloudFunctionAction(options) { const params = pick(options, ['appId', 'spaceId']); const spinner = ora('开始下载云函数,请稍等...').start(); const action = 'CLOUD_FUNCTION_DOWNLOAD'; try { postLog({ action, appId: params.appId, }); await downloadCloudFunction({ unzip: true, path: options.path, name: options.name, ...params, }); spinner.succeed(`云函数下载成功: ${options.path}`); } catch (e) { spinner.stop(); logger.error(e, { action, }); } } async function setCloudFunctionHttpTriggerAction(options) { const params = pick(options, ['appId', 'spaceId']); const spinner = ora('开始设置云函数HTTP触发,请稍等...').start(); const action = 'CLOUD_FUNCTION_HTTP'; try { postLog({ action, appId: params.appId, }); const res = await updateFunctionHttpTrigger({ enable: options.enable, spaceId: params.spaceId, appId: params.appId, name: options.name, httpTriggerPath: options.path, }); if (res.enable) { spinner.succeed(`空间${params.spaceId}下的${options.name}云函数成功开启HTTP触发器功能!`); spinner.succeed(`云函数HTTP触发全路径为:${res.httpTriggerPath}`); } else { spinner.succeed(`空间${params.spaceId}已关闭HTTP触发器功能!`); } } catch (e) { spinner.stop(); logger.error(e, { action, }); } } /** * 云开发操作 */ export default function registerCloudCommand(program: Command) { const child = program.command('cloud').description('小程序云开发相关操作'); const spaceChild = child.command('space').description('服务空间相关操作'); const functionChild = child.command('function').description('云函数相关操作'); // // 云开发服务空间 spaceChild .command('list') .requiredOption('-i, --app-id ', '小程序appId') .description('查询云开发服务空间') .action(cloudSpaceListAction); // 云函数 functionChild .command('list') .requiredOption('-i, --app-id ', '小程序appId') .requiredOption('-s, --space-id ', '云开发服务空间ID') .description('查询服务空间云函数列表') .action(cloudFunctionListAction); functionChild .command('deploy') .storeOptionsAsProperties(false) .requiredOption('-i, --app-id ', '小程序appId') .requiredOption('-s, --space-id ', '云开发服务空间ID') .requiredOption('-n, --name ', '云函数名') .requiredOption('-p, --project ', '云函数本地文件地址') .description('上传部署云函数') .action(deployCloudFunctionAction); functionChild .command('http') .storeOptionsAsProperties(false) .requiredOption('-i, --app-id ', '小程序appId') .requiredOption('-s, --space-id ', '云开发服务空间ID') .requiredOption('-n, --name ', '云函数名') .requiredOption('-p, --path ', '云函数HTTP触发路径,须以/http开头') .option('--enable', '开启HTTP触发功能', true) .option('--no-enable', '关闭HTTP触发功能') .description('设置云函数HTTP触发器') .action(setCloudFunctionHttpTriggerAction); // 解决name不能作为参数的问题 functionChild .command('download') .description('下载云函数') .storeOptionsAsProperties(false) .requiredOption('-i, --app-id ', '小程序appId') .requiredOption('-s, --space-id ', '云开发服务空间ID') .requiredOption('-p, --path ', '下载后保存路径') .requiredOption('-n, --name ', '云函数名') .action(downloadCloudFunctionAction); registerFunctionRunCommand(functionChild); }