Skip to content

Commit

Permalink
Merge pull request #34 from Serverless-Devs/feat-yilun
Browse files Browse the repository at this point in the history
feat: load custom component.
  • Loading branch information
zxypro1 authored Feb 28, 2024
2 parents 20706b9 + af6d4ef commit da1d84a
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ jobs:

- name: Build
run: npm run build

- name: Install s
run: npm install -g @serverless-devs/s

- name: s config
run: s config add --AccessKeyID 123 --AccessKeySecret 123 --AccountID 123 --SecurityToken 123 -a default

- name: export env
if: matrix.os != 'windows-latest'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ jobs:
npm run pub
else
echo "No changes in ${{ matrix.package }}/package.json"
fi
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
2 changes: 1 addition & 1 deletion packages/credential/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/credential",
"version": "0.0.4",
"version": "0.0.5",
"description": "credential for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion packages/credential/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export async function writeData(content: Record<string, Record<string, string>>)
fs.writeFileSync(getYamlPath(), yaml.dump(content));
} catch (ex: any) {
Logger.logger.debug(`write data error: ${ex.message}`);
throw new Error('Configuration failed');
if (ex.message.includes('EACCES')) throw new Error(`Configuration failed. Please check the permission.`);;
throw new Error('Configuration failed.');
}
}
4 changes: 2 additions & 2 deletions packages/engine/__tests__/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('not specify --env', () => {
});
test('env name was not found', async () => {
fs.ensureFileSync(ENVIRONMENT_FILE_PATH);
fs.writeJSONSync(ENVIRONMENT_FILE_PATH, [{ 'project': 'demo', 'default': 'testing11' }], { spaces: 2 })
fs.writeJSONSync(ENVIRONMENT_FILE_PATH, [{ 'project': 'web-framework-app', 'default': 'testing11' }], { spaces: 2 })
const template = 's.yaml'
const engine = new Engine({
template,
Expand All @@ -98,7 +98,7 @@ describe('not specify --env', () => {
});
test('basic', async () => {
fs.ensureFileSync(ENVIRONMENT_FILE_PATH);
fs.writeJSONSync(ENVIRONMENT_FILE_PATH, [{ 'project': 'demo', 'default': 'testing' }], { spaces: 2 })
fs.writeJSONSync(ENVIRONMENT_FILE_PATH, [{ 'project': 'web-framework-app', 'default': 'testing' }], { spaces: 2 })
const template = 's.yaml'
const engine = new Engine({
template,
Expand Down
1 change: 0 additions & 1 deletion packages/engine/__tests__/mock/environment/env.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
project: demo # required,string类型。环境属于一个project,一个env.yaml描述project所有环境
environments: # required,数组类型。定义所有环境
- name: testing # required,string类型。环境名
access: default # required,string类型。声明环境使用的云账号
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/engine",
"version": "0.1.0-beta.2",
"version": "0.1.0",
"description": "a engine lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/load-application/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/load-application",
"version": "0.0.12-beta.1",
"version": "0.0.12",
"description": "load application for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -18,7 +18,7 @@
},
"dependencies": {
"@serverless-cd/debug": "^4.3.4",
"@serverless-devs/art-template": "^4.13.7",
"@serverless-devs/art-template": "^4.13.15",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/downloads": "workspace:^",
"@serverless-devs/utils": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion packages/load-component/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/load-component",
"version": "0.0.6",
"version": "0.0.7-beta.1",
"description": "request for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
10 changes: 7 additions & 3 deletions packages/load-component/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Component {
}
async update() {
const [componentName, componentVersion] = getProvider(this.name);
if(componentVersion === 'CUSTOM') {
this.params.logger(`Skip outside component's update. You could use 's clean --component <component_name>' before deploy to update your custom component cache manually.`);
return;
}
debug(`componentName: ${componentName}, componentVersion: ${componentVersion}`);
const componentCachePath = getComponentCachePath(componentName, componentVersion);
debug(`componentCachePath: ${componentCachePath}`);
Expand All @@ -42,18 +46,18 @@ export class Component {
private async getDevComponent() {
const [componentName, componentVersion] = getProvider(this.name);
debug(`componentName: ${componentName}, componentVersion: ${componentVersion}`);
const componentCachePath = getComponentCachePath(componentName, componentVersion);
const componentCachePath = getComponentCachePath(componentName, componentVersion === 'CUSTOM' ? '' : componentVersion);
debug(`componentCachePath: ${componentCachePath}`);
const lockPath = getLockFile(componentCachePath);
if (fs.existsSync(lockPath)) {
return await buildComponentInstance(componentCachePath, this.params);
}
const { zipballUrl, version } = await getZipballUrl(componentName, componentVersion);
const { zipballUrl = componentName, version = componentVersion } = await getZipballUrl(componentName, componentVersion);
debug(`zipballUrl: ${zipballUrl}`);
await download(zipballUrl, {
logger: get(this.params, 'engineLogger', get(this.params, 'logger')),
dest: componentCachePath,
filename: `${componentName}${componentVersion ? `@${componentVersion}` : ''}`,
filename: componentVersion === 'CUSTOM' ? componentName.split('/').pop()?.split('.')[0] : `${componentName}${componentVersion ? `@${componentVersion}` : ''}`,
extract: true,
headers: registry.getSignHeaders(),
});
Expand Down
16 changes: 16 additions & 0 deletions packages/load-component/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const buildComponentInstance = async (componentPath: string, params?: any
};

export function getProvider(name: string) {
if (isValidUrl(name)) {
return [name, 'CUSTOM'];
}
assert(!includes(name, '/'), `The component name ${name} cannot contain /`);
const [componentName, componentVersion] = split(name, '@');
const { core_load_serverless_devs_component } = process.env;
Expand All @@ -84,6 +87,9 @@ export function getProvider(name: string) {
}

export const getZipballUrl = async (componentName: string, componentVersion?: string) => {
if(componentVersion === 'CUSTOM') {
return {};
}
const url = componentVersion ? getUrlWithVersion(componentName, componentVersion) : getUrlWithLatest(componentName);
debug(`url: ${url}`);
try {
Expand All @@ -102,5 +108,15 @@ export const getZipballUrl = async (componentName: string, componentVersion?: st
};

export const getComponentCachePath = (componentName: string, componentVersion?: string) => {
componentName = isValidUrl(componentName) ? componentName.split('/').pop()?.split('.')[0] || '' : componentName;
return path.join(getRootHome(), 'components', 'devsapp.cn', 'v3', componentVersion ? `${componentName}@${componentVersion}` : componentName);
};

export function isValidUrl(s: string) {
try {
new URL(s);
return true;
} catch (err) {
return false;
}
}
4 changes: 2 additions & 2 deletions packages/parse-spec/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/parse-spec",
"version": "0.0.23",
"version": "0.0.24",
"description": "a parse yaml spec lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -22,7 +22,7 @@
},
"dependencies": {
"@serverless-cd/debug": "^4.3.4",
"@serverless-devs/art-template": "^4.13.14",
"@serverless-devs/art-template": "^4.13.15",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/utils": "workspace:^",
"chalk": "^4.1.2",
Expand Down
7 changes: 7 additions & 0 deletions packages/parse-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ class ParseSpec {
this.parseArgv();
if (!this.yaml.use3x) return this.v1();
const { steps, content, originSteps } = await new ParseContent(this.yaml.content, this.getParsedContentOptions(this.yaml.path)).start();
const services = get(this.yaml.content, 'services', {});
if (isEmpty(steps) && !isEmpty(services)) {
this.options.logger.tips('Check https://docs.serverless-devs.com/serverless-devs/yaml for more details. Use the \'s cli fc3 s2tos3\' command for automatic YAML transformation.');
throw new DevsError(`Keyword 'services' has been replaced by 'resources' in 3.0.0 YAML.`, {
trackerType: ETrackerType.parseException,
});
}
// steps 存放每个FC组件/函数的 yaml 配置 ([content.resource] => steps)
// content 为 yaml 已解析的整体完整信息
// originSteps 为 steps 的未解析版
Expand Down
32 changes: 7 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da1d84a

Please sign in to comment.