From 2fcb86e666a20b3a0f8970847216685ee05e08cf Mon Sep 17 00:00:00 2001 From: rsonghuster Date: Thu, 9 May 2024 18:20:22 +0800 Subject: [PATCH] fix: format and refine --- .../e2e/local/layer/nodejs/code/index.js | 11 +-- src/subCommands/layer/index.ts | 2 +- src/subCommands/local/impl/baseLocal.ts | 97 ++++++++++--------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/__tests__/e2e/local/layer/nodejs/code/index.js b/__tests__/e2e/local/layer/nodejs/code/index.js index a2f372b..58ec4f9 100644 --- a/__tests__/e2e/local/layer/nodejs/code/index.js +++ b/__tests__/e2e/local/layer/nodejs/code/index.js @@ -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'); }; @@ -16,4 +15,4 @@ exports.handler = function (event, context, callback) { exports.handler = async function (event, context, callback) { callback(null, 'hello world'); }; -*/ \ No newline at end of file +*/ diff --git a/src/subCommands/layer/index.ts b/src/subCommands/layer/index.ts index 1f8d11d..5daef88 100644 --- a/src/subCommands/layer/index.ts +++ b/src/subCommands/layer/index.ts @@ -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'); diff --git a/src/subCommands/local/impl/baseLocal.ts b/src/subCommands/local/impl/baseLocal.ts index dc806f9..c2a7a57 100644 --- a/src/subCommands/local/impl/baseLocal.ts +++ b/src/subCommands/local/impl/baseLocal.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ /* eslint-disable guard-for-in */ import path from 'path'; import _ from 'lodash'; @@ -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; @@ -534,61 +535,63 @@ export class BaseLocal { } async getLayerMountString(): Promise { - 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; }