esa-cli
Version:
A CLI for operating Alibaba Cloud ESA Functions and Pages.
149 lines (146 loc) β’ 8.16 kB
JavaScript
/*
Toml Example:
name = "DeepSeek model invocation"
description = 'How to invoke DeepSeek series models through API calls on the BaiLian platform.'
entry = "src/index.js"
assets = ["src/assets"]
codeVersions = [ ]
[assets]
directory = './assets/'
[dev]
port = 18080
localUpstream = ''
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fs from 'fs';
import path from 'path';
import { exit } from 'process';
import AdmZip from 'adm-zip';
import chalk from 'chalk';
import prodBuild from '../commands/commit/prodBuild.js';
import t from '../i18n/index.js';
import logger from '../libs/logger.js';
import { checkEdgeRoutineType, EDGE_ROUTINE_TYPE } from './checkAssetsExist.js';
import { getProjectConfig, readEdgeRoutineFile } from './fileUtils/index.js';
const compress = (scriptEntry_1, assetsDir_1, ...args_1) => __awaiter(void 0, [scriptEntry_1, assetsDir_1, ...args_1], void 0, function* (scriptEntry, assetsDir, minify = false, projectPath, noBundle = false) {
var _a;
let code;
const zip = new AdmZip();
const fileList = [];
const sourceList = [];
const dynamicSources = [];
const projectConfig = getProjectConfig(projectPath);
let assetsDirectory = assetsDir || ((_a = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.assets) === null || _a === void 0 ? void 0 : _a.directory);
const routineType = checkEdgeRoutineType(scriptEntry, assetsDir, projectPath);
if (!projectConfig && !scriptEntry && !assetsDir) {
logger.error([
'esa.jsonc (recommended) or esa.toml is not found and script entry or assets directory is not provided by command line',
'',
'See configuration guide:',
`- English: ${chalk.underline('https://github.com/aliyun/alibabacloud-esa-cli/blob/main/docs/Config_en.md')}`,
`- δΈζ: ${chalk.underline('https://github.com/aliyun/alibabacloud-esa-cli/blob/main/docs/Config_zh_CN.md')}`
].join('\n'));
exit(1);
}
// Parameter priority: use parameters if available, otherwise use values from config file
const entry = scriptEntry || (projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry);
if (routineType === EDGE_ROUTINE_TYPE.NOT_EXIST) {
const errorMessage = [
chalk.red.bold('β File upload failed'),
'',
chalk.cyan('π Current configuration information:'),
`${chalk.white(` π Entry file ${chalk.yellow('(dynamic)')} :`)} ${chalk.yellow(scriptEntry ||
(projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry) ||
chalk.gray(t('compress_not_configured').d('Not configured')))}`,
`${chalk.white(` ποΈ Assets directory ${chalk.yellow('(static)')} :`)} ${chalk.yellow(assetsDirectory || chalk.gray(t('compress_not_configured').d('Not configured')))}`,
'',
chalk.cyan('π Possible issue causes:'),
chalk.white(' 1. Entry file path is incorrect or file does not exist'),
chalk.white(' 2. Assets directory path is incorrect or directory does not exist'),
chalk.white(` 3. Project configuration file ${chalk.yellow('esa.jsonc')} (recommended) or ${chalk.yellow('esa.toml')} format error`),
chalk.white(` 4. Relative path format error, please use ${chalk.yellow('./xxx')} format`),
'',
chalk.yellow.bold(`π Please check if the following ${chalk.red('absolute paths')} are correct:`),
...(scriptEntry || (projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry)
? [
`${chalk.white(' π Entry file:')} ${chalk.cyan.bold(path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', scriptEntry || (projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry) || ''))} ${chalk.gray(t('compress_check_file_exists').d('(Check if file exists)'))}`
]
: []),
...(assetsDirectory
? [
`${chalk.white(' ποΈ Assets directory:')} ${chalk.cyan.bold(path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', assetsDirectory))} ${chalk.gray(t('compress_check_directory_exists').d('(Check if directory exists)'))}`
]
: []),
...(!scriptEntry && !(projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry) && !assetsDirectory
? [
chalk.yellow.bold(' β οΈ You need to configure at least one of entry file or assets directory')
]
: []),
''
].join('\n');
logger.error(errorMessage);
exit(1);
}
if (routineType === EDGE_ROUTINE_TYPE.JS_ONLY ||
routineType === EDGE_ROUTINE_TYPE.JS_AND_ASSETS) {
const buildEntry = path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', entry !== null && entry !== void 0 ? entry : '');
if (noBundle) {
// Skip esbuild bundling, use original entry source directly
code = fs.readFileSync(buildEntry, 'utf-8');
}
else {
yield prodBuild(minify, buildEntry, projectPath);
code = readEdgeRoutineFile(projectPath);
}
zip.addFile(`routine/index.js`, Buffer.from(code || ''));
fileList.push('routine/index.js');
const relativeEntry = path
.relative(projectPath !== null && projectPath !== void 0 ? projectPath : '', buildEntry)
.split(path.sep)
.join('/');
sourceList.push(relativeEntry);
dynamicSources.push(relativeEntry);
}
assetsDirectory = path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', assetsDirectory !== null && assetsDirectory !== void 0 ? assetsDirectory : '');
// Add all files in the assets directory to the /assets directory
if ((routineType === EDGE_ROUTINE_TYPE.JS_AND_ASSETS ||
routineType === EDGE_ROUTINE_TYPE.ASSETS_ONLY) &&
assetsDirectory &&
fs.existsSync(assetsDirectory)) {
const addDirectoryToZip = (dirPath, zipPath) => {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const fullPath = path.join(dirPath, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
addDirectoryToZip(fullPath, path.join(zipPath, file));
}
else {
const fileContent = fs.readFileSync(fullPath);
const relativePath = path
.relative(assetsDirectory, fullPath)
.split(path.sep)
.join('/');
zip.addFile(`assets/${relativePath}`, fileContent);
fileList.push(`assets/${relativePath}`);
const relativeSrcPath = path
.relative(projectPath !== null && projectPath !== void 0 ? projectPath : '', fullPath)
.split(path.sep)
.join('/');
sourceList.push(relativeSrcPath);
}
}
};
addDirectoryToZip(assetsDirectory, 'assets');
}
return { zip, fileList, sourceList, dynamicSources };
});
export default compress;