Skip to content

Commit

Permalink
fix: format and refine
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed May 9, 2024
1 parent ad4146e commit 2fcb86e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
11 changes: 5 additions & 6 deletions __tests__/e2e/local/layer/nodejs/code/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

const puppeteer = require('puppeteer')
const puppeteer = require('puppeteer');
const koa = require('koa');

exports.handler = function (event, context, callback) {
let a = 1;
console.log(event.toString());
console.log("My log")
console.log("My log 2")
console.log(process.env.FC_FUNCTION_MEMORY_SIZE)
console.log('My log');
console.log('My log 2');
console.log(process.env.FC_FUNCTION_MEMORY_SIZE);
callback(null, JSON.parse(event.toString()));
// callback(null, 'hello world');
};
Expand All @@ -16,4 +15,4 @@ exports.handler = function (event, context, callback) {
exports.handler = async function (event, context, callback) {
callback(null, 'hello world');
};
*/
*/
2 changes: 1 addition & 1 deletion src/subCommands/layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class Layer {
);
const fileName = path.join(localDir, `${version}.zip`);
if (fs.existsSync(fileName)) {
logger.debug('The layer code already exists locally, skip the download');
logger.debug(`The layer code ${fileName} already exists locally, skip the download`);
return fileName;
}
const codeUrl = url.replace('-internal.aliyuncs.com', '.aliyuncs.com');
Expand Down
97 changes: 50 additions & 47 deletions src/subCommands/local/impl/baseLocal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable guard-for-in */
import path from 'path';
import _ from 'lodash';
Expand All @@ -23,7 +24,7 @@ import chalk from 'chalk';
import * as httpx from 'httpx';
import FC from '../../../resources/fc';
import downloads from '@serverless-devs/downloads';
import decompress from 'decompress'
import decompress from 'decompress';

export class BaseLocal {
protected defaultDebugArgs: string;
Expand Down Expand Up @@ -534,61 +535,63 @@ export class BaseLocal {
}

async getLayerMountString(): Promise<string> {
let result = '';
const { layers, runtime } = this.inputs.props;
if (_.isEmpty(layers)) {
return '';
}

let result = '';
const localLayerBaseDir = path.join(getRootHome(), 'layer');
const mergedDir = path.join(getRootHome(), 'temp', `${Date.now()}`);

if (!_.isEmpty(layers)) {
const localLayerBaseDir = path.join(getRootHome(), 'layer');
const mergedDir = path.join(getRootHome(), 'temp', `${Date.now()}`);
if (fs.existsSync(mergedDir)) {
fs.rmdirSync(mergedDir);
}
fs.mkdirSync(mergedDir, { recursive: true });
process.on('exit', () => {
fs.rmdirSync(mergedDir, { recursive: true });
});

if (fs.existsSync(mergedDir)) {
fs.rmdirSync(mergedDir);
for (const arn of layers) {
const layerInfo = await this.fcSdk.getLayerVersionByArn(arn);
if (!layerInfo.compatibleRuntime.includes(runtime)) {
throw new Error(
`The Layer ${layerInfo.layerName} is not compatible with this runtime. The layer's ARN is ${arn}`,
);
}
fs.mkdirSync(mergedDir, { recursive: true });
process.on('exit', () => {
fs.rmdirSync(mergedDir, { recursive: true });
});
const region = arn.split(':')[2];
const ownerId = arn.split(':')[3];
const localLayerDir = path.join(
localLayerBaseDir,
region,
ownerId,
layerInfo.layerName,
layerInfo.version.toString(),
);

for (const arn of layers) {
if (fs.existsSync(localLayerDir)) {
logger.info(
`The layer code ${localLayerDir} already exists locally, skip the download: ${arn}`,
);
} else {
logger.info(`fetching layer ${arn} ···`);
// 此处因为downloads方法问题,被迫分解下载和解压操作,进行手动解压删除;后面有时间建议修复downloads,一步到位
// eslint-disable-next-line no-await-in-loop
const layerInfo = await this.fcSdk.getLayerVersionByArn(arn);
if (!layerInfo.compatibleRuntime.includes(runtime)) {
throw new Error(
`The Layer ${layerInfo.layerName} is not compatible with this runtime. The layer's ARN is ${arn}`,
);
}
const region = arn.split(':')[2];
const ownerId = arn.split(':')[3];
const localLayerDir = path.join(
localLayerBaseDir,
region,
ownerId,
layerInfo.layerName,
layerInfo.version.toString(),
await downloads(
layerInfo.code.location.replace('-internal.aliyuncs.com', '.aliyuncs.com'),
{
dest: localLayerDir,
filename: layerInfo.version.toString(),
extract: false,
},
);

if (fs.existsSync(localLayerDir)) {
logger.info(`The layer code already exists locally, skip the download: ${arn}`);
} else {
logger.info(`fetching layer ${arn} ···`);
// 此处因为downloads方法问题,被迫分解下载和解压操作,进行手动解压删除;后面有时间建议修复downloads,一步到位
// eslint-disable-next-line no-await-in-loop
await downloads(
layerInfo.code.location.replace('-internal.aliyuncs.com', '.aliyuncs.com'),
{
dest: localLayerDir,
filename: layerInfo.version.toString(),
extract: false,
},
);
const zipPath = path.join(localLayerDir, layerInfo.version.toString() + ".zip");
await decompress(zipPath, localLayerDir);
fs.unlinkSync(zipPath);
}
this.copyFolderContents(localLayerDir, mergedDir);
const zipPath = path.join(localLayerDir, `${layerInfo.version.toString()}.zip`);
await decompress(zipPath, localLayerDir);
fs.unlinkSync(zipPath);
}
result += ` -v ${mergedDir}:/opt`;
this.copyFolderContents(localLayerDir, mergedDir);
}
result += ` -v ${mergedDir}:/opt`;
return result;
}

Expand Down

0 comments on commit 2fcb86e

Please sign in to comment.