Skip to content

Commit cbf7804

Browse files
authored
fix: yaml extends bug (#101)
1 parent 9a98c10 commit cbf7804

10 files changed

Lines changed: 324 additions & 24 deletions

File tree

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@
143143
"skipFiles": [
144144
"<node_internals>/**",
145145
]
146+
},
147+
{
148+
"name": "s verify",
149+
"type": "node",
150+
"request": "launch",
151+
"runtimeExecutable": "/Users/shihuali/workspace/s/bin/s",
152+
"runtimeArgs": [
153+
"verify",
154+
],
155+
"console": "integratedTerminal",
156+
"cwd": "${workspaceFolder}/test/fixtures/start-fc-event-nodejs14",
157+
"skipFiles": [
158+
"<node_internals>/**",
159+
]
146160
}
147161
]
148162
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@serverless-devs/core",
3-
"version": "0.1.53",
3+
"version": "0.1.54",
44
"description": "Serverless Devs Tool Core Component",
55
"keywords": [
66
"Serverless",

publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Component
22
Name: core
33
Provider:
44
- 阿里云
5-
Version: 0.1.53
5+
Version: 0.1.54
66
Description: Serverless Devs 核心组件
77
HomePage: https://github.com/Serverless-Devs/core
88
Tags: #标签详情

src/common/execCommand/getTemplatePath.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,41 @@ async function setupEnv(templateFile: string) {
6969
}
7070
}
7171

72-
async function extendsYaml(spath: string, dotspath: string, data: any) {
72+
async function extendsYaml(spath: string, dotspath: string) {
73+
const data = await getYamlContent(spath);
7374
const extendsPath = data?.extends ? first(data.extends) : undefined;
74-
let yamlPath = data?.extend ? data.extend : extendsPath;
75-
if (isEmpty(yamlPath)) return;
75+
let baseYamlPath = data?.extend ? data.extend : extendsPath;
76+
if (isEmpty(baseYamlPath)) return;
7677

77-
if (!path.isAbsolute(yamlPath)) {
78-
let dirname = path.dirname(spath);
79-
let fixedPath = path.resolve(dirname, yamlPath);
80-
if (fs.existsSync(fixedPath)) yamlPath = fixedPath;
78+
if (!path.isAbsolute(baseYamlPath)) {
79+
let fixedPath = path.resolve(path.dirname(spath), baseYamlPath);
80+
if (fs.existsSync(fixedPath)) baseYamlPath = fixedPath;
8181
}
8282

83-
await isYamlFile(yamlPath);
84-
if (data?.vars) {
85-
const doc = await getYamlContent(yamlPath);
86-
const newData = extend2(true, doc, { vars: data.vars });
87-
fs.writeFileSync(dotspath, yaml.dump(newData));
88-
return await parseYaml(fs.readFileSync(dotspath, 'utf-8'));
89-
}
90-
return await parseYaml(fs.readFileSync(yamlPath, 'utf-8'));
83+
await isYamlFile(baseYamlPath);
84+
// 解析base yaml
85+
const baseYamlData = await parseYaml(fs.readFileSync(baseYamlPath, 'utf-8'));
86+
// 只合并vars
87+
const newData = extend2(true, {}, { vars: baseYamlData?.vars }, data);
88+
fs.writeFileSync(dotspath, yaml.dump(newData));
89+
// 解析yaml
90+
const parsedData = await parseYaml(fs.readFileSync(dotspath, 'utf-8'));
91+
// 合并base yaml
92+
return extend2(true, {}, baseYamlData, parsedData);
9193
}
9294

9395
export async function transforYamlPath(spath: string = '') {
9496
await isYamlFile(spath);
9597
await setupEnv(spath);
96-
97-
const data = await parseYaml(fs.readFileSync(spath, 'utf-8'));
98+
const data = await getYamlContent(spath);
9899
// 兼容 extends 只取第一个即可
99100
if (isEmpty(data?.extends) && isEmpty(data?.extend)) {
100101
return checkYaml(spath);
101102
}
102103
const dotspath = path.join(path.dirname(spath), '.s', path.basename(spath));
103104
fs.ensureFileSync(dotspath);
104-
105-
const tmp = await extendsYaml(spath, dotspath, data);
106-
const extend2Data = extend2(true, tmp, omit(data, ['extends', 'extend']));
107-
fs.writeFileSync(dotspath, yaml.dump(extend2Data));
105+
const newData = await extendsYaml(spath, dotspath);
106+
fs.writeFileSync(dotspath, yaml.dump(omit(newData, ['extends', 'extend'])));
108107
return checkYaml(dotspath);
109108
}
110109

src/daemon/constant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
DEFAULT_CORE_VERSION: '0.1.53',
2+
DEFAULT_CORE_VERSION: '0.1.54',
33
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# ------------------------------------
2+
# If you need English case, you can refer to [s_en.yaml] file
3+
# ------------------------------------
4+
# 欢迎您使用阿里云函数计算 FC 组件进行项目开发
5+
# 组件仓库地址:https://github.com/devsapp/fc
6+
# 组件帮助文档:https://www.serverless-devs.com/fc/readme
7+
# Yaml参考文档:https://www.serverless-devs.com/fc/yaml/readme
8+
# 关于:
9+
# - Serverless Devs和FC组件的关系、如何声明/部署多个函数、超过50M的代码包如何部署
10+
# - 关于.fcignore使用方法、工具中.s目录是做什么、函数进行build操作之后如何处理build的产物
11+
# 等问题,可以参考文档:https://www.serverless-devs.com/fc/tips
12+
# 关于如何做CICD等问题,可以参考:https://www.serverless-devs.com/serverless-devs/cicd
13+
# 关于如何进行环境划分等问题,可以参考:https://www.serverless-devs.com/serverless-devs/extend
14+
# 更多函数计算案例,可参考:https://github.com/devsapp/awesome/
15+
# 有问题快来钉钉群问一下吧:33947367
16+
# ------------------------------------
17+
edition: 1.0.0
18+
name: hello-world-app
19+
# access 是当前应用所需要的密钥信息配置:
20+
# 密钥配置可以参考:https://www.serverless-devs.com/serverless-devs/command/config
21+
# 密钥使用顺序可以参考:https://www.serverless-devs.com/serverless-devs/tool#密钥使用顺序与规范
22+
access: default
23+
24+
vars: # 全局变量
25+
region: cn-hangzhou
26+
service:
27+
name: hello-world-service
28+
description: hello world by serverless devs
29+
environmentVariables:
30+
c: "c"
31+
d: "d"
32+
a: "a"
33+
b: "b"
34+
e: 'e'
35+
36+
services:
37+
helloworld: # 业务名称/模块名称
38+
component: fc # 组件名称,Serverless Devs 工具本身类似于一种游戏机,不具备具体的业务能力,组件类似于游戏卡,用户通过向游戏机中插入不同的游戏卡实现不同的功能,即通过使用不同的组件实现不同的具体业务能力
39+
# actions: # 自定义执行逻辑,关于actions 的使用,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#行为描述
40+
# pre-deploy: # 在deploy之前运行
41+
# - component: fc build --use-docker --dockerfile ./code/Dockerfile # 要运行的组件,格式为【component: 组件名 命令 参数】(可以通过s cli registry search --type Component 获取组件列表)
42+
# - run: docker build xxx # 要执行的系统命令,类似于一种钩子的形式
43+
# path: ./src # 执行系统命令/钩子的路径
44+
# - plugin: myplugin # 与运行的插件 (可以通过s cli registry search --type Plugin 获取组件列表)
45+
# args: # 插件的参数信息
46+
# testKey: testValue
47+
# post-deploy: # 在deploy之后运行
48+
# - component: fc versions publish # 要运行的命令行
49+
props:
50+
region: ${vars.region} # 关于变量的使用方法,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#变量赋值
51+
service: ${vars.service}
52+
function:
53+
name: start-fc-event-nodejs14
54+
description: hello world by serverless devs
55+
runtime: nodejs14
56+
codeUri: ./code
57+
handler: index.handler
58+
memorySize: 128
59+
timeout: 60
60+
environmentVariables: ${vars.environmentVariables}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
/*
3+
To enable the initializer feature (https://help.aliyun.com/document_detail/156876.html)
4+
please implement the initializer function as below:
5+
exports.initializer = (context, callback) => {
6+
console.log('initializing');
7+
callback(null, '');
8+
};
9+
*/
10+
exports.handler = (event, context, callback) => {
11+
console.log('hello world');
12+
callback(null, 'hello world');
13+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# start-fc-event-nodejs14 帮助文档
2+
3+
<p align="center" class="flex justify-center">
4+
<a href="https://www.serverless-devs.com" class="ml-1">
5+
<img src="http://editor.devsapp.cn/icon?package=start-fc-event-nodejs14&type=packageType">
6+
</a>
7+
<a href="http://www.devsapp.cn/details.html?name=start-fc-event-nodejs14" class="ml-1">
8+
<img src="http://editor.devsapp.cn/icon?package=start-fc-event-nodejs14&type=packageVersion">
9+
</a>
10+
<a href="http://www.devsapp.cn/details.html?name=start-fc-event-nodejs14" class="ml-1">
11+
<img src="http://editor.devsapp.cn/icon?package=start-fc-event-nodejs14&type=packageDownload">
12+
</a>
13+
</p>
14+
15+
<description>
16+
17+
快速部署一个 Node.JS 14 的 Event 类型的 Hello World 函数到阿里云函数计算。
18+
19+
</description>
20+
21+
<table>
22+
23+
## 前期准备
24+
使用该项目,推荐您拥有以下的产品权限 / 策略:
25+
26+
| 服务/业务 | 函数计算 |
27+
| --- | --- |
28+
| 权限/策略 | AliyunFCFullAccess |
29+
30+
</table>
31+
32+
<codepre id="codepre">
33+
34+
# 代码 & 预览
35+
36+
- [ :smiley_cat: 源代码](https://github.com/devsapp/start-fc/blob/main/event-function/fc-event-node.js14)
37+
38+
</codepre>
39+
40+
<deploy>
41+
42+
## 部署 & 体验
43+
44+
<appcenter>
45+
46+
- :fire: 通过 [Serverless 应用中心](https://fcnext.console.aliyun.com/applications/create?template=start-fc-event-nodejs14)
47+
[![Deploy with Severless Devs](https://img.alicdn.com/imgextra/i1/O1CN01w5RFbX1v45s8TIXPz_!!6000000006118-55-tps-95-28.svg)](https://fcnext.console.aliyun.com/applications/create?template=start-fc-event-nodejs14) 该应用。
48+
49+
</appcenter>
50+
51+
- 通过 [Serverless Devs Cli](https://www.serverless-devs.com/serverless-devs/install) 进行部署:
52+
- [安装 Serverless Devs Cli 开发者工具](https://www.serverless-devs.com/serverless-devs/install) ,并进行[授权信息配置](https://www.serverless-devs.com/fc/config)
53+
- 初始化项目:`s init start-fc-event-nodejs14 -d start-fc-event-nodejs14`
54+
- 进入项目,并进行项目部署:`cd start-fc-event-nodejs14 && s deploy -y`
55+
56+
</deploy>
57+
58+
<appdetail id="flushContent">
59+
60+
# 应用详情
61+
62+
63+
64+
本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑
65+
66+
67+
68+
</appdetail>
69+
70+
<devgroup>
71+
72+
## 开发者社区
73+
74+
您如果有关于错误的反馈或者未来的期待,您可以在 [Serverless Devs repo Issues](https://github.com/serverless-devs/serverless-devs/issues) 中进行反馈和交流。如果您想要加入我们的讨论组或者了解 FC 组件的最新动态,您可以通过以下渠道进行:
75+
76+
<p align="center">
77+
78+
| <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407298906_20211028074819117230.png" width="130px" > | <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407044136_20211028074404326599.png" width="130px" > | <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407252200_20211028074732517533.png" width="130px" > |
79+
|--- | --- | --- |
80+
| <center>微信公众号:`serverless`</center> | <center>微信小助手:`xiaojiangwh`</center> | <center>钉钉交流群:`33947367`</center> |
81+
82+
</p>
83+
84+
</devgroup>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# ------------------------------------
2+
# If you need English case, you can refer to [s_en.yaml] file
3+
# ------------------------------------
4+
# 欢迎您使用阿里云函数计算 FC 组件进行项目开发
5+
# 组件仓库地址:https://github.com/devsapp/fc
6+
# 组件帮助文档:https://www.serverless-devs.com/fc/readme
7+
# Yaml参考文档:https://www.serverless-devs.com/fc/yaml/readme
8+
# 关于:
9+
# - Serverless Devs和FC组件的关系、如何声明/部署多个函数、超过50M的代码包如何部署
10+
# - 关于.fcignore使用方法、工具中.s目录是做什么、函数进行build操作之后如何处理build的产物
11+
# 等问题,可以参考文档:https://www.serverless-devs.com/fc/tips
12+
# 关于如何做CICD等问题,可以参考:https://www.serverless-devs.com/serverless-devs/cicd
13+
# 关于如何进行环境划分等问题,可以参考:https://www.serverless-devs.com/serverless-devs/extend
14+
# 更多函数计算案例,可参考:https://github.com/devsapp/awesome/
15+
# 有问题快来钉钉群问一下吧:33947367
16+
# ------------------------------------
17+
edition: 1.0.0
18+
name: hello-world-app
19+
# access 是当前应用所需要的密钥信息配置:
20+
# 密钥配置可以参考:https://www.serverless-devs.com/serverless-devs/command/config
21+
# 密钥使用顺序可以参考:https://www.serverless-devs.com/serverless-devs/tool#密钥使用顺序与规范
22+
access: default
23+
extend: base.yaml
24+
25+
vars: # 全局变量
26+
region: cn-hangzhou
27+
service:
28+
name: hello-world-service
29+
description: hello world by serverless devs
30+
environmentVariables:
31+
c: "c"
32+
d: "d"
33+
a: "aa"
34+
b: "bb"
35+
36+
services:
37+
helloworld: # 业务名称/模块名称
38+
component: fc # 组件名称,Serverless Devs 工具本身类似于一种游戏机,不具备具体的业务能力,组件类似于游戏卡,用户通过向游戏机中插入不同的游戏卡实现不同的功能,即通过使用不同的组件实现不同的具体业务能力
39+
# actions: # 自定义执行逻辑,关于actions 的使用,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#行为描述
40+
# pre-deploy: # 在deploy之前运行
41+
# - component: fc build --use-docker --dockerfile ./code/Dockerfile # 要运行的组件,格式为【component: 组件名 命令 参数】(可以通过s cli registry search --type Component 获取组件列表)
42+
# - run: docker build xxx # 要执行的系统命令,类似于一种钩子的形式
43+
# path: ./src # 执行系统命令/钩子的路径
44+
# - plugin: myplugin # 与运行的插件 (可以通过s cli registry search --type Plugin 获取组件列表)
45+
# args: # 插件的参数信息
46+
# testKey: testValue
47+
# post-deploy: # 在deploy之后运行
48+
# - component: fc versions publish # 要运行的命令行
49+
props:
50+
region: ${vars.region} # 关于变量的使用方法,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#变量赋值
51+
service: ${vars.service}
52+
function:
53+
name: start-fc-event-nodejs14
54+
description: hello world by serverless devs xxx
55+
runtime: nodejs14
56+
codeUri: ./code
57+
handler: index.handler
58+
memorySize: 128
59+
timeout: 60
60+
environmentVariables: ${vars.environmentVariables}

0 commit comments

Comments
 (0)