Init 命令

init命令是初始化Serverless项目的脚手架。

命令解析

当执行s init -h之后,可以进行相关帮助信息的查看:

$ s init -h
Usage: s init [options] [name | url]

Initialize a new project based on a template. You can initialize the application that conforms to the serverless devs project specification through GitHub, or you can initialize the application provided by the source by configuring the source.

    Example:
        $ s init
        $ s init project
        $ s init project -d my_dir
        $ s init project --appName my-express
        $ s init project --parameters '{"serviceName":"websiteService"}'
        $ s init git@github.com:foo/bar.git
        $ s init https://github.com/foo/bar.git
        
🚀  More applications: https://registry.serverless-devs.com

Options:
  -d, --dir <dir>           Where to output the initialized app into (default: ./<ProjectName> )
  -r, --registry <url>      Use specify registry
  -h, --help                Display help for command
  --parameters <parameters> Initialize with custom parameters
  --appName <appName>       Modify default Application name

参数解析

参数全称参数缩写默认取值参数含义
dird./<ProjectName>项目初始化的路径/目录
registry-rhttp://registry.devsapp.cn/simple源配置地址,类似于Python中指定pip源,或者Node.js中指定NPM源
parameters{"serviceName":"websiteService"}初始化参数
appNamemy-express应用/项目名称

初始化项目

引导式初始化

通过s init可以直接进入项目初始化的引导模块:

$ s init

🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome

? Hello Serverless for Cloud Vendors (Use arrow keys or type to search)
❯ Alibaba Cloud Serverless 
  AWS Cloud Serverless 
  Baidu Cloud Serverless 
  Huawei Cloud Serverless 
  Tencent Cloud Serverless 
  Dev Template for Serverless Devs 

此时只需要选择对应的选项,按照引导进行操作,即可。例如选择Alibaba Cloud Serverless,就可以看到阿里云Serverless产品下的应用模板分类:

? Please select an Serverless-Devs Application (Use arrow keys or type to search)
❯ Quick start [Deploy a Hello World function to FaaS] 
  Container example [Deploy function to FaaS with custom-container] 
  Web Framework [Deploy a web framework to FaaS] 
  Static website [Deploy a static website] 
  Best practice [Experience serverless project] 

此时可以继续选择某分类下的具体应用进行初始化,例如选择fc-runtime-starter之后,可以看到该分类下的具体模板应用:

? Which template do you like? (Use arrow keys or type to search)[HTTP] Node.js 12 
  [HTTP] Python3 
  [HTTP] Java8 
  [HTTP] PHP7 
  [HTTP] C++ (custom) 
  [Event] Node.js 12 
  [Event] Python3 
(Move up and down to reveal more choices)

选择fc-http-nodejs即可完成创建:

$ s init                                         

🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome

? Hello Serverless for Cloud Vendors Alibaba Cloud Serverless
? Please select an Serverless-Devs Application fc-runtime-starter - 快速部署一个 FC 函数
? Please select an templete fc-http-nodejs - 快速部署一个 nodejs12 http函数

......

💞 Document ❤ Star:https://github.com/Serverless-Devs/Serverless-Devs

更多关于默认源对应的应用信息,可以参考Package Awesome

直接初始化

通过s init [name | url],可以从配置的Registry或者指定的仓库Url获取模板项目。

初始化Registry应用

以默认的Registry为例,可以初始化相对应的案例项目:start-fc-http-nodejs12,可以通过s init start-fc-http-nodejs12命令来进行:

$ s init start-fc-http-nodejs12

🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome

? Please input your project name (init dir) start-fc-http-nodejs12
✔ file decompression completed

......

💞 Document ❤ Star:https://github.com/Serverless-Devs/Serverless-Devs
初始化仓库应用

以Github仓库 https://github.com/devsapp/puppeteer-app 为例,可以通过s init git@github.com:devsapp/puppeteer-app.git命令初始化该案例项目:

$ s init git@github.com:devsapp/puppeteer-app.git

🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome

Cloning into 'puppeteer-app'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 35 (delta 10), reused 30 (delta 6), pack-reused 0
Receiving objects: 100% (35/35), 6.59 KiB | 3.30 MiB/s, done.
Resolving deltas: 100% (10/10), done.
在 GitHub 上编辑本页面 更新时间: Thu, May 19, 2022