create-omni-func
v0.0.28
Published
A scaffold for initialing EF omni serverless app.
Downloads
29
Readme
create-omni-func Usage Guide
create-omni-func
is a command-line tool for creating Serverless Cloud Functions (SCF) or adding new entry points in omni-serverless
or omni-web
projects.
Scaffolding
Creating a New Function
To create a new function in omni-serverless
, run the following command in the project root:
npx create-omni-func@latest
This command will prompt you for the following information:
- App Name: Application name (defaults to the target directory name).
- Remove existing files and continue?: If the target directory is not empty, decide whether to remove existing files and continue.
- Package Name: Package name (if the target directory name is not a valid npm package name).
- Description: Project description.
- Add TypeScript?: Whether to add TypeScript support.
- Template: Choose a template (e.g.,
scf-webpack
).
You will need to input the function name and description, and choose the template type. If you attempt to create a function that already exists, the command will ask whether you'd like to override it.
Since pnpm
is used as the package manager, after creating a new function, simply run pnpm i
to install the dependencies. To add a new dependency, use pnpm add <dependency>
.
Creating a New Entry Point
After creating the function, you can run the command again in the function's directory:
npx create-omni-func@latest
This command will prompt you to choose the entry point type:
- API: API Gateway handlers.
- Kafka: Kafka handlers.
- Batch: Batch handlers.
- Custom: Custom handlers.
Based on your selection, the appropriate handler scheme will be generated and injected into the eventHandler.
You can code in the handler folders, but you can remove them if you prefer to code in your own way.
Testing
Unit Testing
The scaffolder utilizes vitest
to enable unit testing. Basic testing settings are configured in vitest.config.js
.
All tests should be coded in the tests
folder, with filenames ending in test.ts
or test.js
.
For a coding guide or usage information for unit testing, visit: Vitest Guide.
Test UI
Testing can be automatically detected by VSCode
or WebStorm
, and can be found in the test tab.
Additionally, there is a vitest
UI which can be started with the command pnpm test-ui
. This will launch a web UI for managing and reporting tests.
Deployment
Use the following commands to deploy your function:
pnpm deploy:stg
to deploy to theomni-stg
namespace.pnpm deploy:live
to deploy to theomni-live
namespace.
Environment Variables
During deployment, the scripts will load environment variables first from the function root and then from the project root. This means you only need to set variables specific to the function.
create-omni-func 使用指南
create-omni-func
是一个命令行工具,用于创建无服务器云函数(SCF)或在 omni-serverless
或 omni-web
项目中添加新的入口点。
脚手架
创建新函数
要在 omni-serverless
中创建一个新函数,请在项目根目录运行以下命令:
npx create-omni-func@latest
该命令会提示您输入以下信息:
- 应用名称:应用名称(默认为目标目录名)。
- 移除现有文件并继续?:如果目标目录不为空,决定是否移除现有文件并继续。
- 包名:包名(如果目标目录名不是有效的 npm 包名)。
- 描述:项目描述。
- 添加 TypeScript?:是否添加 TypeScript 支持。
- 模板:选择一个模板(例如
scf-webpack
)。
您需要输入函数名称和描述,并选择模板类型。如果您尝试创建已存在的函数,命令会询问是否覆盖。
由于使用 pnpm
作为包管理器,创建新函数后,只需运行 pnpm i
安装依赖项。要添加新依赖项,请使用 pnpm add <dependency>
。
创建新入口点
在创建函数后,您可以在该函数的目录中再次运行该命令:
npx create-omni-func@latest
该命令会提示您选择入口点类型:
- API:API 网关处理器。
- Kafka:Kafka 处理器。
- Batch:批处理器。
- Custom:自定义处理器。
根据您的选择,适当的处理器方案将生成并注入到 eventHandler
中。
您可以在处理器文件夹中编写代码,但如果您更喜欢以自己的方式编写代码,可以删除这些文件夹。
测试
单元测试
脚手架使用 vitest
来实现单元测试。基本的测试设置已配置在 vitest.config.js
中。
所有测试应编写在 tests
文件夹中,文件名应以 test.ts
或 test.js
结尾。
有关单元测试的编写指南或使用信息,请访问:Vitest 指南。
测试 UI
VSCode
或 WebStorm
可以自动检测测试,并在测试选项卡中显示。
此外,还有一个 vitest
UI,可以通过命令 pnpm test-ui
启动。该命令将启动一个用于管理和报告测试的 Web UI。
部署
使用以下命令来部署您的函数:
pnpm deploy:stg
部署到omni-stg
命名空间。pnpm deploy:live
部署到omni-live
命名空间。
环境变量
在部署期间,脚本将首先从函数根目录加载环境变量,然后从项目根目录加载。这意味着您只需要设置函数特定的变量。