esa-cli
Version:
A CLI for operating Alibaba Cloud ESA Functions and Pages.
81 lines (80 loc) • 3.02 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { getProjectConfig } from './fileUtils/index.js';
/**
* Check if the assets directory exists in the project config
* @returns {boolean} true if the assets directory exists, false otherwise
*/
const checkConfigAssetsExist = () => {
var _a;
const projectConfig = getProjectConfig();
if (!projectConfig) {
return false;
}
const directory = (_a = projectConfig.assets) === null || _a === void 0 ? void 0 : _a.directory;
if (!directory) {
return false;
}
return true;
};
export var EDGE_ROUTINE_TYPE;
(function (EDGE_ROUTINE_TYPE) {
EDGE_ROUTINE_TYPE["ASSETS_ONLY"] = "assets_only";
EDGE_ROUTINE_TYPE["JS_ONLY"] = "js_only";
EDGE_ROUTINE_TYPE["JS_AND_ASSETS"] = "js_and_assets";
EDGE_ROUTINE_TYPE["NOT_EXIST"] = "not_exist";
})(EDGE_ROUTINE_TYPE || (EDGE_ROUTINE_TYPE = {}));
/**
* Check if a path exists and is valid
* @param filePath - The path to check
* @param isDirectory - Whether the path should be a directory
* @returns boolean
*/
const isValidPath = (filePath, isDirectory = false) => {
if (!filePath || typeof filePath !== 'string' || filePath.trim() === '') {
return false;
}
try {
const resolvedPath = path.isAbsolute(filePath)
? filePath
: path.resolve(filePath);
const exists = fs.existsSync(resolvedPath);
if (!exists) {
return false;
}
if (isDirectory) {
return fs.statSync(resolvedPath).isDirectory();
}
else {
return fs.statSync(resolvedPath).isFile();
}
}
catch (error) {
return false;
}
};
export const checkEdgeRoutineType = (scriptEntry, assetsDirectory, projectPath) => {
var _a;
const projectConfig = getProjectConfig(projectPath);
const entry = scriptEntry || (projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.entry);
const assets = assetsDirectory || ((_a = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.assets) === null || _a === void 0 ? void 0 : _a.directory);
const entryPath = path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', entry !== null && entry !== void 0 ? entry : '');
const assetsPath = path.resolve(projectPath !== null && projectPath !== void 0 ? projectPath : '', assets !== null && assets !== void 0 ? assets : '');
const hasAssets = isValidPath(assetsPath, true) && assets;
const hasEntry = isValidPath(entryPath, false) && entry;
// Both assets and entry exist
if (hasAssets && hasEntry) {
return EDGE_ROUTINE_TYPE.JS_AND_ASSETS;
}
// Only assets exist
if (hasAssets && !hasEntry) {
return EDGE_ROUTINE_TYPE.ASSETS_ONLY;
}
// Only entry exists
if (!hasAssets && hasEntry) {
return EDGE_ROUTINE_TYPE.JS_ONLY;
}
// Neither exists
return EDGE_ROUTINE_TYPE.NOT_EXIST;
};
export default checkConfigAssetsExist;