|
| 1 | +const inquirer = require('inquirer'); |
| 2 | +const chalk = require('chalk'); |
| 3 | +const fse = require('fs-extra'); |
| 4 | +const path = require('path'); |
| 5 | +const SDK = require('./lib/sdk'); |
| 6 | +const pack = require('./lib/pack'); |
| 7 | +const { input, envEditor } = require('./lib/prompt'); |
| 8 | + |
| 9 | +module.exports = async (fn = '') => { |
| 10 | + const FunctionName = fn || await input(); |
| 11 | + const prompts = await inquirer.prompt([ |
| 12 | + { |
| 13 | + type: 'input', |
| 14 | + name: 'MemorySize', |
| 15 | + message: '函数运行时内存大小, 以 128MB 为阶梯', |
| 16 | + default: 128, |
| 17 | + validate: v => ((~~v >= 128 && ~~v <= 1536 && ~~v % 128 === 0) ? true : '可选范围 128MB-1536MB,并且以 128MB 为阶梯') |
| 18 | + }, |
| 19 | + { |
| 20 | + type: 'input', |
| 21 | + name: 'Timeout', |
| 22 | + message: '函数最长执行时间, 单位为秒', |
| 23 | + default: 3, |
| 24 | + validate: v => ((~~v >= 1 && ~~v <= 300) ? true : '可选值范围 1-300 秒') |
| 25 | + } |
| 26 | + ]); |
| 27 | + const env = await envEditor(); |
| 28 | + |
| 29 | + const zip = await pack(); |
| 30 | + |
| 31 | + const sdk = SDK(); |
| 32 | + const { |
| 33 | + Response: { |
| 34 | + Error: { |
| 35 | + Message = '' |
| 36 | + } = {} |
| 37 | + } |
| 38 | + } = await sdk.CreateFunction({ |
| 39 | + FunctionName, |
| 40 | + 'Code.ZipFile': zip, |
| 41 | + Handler: 'index.main_handler', |
| 42 | + ...prompts, |
| 43 | + ...env, |
| 44 | + Runtime: 'Nodejs8.9' |
| 45 | + }); |
| 46 | + if (Message === '') { |
| 47 | + // eslint-disable-next-line no-console |
| 48 | + console.log(chalk`{green.bold 创建成功!}`); |
| 49 | + fse.copySync(path.join(__dirname, '../template'), `./${FunctionName}`); |
| 50 | + } else { |
| 51 | + // eslint-disable-next-line no-console |
| 52 | + console.log(chalk`{red.bold 创建失败:} ${Message}`); |
| 53 | + } |
| 54 | +}; |
0 commit comments